feat: sync game

This commit is contained in:
2021-12-19 18:40:14 +01:00
parent f70661694b
commit 5d366b6f6c
11 changed files with 142 additions and 4 deletions

View File

@@ -111,11 +111,13 @@ public:
virtual bool OnDeath(World* world) { return true; }
MobID getMobID() const { return m_ID; }
const TowerImmunities& getTowerImmunities() const { return getMobTowerImmunities(getType(), m_Level); }
const EffectImmunities& getEffectImmunities() const { return getMobEffectImmunities(getType(), m_Level); }
PlayerID getSender() const { return m_Sender; }
MobLevel getLevel() const { return m_Level; }
const MobStats* getStats() const { return getMobStats(getType(), m_Level); }
void setHealth(float newHealth) { m_Health = newHealth; }
float getHealth() const { return m_Health; }
bool isDead() const { return m_Health <= 0; }
bool isAlive() const { return m_Health > 0; }

View File

@@ -20,6 +20,8 @@ public:
virtual void HandlePacket(const protocol::UpgradeTowerPacket* packet);
virtual void HandlePacket(const protocol::WorldAddTowerPacket* packet);
virtual void HandlePacket(const protocol::RemoveTowerPacket* packet);
virtual void HandlePacket(const protocol::UpdateMobStatesPacket* packet);
virtual void HandlePacket(const protocol::UpdateCastleLifePacket* packet);
};

View File

@@ -13,8 +13,9 @@ class ServerGame : public game::Game, public game::GameListener {
private:
Server* m_Server;
ServerWorld m_ServerWorld;
utils::AutoTimer m_GoldMineTimer{ 1000, std::bind(&ServerGame::updateGoldMines, this) };
utils::CooldownTimer m_EndGameCooldown{ 1000 * 10 };
utils::AutoTimer m_GoldMineTimer;
utils::AutoTimer m_MobStatesTimer;
utils::CooldownTimer m_EndGameCooldown;
public:
ServerGame(Server* server);
~ServerGame() {}
@@ -32,6 +33,7 @@ public:
virtual void OnGameClose();
private:
void balanceTeams();
void updateMobStates();
void updateGoldMines();
void updatePlayerStats();
};

View File

@@ -23,6 +23,8 @@ public:
virtual void OnMobDie(game::Mob* mob);
virtual void OnMobCastleDamage(game::Mob* damager, game::TeamCastle* enemyCastle, float damage);
};
} // namespace server

View File

@@ -38,6 +38,8 @@ public:
virtual void HandlePacket(const RemoveTowerPacket* packet) {}
virtual void HandlePacket(const SendMobsPacket* packet) {}
virtual void HandlePacket(const UpgradeTowerPacket* packet) {}
virtual void HandlePacket(const UpdateMobStatesPacket* packet) {}
virtual void HandlePacket(const UpdateCastleLifePacket* packet) {}
};
} // namespace protocol

View File

@@ -33,6 +33,8 @@ enum class PacketType : std::uint8_t {
UpdatePlayerTeam,
ServerTps,
WorldAddTower,
UpdateMobStates,
UpdateCastleLife,
// client <--> server
KeepAlive,
@@ -515,5 +517,60 @@ public:
virtual PacketType getType() const { return PacketType::UpgradeTower; }
};
class MobState {
using Point = utils::shape::Point;
private:
game::MobID m_MobID;
Point m_MobPosition;
float m_MobLife;
game::Direction m_MobDirection;
public:
MobState() {}
MobState(game::MobID id, Point position, float life, game::Direction direction) :
m_MobID(id), m_MobPosition(position), m_MobLife(life), m_MobDirection(direction) {}
game::MobID getMobId() const { return m_MobID; }
Point getMobPosition() const { return m_MobPosition; }
float getMobLife() const { return m_MobLife; }
game::Direction getMobDirection() const { return m_MobDirection; }
};
class UpdateMobStatesPacket : public Packet {
private:
std::vector<MobState> m_MobStates;
public:
UpdateMobStatesPacket() {}
virtual ~UpdateMobStatesPacket() {}
virtual DataBuffer Serialize() const;
virtual void Deserialize(DataBuffer& data);
virtual void Dispatch(PacketHandler* handler) const;
void addMobState(MobState mobState) { m_MobStates.push_back(mobState); }
const std::vector<MobState>& getMobStates() const { return m_MobStates; }
virtual PacketType getType() const { return PacketType::UpdateMobStates; }
};
class UpdateCastleLifePacket : public Packet {
private:
std::uint16_t m_CastleLife;
game::TeamColor m_Team;
public:
UpdateCastleLifePacket() {}
UpdateCastleLifePacket(std::uint16_t life, game::TeamColor team) : m_CastleLife(life), m_Team(team) {}
virtual ~UpdateCastleLifePacket() {}
virtual DataBuffer Serialize() const;
virtual void Deserialize(DataBuffer& data);
virtual void Dispatch(PacketHandler* handler) const;
std::uint16_t getCastleLife() const { return m_CastleLife; }
game::TeamColor getTeamColor() const { return m_Team; }
virtual PacketType getType() const { return PacketType::UpdateCastleLife; }
};
}
}