diff --git a/client/components/crop.ts b/client/components/crop.ts index 3e0987a..db3f203 100644 --- a/client/components/crop.ts +++ b/client/components/crop.ts @@ -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("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("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("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("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("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 { + const crop = new Crop(data.crop_id); + crop.current_stage = data.current_stage; + crop.growth_time = data.growth_time; + return crop; } } diff --git a/client/game.ts b/client/game.ts index 0fdb534..0559fbe 100644 --- a/client/game.ts +++ b/client/game.ts @@ -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"), )); } diff --git a/common/ecs/component.ts b/common/ecs/component.ts index b634aaa..d9d5230 100644 --- a/common/ecs/component.ts +++ b/common/ecs/component.ts @@ -1,3 +1,13 @@ +import { EverythingRegistry } from "../everything_registry.ts"; + +EverythingRegistry.add_registry("components"); + export abstract class Component { __component = true; } + +export abstract class SerializableComponent extends Component { + abstract serialize(): unknown; + // right, this is static you cant do this,, + // abstract deserialize(data: unknown): T; +} diff --git a/common/everything_registry.ts b/common/everything_registry.ts new file mode 100644 index 0000000..e282716 --- /dev/null +++ b/common/everything_registry.ts @@ -0,0 +1,15 @@ +export class EverythingRegistry { + static #registries = new Map>(); + + static add_registry(name: string) { + this.#registries.set(name, new Map()); + } + + static register(registry_name: string, key: string, value: T) { + this.#registries.get(registry_name)?.set(key, value); + } + + static get(registry_name: string, key: string): T { + return this.#registries.get(registry_name)?.get(key) as T; + } +}