Start making serializable components
This commit is contained in:
@@ -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 { Position } from "$/common/components/position.ts";
|
||||||
import { AssetManager } from "../assets.ts";
|
import { AssetManager } from "../assets.ts";
|
||||||
import { Sprite } from "./sprite.ts";
|
import { Sprite } from "./sprite.ts";
|
||||||
import { ClickableSprite } from "./clickable.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;
|
current_stage = 0;
|
||||||
growth_time = 0;
|
growth_time = 0;
|
||||||
total_stages: number;
|
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" ...]
|
// you know ["bworld:carrot_seeds", "bworld:carrot_stage_1", "bworld:carrot_stage_2" ...]
|
||||||
sprite_ids: string[];
|
sprite_ids: string[];
|
||||||
|
|
||||||
constructor(total_stages: number, time_to_grow: number[], sprite_ids: string[], item_drop: string) {
|
constructor(
|
||||||
|
crop_id: string,
|
||||||
|
) {
|
||||||
super();
|
super();
|
||||||
this.total_stages = total_stages;
|
this.crop_id = crop_id;
|
||||||
this.time_to_grow = time_to_grow;
|
const crop_info = EverythingRegistry.get<CropsRegistry>("crops", crop_id);
|
||||||
this.sprite_ids = sprite_ids;
|
this.total_stages = crop_info.total_stages;
|
||||||
this.item_drop = item_drop;
|
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
@@ -40,45 +40,21 @@ export function start_game(world: ClientWorld) {
|
|||||||
world.add_entity(create_crop_entity(
|
world.add_entity(create_crop_entity(
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
new Crop(5, [4, 4, 4, 4], [
|
new Crop("bworld:potato"),
|
||||||
"bworld:potato_seeds",
|
|
||||||
"bworld:potato_stage_1",
|
|
||||||
"bworld:potato_stage_2",
|
|
||||||
"bworld:potato_stage_3",
|
|
||||||
"bworld:potato_stage_4",
|
|
||||||
], "bworld:potato"),
|
|
||||||
));
|
));
|
||||||
world.add_entity(create_crop_entity(
|
world.add_entity(create_crop_entity(
|
||||||
32,
|
32,
|
||||||
0,
|
0,
|
||||||
new Crop(5, [5, 5, 5, 5], [
|
new Crop("bworld:carrot"),
|
||||||
"bworld:carrot_seeds",
|
|
||||||
"bworld:carrot_stage_1",
|
|
||||||
"bworld:carrot_stage_2",
|
|
||||||
"bworld:carrot_stage_3",
|
|
||||||
"bworld:carrot_stage_4",
|
|
||||||
], "bworld:carrot"),
|
|
||||||
));
|
));
|
||||||
world.add_entity(create_crop_entity(
|
world.add_entity(create_crop_entity(
|
||||||
0,
|
0,
|
||||||
32,
|
32,
|
||||||
new Crop(5, [10, 10, 10, 10], [
|
new Crop("bworld:pumpkin"),
|
||||||
"bworld:pumpkin_seeds",
|
|
||||||
"bworld:pumpkin_stage_1",
|
|
||||||
"bworld:pumpkin_stage_2",
|
|
||||||
"bworld:pumpkin_stage_3",
|
|
||||||
"bworld:pumpkin_stage_4",
|
|
||||||
], "bworld:pumpkin"),
|
|
||||||
));
|
));
|
||||||
world.add_entity(create_crop_entity(
|
world.add_entity(create_crop_entity(
|
||||||
32,
|
32,
|
||||||
32,
|
32,
|
||||||
new Crop(5, [7, 7, 7, 7], [
|
new Crop("bworld:tomato"),
|
||||||
"bworld:tomato_seeds",
|
|
||||||
"bworld:tomato_stage_1",
|
|
||||||
"bworld:tomato_stage_2",
|
|
||||||
"bworld:tomato_stage_3",
|
|
||||||
"bworld:tomato_stage_4",
|
|
||||||
], "bworld:tomato"),
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,13 @@
|
|||||||
|
import { EverythingRegistry } from "../everything_registry.ts";
|
||||||
|
|
||||||
|
EverythingRegistry.add_registry("components");
|
||||||
|
|
||||||
export abstract class Component {
|
export abstract class Component {
|
||||||
__component = true;
|
__component = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export abstract class SerializableComponent extends Component {
|
||||||
|
abstract serialize(): unknown;
|
||||||
|
// right, this is static you cant do this,,
|
||||||
|
// abstract deserialize<T>(data: unknown): T;
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
export class EverythingRegistry {
|
||||||
|
static #registries = new Map<string, Map<string, unknown>>();
|
||||||
|
|
||||||
|
static add_registry(name: string) {
|
||||||
|
this.#registries.set(name, new Map());
|
||||||
|
}
|
||||||
|
|
||||||
|
static register<T>(registry_name: string, key: string, value: T) {
|
||||||
|
this.#registries.get(registry_name)?.set(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static get<T>(registry_name: string, key: string): T {
|
||||||
|
return this.#registries.get(registry_name)?.get(key) as T;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user