aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/src/game.rs52
-rw-r--r--server/src/interaction.rs41
2 files changed, 76 insertions, 17 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,
}
}
diff --git a/server/src/interaction.rs b/server/src/interaction.rs
index fadfe8d9..f2d44dee 100644
--- a/server/src/interaction.rs
+++ b/server/src/interaction.rs
@@ -12,6 +12,7 @@ pub enum Out {
Put,
Produce(ItemIndex),
Consume(usize),
+ SetActive(Option<f32>),
}
pub fn interact(
@@ -38,16 +39,18 @@ pub fn interact(
return;
}
- if !items.is_empty() && hand.is_none() {
+ if active.is_none() && !items.is_empty() && hand.is_none() {
out(Take(items.len() - 1));
return;
}
- if let Some(hi) = hand {
- if allowed.contains(&hi) {
- out(Put);
- items.push(hi);
- hand = None;
+ if active.is_none() {
+ if let Some(hi) = hand {
+ if allowed.contains(&hi) {
+ out(Put);
+ items.push(hi);
+ hand = None;
+ }
}
}
@@ -99,3 +102,29 @@ pub fn interact(
}
}
}
+
+pub fn tick_tile(
+ dt: f32,
+ data: &Gamedata,
+ _tile: TileIndex,
+ active: &mut Option<ActiveRecipe>,
+ mut items: Vec<ItemIndex>,
+ mut out: impl FnMut(Out),
+) {
+ if let Some(a) = active {
+ let r = &data.recipes[a.recipe];
+ a.progress += dt / r.action.duration();
+ if a.progress >= 1. {
+ for i in 0..items.len() {
+ out(Consume(i))
+ }
+ for i in &r.outputs {
+ out(Produce(*i));
+ }
+ out(SetActive(None));
+ active.take();
+ } else {
+ out(SetActive(Some(a.progress)));
+ }
+ }
+}