aboutsummaryrefslogtreecommitdiff
path: root/server/src/game.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-07-18 15:42:11 +0200
committermetamuffin <metamuffin@disroot.org>2024-07-18 15:42:11 +0200
commit5f883c80e7fc63c97910d003c44aea814ab8a5d6 (patch)
treeb73a8c8f78db103671128e686136f08aa276923a /server/src/game.rs
parentefc29c03f7be043ae8d037a93efce8cfa7c384cc (diff)
downloadhurrycurry-5f883c80e7fc63c97910d003c44aea814ab8a5d6.tar
hurrycurry-5f883c80e7fc63c97910d003c44aea814ab8a5d6.tar.bz2
hurrycurry-5f883c80e7fc63c97910d003c44aea814ab8a5d6.tar.zst
reimplement customers as entity
Diffstat (limited to 'server/src/game.rs')
-rw-r--r--server/src/game.rs105
1 files changed, 45 insertions, 60 deletions
diff --git a/server/src/game.rs b/server/src/game.rs
index b3b23ce0..370c2e8f 100644
--- a/server/src/game.rs
+++ b/server/src/game.rs
@@ -16,7 +16,6 @@
*/
use crate::{
- customer::DemandState,
data::Gamedata,
entity::{Entity, EntityT},
interaction::{interact, tick_slot, InteractEffect, TickEffect},
@@ -32,7 +31,7 @@ use hurrycurry_protocol::{
use log::{info, warn};
use std::{
collections::{HashMap, HashSet, VecDeque},
- sync::Arc,
+ sync::{Arc, RwLock},
time::{Duration, Instant},
};
@@ -62,37 +61,44 @@ pub struct Player {
pub communicate_persist: Option<Message>,
movement: MovementBase,
- direction: Vec2,
- boost: bool,
- last_position_update: Instant,
+ pub direction: Vec2,
+ pub boost: bool,
+ pub last_position_update: Instant,
}
pub struct Game {
pub data: Arc<Gamedata>,
- tiles: HashMap<IVec2, Tile>,
- walkable: HashSet<IVec2>,
+ pub tiles: HashMap<IVec2, Tile>,
+ pub walkable: HashSet<IVec2>,
pub players: HashMap<PlayerID, Player>,
players_spatial_index: SpatialIndex<PlayerID>,
- packet_out: VecDeque<PacketC>,
- demand: Option<DemandState>,
- pub points: i64,
- entities: Vec<Entity>,
+ pub packet_out: VecDeque<PacketC>,
+ entities: Arc<RwLock<Vec<Entity>>>,
end: Option<Instant>,
+ pub lobby: bool,
+
+ pub score_changed: bool,
+ pub points: i64,
+ pub demands_failed: usize,
+ pub demands_completed: usize,
}
impl Game {
pub fn new() -> Self {
Self {
+ lobby: false,
data: Gamedata::default().into(),
packet_out: Default::default(),
players: HashMap::new(),
tiles: HashMap::new(),
walkable: HashSet::new(),
- demand: None,
end: None,
- entities: vec![],
+ entities: Arc::new(RwLock::new(vec![])),
players_spatial_index: SpatialIndex::default(),
points: 0,
+ demands_failed: 0,
+ demands_completed: 0,
+ score_changed: false,
}
}
@@ -111,7 +117,7 @@ impl Game {
neighbors: [None, None, None, None],
})
}
- self.demand = None;
+ self.walkable.clear();
}
pub fn load(&mut self, gamedata: Gamedata, timer: Option<Duration>) {
let players = self
@@ -126,7 +132,7 @@ impl Game {
self.data = gamedata.into();
self.points = 0;
self.end = timer.map(|dur| Instant::now() + dur);
- self.entities = self.data.entities.clone();
+ self.entities = Arc::new(RwLock::new(self.data.entities.clone()));
for (&p, (tile, item)) in &self.data.initial_map {
self.tiles.insert(
@@ -150,7 +156,11 @@ impl Game {
item: None,
character,
movement: MovementBase {
- position: self.data.chef_spawn,
+ position: if character < 0 {
+ self.data.customer_spawn
+ } else {
+ self.data.chef_spawn
+ },
facing: Vec2::X,
rotation: 0.,
velocity: Vec2::ZERO,
@@ -167,17 +177,9 @@ impl Game {
);
}
- if !self.data.demands.is_empty() {
- self.demand = Some(DemandState::new(self.data.clone(), &self.tiles))
- }
-
self.packet_out.extend(self.prime_client());
}
- pub fn tiles(&self) -> &HashMap<IVec2, Tile> {
- &self.tiles
- }
-
pub fn packet_out(&mut self) -> Option<PacketC> {
self.packet_out.pop_front()
}
@@ -249,7 +251,7 @@ impl Game {
out.push(self.score());
out.push(PacketC::SetIngame {
state: true,
- lobby: self.demand.is_none(),
+ lobby: self.lobby,
});
out
}
@@ -258,12 +260,8 @@ impl Game {
PacketC::Score {
time_remaining: self.end.map(|t| (t - Instant::now()).as_secs_f32()),
points: self.points,
- demands_failed: self.demand.as_ref().map(|d| d.failed).unwrap_or_default(),
- demands_completed: self
- .demand
- .as_ref()
- .map(|d| d.completed)
- .unwrap_or_default(),
+ demands_failed: self.demands_failed,
+ demands_completed: self.demands_completed,
}
}
pub fn packet_in(&mut self, player: PlayerID, packet: PacketS) -> Result<()> {
@@ -285,7 +283,11 @@ impl Game {
item: None,
character,
movement: MovementBase {
- position: self.data.chef_spawn,
+ position: if character < 0 {
+ self.data.customer_spawn
+ } else {
+ self.data.chef_spawn
+ },
facing: Vec2::X,
rotation: 0.,
velocity: Vec2::ZERO,
@@ -471,26 +473,9 @@ impl Game {
/// Returns true if the game should end
pub fn tick(&mut self, dt: f32) -> bool {
- if let Some(demand) = &mut self.demand {
- let mut packet_out = Vec::new();
- if let Err(err) = demand.tick(
- &mut packet_out,
- &mut self.tiles,
- &self.data,
- dt,
- &mut self.points,
- ) {
- warn!("demand tick {err}");
- }
- if demand.score_changed {
- demand.score_changed = false;
- self.packet_out.push_back(self.score());
- }
- for (player, packet) in packet_out {
- if let Err(err) = self.packet_in(player, packet) {
- warn!("demand packet {err}");
- }
- }
+ if self.score_changed {
+ self.score_changed = false;
+ self.packet_out.push_back(self.score());
}
for (&pos, tile) in &mut self.tiles {
@@ -583,14 +568,8 @@ impl Game {
let _ = self.packet_in(pid, PacketS::Interact { pos: None });
}
- for entity in &mut self.entities {
- if let Err(e) = entity.tick(
- &self.data,
- &mut self.points,
- &mut self.packet_out,
- &mut self.tiles,
- dt,
- ) {
+ for entity in self.entities.clone().write().unwrap().iter_mut() {
+ if let Err(e) = entity.tick(self, dt) {
warn!("entity tick failed: {e}")
}
}
@@ -686,3 +665,9 @@ pub fn interact_effect(
}
}
}
+
+impl Player {
+ pub fn position(&self) -> Vec2 {
+ self.movement.position
+ }
+}