summaryrefslogtreecommitdiff
path: root/server/src/entity/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/entity/mod.rs')
-rw-r--r--server/src/entity/mod.rs70
1 files changed, 37 insertions, 33 deletions
diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs
index 10d0c155..efee6a6d 100644
--- a/server/src/entity/mod.rs
+++ b/server/src/entity/mod.rs
@@ -15,6 +15,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+pub mod bot;
pub mod conveyor;
pub mod customers;
pub mod environment_effect;
@@ -29,44 +30,51 @@ use environment_effect::{EnvironmentController, EnvironmentEffect, EnvironmentEf
use hurrycurry_client_lib::Game;
use hurrycurry_protocol::{
glam::{IVec2, Vec2},
- ItemIndex, PacketC, Recipe, TileIndex,
+ ItemIndex, PacketC, PacketS, Recipe, TileIndex,
};
use item_portal::ItemPortal;
use player_portal::PlayerPortal;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};
+pub type DynEntity = Box<dyn Entity + Send + Sync + 'static>;
+pub type Entities = Vec<DynEntity>;
+
pub struct EntityContext<'a> {
pub game: &'a mut Game,
pub packet_out: &'a mut VecDeque<PacketC>,
+ pub packet_in: &'a mut VecDeque<PacketS>,
pub score_changed: &'a mut bool,
pub dt: f32,
}
-pub trait EntityT: Clone {
+pub trait Entity {
fn tick(&mut self, c: EntityContext<'_>) -> Result<()>;
+ fn destructor(&mut self, _c: EntityContext<'_>) {}
}
-macro_rules! entities {
- ($($e:ident),*) => {
- #[derive(Debug, Clone)]
- pub enum Entity { $($e($e)),* }
- impl EntityT for Entity {
- fn tick(&mut self, c: EntityContext<'_>) -> Result<()> {
- match self { $(Entity::$e(x) => x.tick(c)),*, }
- }
- }
- };
-}
-
-entities!(
- Conveyor,
- ItemPortal,
- PlayerPortal,
- Customers,
- EnvironmentEffectController,
- EnvironmentController
-);
+// macro_rules! entities {
+// ($($e:ident),*) => {
+// #[derive(Debug)]
+// pub enum Entity { $($e($e)),* }
+// impl EntityT for Entity {
+// fn tick(&mut self, c: EntityContext<'_>) -> Result<()> {
+// match self { $(Entity::$e(x) => x.tick(c)),*, }
+// }
+// fn destructor(&mut self, c: EntityContext<'_>) {
+// match self { $(Entity::$e(x) => x.destructor(c)),*, }
+// }
+// }
+// };
+// }
+// entities!(
+// Conveyor,
+// ItemPortal,
+// PlayerPortal,
+// Customers,
+// EnvironmentEffectController,
+// EnvironmentController
+// );
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
@@ -101,13 +109,13 @@ pub fn construct_entity(
raw_demands: &[(ItemIndex, Option<ItemIndex>, f32)],
recipes: &[Recipe],
initial_map: &HashMap<IVec2, (TileIndex, Option<ItemIndex>)>,
-) -> Result<Entity> {
+) -> Result<DynEntity> {
Ok(match decl.to_owned() {
- EntityDecl::ItemPortal { from, to } => Entity::ItemPortal(ItemPortal {
+ EntityDecl::ItemPortal { from, to } => Box::new(ItemPortal {
from: from.or(pos).ok_or(anyhow!("no item portal start"))?,
to,
}),
- EntityDecl::PlayerPortal { from, to } => Entity::PlayerPortal(PlayerPortal {
+ EntityDecl::PlayerPortal { from, to } => Box::new(PlayerPortal {
from: from
.or(pos.map(|v| v.as_vec2()))
.ok_or(anyhow!("no player portal start"))?,
@@ -125,7 +133,7 @@ pub fn construct_entity(
let to = to
.or(dir.map(|s| s + from))
.ok_or(anyhow!("conveyor has no destination"))?;
- Entity::Conveyor(Conveyor {
+ Box::new(Conveyor {
from,
to,
max_cooldown: 1. / speed.unwrap_or(2.),
@@ -142,13 +150,9 @@ pub fn construct_entity(
.filter(|(_, (tile, _))| *tile == chair)
.map(|(e, _)| (*e, true))
.collect();
- Entity::Customers(Customers::new(chairs, demands)?)
- }
- EntityDecl::EnvironmentEffect(config) => {
- Entity::EnvironmentEffectController(EnvironmentEffectController::new(config))
- }
- EntityDecl::Environment(names) => {
- Entity::EnvironmentController(EnvironmentController(names))
+ Box::new(Customers::new(chairs, demands)?)
}
+ EntityDecl::EnvironmentEffect(config) => Box::new(EnvironmentEffectController::new(config)),
+ EntityDecl::Environment(names) => Box::new(EnvironmentController(names)),
})
}