diff options
-rw-r--r-- | server/src/game.rs | 19 | ||||
-rw-r--r-- | server/src/interaction.rs | 11 | ||||
-rw-r--r-- | server/src/main.rs | 4 | ||||
-rw-r--r-- | server/src/protocol.rs | 2 | ||||
-rw-r--r-- | server/src/recipes.rs | 8 | ||||
-rw-r--r-- | test-client/main.ts | 10 | ||||
-rw-r--r-- | test-client/protocol.ts | 2 |
7 files changed, 44 insertions, 12 deletions
diff --git a/server/src/game.rs b/server/src/game.rs index 3b16cc05..f207e8f6 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -22,6 +22,7 @@ pub struct Tile { kind: TileIndex, items: Vec<ItemID>, active: Option<ActiveRecipe>, + last_active: bool, } struct Player { @@ -77,8 +78,8 @@ impl Game { g.tiles.extend( [ - ([-5, 1], "pan"), - ([-5, 2], "pan"), + ([2, 4], "pan"), + ([3, 4], "pan"), ([4, 3], "meat-spawn"), ([4, 1], "trash"), ] @@ -210,6 +211,19 @@ impl Game { } Ok(()) } + + pub fn tick(&mut self, dt: f32) { + for (&pos, tile) in &mut self.tiles { + if let Some(active) = &mut tile.active { + active.progress += dt / self.data.recipes[active.recipe].action.duration(); + self.packet_out.push_back(PacketC::SetActive { + tile: pos, + progress: Some(active.progress), + }); + } + tile.last_active = tile.active.is_some() + } + } } impl From<TileIndex> for Tile { @@ -217,6 +231,7 @@ impl From<TileIndex> for Tile { Self { kind, items: vec![], + last_active: false, active: None, } } diff --git a/server/src/interaction.rs b/server/src/interaction.rs index 7ef4a9b4..fadfe8d9 100644 --- a/server/src/interaction.rs +++ b/server/src/interaction.rs @@ -38,6 +38,11 @@ pub fn interact( return; } + if !items.is_empty() && hand.is_none() { + out(Take(items.len() - 1)); + return; + } + if let Some(hi) = hand { if allowed.contains(&hi) { out(Put); @@ -66,6 +71,7 @@ pub fn interact( match r.action { Action::Passive(_) => { + info!("use recipe {r:?}"); *active = Some(ActiveRecipe { recipe: ri, progress: 0., @@ -85,14 +91,11 @@ pub fn interact( if !r.outputs.is_empty() { out(Take(r.outputs.len() - 1)); } + items.clear(); break 'rloop; } Action::Never => (), } } } - - if !items.is_empty() && hand.is_none() { - out(Take(items.len() - 1)); - } } diff --git a/server/src/main.rs b/server/src/main.rs index 441487e8..d747e737 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -36,15 +36,17 @@ async fn main() -> Result<()> { { let game = game.clone(); spawn(async move { + let dt = 1. / 25.; loop { { let mut g = game.write().await; + g.tick(dt); while let Some(p) = g.packet_out() { debug!("-> {p:?}"); let _ = tx.send(p); } } - sleep(Duration::from_millis(20)).await; + sleep(Duration::from_secs_f32(dt)).await; } }); } diff --git a/server/src/protocol.rs b/server/src/protocol.rs index fa34954b..ef731b0d 100644 --- a/server/src/protocol.rs +++ b/server/src/protocol.rs @@ -56,7 +56,7 @@ pub enum PacketC { }, SetActive { tile: IVec2, - progress: f32, + progress: Option<f32>, }, UpdateMap { pos: IVec2, diff --git a/server/src/recipes.rs b/server/src/recipes.rs index 11c455ba..b99cd21e 100644 --- a/server/src/recipes.rs +++ b/server/src/recipes.rs @@ -73,3 +73,11 @@ impl Gamedata { self.tile_names.iter().position(|t| t == name) } } +impl Action { + pub fn duration(&self) -> f32 { + match self { + Action::Instant | Action::Never => 0., + Action::Passive(x) | Action::Active(x) => *x, + } + } +} diff --git a/test-client/main.ts b/test-client/main.ts index 6622d37f..3926dffc 100644 --- a/test-client/main.ts +++ b/test-client/main.ts @@ -36,7 +36,7 @@ interface PlayerData { x: number; y: number, name: string, rot: number, hand?: I const players = new Map<PlayerID, PlayerData>() interface ItemData { kind: ItemIndex, tile?: V2, player?: PlayerID, tracking_player: boolean, x: number, y: number } const items = new Map<ItemID, ItemData>() -interface TileData { x: number; y: number, kind: TileIndex, items: ItemID[], active: boolean } +interface TileData { x: number; y: number, kind: TileIndex, items: ItemID[], active_progress?: number } const tiles = new Map<string, TileData>() let data: Gamedata = { item_names: [], tile_names: [] } @@ -79,9 +79,9 @@ function packet(p: PacketC) { t.items.splice(t.items.indexOf(p.consume_item.id)) items.delete(p.consume_item.id) } else if ("set_active" in p) { - // TODO + tiles.get(p.set_active.tile.toString())!.active_progress = p.set_active.progress } else if ("update_map" in p) { - tiles.set(p.update_map.pos.toString(), { x: p.update_map.pos[0], y: p.update_map.pos[1], kind: p.update_map.tile, active: false, items: [] }) + tiles.set(p.update_map.pos.toString(), { x: p.update_map.pos[0], y: p.update_map.pos[1], kind: p.update_map.tile, items: [] }) } else console.warn("unknown packet", p); } @@ -197,6 +197,10 @@ function draw_ingame() { for (const c of comps) { c(ctx) } + if (tile.active_progress !== null && tile.active_progress !== undefined) { + ctx.fillStyle = "rgba(115, 230, 58, 0.66)" + ctx.fillRect(0, 0, 1, tile.active_progress) + } ctx.restore() } diff --git a/test-client/protocol.ts b/test-client/protocol.ts index d5cb2034..ccd891dd 100644 --- a/test-client/protocol.ts +++ b/test-client/protocol.ts @@ -24,6 +24,6 @@ export type PacketC = | { put_item: { item: ItemID, pos: Vec2 } } | { produce_item: { id: ItemID, pos: Vec2, kind: ItemIndex } } | { consume_item: { id: ItemID, pos: Vec2 } } - | { set_active: { tile: Vec2 } } + | { set_active: { tile: Vec2, progress?: number } } | { update_map: { pos: Vec2, tile: TileIndex } } |