1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 18:24:05 +02:00

building fixes

This commit is contained in:
Robin Appelman 2022-08-27 14:31:15 +02:00
commit cf58a0609a

View file

@ -14,6 +14,7 @@ use crate::demo::sendprop::{SendProp, SendPropIdentifier, SendPropValue};
use crate::demo::vector::{Vector, VectorXY};
use crate::{MessageType, ParserState, ReadResult, Stream};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::convert::TryFrom;
use std::str::FromStr;
@ -256,7 +257,7 @@ impl Kill {
#[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
pub struct GameState {
pub players: Vec<Player>,
pub buildings: Vec<Building>,
pub buildings: BTreeMap<EntityId, Building>,
pub world: Option<World>,
pub kills: Vec<Kill>,
pub tick: u32,
@ -289,37 +290,13 @@ impl GameState {
entity_id: EntityId,
class: BuildingClass,
) -> &mut Building {
let index = match self
.buildings
.iter()
.enumerate()
.find(|(_index, building)| building.entity_id() == entity_id)
.map(|(index, _)| index)
{
Some(index) => index,
None => {
let index = self.buildings.len();
self.buildings.push(Building::new(entity_id, class));
index
}
};
&mut self.buildings[index]
self.buildings
.entry(entity_id)
.or_insert_with(|| Building::new(entity_id, class))
}
pub fn remove_building(&mut self, entity_id: EntityId) {
match self
.buildings
.iter()
.enumerate()
.find(|(_index, building)| building.entity_id() == entity_id)
.map(|(index, _)| index)
{
Some(index) => {
self.buildings.remove(index);
}
_ => {}
};
self.buildings.remove(&entity_id);
}
}
@ -354,6 +331,9 @@ impl MessageHandler for GameStateAnalyser {
GameEvent::RoundStart(_) => {
self.state.buildings.clear();
}
GameEvent::TeamPlayRoundStart(_) => {
self.state.buildings.clear();
}
GameEvent::ObjectDestroyed(ObjectDestroyedEvent { index, .. }) => {
self.state.remove_building((*index as u32).into());
}