diff options
-rw-r--r-- | data/items.yaml | 4 | ||||
-rw-r--r-- | data/recipes.yaml | 5 | ||||
-rw-r--r-- | server/src/game.rs | 45 | ||||
-rw-r--r-- | server/src/protocol.rs | 26 | ||||
-rw-r--r-- | test-client/main.ts | 6 | ||||
-rw-r--r-- | test-client/protocol.ts | 2 |
6 files changed, 47 insertions, 41 deletions
diff --git a/data/items.yaml b/data/items.yaml new file mode 100644 index 00000000..a485daa5 --- /dev/null +++ b/data/items.yaml @@ -0,0 +1,4 @@ +- flour +- water +- dough + diff --git a/data/recipes.yaml b/data/recipes.yaml new file mode 100644 index 00000000..571b9633 --- /dev/null +++ b/data/recipes.yaml @@ -0,0 +1,5 @@ + +- tile: table + inputs: [tomato] + outputs: [sliced-tomato] + duration: 3 diff --git a/server/src/game.rs b/server/src/game.rs index 18bd6519..a9ee8f3d 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -8,6 +8,7 @@ struct TileData { kind: Tile, items: Vec<ID>, active: bool, + progress: f32, } struct Player { @@ -29,31 +30,32 @@ impl Game { let mut g = Self::default(); for x in -5..5 { for y in -5..5 { - g.tiles.insert(IVec2 { x, y }, Tile::Floor.into()); + g.tiles + .insert(IVec2 { x, y }, Tile("floor".to_string()).into()); } } for x in -5..5 { - g.tiles.insert(IVec2 { x, y: -5 }, Tile::Table.into()); - g.tiles.insert(IVec2 { x, y: 4 }, Tile::Table.into()); + g.tiles + .insert(IVec2 { x, y: -5 }, Tile("table".to_string()).into()); + g.tiles + .insert(IVec2 { x, y: 4 }, Tile("table".to_string()).into()); } for y in -5..5 { - g.tiles.insert(IVec2 { x: -5, y }, Tile::Table.into()); - g.tiles.insert(IVec2 { x: 4, y }, Tile::Table.into()); + g.tiles + .insert(IVec2 { x: -5, y }, Tile("table".to_string()).into()); + g.tiles + .insert(IVec2 { x: 4, y }, Tile("table".to_string()).into()); } g.tiles.extend( - [ - ([-5, 1], Tile::Pan), - ([-5, 2], Tile::Pan), - ([4, 3], Tile::FlourBag), - ] - .map(|(k, v)| { + [([-5, 1], "pan"), ([-5, 2], "pan"), ([4, 3], "flour_bag")].map(|(k, v)| { ( IVec2::from_array(k), TileData { active: false, items: vec![], - kind: v, + kind: Tile(v.to_string()).into(), + progress: 0., }, ) }), @@ -72,19 +74,19 @@ impl Game { out.push(PacketC::AddPlayer { id, name: player.name.clone(), - hand: player.hand.map(|i| (i, self.items[&i])), + hand: player.hand.map(|i| (i, self.items[&i].clone())), }) } for (&pos, tdata) in &self.tiles { out.push(PacketC::UpdateMap { pos, - tile: tdata.kind, + tile: tdata.kind.clone(), }); for &id in &tdata.items { out.push(PacketC::ProduceItem { id, pos, - kind: self.items[&id], + kind: self.items[&id].clone(), }) } } @@ -123,7 +125,10 @@ impl Game { self.packet_out .push_back(PacketC::Position { player, pos, rot }); } - PacketS::Interact { pos } => { + PacketS::Interact { pos, edge } => { + if !edge { + return Ok(()); + } let tile = self .tiles .get_mut(&pos) @@ -133,14 +138,15 @@ impl Game { .get_mut(&player) .ok_or(anyhow!("player does not exist"))?; - if let Tile::FlourBag = tile.kind { + if tile.kind.0 == "flour_bag" { info!("new flour"); - self.items.insert(self.item_id_counter, Item::Flour); + let item = Item("flour".to_string()); + self.items.insert(self.item_id_counter, item.clone()); tile.items.push(self.item_id_counter); self.packet_out.push_back(PacketC::ProduceItem { id: self.item_id_counter, pos, - kind: Item::Flour, + kind: item, }); self.item_id_counter += 1; } @@ -166,6 +172,7 @@ impl From<Tile> for TileData { fn from(kind: Tile) -> Self { Self { kind, + progress: 0., active: false, items: vec![], } diff --git a/server/src/protocol.rs b/server/src/protocol.rs index 0218658d..a318a9b2 100644 --- a/server/src/protocol.rs +++ b/server/src/protocol.rs @@ -3,24 +3,13 @@ use serde::{Deserialize, Serialize}; pub type ID = u32; -#[derive(Debug, Clone, Copy, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum Item { - Flour, - Water, - Dough, - Pancake, -} +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(transparent)] +pub struct Item(pub String); -#[derive(Debug, Clone, Copy, Serialize, Deserialize)] -#[serde(rename_all = "snake_case")] -pub enum Tile { - Sink, - FlourBag, - Floor, - Table, - Pan, -} +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(transparent)] +pub struct Tile(pub String); #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] @@ -28,7 +17,7 @@ pub enum PacketS { Join { name: String }, Leave, Position { pos: Vec2, rot: f32 }, - Interact { pos: IVec2 }, + Interact { pos: IVec2, edge: bool }, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -69,6 +58,7 @@ pub enum PacketC { }, SetActive { tile: IVec2, + progress: f32, }, UpdateMap { pos: IVec2, diff --git a/test-client/main.ts b/test-client/main.ts index f31176cd..3e04dc05 100644 --- a/test-client/main.ts +++ b/test-client/main.ts @@ -84,7 +84,7 @@ const keys_down = new Set(); const HANDLED_KEYS = ["KeyW", "KeyA", "KeyS", "KeyD", "Space"] function keyboard(ev: KeyboardEvent, down: boolean) { if (HANDLED_KEYS.includes(ev.code)) ev.preventDefault() - if (ev.code == "Space" && down) interact() + if (ev.code == "Space") interact(down) if (down) keys_down.add(ev.code) else keys_down.delete(ev.code) } @@ -98,9 +98,9 @@ function get_interact_target(): V2 | undefined { } } -function interact() { +function interact(edge: boolean) { const { x, y } = get_interact_target()!; - send({ interact: { pos: [x, y] } }) + send({ interact: { pos: [x, y], edge } }) } function tick_update() { diff --git a/test-client/protocol.ts b/test-client/protocol.ts index 717383d4..707c09a3 100644 --- a/test-client/protocol.ts +++ b/test-client/protocol.ts @@ -7,7 +7,7 @@ export type PacketS = { join: { name: string } } | "leave" | { position: { pos: Vec2, rot: number } } - | { interact: { pos: Vec2 } } + | { interact: { pos: Vec2, edge: boolean } } export type PacketC = |