aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/protocol/src/lib.rs9
-rw-r--r--server/protocol/src/movement.rs8
-rw-r--r--server/src/entity/customers/mod.rs14
-rw-r--r--server/src/game.rs109
4 files changed, 73 insertions, 67 deletions
diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs
index a56b6edb..f160a751 100644
--- a/server/protocol/src/lib.rs
+++ b/server/protocol/src/lib.rs
@@ -181,11 +181,6 @@ pub enum PacketC {
kind: Option<TileIndex>,
neighbors: [Option<TileIndex>; 4],
},
- Collide {
- player: PlayerID,
- #[bincode(with_serde)]
- force: Vec2,
- },
Communicate {
player: PlayerID,
message: Option<Message>,
@@ -203,7 +198,9 @@ pub enum PacketC {
message: String,
},
Menu(Menu),
- MovementSync,
+ MovementSync {
+ player: PlayerID,
+ },
Environment {
effects: HashSet<String>,
},
diff --git a/server/protocol/src/movement.rs b/server/protocol/src/movement.rs
index 5525c5e6..dad9d300 100644
--- a/server/protocol/src/movement.rs
+++ b/server/protocol/src/movement.rs
@@ -29,6 +29,7 @@ const BOOST_DURATION: f32 = 0.3;
const BOOST_RESTORE: f32 = 0.5;
pub struct MovementBase {
+ pub direction: Vec2,
pub position: Vec2,
pub facing: Vec2,
pub rotation: f32,
@@ -41,6 +42,7 @@ impl MovementBase {
pub fn new(position: Vec2) -> Self {
Self {
position,
+ direction: Vec2::ZERO,
facing: Vec2::X,
velocity: Vec2::ZERO,
boosting: false,
@@ -49,7 +51,7 @@ impl MovementBase {
}
}
pub fn update(&mut self, map: &HashSet<IVec2>, direction: Vec2, mut boost: bool, dt: f32) {
- let direction = direction.clamp_length_max(1.);
+ self.direction = direction.clamp_length_max(1.);
if direction.length() > 0.1 {
self.facing = direction + (self.facing - direction) * (-dt * 10.).exp();
}
@@ -69,11 +71,11 @@ impl MovementBase {
collide_player_tiles(self, map);
}
- pub fn movement_packet(&self, direction: Vec2, player: PlayerID) -> PacketS {
+ pub fn movement_packet(&self, player: PlayerID) -> PacketS {
PacketS::Movement {
pos: Some(self.position),
boosting: self.boosting,
- direction,
+ direction: self.direction,
player,
}
}
diff --git a/server/src/entity/customers/mod.rs b/server/src/entity/customers/mod.rs
index e6067110..0bb3f918 100644
--- a/server/src/entity/customers/mod.rs
+++ b/server/src/entity/customers/mod.rs
@@ -33,7 +33,6 @@ pub struct Customers {
demands: Vec<Demand>,
cpackets: VecDeque<PacketS>,
chairs: HashMap<IVec2, bool>,
- customer_id_counter: PlayerID,
customers: HashMap<PlayerID, CustomerState>,
spawn_cooldown: f32,
}
@@ -67,7 +66,6 @@ impl Customers {
}
Ok(Self {
chairs,
- customer_id_counter: PlayerID(0),
customers: Default::default(),
demands,
spawn_cooldown: 0.,
@@ -82,12 +80,12 @@ impl EntityT for Customers {
self.spawn_cooldown = self.spawn_cooldown.max(0.);
if self.customers.len() < 5 && self.spawn_cooldown <= 0. {
self.spawn_cooldown = 10. + random::<f32>() * 10.;
- self.customer_id_counter.0 -= 1;
- let id = self.customer_id_counter;
- self.cpackets.push_back(PacketS::Join {
- name: faker::name::fr_fr::Name().fake(),
- character: -1 - (random::<u16>() as i32),
- });
+ let id = game.join_player(
+ faker::name::fr_fr::Name().fake(),
+ -1 - (random::<u16>() as i32),
+ packet_out,
+ );
+
let chair = self.select_chair().ok_or(anyhow!("no free chair found"))?;
let from = game.data.customer_spawn.as_ivec2();
let path = find_path(&game.walkable, from, chair)
diff --git a/server/src/game.rs b/server/src/game.rs
index 6477d9fa..527ea65b 100644
--- a/server/src/game.rs
+++ b/server/src/game.rs
@@ -178,6 +178,7 @@ impl Game {
} else {
self.data.chef_spawn
},
+ direction: Vec2::ZERO,
facing: Vec2::X,
rotation: 0.,
velocity: Vec2::ZERO,
@@ -273,6 +274,55 @@ impl Game {
out
}
+ pub fn join_player(
+ &mut self,
+ name: String,
+ character: i32,
+ packet_out: &mut VecDeque<PacketC>,
+ ) -> PlayerID {
+ let id = self.player_id_counter;
+ self.player_id_counter.0 += 1;
+ let position = if id.0 < 0 {
+ self.data.customer_spawn
+ } else {
+ self.data.chef_spawn
+ };
+ self.players.insert(
+ id,
+ Player {
+ item: None,
+ character,
+ movement: MovementBase {
+ position: if character < 0 {
+ self.data.customer_spawn
+ } else {
+ self.data.chef_spawn
+ },
+ direction: Vec2::ZERO,
+ facing: Vec2::X,
+ rotation: 0.,
+ velocity: Vec2::ZERO,
+ boosting: false,
+ stamina: 0.,
+ },
+ last_position_update: Instant::now(),
+ boost: false,
+ direction: Vec2::ZERO,
+ communicate_persist: None,
+ interacting: None,
+ name: name.clone(),
+ },
+ );
+ self.score.players = self.score.players.max(self.players.len());
+ packet_out.push_back(PacketC::AddPlayer {
+ id,
+ name,
+ position,
+ character,
+ });
+ id
+ }
+
pub fn packet_in(
&mut self,
packet: PacketS,
@@ -281,48 +331,7 @@ impl Game {
) -> Result<()> {
match packet {
PacketS::Join { name, character } => {
- let id = self.player_id_counter;
- self.player_id_counter.0 += 1;
- if self.players.contains_key(&id) {
- bail!("You already joined.")
- }
- let position = if id.0 < 0 {
- self.data.customer_spawn
- } else {
- self.data.chef_spawn
- };
- self.players.insert(
- id,
- Player {
- item: None,
- character,
- movement: MovementBase {
- position: if character < 0 {
- self.data.customer_spawn
- } else {
- self.data.chef_spawn
- },
- facing: Vec2::X,
- rotation: 0.,
- velocity: Vec2::ZERO,
- boosting: false,
- stamina: 0.,
- },
- last_position_update: Instant::now(),
- boost: false,
- direction: Vec2::ZERO,
- communicate_persist: None,
- interacting: None,
- name: name.clone(),
- },
- );
- self.score.players = self.score.players.max(self.players.len());
- packet_out.push_back(PacketC::AddPlayer {
- id,
- name,
- position,
- character,
- });
+ let id = self.join_player(name, character, packet_out);
replies.push(PacketC::Joined { id })
}
PacketS::Leave { player } => {
@@ -353,22 +362,22 @@ impl Game {
direction,
player,
} => {
- let player = self
+ let pd = self
.players
.get_mut(&player)
.ok_or(anyhow!("player does not exist"))?;
- player.direction = direction;
- player.boost = boosting;
+ pd.direction = direction;
+ pd.boost = boosting;
if let Some(pos) = pos {
- let dt = player.last_position_update.elapsed();
- player.last_position_update += dt;
- let diff = pos - player.movement.position;
- player.movement.position += diff.clamp_length_max(dt.as_secs_f32());
+ let dt = pd.last_position_update.elapsed();
+ pd.last_position_update += dt;
+ let diff = pos - pd.movement.position;
+ pd.movement.position += diff.clamp_length_max(dt.as_secs_f32());
if diff.length() > 1. {
- replies.push(PacketC::MovementSync);
+ replies.push(PacketC::MovementSync { player });
}
}
}