diff options
Diffstat (limited to 'server/src/game.rs')
| -rw-r--r-- | server/src/game.rs | 52 | 
1 files changed, 41 insertions, 11 deletions
| diff --git a/server/src/game.rs b/server/src/game.rs index f207e8f6..14d096e1 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -1,5 +1,5 @@  use crate::{ -    interaction::{interact, Out}, +    interaction::{interact, tick_tile, Out},      protocol::{ItemID, ItemIndex, PacketC, PacketS, PlayerID, RecipeIndex, TileIndex},      recipes::Gamedata,  }; @@ -22,7 +22,6 @@ pub struct Tile {      kind: TileIndex,      items: Vec<ItemID>,      active: Option<ActiveRecipe>, -    last_active: bool,  }  struct Player { @@ -205,6 +204,12 @@ impl Game {                              info!("left {:?}", tile.items);                              self.packet_out.push_back(PacketC::ConsumeItem { id, pos });                          } +                        Out::SetActive(progress) => { +                            self.packet_out.push_back(PacketC::SetActive { +                                tile: pos, +                                progress, +                            }); +                        }                      },                  );              } @@ -214,14 +219,40 @@ impl Game {      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() +            let items = tile.items.iter().map(|e| self.items[e]).collect::<Vec<_>>(); +            tick_tile( +                dt, +                &self.data, +                tile.kind, +                &mut tile.active, +                items, +                |out| match out { +                    Out::Take(_) | Out::Put => { +                        unreachable!() +                    } +                    Out::Produce(kind) => { +                        info!("produce"); +                        let id = self.item_id_counter; +                        self.item_id_counter += 1; +                        self.items.insert(id, kind); +                        tile.items.push(id); +                        self.packet_out +                            .push_back(PacketC::ProduceItem { id, pos, kind }); +                    } +                    Out::Consume(index) => { +                        info!("consume"); +                        let id = tile.items.remove(index); +                        info!("left {:?}", tile.items); +                        self.packet_out.push_back(PacketC::ConsumeItem { id, pos }); +                    } +                    Out::SetActive(progress) => { +                        self.packet_out.push_back(PacketC::SetActive { +                            tile: pos, +                            progress, +                        }); +                    } +                }, +            );          }      }  } @@ -231,7 +262,6 @@ impl From<TileIndex> for Tile {          Self {              kind,              items: vec![], -            last_active: false,              active: None,          }      } | 
