diff options
author | metamuffin <metamuffin@disroot.org> | 2024-06-26 13:19:07 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-06-26 13:19:07 +0200 |
commit | cf24bc076e9d3746210e507875a7178df19b51b1 (patch) | |
tree | d90048a7f5acd84b6f1ccb6815baae59e97cd758 /server/src | |
parent | eff1b80e06fd0b50675d235e7a9b3dadc6048d40 (diff) | |
download | hurrycurry-cf24bc076e9d3746210e507875a7178df19b51b1.tar hurrycurry-cf24bc076e9d3746210e507875a7178df19b51b1.tar.bz2 hurrycurry-cf24bc076e9d3746210e507875a7178df19b51b1.tar.zst |
generalize interact
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/game.rs | 8 | ||||
-rw-r--r-- | server/src/interaction.rs | 61 |
2 files changed, 38 insertions, 31 deletions
diff --git a/server/src/game.rs b/server/src/game.rs index 48427ba1..defaf7bf 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -290,7 +290,13 @@ impl Game { let tile_had_item = tile.item.is_some(); let player_had_item = player.item.is_some(); - if let Some(effect) = interact(&self.data, edge, tile, player) { + if let Some(effect) = interact( + &self.data, + edge, + tile.kind, + &mut tile.item, + &mut player.item, + ) { match effect { InteractEffect::Put => self.packet_out.push_back(PacketC::PutItem { player: pid, diff --git a/server/src/interaction.rs b/server/src/interaction.rs index 938ab715..615d2aab 100644 --- a/server/src/interaction.rs +++ b/server/src/interaction.rs @@ -1,23 +1,23 @@ /* Undercooked - a game about cooking Copyright 2024 metamuffin - + This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, version 3 of the License only. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. - + */ use crate::{ data::Gamedata, - game::{Involvement, Item, Player, Tile}, + game::{Involvement, Item, Tile}, protocol::{ItemIndex, TileIndex}, }; use log::info; @@ -117,23 +117,24 @@ pub enum InteractEffect { pub fn interact( data: &Gamedata, edge: bool, - tile: &mut Tile, - player: &mut Player, + tile_kind: TileIndex, + this: &mut Option<Item>, + other: &mut Option<Item>, ) -> Option<InteractEffect> { - let interactable = data.is_tile_interactable(tile.kind); - if interactable && player.item.is_none() { - if let Some(item) = &mut tile.item { + let interactable = data.is_tile_interactable(tile_kind); + if interactable && other.is_none() { + if let Some(item) = this { if let Some(active) = &mut item.active { let recipe = &data.recipe(active.recipe); - if recipe.supports_tile(tile.kind) { + if recipe.supports_tile(tile_kind) { if let Recipe::Active { outputs, .. } = recipe { if edge { active.working += 1; } else { active.working -= 1; if active.progress >= 1. { - player.item = outputs[0].map(|kind| Item { kind, active: None }); - tile.item = outputs[1].map(|kind| Item { kind, active: None }); + *other = outputs[0].map(|kind| Item { kind, active: None }); + *this = outputs[1].map(|kind| Item { kind, active: None }); return Some(InteractEffect::Produce); } } @@ -147,13 +148,13 @@ pub fn interact( } if interactable { for (ri, recipe) in data.recipes() { - if !recipe.supports_tile(tile.kind) { + if !recipe.supports_tile(tile_kind) { continue; } match recipe { Recipe::Active { input, .. } => { - if player.item.is_none() { - if let Some(item) = &mut tile.item { + if other.is_none() { + if let Some(item) = this { if item.kind == *input { if item.active.is_none() { info!("start active recipe {ri:?}"); @@ -166,10 +167,10 @@ pub fn interact( } } } - if tile.item.is_none() { - if let Some(item) = &player.item { + if this.is_none() { + if let Some(item) = &other { if item.kind == *input { - let mut item = player.item.take().unwrap(); + let mut item = other.take().unwrap(); if let Some(active) = &mut item.active { active.working += 1; } else { @@ -180,7 +181,7 @@ pub fn interact( progress: 0., }); } - tile.item = Some(item); + *this = Some(item); return Some(InteractEffect::Put); } } @@ -189,14 +190,14 @@ pub fn interact( Recipe::Instant { inputs, outputs, .. } => { - let on_tile = tile.item.as_ref().map(|i| i.kind); - let in_hand = player.item.as_ref().map(|i| i.kind); + let on_tile = this.as_ref().map(|i| i.kind); + let in_hand = other.as_ref().map(|i| i.kind); let ok = (inputs[0] == on_tile && inputs[1] == in_hand) || (inputs[1] == on_tile && inputs[0] == in_hand); if ok { info!("instant recipe {ri:?}"); - player.item = outputs[0].map(|kind| Item { kind, active: None }); - tile.item = outputs[1].map(|kind| Item { kind, active: None }); + *other = outputs[0].map(|kind| Item { kind, active: None }); + *this = outputs[1].map(|kind| Item { kind, active: None }); return Some(InteractEffect::Produce); } } @@ -205,15 +206,15 @@ pub fn interact( } } - if interactable && tile.item.is_none() { - if let Some(item) = player.item.take() { - tile.item = Some(item); + if interactable && this.is_none() { + if let Some(item) = other.take() { + *this = Some(item); return Some(InteractEffect::Put); } } - if player.item.is_none() { - if let Some(item) = tile.item.take() { - player.item = Some(item); + if other.is_none() { + if let Some(item) = this.take() { + *other = Some(item); return Some(InteractEffect::Take); } } |