From cf24bc076e9d3746210e507875a7178df19b51b1 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Wed, 26 Jun 2024 13:19:07 +0200 Subject: generalize interact --- server/src/interaction.rs | 61 ++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 30 deletions(-) (limited to 'server/src/interaction.rs') 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 . - + */ 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, + other: &mut Option, ) -> Option { - 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); } } -- cgit v1.2.3-70-g09d2