From 88d26442196190207d23880f4056856199fe88f0 Mon Sep 17 00:00:00 2001 From: Persson Date: Sat, 21 Aug 2021 11:46:21 +0200 Subject: [PATCH] changed team structure --- include/game/Mobs.h | 9 +-------- include/game/Team.h | 20 +++++++++++++++++++ include/game/Types.h | 16 ++++++++++++++++ include/game/World.h | 30 +++++++++-------------------- src/game/World.cpp | 32 +++++++++++++++++++++++++++---- src/game/server/ServerWorld.cpp | 4 ++-- src/protocol/Protocol.cpp | 4 ++-- src/render/loader/WorldLoader.cpp | 4 +++- 8 files changed, 81 insertions(+), 38 deletions(-) create mode 100644 include/game/Types.h diff --git a/include/game/Mobs.h b/include/game/Mobs.h index 1a88e6e..9cf73f3 100644 --- a/include/game/Mobs.h +++ b/include/game/Mobs.h @@ -1,21 +1,14 @@ #pragma once #include "Towers.h" +#include "Types.h" -#include #include #include namespace td { namespace game { -enum class Direction : std::uint8_t{ - PositiveX = 1 << 0, - NegativeX = 1 << 1, - PositiveY = 1 << 2, - NegativeY = 1 << 3, -}; - enum class EffectType : std::uint8_t{ Slowness = 0, Stun, diff --git a/include/game/Team.h b/include/game/Team.h index c6e750d..cf38d36 100644 --- a/include/game/Team.h +++ b/include/game/Team.h @@ -1,5 +1,7 @@ #pragma once +#include "Types.h" + #include #include #include @@ -15,10 +17,22 @@ enum class TeamColor : std::int8_t{ Blue }; +struct Spawn{ + Direction direction; + std::int32_t x, y; +}; + +struct TeamCastle{ + std::int32_t x, y; + std::uint16_t life = 1000; +}; + class Team{ private: std::vector m_Players; TeamColor m_Color; + Spawn m_Spawn; + TeamCastle m_TeamCastle; public: Team(TeamColor color); @@ -27,6 +41,12 @@ public: TeamColor getColor() const; + const Spawn& getSpawn() const {return m_Spawn;} + Spawn& getSpawn() {return m_Spawn;} + + const TeamCastle& getCastle() const {return m_TeamCastle;} + TeamCastle& getCastle() {return m_TeamCastle;} + std::uint8_t getPlayerCount() const; }; diff --git a/include/game/Types.h b/include/game/Types.h new file mode 100644 index 0000000..dea29b7 --- /dev/null +++ b/include/game/Types.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +namespace td { +namespace game { + +enum class Direction : std::uint8_t{ + PositiveX = 1 << 0, + NegativeX = 1 << 1, + PositiveY = 1 << 2, + NegativeY = 1 << 3, +}; + +} // namespace game +} // namespace td diff --git a/include/game/World.h b/include/game/World.h index b7995f2..3dcd5c6 100644 --- a/include/game/World.h +++ b/include/game/World.h @@ -76,16 +76,6 @@ struct DecorationTile : Tile{ virtual TileType getType() const{ return TileType::Decoration; } }; -struct Spawn{ - Direction direction; - std::int32_t x, y; -}; - -struct TeamCastle{ - std::int32_t x, y; - std::uint16_t life = 1000; -}; - typedef std::shared_ptr TilePtr; typedef std::vector ChunkPalette; @@ -130,9 +120,6 @@ protected: SpawnColorPalette m_SpawnColorPalette; - Spawn m_Spawns[2]; - TeamCastle m_Castles[2]; - TilePalette m_TilePalette; MobList m_Mobs; @@ -167,21 +154,22 @@ public: const std::unordered_map& getChunks() const{ return m_Chunks; } - const Spawn& getRedSpawn() const{ return m_Spawns[(std::size_t) TeamColor::Red]; } - const Spawn& getBlueSpawn() const{ return m_Spawns[(std::size_t) TeamColor::Blue]; } - - const Spawn& getSpawn(TeamColor color) const{ return m_Spawns[(std::size_t) color]; } - const Color& getSpawnColor(TeamColor color) const{ return m_SpawnColorPalette[(std::size_t) color]; } const SpawnColorPalette& getSpawnColors() const{ return m_SpawnColorPalette; } - const TeamCastle& getRedCastle() const{ return m_Castles[(std::size_t) TeamColor::Red]; } - const TeamCastle& getBlueCastle() const{ return m_Castles[(std::size_t) TeamColor::Blue]; } - const MobList& getMobList() const{ return m_Mobs; } MobList& getMobList(){ return m_Mobs; } const Color& getTileColor(TilePtr tile) const; + + Team& getRedTeam(); + const Team& getRedTeam() const; + + Team& getBlueTeam(); + const Team& getBlueTeam() const; + + Team& getTeam(TeamColor team); + const Team& getTeam(TeamColor team) const; private: void moveMobs(std::uint64_t delta); }; diff --git a/src/game/World.cpp b/src/game/World.cpp index 483e860..137f5e9 100644 --- a/src/game/World.cpp +++ b/src/game/World.cpp @@ -41,13 +41,13 @@ bool World::loadMap(const protocol::WorldBeginDataPacket* worldHeader){ m_WalkablePalette = worldHeader->getWalkableTileColor(); m_DecorationPalette = worldHeader->getDecorationPalette(); - m_Spawns[(std::size_t) TeamColor::Red] = worldHeader->getRedSpawn(); - m_Spawns[(std::size_t) TeamColor::Blue] = worldHeader->getBlueSpawn(); + getRedTeam().getSpawn() = worldHeader->getRedSpawn(); + getBlueTeam().getSpawn() = worldHeader->getBlueSpawn(); m_SpawnColorPalette = worldHeader->getSpawnPalette(); - m_Castles[(std::size_t) TeamColor::Red] = worldHeader->getRedCastle(); - m_Castles[(std::size_t) TeamColor::Blue] = worldHeader->getBlueCastle(); + getRedTeam().getCastle() = worldHeader->getRedCastle(); + getBlueTeam().getCastle() = worldHeader->getBlueCastle(); m_TilePalette = worldHeader->getTilePalette(); } @@ -382,5 +382,29 @@ const Color& World::getTileColor(TilePtr tile) const{ return m_DecorationPalette[0]; } +Team& World::getRedTeam(){ + return m_Game->getRedTeam(); +} + +const Team& World::getRedTeam() const{ + return m_Game->getRedTeam(); +} + +Team& World::getBlueTeam(){ + return m_Game->getBlueTeam(); +} + +const Team& World::getBlueTeam() const{ + return m_Game->getBlueTeam(); +} + +Team& World::getTeam(TeamColor team){ + return m_Game->getTeam(team); +} + +const Team& World::getTeam(TeamColor team) const{ + return m_Game->getTeam(team); +} + } // namespace game } // namespace td diff --git a/src/game/server/ServerWorld.cpp b/src/game/server/ServerWorld.cpp index 90360ec..d304a24 100644 --- a/src/game/server/ServerWorld.cpp +++ b/src/game/server/ServerWorld.cpp @@ -17,9 +17,9 @@ void ServerWorld::spawnMobs(game::MobType type, std::uint8_t level, game::Player game::Spawn* enemyMobSpawn; if(senderTeam == game::TeamColor::Red){ - enemyMobSpawn = &m_Spawns[(std::size_t) game::TeamColor::Blue]; + enemyMobSpawn = &getTeam(game::TeamColor::Blue).getSpawn(); }else{ - enemyMobSpawn = &m_Spawns[(std::size_t) game::TeamColor::Red]; + enemyMobSpawn = &getTeam(game::TeamColor::Red).getSpawn(); } std::int32_t spawnCenterX = enemyMobSpawn->x; diff --git a/src/protocol/Protocol.cpp b/src/protocol/Protocol.cpp index 255964a..3233218 100644 --- a/src/protocol/Protocol.cpp +++ b/src/protocol/Protocol.cpp @@ -87,8 +87,8 @@ DataBuffer WorldBeginDataPacket::Serialize() const{ memcpy((void*)data.data() + bufferSize, decoTilePalette.data(), decoTilePalette.size() * sizeof(game::Color)); - const game::Spawn& redSpawn = m_World->getRedSpawn(), blueSpawn = m_World->getBlueSpawn(); - const game::TeamCastle& redCastle = m_World->getRedCastle(), blueCastle = m_World->getBlueCastle(); + const game::Spawn& redSpawn = m_World->getRedTeam().getSpawn(), blueSpawn = m_World->getBlueTeam().getSpawn(); + const game::TeamCastle& redCastle = m_World->getRedTeam().getCastle(), blueCastle = m_World->getBlueTeam().getCastle(); data << redSpawn << redCastle; data << blueSpawn << blueCastle; diff --git a/src/render/loader/WorldLoader.cpp b/src/render/loader/WorldLoader.cpp index f128b47..9188763 100644 --- a/src/render/loader/WorldLoader.cpp +++ b/src/render/loader/WorldLoader.cpp @@ -3,6 +3,8 @@ #include #include +#include "game/BaseGame.h" + namespace td { namespace render { @@ -114,7 +116,7 @@ GL::VAO loadWorldModel(const td::game::World* world){ } for (int spawnColor = 0; spawnColor < 2; spawnColor++){ - const game::Spawn& spawn = world->getSpawn(game::TeamColor(spawnColor)); + const game::Spawn& spawn = world->getTeam(game::TeamColor(spawnColor)).getSpawn(); float fromX = spawn.x - 2, toX = spawn.x + 3; float fromY = spawn.y - 2, toY = spawn.y + 3;