summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
Diffstat (limited to 'server/src')
-rw-r--r--server/src/game.rs24
-rw-r--r--server/src/interaction.rs42
2 files changed, 50 insertions, 16 deletions
diff --git a/server/src/game.rs b/server/src/game.rs
index 14d096e1..4f724446 100644
--- a/server/src/game.rs
+++ b/server/src/game.rs
@@ -3,7 +3,7 @@ use crate::{
protocol::{ItemID, ItemIndex, PacketC, PacketS, PlayerID, RecipeIndex, TileIndex},
recipes::Gamedata,
};
-use anyhow::{anyhow, Result};
+use anyhow::{anyhow, bail, Result};
use glam::IVec2;
use log::info;
use std::{
@@ -26,6 +26,7 @@ pub struct Tile {
struct Player {
name: String,
+ interacting: bool,
hand: Option<ItemID>,
}
@@ -57,7 +58,7 @@ impl Game {
for x in -5..5 {
g.tiles.insert(
IVec2 { x, y: -5 },
- gamedata.get_tile("table").unwrap().into(),
+ gamedata.get_tile("counter").unwrap().into(),
);
g.tiles.insert(
IVec2 { x, y: 4 },
@@ -77,10 +78,14 @@ impl Game {
g.tiles.extend(
[
+ ([1, 4], "pan"),
([2, 4], "pan"),
- ([3, 4], "pan"),
- ([4, 3], "meat-spawn"),
- ([4, 1], "trash"),
+ ([-5, 2], "sink"),
+ ([-5, 3], "dirty-plate-spawn"),
+ ([4, 0], "flour-spawn"),
+ ([4, 1], "tomato-spawn"),
+ ([4, 2], "raw-steak-spawn"),
+ ([4, -4], "trash"),
]
.map(|(k, v)| (IVec2::from_array(k), gamedata.get_tile(v).unwrap().into())),
);
@@ -128,6 +133,7 @@ impl Game {
player,
Player {
hand: None,
+ interacting: false,
name: name.clone(),
},
);
@@ -153,6 +159,8 @@ impl Game {
.push_back(PacketC::Position { player, pos, rot });
}
PacketS::Interact { pos, edge } => {
+ info!("interact {pos:?} edge={edge}");
+
let pid = player;
let player = self
.players
@@ -163,6 +171,10 @@ impl Game {
.get_mut(&pos)
.ok_or(anyhow!("tile does not exist"))?;
+ if edge == player.interacting {
+ bail!("already (not) interacting")
+ }
+
let items = tile.items.iter().map(|e| self.items[e]).collect::<Vec<_>>();
let tilekind = tile.kind;
let hand = player.hand.map(|e| self.items[&e]);
@@ -212,6 +224,8 @@ impl Game {
}
},
);
+
+ player.interacting = edge;
}
}
Ok(())
diff --git a/server/src/interaction.rs b/server/src/interaction.rs
index f2d44dee..11409b10 100644
--- a/server/src/interaction.rs
+++ b/server/src/interaction.rs
@@ -31,14 +31,26 @@ pub fn interact(
}
}
if !edge {
+ debug!("falling edge");
if let Some(ac) = active {
if matches!(data.recipes[ac.recipe].action, Action::Active(_)) {
+ debug!("workers--");
ac.working -= 1;
}
}
return;
}
+ if hand.is_none() {
+ debug!("rising edge");
+ if let Some(active) = active {
+ if matches!(data.recipes[active.recipe].action, Action::Active(_)) {
+ debug!("workers++");
+ active.working += 1;
+ }
+ }
+ }
+
if active.is_none() && !items.is_empty() && hand.is_none() {
out(Take(items.len() - 1));
return;
@@ -54,7 +66,7 @@ pub fn interact(
}
}
- if hand.is_none() {
+ if hand.is_none() && active.is_none() {
'rloop: for (ri, r) in data.recipes.iter().enumerate() {
if tile != r.tile {
continue;
@@ -74,19 +86,27 @@ pub fn interact(
match r.action {
Action::Passive(_) => {
- info!("use recipe {r:?}");
+ info!("use passive recipe {ri}");
*active = Some(ActiveRecipe {
recipe: ri,
progress: 0.,
- working: 0,
+ working: 1,
+ });
+ break 'rloop;
+ }
+ Action::Active(_) => {
+ info!("use active recipe {ri}");
+ *active = Some(ActiveRecipe {
+ recipe: ri,
+ progress: 0.,
+ working: 1,
});
break 'rloop;
}
- Action::Active(_) => {}
Action::Instant => {
- info!("use recipe {r:?}");
- for i in 0..items.len() {
- out(Consume(i))
+ info!("use instant recipe {ri}");
+ for _ in 0..items.len() {
+ out(Consume(0))
}
for i in &r.outputs {
out(Produce(*i));
@@ -108,15 +128,15 @@ pub fn tick_tile(
data: &Gamedata,
_tile: TileIndex,
active: &mut Option<ActiveRecipe>,
- mut items: Vec<ItemIndex>,
+ 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();
+ a.progress += a.working as f32 * dt / r.action.duration();
if a.progress >= 1. {
- for i in 0..items.len() {
- out(Consume(i))
+ for _ in 0..items.len() {
+ out(Consume(0))
}
for i in &r.outputs {
out(Produce(*i));