Start making serializable components

This commit is contained in:
2026-02-27 17:24:08 -03:00
parent b52b7f4c44
commit 0f3f8de735
4 changed files with 114 additions and 35 deletions
+85 -7
View File
@@ -1,10 +1,69 @@
import { Component, Entity } from "$/common/ecs/mod.ts";
import { Entity, SerializableComponent } from "$/common/ecs/mod.ts";
import { Position } from "$/common/components/position.ts";
import { AssetManager } from "../assets.ts";
import { Sprite } from "./sprite.ts";
import { ClickableSprite } from "./clickable.ts";
import { EverythingRegistry } from "$/common/everything_registry.ts";
export class Crop extends Component {
interface CropsRegistry {
total_stages: number;
time_to_grow: number[];
item_drop: string;
sprite_ids: string[];
}
EverythingRegistry.add_registry("crops");
EverythingRegistry.register<CropsRegistry>("crops", "bworld:carrot", {
total_stages: 5,
time_to_grow: [60, 60 * 3, 60 * 3, 60 * 3],
sprite_ids: [
"bworld:carrot_seeds",
"bworld:carrot_stage_1",
"bworld:carrot_stage_2",
"bworld:carrot_stage_3",
"bworld:carrot_stage_4",
],
item_drop: "bworld:carrot",
});
EverythingRegistry.register<CropsRegistry>("crops", "bworld:potato", {
total_stages: 5,
time_to_grow: [45, 60 * 2, 60 * 2, 60 * 2],
sprite_ids: [
"bworld:potato_seeds",
"bworld:potato_stage_1",
"bworld:potato_stage_2",
"bworld:potato_stage_3",
"bworld:potato_stage_4",
],
item_drop: "bworld:potato",
});
EverythingRegistry.register<CropsRegistry>("crops", "bworld:tomato", {
total_stages: 5,
time_to_grow: [90, 60 * 2, 60 * 2, 60 * 3],
sprite_ids: [
"bworld:tomato_seeds",
"bworld:tomato_stage_1",
"bworld:tomato_stage_2",
"bworld:tomato_stage_3",
"bworld:tomato_stage_4",
],
item_drop: "bworld:tomato",
});
EverythingRegistry.register<CropsRegistry>("crops", "bworld:pumpkin", {
total_stages: 5,
time_to_grow: [60 * 2, 60 * 4, 60 * 4, 60 * 4],
sprite_ids: [
"bworld:pumpkin_seeds",
"bworld:pumpkin_stage_1",
"bworld:pumpkin_stage_2",
"bworld:pumpkin_stage_3",
"bworld:pumpkin_stage_4",
],
item_drop: "bworld:pumpkin",
});
export class Crop extends SerializableComponent {
crop_id: string;
current_stage = 0;
growth_time = 0;
total_stages: number;
@@ -14,12 +73,31 @@ export class Crop extends Component {
// you know ["bworld:carrot_seeds", "bworld:carrot_stage_1", "bworld:carrot_stage_2" ...]
sprite_ids: string[];
constructor(total_stages: number, time_to_grow: number[], sprite_ids: string[], item_drop: string) {
constructor(
crop_id: string,
) {
super();
this.total_stages = total_stages;
this.time_to_grow = time_to_grow;
this.sprite_ids = sprite_ids;
this.item_drop = item_drop;
this.crop_id = crop_id;
const crop_info = EverythingRegistry.get<CropsRegistry>("crops", crop_id);
this.total_stages = crop_info.total_stages;
this.time_to_grow = crop_info.time_to_grow;
this.sprite_ids = crop_info.sprite_ids;
this.item_drop = crop_info.item_drop;
}
override serialize() {
return {
crop_id: this.crop_id,
current_stage: this.current_stage,
growth_time: this.growth_time,
};
}
static deserialize(data: ReturnType<Crop["serialize"]>): Crop {
const crop = new Crop(data.crop_id);
crop.current_stage = data.current_stage;
crop.growth_time = data.growth_time;
return crop;
}
}
+4 -28
View File
@@ -40,45 +40,21 @@ export function start_game(world: ClientWorld) {
world.add_entity(create_crop_entity(
0,
0,
new Crop(5, [4, 4, 4, 4], [
"bworld:potato_seeds",
"bworld:potato_stage_1",
"bworld:potato_stage_2",
"bworld:potato_stage_3",
"bworld:potato_stage_4",
], "bworld:potato"),
new Crop("bworld:potato"),
));
world.add_entity(create_crop_entity(
32,
0,
new Crop(5, [5, 5, 5, 5], [
"bworld:carrot_seeds",
"bworld:carrot_stage_1",
"bworld:carrot_stage_2",
"bworld:carrot_stage_3",
"bworld:carrot_stage_4",
], "bworld:carrot"),
new Crop("bworld:carrot"),
));
world.add_entity(create_crop_entity(
0,
32,
new Crop(5, [10, 10, 10, 10], [
"bworld:pumpkin_seeds",
"bworld:pumpkin_stage_1",
"bworld:pumpkin_stage_2",
"bworld:pumpkin_stage_3",
"bworld:pumpkin_stage_4",
], "bworld:pumpkin"),
new Crop("bworld:pumpkin"),
));
world.add_entity(create_crop_entity(
32,
32,
new Crop(5, [7, 7, 7, 7], [
"bworld:tomato_seeds",
"bworld:tomato_stage_1",
"bworld:tomato_stage_2",
"bworld:tomato_stage_3",
"bworld:tomato_stage_4",
], "bworld:tomato"),
new Crop("bworld:tomato"),
));
}