aboutsummaryrefslogtreecommitdiff
path: root/server/src/game.rs
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/game.rs')
-rw-r--r--server/src/game.rs67
1 files changed, 36 insertions, 31 deletions
diff --git a/server/src/game.rs b/server/src/game.rs
index 1f368b00..21bb5f33 100644
--- a/server/src/game.rs
+++ b/server/src/game.rs
@@ -72,7 +72,6 @@ pub struct Game {
pub walkable: HashSet<IVec2>,
pub players: HashMap<PlayerID, Player>,
players_spatial_index: SpatialIndex<PlayerID>,
- pub packet_out: VecDeque<PacketC>,
entities: Arc<RwLock<Vec<Entity>>>,
end: Option<Instant>,
pub lobby: bool,
@@ -92,7 +91,6 @@ impl Game {
Self {
lobby: false,
data: Gamedata::default().into(),
- packet_out: Default::default(),
players: HashMap::new(),
tiles: HashMap::new(),
walkable: HashSet::new(),
@@ -104,16 +102,16 @@ impl Game {
}
}
- fn unload(&mut self) {
- self.packet_out.push_back(PacketC::SetIngame {
+ fn unload(&mut self, packet_out: &mut VecDeque<PacketC>) {
+ packet_out.push_back(PacketC::SetIngame {
state: false,
lobby: false,
});
for (id, _) in self.players.drain() {
- self.packet_out.push_back(PacketC::RemovePlayer { id })
+ packet_out.push_back(PacketC::RemovePlayer { id })
}
for (pos, _) in self.tiles.drain() {
- self.packet_out.push_back(PacketC::UpdateMap {
+ packet_out.push_back(PacketC::UpdateMap {
tile: pos,
kind: None,
neighbors: [None, None, None, None],
@@ -121,7 +119,12 @@ impl Game {
}
self.walkable.clear();
}
- pub fn load(&mut self, gamedata: Gamedata, timer: Option<Duration>) {
+ pub fn load(
+ &mut self,
+ gamedata: Gamedata,
+ timer: Option<Duration>,
+ packet_out: &mut VecDeque<PacketC>,
+ ) {
let players = self
.players
.iter()
@@ -129,7 +132,7 @@ impl Game {
.map(|(id, p)| (*id, (p.name.to_owned(), p.character)))
.collect::<HashMap<_, _>>();
- self.unload();
+ self.unload(packet_out);
self.lobby = gamedata.map_name == "lobby";
self.data = gamedata.into();
@@ -180,7 +183,7 @@ impl Game {
);
}
- self.packet_out.extend(self.prime_client());
+ packet_out.extend(self.prime_client());
}
pub fn prime_client(&self) -> Vec<PacketC> {
@@ -261,6 +264,7 @@ impl Game {
player: PlayerID,
packet: PacketS,
replies: &mut Vec<PacketC>,
+ packet_out: &mut VecDeque<PacketC>,
) -> Result<()> {
match packet {
PacketS::Join { name, character } => {
@@ -297,7 +301,7 @@ impl Game {
name: name.clone(),
},
);
- self.packet_out.push_back(PacketC::AddPlayer {
+ packet_out.push_back(PacketC::AddPlayer {
id: player,
name,
position,
@@ -316,7 +320,7 @@ impl Game {
let pos = p.movement.position.floor().as_ivec2();
if let Some(tile) = self.tiles.get_mut(&pos) {
if tile.item.is_none() {
- self.packet_out.push_back(PacketC::SetItem {
+ packet_out.push_back(PacketC::SetItem {
location: ItemLocation::Tile(pos),
item: Some(item.kind),
});
@@ -324,8 +328,7 @@ impl Game {
}
}
}
- self.packet_out
- .push_back(PacketC::RemovePlayer { id: player })
+ packet_out.push_back(PacketC::RemovePlayer { id: player })
}
PacketS::Movement {
pos,
@@ -352,8 +355,7 @@ impl Game {
}
}
PacketS::Collide { player, force } => {
- self.packet_out
- .push_back(PacketC::Collide { player, force });
+ packet_out.push_back(PacketC::Collide { player, force });
}
PacketS::Interact { pos } => {
let pid = player;
@@ -410,7 +412,7 @@ impl Game {
&mut other.item,
ItemLocation::Player(pid),
None,
- &mut self.packet_out,
+ packet_out,
&mut self.score,
false,
)
@@ -428,7 +430,7 @@ impl Game {
&mut player.item,
ItemLocation::Player(pid),
Some(tile.kind),
- &mut self.packet_out,
+ packet_out,
&mut self.score,
false,
)
@@ -441,7 +443,7 @@ impl Game {
player.communicate_persist = message.clone()
}
}
- self.packet_out.push_back(PacketC::Communicate {
+ packet_out.push_back(PacketC::Communicate {
player,
message,
persist,
@@ -456,7 +458,7 @@ impl Game {
kind: i,
active: None,
});
- self.packet_out.push_back(PacketC::SetItem {
+ packet_out.push_back(PacketC::SetItem {
location: ItemLocation::Player(player),
item,
})
@@ -467,11 +469,10 @@ impl Game {
}
/// Returns true if the game should end
- pub fn tick(&mut self, dt: f32) -> bool {
+ pub fn tick(&mut self, dt: f32, packet_out: &mut VecDeque<PacketC>) -> bool {
if self.score_changed {
self.score_changed = false;
- self.packet_out
- .push_back(PacketC::Score(self.score.clone()));
+ packet_out.push_back(PacketC::Score(self.score.clone()));
}
for (&pos, tile) in &mut self.tiles {
@@ -483,7 +484,7 @@ impl Game {
&mut self.score,
) {
match effect {
- TickEffect::Progress(warn) => self.packet_out.push_back(PacketC::SetProgress {
+ TickEffect::Progress(warn) => packet_out.push_back(PacketC::SetProgress {
warn,
item: ItemLocation::Tile(pos),
progress: tile
@@ -495,7 +496,7 @@ impl Game {
.map(|i| i.progress),
}),
TickEffect::Produce => {
- self.packet_out.push_back(PacketC::SetItem {
+ packet_out.push_back(PacketC::SetItem {
location: ItemLocation::Tile(pos),
item: tile.item.as_ref().map(|i| i.kind),
});
@@ -522,7 +523,7 @@ impl Game {
});
for (&pid, player) in &mut self.players {
- self.packet_out.push_back(PacketC::Position {
+ packet_out.push_back(PacketC::Position {
player: pid,
pos: player.movement.position,
boosting: player.movement.boosting,
@@ -532,7 +533,7 @@ impl Game {
if let Some(effect) = tick_slot(dt, &self.data, None, &mut player.item, &mut self.score)
{
match effect {
- TickEffect::Progress(warn) => self.packet_out.push_back(PacketC::SetProgress {
+ TickEffect::Progress(warn) => packet_out.push_back(PacketC::SetProgress {
warn,
item: ItemLocation::Player(pid),
progress: player
@@ -544,7 +545,7 @@ impl Game {
.map(|i| i.progress),
}),
TickEffect::Produce => {
- self.packet_out.push_back(PacketC::SetItem {
+ packet_out.push_back(PacketC::SetItem {
location: ItemLocation::Player(pid),
item: player.item.as_ref().map(|i| i.kind),
});
@@ -568,11 +569,16 @@ impl Game {
}
}
for pid in players_auto_release.drain(..) {
- let _ = self.packet_in(pid, PacketS::Interact { pos: None }, &mut vec![]);
+ let _ = self.packet_in(
+ pid,
+ PacketS::Interact { pos: None },
+ &mut vec![],
+ packet_out,
+ );
}
for entity in self.entities.clone().write().unwrap().iter_mut() {
- if let Err(e) = entity.tick(self, dt) {
+ if let Err(e) = entity.tick(self, packet_out, dt) {
warn!("entity tick failed: {e}")
}
}
@@ -580,8 +586,7 @@ impl Game {
if let Some(end) = self.end {
self.score.time_remaining = (end - Instant::now()).as_secs_f64();
if end < Instant::now() {
- self.packet_out
- .push_back(PacketC::Menu(Menu::Score(self.score.clone())));
+ packet_out.push_back(PacketC::Menu(Menu::Score(self.score.clone())));
true
} else {
false