Compare commits
25 Commits
alpha-0.0.
...
alpha-0.1.
| Author | SHA1 | Date | |
|---|---|---|---|
| f70661694b | |||
| 36a1ab0572 | |||
| 409268b604 | |||
| 174d144d26 | |||
| 360258e4cf | |||
| 2148c0050c | |||
| 61166023df | |||
| e7bf22cea6 | |||
| f09f79198d | |||
| 8c19d3cc3c | |||
| 208892d266 | |||
| 4384806cf0 | |||
| 1a091baeaf | |||
| 43f21ffd44 | |||
| 24617c539f | |||
| 4611a198c9 | |||
| d40ffe8f6c | |||
| 384b7ad51e | |||
| fd9b448fa4 | |||
| c4a2b08416 | |||
| a241d7691b | |||
| 0af4cd506c | |||
| 6b5c56b37d | |||
| 1474220a77 | |||
| a802b5cef5 |
@@ -12,14 +12,28 @@ enum class GameState : std::uint8_t {
|
|||||||
Game,
|
Game,
|
||||||
EndGame,
|
EndGame,
|
||||||
Disconnected,
|
Disconnected,
|
||||||
|
Closed
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::map<std::uint8_t, Player> PlayerList;
|
typedef std::map<std::uint8_t, Player> PlayerList;
|
||||||
|
|
||||||
class Game {
|
class GameListener {
|
||||||
|
public:
|
||||||
|
virtual void OnPlayerJoin(PlayerID player) {}
|
||||||
|
virtual void OnPlayerLeave(PlayerID player) {}
|
||||||
|
|
||||||
|
virtual void OnGameStateUpdate(GameState newState) {}
|
||||||
|
virtual void OnGameBegin() {}
|
||||||
|
virtual void OnGameEnd() {}
|
||||||
|
virtual void OnGameClose() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef utils::ObjectNotifier<GameListener> GameNotifier;
|
||||||
|
|
||||||
|
class Game : public GameNotifier {
|
||||||
protected:
|
protected:
|
||||||
World* m_World;
|
World* m_World;
|
||||||
std::array<Team, 2> m_Teams = { Team{TeamColor::Red}, Team{TeamColor::Blue} };
|
TeamList m_Teams = { Team{TeamColor::Red}, Team{TeamColor::Blue} };
|
||||||
GameState m_GameState = GameState::Lobby;
|
GameState m_GameState = GameState::Lobby;
|
||||||
PlayerList m_Players;
|
PlayerList m_Players;
|
||||||
public:
|
public:
|
||||||
@@ -46,8 +60,10 @@ public:
|
|||||||
const PlayerList& getPlayers() const { return m_Players; }
|
const PlayerList& getPlayers() const { return m_Players; }
|
||||||
PlayerList& getPlayers() { return m_Players; }
|
PlayerList& getPlayers() { return m_Players; }
|
||||||
|
|
||||||
const Player& getPlayerById(PlayerID id) const { return m_Players.find(id)->second; }
|
const Player* getPlayerById(PlayerID id) const;
|
||||||
Player& getPlayerById(PlayerID id) { return m_Players.find(id)->second; }
|
Player* getPlayerById(PlayerID id);
|
||||||
|
|
||||||
|
const TeamList& getTeams() const { return m_Teams; }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,8 @@
|
|||||||
#include "Types.h"
|
#include "Types.h"
|
||||||
#include "Team.h"
|
#include "Team.h"
|
||||||
|
|
||||||
|
#include "misc/ObjectNotifier.h"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
@@ -12,6 +14,8 @@
|
|||||||
namespace td {
|
namespace td {
|
||||||
namespace game {
|
namespace game {
|
||||||
|
|
||||||
|
struct WalkableTile;
|
||||||
|
|
||||||
enum class EffectType : std::uint8_t {
|
enum class EffectType : std::uint8_t {
|
||||||
Slowness = 0,
|
Slowness = 0,
|
||||||
Stun,
|
Stun,
|
||||||
@@ -69,7 +73,7 @@ public:
|
|||||||
struct EffectDuration {
|
struct EffectDuration {
|
||||||
EffectType type;
|
EffectType type;
|
||||||
float duration; // in seconds
|
float duration; // in seconds
|
||||||
const Tower* tower; // the tower that gived the effect
|
Tower* tower; // the tower that gived the effect
|
||||||
};
|
};
|
||||||
|
|
||||||
const MobStats* getMobStats(MobType type, std::uint8_t level);
|
const MobStats* getMobStats(MobType type, std::uint8_t level);
|
||||||
@@ -86,22 +90,24 @@ private:
|
|||||||
Direction m_Direction;
|
Direction m_Direction;
|
||||||
std::vector<EffectDuration> m_Effects;
|
std::vector<EffectDuration> m_Effects;
|
||||||
const Tower* m_LastDamage; // the last tower that damaged the mob
|
const Tower* m_LastDamage; // the last tower that damaged the mob
|
||||||
bool m_HasReachedCastle; // if true, no need to walk anymore
|
|
||||||
|
|
||||||
utils::Timer m_EffectFireTimer;
|
utils::Timer m_EffectFireTimer;
|
||||||
utils::Timer m_EffectPoisonTimer;
|
utils::Timer m_EffectPoisonTimer;
|
||||||
utils::Timer m_EffectHealTimer;
|
utils::Timer m_EffectHealTimer;
|
||||||
|
|
||||||
|
TeamCastle* m_CastleTarget;
|
||||||
|
utils::CooldownTimer m_AttackTimer;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Mob(MobID id, MobLevel level, PlayerID sender) : m_Sender(sender), m_Level(level),
|
Mob(MobID id, MobLevel level, PlayerID sender) : m_Sender(sender), m_Level(level),
|
||||||
m_HasReachedCastle(false), m_EffectFireTimer(1000), m_EffectPoisonTimer(1000),
|
m_EffectFireTimer(1000), m_EffectPoisonTimer(1000),
|
||||||
m_EffectHealTimer(1000) {
|
m_EffectHealTimer(1000), m_CastleTarget(nullptr), m_AttackTimer(1000) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual MobType getType() const = 0;
|
virtual MobType getType() const = 0;
|
||||||
|
|
||||||
virtual void tick(std::uint64_t delta);
|
virtual void tick(std::uint64_t delta, World* world);
|
||||||
|
|
||||||
virtual bool OnDeath(World* world) { return true; }
|
virtual bool OnDeath(World* world) { return true; }
|
||||||
|
|
||||||
@@ -114,16 +120,16 @@ public:
|
|||||||
bool isDead() const { return m_Health <= 0; }
|
bool isDead() const { return m_Health <= 0; }
|
||||||
bool isAlive() const { return m_Health > 0; }
|
bool isAlive() const { return m_Health > 0; }
|
||||||
const Tower* getLastDamageTower() { return m_LastDamage; }
|
const Tower* getLastDamageTower() { return m_LastDamage; }
|
||||||
bool hasReachedEnemyCastle() { return m_HasReachedCastle; }
|
bool hasReachedEnemyCastle() { return m_CastleTarget != nullptr; }
|
||||||
|
|
||||||
void damage(float dmg, const Tower* damager) { m_Health = std::max(0.0f, m_Health - dmg); m_LastDamage = damager; }
|
void damage(float dmg, const Tower* damager) { m_Health = std::max(0.0f, m_Health - dmg); m_LastDamage = damager; }
|
||||||
void heal(float heal) { m_Health = std::min(static_cast<float>(getStats()->getMaxLife()), m_Health + heal); }
|
void heal(float heal) { m_Health = std::min(static_cast<float>(getStats()->getMaxLife()), m_Health + heal); }
|
||||||
void setMobReachedCastle() { m_HasReachedCastle = true; } // used when mob is in front of the castle
|
void setMobReachedCastle(TeamCastle* castle) { m_CastleTarget = castle; } // used when mob is in front of the castle
|
||||||
|
|
||||||
bool isImmuneTo(TowerType type);
|
bool isImmuneTo(TowerType type);
|
||||||
|
|
||||||
bool isImmuneTo(EffectType type);
|
bool isImmuneTo(EffectType type);
|
||||||
void addEffect(EffectType type, float durationSec, const Tower* tower);
|
void addEffect(EffectType type, float durationSec, Tower* tower);
|
||||||
bool hasEffect(EffectType type);
|
bool hasEffect(EffectType type);
|
||||||
|
|
||||||
float getTileX() { return getCenterX() - static_cast<float>(static_cast<std::int32_t>(getCenterX())); } // returns a float between 0 and 1 excluded
|
float getTileX() { return getCenterX() - static_cast<float>(static_cast<std::int32_t>(getCenterX())); } // returns a float between 0 and 1 excluded
|
||||||
@@ -137,7 +143,13 @@ protected:
|
|||||||
setSize(getStats()->getSize().x, getStats()->getSize().y);
|
setSize(getStats()->getSize().x, getStats()->getSize().y);
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
void updateEffects(std::uint64_t delta);
|
void updateEffects(std::uint64_t delta, World* world);
|
||||||
|
void attackCastle(std::uint64_t delta, World* world);
|
||||||
|
void move(std::uint64_t delta, World* world);
|
||||||
|
void walk(std::uint64_t delta, World* world);
|
||||||
|
void moveBack(const TeamCastle& castle, World* world);
|
||||||
|
void changeDirection(const WalkableTile& tile, World* world);
|
||||||
|
bool isTouchingCastle(const TeamCastle& castle) const;
|
||||||
EffectDuration& getEffect(EffectType type);
|
EffectDuration& getEffect(EffectType type);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -218,6 +230,18 @@ MobPtr createMob(MobID id, MobType type, std::uint8_t level, PlayerID sender);
|
|||||||
std::string getMobName(MobType type);
|
std::string getMobName(MobType type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MobListener {
|
||||||
|
public:
|
||||||
|
virtual void OnMobSpawn(Mob* mob) {}
|
||||||
|
virtual void OnMobDie(Mob* mob) {}
|
||||||
|
|
||||||
|
virtual void OnMobDamage(Mob* target, float damage, Tower* damager) {}
|
||||||
|
|
||||||
|
virtual void OnMobTouchCastle(Mob* damager, TeamCastle* enemyCastle) {}
|
||||||
|
virtual void OnMobCastleDamage(Mob* damager, TeamCastle* enemyCastle, float damage) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef utils::ObjectNotifier<MobListener> MobNotifier;
|
||||||
|
|
||||||
} // namespace game
|
} // namespace game
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class Spawn : public utils::shape::Rectangle {
|
|||||||
private:
|
private:
|
||||||
Direction m_Direction;
|
Direction m_Direction;
|
||||||
public:
|
public:
|
||||||
Spawn(){
|
Spawn() {
|
||||||
setWidth(5);
|
setWidth(5);
|
||||||
setHeight(5);
|
setHeight(5);
|
||||||
}
|
}
|
||||||
@@ -33,19 +33,34 @@ public:
|
|||||||
void setDirection(Direction direction) { m_Direction = direction; }
|
void setDirection(Direction direction) { m_Direction = direction; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TeamCastle : public utils::shape::Rectangle{
|
class Team;
|
||||||
|
|
||||||
|
class TeamCastle : public utils::shape::Rectangle {
|
||||||
private:
|
private:
|
||||||
|
const Team* m_Team;
|
||||||
float m_Life;
|
float m_Life;
|
||||||
public:
|
public:
|
||||||
TeamCastle() : m_Life(1000) {
|
static constexpr int CastleMaxLife = 1000;
|
||||||
|
|
||||||
|
TeamCastle(const Team* team) : m_Team(team), m_Life(CastleMaxLife) {
|
||||||
setWidth(5);
|
setWidth(5);
|
||||||
setHeight(5);
|
setHeight(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TeamCastle() : TeamCastle(nullptr) {}
|
||||||
|
|
||||||
float getLife() const { return m_Life; }
|
float getLife() const { return m_Life; }
|
||||||
|
|
||||||
|
const Team* getTeam() const { return m_Team; }
|
||||||
|
void setTeam(const Team* team) { m_Team = team; }
|
||||||
|
|
||||||
void setLife(float life) { m_Life = life; }
|
void setLife(float life) { m_Life = life; }
|
||||||
void damage(float damage) { m_Life = std::max(0.0f, m_Life - damage); }
|
void damage(float damage) { m_Life = std::max(0.0f, m_Life - damage); }
|
||||||
|
|
||||||
|
void setShape(utils::shape::Rectangle rect) {
|
||||||
|
setCenter(rect.getCenter());
|
||||||
|
setSize(rect.getSize());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Team {
|
class Team {
|
||||||
@@ -71,5 +86,7 @@ public:
|
|||||||
std::uint8_t getPlayerCount() const;
|
std::uint8_t getPlayerCount() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
typedef std::array<Team, 2> TeamList;
|
||||||
|
|
||||||
} // namespace game
|
} // namespace game
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -10,8 +10,6 @@
|
|||||||
#include "Mobs.h"
|
#include "Mobs.h"
|
||||||
#include "Team.h"
|
#include "Team.h"
|
||||||
|
|
||||||
#include "misc/ObjectNotifier.h"
|
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace game {
|
namespace game {
|
||||||
typedef std::pair<std::int16_t, std::int16_t> ChunkCoord;
|
typedef std::pair<std::int16_t, std::int16_t> ChunkCoord;
|
||||||
@@ -58,6 +56,13 @@ struct Color {
|
|||||||
std::uint8_t r, g, b;
|
std::uint8_t r, g, b;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static constexpr Color BLACK{ 0, 0, 0 };
|
||||||
|
static constexpr Color WHITE{ 255, 255, 255 };
|
||||||
|
|
||||||
|
static constexpr Color RED{ 255, 0, 0 };
|
||||||
|
static constexpr Color GREEN{ 0, 255, 0 };
|
||||||
|
static constexpr Color BLUE{ 0, 0, 255 };
|
||||||
|
|
||||||
struct Tile {
|
struct Tile {
|
||||||
virtual TileType getType() const = 0;
|
virtual TileType getType() const = 0;
|
||||||
};
|
};
|
||||||
@@ -127,19 +132,16 @@ public:
|
|||||||
|
|
||||||
virtual void OnArrowShot(MobPtr target, bool fire, Tower* shooter) {}
|
virtual void OnArrowShot(MobPtr target, bool fire, Tower* shooter) {}
|
||||||
virtual void OnExplosion(utils::shape::Circle explosion, float centerDamage, Tower* shooter) {}
|
virtual void OnExplosion(utils::shape::Circle explosion, float centerDamage, Tower* shooter) {}
|
||||||
|
|
||||||
virtual void OnMobDamage(MobPtr target, float damage, Tower* damager) {}
|
|
||||||
|
|
||||||
virtual void OnMobDead(MobPtr mob) {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef utils::ObjectNotifier<WorldListener> WorldNotifier;
|
typedef utils::ObjectNotifier<WorldListener> WorldNotifier;
|
||||||
|
|
||||||
class World : public WorldNotifier, public WorldListener {
|
class World : public WorldListener, public MobListener {
|
||||||
protected:
|
protected:
|
||||||
TowerTileColorPalette m_TowerPlacePalette;
|
TowerTileColorPalette m_TowerPlacePalette;
|
||||||
Color m_WalkablePalette;
|
Color m_WalkablePalette;
|
||||||
std::vector<Color> m_DecorationPalette;
|
std::vector<Color> m_DecorationPalette;
|
||||||
|
Color m_Background;
|
||||||
|
|
||||||
std::unordered_map<ChunkCoord, ChunkPtr> m_Chunks;
|
std::unordered_map<ChunkCoord, ChunkPtr> m_Chunks;
|
||||||
|
|
||||||
@@ -152,11 +154,12 @@ protected:
|
|||||||
TowerList m_Towers;
|
TowerList m_Towers;
|
||||||
|
|
||||||
Game* m_Game;
|
Game* m_Game;
|
||||||
|
|
||||||
|
WorldNotifier m_WorldNotifier;
|
||||||
|
MobNotifier m_MobNotifier;
|
||||||
public:
|
public:
|
||||||
World(Game* game);
|
World(Game* game);
|
||||||
|
|
||||||
static constexpr std::uint8_t CastleWidth = 5, CastleHeight = 5;
|
|
||||||
|
|
||||||
bool loadMap(const protocol::WorldBeginDataPacket* worldHeader);
|
bool loadMap(const protocol::WorldBeginDataPacket* worldHeader);
|
||||||
bool loadMap(const protocol::WorldDataPacket* worldData);
|
bool loadMap(const protocol::WorldDataPacket* worldData);
|
||||||
|
|
||||||
@@ -175,6 +178,7 @@ public:
|
|||||||
const TowerTileColorPalette& getTowerTileColorPalette() const { return m_TowerPlacePalette; }
|
const TowerTileColorPalette& getTowerTileColorPalette() const { return m_TowerPlacePalette; }
|
||||||
const Color& getWalkableTileColor() const { return m_WalkablePalette; }
|
const Color& getWalkableTileColor() const { return m_WalkablePalette; }
|
||||||
const std::vector<Color>& getDecorationPalette() const { return m_DecorationPalette; }
|
const std::vector<Color>& getDecorationPalette() const { return m_DecorationPalette; }
|
||||||
|
const Color& getBackgroundColor() const { return m_Background; }
|
||||||
|
|
||||||
const TilePalette& getTilePalette() const { return m_TilePalette; }
|
const TilePalette& getTilePalette() const { return m_TilePalette; }
|
||||||
|
|
||||||
@@ -208,9 +212,16 @@ public:
|
|||||||
Team& getTeam(TeamColor team);
|
Team& getTeam(TeamColor team);
|
||||||
const Team& getTeam(TeamColor team) const;
|
const Team& getTeam(TeamColor team) const;
|
||||||
|
|
||||||
|
const TeamList& getTeams() const;
|
||||||
|
|
||||||
const TowerList& getTowers() const { return m_Towers; };
|
const TowerList& getTowers() const { return m_Towers; };
|
||||||
TowerPtr getTowerById(TowerID tower);
|
TowerPtr getTowerById(TowerID tower);
|
||||||
|
|
||||||
|
const Player* getPlayerById(PlayerID id) const;
|
||||||
|
|
||||||
|
WorldNotifier& getWorldNotifier() { return m_WorldNotifier; }
|
||||||
|
MobNotifier& getMobNotifier() { return m_MobNotifier; }
|
||||||
|
|
||||||
// WorldListener
|
// WorldListener
|
||||||
|
|
||||||
virtual void OnArcherTowerShot(MobPtr target, ArcherTower* shooter);
|
virtual void OnArcherTowerShot(MobPtr target, ArcherTower* shooter);
|
||||||
@@ -218,13 +229,12 @@ public:
|
|||||||
virtual void OnArrowShot(MobPtr target, bool fire, Tower* shooter);
|
virtual void OnArrowShot(MobPtr target, bool fire, Tower* shooter);
|
||||||
virtual void OnExplosion(utils::shape::Circle explosion, float centerDamage, Tower* shooter);
|
virtual void OnExplosion(utils::shape::Circle explosion, float centerDamage, Tower* shooter);
|
||||||
|
|
||||||
virtual void OnMobDamage(MobPtr target, float damage, Tower* source);
|
// MobListener
|
||||||
|
|
||||||
|
virtual void OnMobDamage(Mob* target, float damage, Tower* source);
|
||||||
|
virtual void OnMobCastleDamage(Mob* damager, TeamCastle* enemyCastle, float damage);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void moveMobs(std::uint64_t delta);
|
|
||||||
void moveMob(MobPtr mob, std::uint64_t delta);
|
|
||||||
void moveBackMob(MobPtr mob, const TeamCastle& castle);
|
|
||||||
void changeMobDirection(MobPtr mob, WalkableTilePtr tile);
|
|
||||||
bool isMobTouchingCastle(MobPtr mob, const TeamCastle& castle) const;
|
|
||||||
void tickMobs(std::uint64_t delta);
|
void tickMobs(std::uint64_t delta);
|
||||||
void cleanDeadMobs();
|
void cleanDeadMobs();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -19,16 +19,16 @@ class Client {
|
|||||||
private:
|
private:
|
||||||
render::Renderer* m_Renderer;
|
render::Renderer* m_Renderer;
|
||||||
ClientConnexion m_Connexion;
|
ClientConnexion m_Connexion;
|
||||||
ClientGame m_Game;
|
std::unique_ptr<ClientGame> m_Game;
|
||||||
bool m_Connected;
|
bool m_Connected;
|
||||||
public:
|
public:
|
||||||
Client(render::Renderer* renderer) : m_Renderer(renderer), m_Game(this), m_Connected(false) {}
|
Client(render::Renderer* renderer) : m_Renderer(renderer), m_Game(std::make_unique<ClientGame>(this)), m_Connected(false) {}
|
||||||
|
|
||||||
const ClientGame& getGame() const { return m_Game; }
|
const ClientGame& getGame() const { return *m_Game; }
|
||||||
const ClientConnexion& getConnexion() const { return m_Connexion; }
|
const ClientConnexion& getConnexion() const { return m_Connexion; }
|
||||||
render::Renderer* getRenderer() const { return m_Renderer; }
|
render::Renderer* getRenderer() const { return m_Renderer; }
|
||||||
|
|
||||||
ClientGame& getGame() { return m_Game; }
|
ClientGame& getGame() { return *m_Game; }
|
||||||
ClientConnexion& getConnexion() { return m_Connexion; }
|
ClientConnexion& getConnexion() { return m_Connexion; }
|
||||||
|
|
||||||
void tick(std::uint64_t delta);
|
void tick(std::uint64_t delta);
|
||||||
@@ -45,6 +45,8 @@ public:
|
|||||||
void placeTower(game::TowerType type, const glm::vec2& position);
|
void placeTower(game::TowerType type, const glm::vec2& position);
|
||||||
void upgradeTower(game::TowerID tower, game::TowerLevel level);
|
void upgradeTower(game::TowerID tower, game::TowerLevel level);
|
||||||
void removeTower(game::TowerID tower);
|
void removeTower(game::TowerID tower);
|
||||||
|
private:
|
||||||
|
void reset();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace client
|
} // namespace client
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ public:
|
|||||||
Client* getClient() const { return m_Client; }
|
Client* getClient() const { return m_Client; }
|
||||||
|
|
||||||
render::Renderer* getRenderer() const { return m_Renderer; }
|
render::Renderer* getRenderer() const { return m_Renderer; }
|
||||||
WorldClient& getWorld() { return m_WorldClient; }
|
WorldClient& getWorldClient() { return m_WorldClient; }
|
||||||
|
|
||||||
virtual void HandlePacket(const protocol::ConnexionInfoPacket* packet);
|
virtual void HandlePacket(const protocol::ConnexionInfoPacket* packet);
|
||||||
virtual void HandlePacket(const protocol::PlayerJoinPacket* packet);
|
virtual void HandlePacket(const protocol::PlayerJoinPacket* packet);
|
||||||
|
|||||||
@@ -61,12 +61,11 @@ private:
|
|||||||
bool m_ServerRunning;
|
bool m_ServerRunning;
|
||||||
public:
|
public:
|
||||||
Server(const std::string& worldFilePath);
|
Server(const std::string& worldFilePath);
|
||||||
virtual ~Server() {}
|
virtual ~Server();
|
||||||
|
|
||||||
bool start(std::uint16_t port);
|
bool start(std::uint16_t port);
|
||||||
void stop();
|
void stop(); // force the server to stop
|
||||||
|
void close(); // at the end of a game
|
||||||
void lauchGame();
|
|
||||||
|
|
||||||
void removeConnexion(std::uint8_t connexionID);
|
void removeConnexion(std::uint8_t connexionID);
|
||||||
|
|
||||||
@@ -74,6 +73,8 @@ public:
|
|||||||
|
|
||||||
float getTPS() const { return m_TickCounter.getTPS(); }
|
float getTPS() const { return m_TickCounter.getTPS(); }
|
||||||
|
|
||||||
|
bool isRunning() { return m_ServerRunning; }
|
||||||
|
|
||||||
const ServerGame& getGame() const { return m_Game; }
|
const ServerGame& getGame() const { return m_Game; }
|
||||||
ServerGame& getGame() { return m_Game; }
|
ServerGame& getGame() { return m_Game; }
|
||||||
|
|
||||||
@@ -88,6 +89,7 @@ private:
|
|||||||
void accept();
|
void accept();
|
||||||
void updateSockets();
|
void updateSockets();
|
||||||
|
|
||||||
|
void clean();
|
||||||
void startThread();
|
void startThread();
|
||||||
void stopThread();
|
void stopThread();
|
||||||
void tick(std::uint64_t delta);
|
void tick(std::uint64_t delta);
|
||||||
|
|||||||
@@ -9,11 +9,12 @@ namespace server {
|
|||||||
|
|
||||||
class Server;
|
class Server;
|
||||||
|
|
||||||
class ServerGame : public game::Game {
|
class ServerGame : public game::Game, public game::GameListener {
|
||||||
private:
|
private:
|
||||||
Server* m_Server;
|
Server* m_Server;
|
||||||
ServerWorld m_ServerWorld;
|
ServerWorld m_ServerWorld;
|
||||||
utils::AutoTimer m_GoldMineTimer{ 1000, std::bind(&ServerGame::updateGoldMines, this) };
|
utils::AutoTimer m_GoldMineTimer{ 1000, std::bind(&ServerGame::updateGoldMines, this) };
|
||||||
|
utils::CooldownTimer m_EndGameCooldown{ 1000 * 10 };
|
||||||
public:
|
public:
|
||||||
ServerGame(Server* server);
|
ServerGame(Server* server);
|
||||||
~ServerGame() {}
|
~ServerGame() {}
|
||||||
@@ -22,6 +23,13 @@ public:
|
|||||||
|
|
||||||
virtual void tick(std::uint64_t delta);
|
virtual void tick(std::uint64_t delta);
|
||||||
void startGame();
|
void startGame();
|
||||||
|
|
||||||
|
// GameListener
|
||||||
|
|
||||||
|
virtual void OnGameStateUpdate(game::GameState newState);
|
||||||
|
virtual void OnGameBegin();
|
||||||
|
virtual void OnGameEnd();
|
||||||
|
virtual void OnGameClose();
|
||||||
private:
|
private:
|
||||||
void balanceTeams();
|
void balanceTeams();
|
||||||
void updateGoldMines();
|
void updateGoldMines();
|
||||||
|
|||||||
@@ -14,11 +14,15 @@ private:
|
|||||||
game::TowerID m_CurrentTowerID;
|
game::TowerID m_CurrentTowerID;
|
||||||
Server* m_Server;
|
Server* m_Server;
|
||||||
public:
|
public:
|
||||||
|
static constexpr float MobSpawnBorder = 0.01f;
|
||||||
|
|
||||||
ServerWorld(Server* server, ServerGame* game);
|
ServerWorld(Server* server, ServerGame* game);
|
||||||
|
|
||||||
void spawnMobs(game::MobType type, std::uint8_t level, game::PlayerID sender, std::uint8_t count);
|
void spawnMobs(game::MobType type, std::uint8_t level, game::PlayerID sender, std::uint8_t count);
|
||||||
game::TowerPtr placeTowerAt(game::TowerType type, std::int32_t x, std::int32_t y, game::PlayerID builder);
|
game::TowerPtr placeTowerAt(game::TowerType type, std::int32_t x, std::int32_t y, game::PlayerID builder);
|
||||||
|
|
||||||
|
virtual void OnMobDie(game::Mob* mob);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace server
|
} // namespace server
|
||||||
|
|||||||
@@ -338,7 +338,7 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include <basetsd.h>
|
#include <basetsd.h>
|
||||||
typedef SSIZE_T ssize_t;
|
//typedef SSIZE_T ssize_t;
|
||||||
|
|
||||||
#ifndef NOMINMAX
|
#ifndef NOMINMAX
|
||||||
#define NOMINMAX
|
#define NOMINMAX
|
||||||
|
|||||||
@@ -47,6 +47,9 @@ public:
|
|||||||
void setCenterY(float y) { m_Center.setY(y); }
|
void setCenterY(float y) { m_Center.setY(y); }
|
||||||
|
|
||||||
void setSize(float width, float height) { setWidth(width); setHeight(height); }
|
void setSize(float width, float height) { setWidth(width); setHeight(height); }
|
||||||
|
void setSize(Point size) { setSize(size.getX(), size.getY()); }
|
||||||
|
Point getSize() { return { m_Width, m_Height }; }
|
||||||
|
|
||||||
void setWidth(float width) { m_Width = width; }
|
void setWidth(float width) { m_Width = width; }
|
||||||
void setHeight(float height) { m_Height = height; }
|
void setHeight(float height) { m_Height = height; }
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ public:
|
|||||||
std::uint64_t getInterval() const { return m_Interval; }
|
std::uint64_t getInterval() const { return m_Interval; }
|
||||||
};
|
};
|
||||||
|
|
||||||
// utililty class to call function at regular period of time with a cooldown (used for towers)
|
// utililty class to call function at regular period of time with a cooldown (used for towers and mobs )
|
||||||
class CooldownTimer {
|
class CooldownTimer {
|
||||||
private:
|
private:
|
||||||
std::uint64_t m_Cooldown; // in millis
|
std::uint64_t m_Cooldown; // in millis
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ struct WorldHeader {
|
|||||||
game::TowerTileColorPalette m_TowerPlacePalette;
|
game::TowerTileColorPalette m_TowerPlacePalette;
|
||||||
game::Color m_WalkablePalette;
|
game::Color m_WalkablePalette;
|
||||||
std::vector<game::Color> m_DecorationPalette;
|
std::vector<game::Color> m_DecorationPalette;
|
||||||
|
game::Color m_Background;
|
||||||
|
|
||||||
game::SpawnColorPalette m_SpawnColorPalette;
|
game::SpawnColorPalette m_SpawnColorPalette;
|
||||||
|
|
||||||
@@ -128,6 +129,7 @@ public:
|
|||||||
const game::TowerTileColorPalette& getTowerTilePalette() const { return m_Header.m_TowerPlacePalette; }
|
const game::TowerTileColorPalette& getTowerTilePalette() const { return m_Header.m_TowerPlacePalette; }
|
||||||
const game::Color& getWalkableTileColor() const { return m_Header.m_WalkablePalette; }
|
const game::Color& getWalkableTileColor() const { return m_Header.m_WalkablePalette; }
|
||||||
const std::vector<game::Color>& getDecorationPalette() const { return m_Header.m_DecorationPalette; }
|
const std::vector<game::Color>& getDecorationPalette() const { return m_Header.m_DecorationPalette; }
|
||||||
|
const game::Color& getBackgroundColor() const { return m_Header.m_Background; }
|
||||||
|
|
||||||
const game::Spawn& getRedSpawn() const { return m_Header.m_RedSpawn; }
|
const game::Spawn& getRedSpawn() const { return m_Header.m_RedSpawn; }
|
||||||
const game::Spawn& getBlueSpawn() const { return m_Header.m_BlueSpawn; }
|
const game::Spawn& getBlueSpawn() const { return m_Header.m_BlueSpawn; }
|
||||||
|
|||||||
@@ -29,6 +29,8 @@ private:
|
|||||||
std::unique_ptr<WorldShader> m_WorldShader;
|
std::unique_ptr<WorldShader> m_WorldShader;
|
||||||
std::unique_ptr<EntityShader> m_EntityShader;
|
std::unique_ptr<EntityShader> m_EntityShader;
|
||||||
|
|
||||||
|
glm::vec3 m_BackgroundColor;
|
||||||
|
|
||||||
bool m_IsometricView = true;
|
bool m_IsometricView = true;
|
||||||
float m_IsometricShade = m_IsometricView;
|
float m_IsometricShade = m_IsometricView;
|
||||||
glm::vec2 m_CamPos{};
|
glm::vec2 m_CamPos{};
|
||||||
@@ -49,6 +51,8 @@ public:
|
|||||||
void setCamPos(const glm::vec2& newPos);
|
void setCamPos(const glm::vec2& newPos);
|
||||||
void setIsometricView(bool isometric); // false = 2D true = Isometric
|
void setIsometricView(bool isometric); // false = 2D true = Isometric
|
||||||
|
|
||||||
|
void setBackgroundColor(const glm::vec3& color) { m_BackgroundColor = color; }
|
||||||
|
|
||||||
glm::vec2 getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight);
|
glm::vec2 getCursorWorldPos(const glm::vec2& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight);
|
||||||
private:
|
private:
|
||||||
void updateIsometricView();
|
void updateIsometricView();
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "render/gui/TowerPlacePopup.h"
|
#include "render/gui/TowerPlacePopup.h"
|
||||||
#include "render/gui/MobTooltip.h"
|
#include "render/gui/MobTooltip.h"
|
||||||
|
#include "render/gui/CastleTooltip.h"
|
||||||
|
|
||||||
#include "render/gui/imgui/imgui.h"
|
#include "render/gui/imgui/imgui.h"
|
||||||
|
|
||||||
@@ -40,6 +41,7 @@ private:
|
|||||||
|
|
||||||
std::unique_ptr<gui::TowerPlacePopup> m_TowerPlacePopup;
|
std::unique_ptr<gui::TowerPlacePopup> m_TowerPlacePopup;
|
||||||
std::unique_ptr<gui::MobTooltip> m_MobTooltip;
|
std::unique_ptr<gui::MobTooltip> m_MobTooltip;
|
||||||
|
std::unique_ptr<gui::CastleTooltip> m_CastleTooltip;
|
||||||
public:
|
public:
|
||||||
WorldRenderer(game::World* world, client::ClientGame* client);
|
WorldRenderer(game::World* world, client::ClientGame* client);
|
||||||
~WorldRenderer();
|
~WorldRenderer();
|
||||||
@@ -69,8 +71,11 @@ private:
|
|||||||
void renderPopups();
|
void renderPopups();
|
||||||
void renderTowerUpgradePopup();
|
void renderTowerUpgradePopup();
|
||||||
void renderMobTooltip() const;
|
void renderMobTooltip() const;
|
||||||
|
void renderCastleTooltip() const;
|
||||||
void detectClick();
|
void detectClick();
|
||||||
void detectMobHovering() const;
|
void detectMobHovering() const;
|
||||||
|
void detectCastleHovering() const;
|
||||||
|
void renderTooltips() const;
|
||||||
void removeTower();
|
void removeTower();
|
||||||
glm::vec2 getCursorWorldPos() const;
|
glm::vec2 getCursorWorldPos() const;
|
||||||
glm::vec2 getClickWorldPos() const;
|
glm::vec2 getClickWorldPos() const;
|
||||||
|
|||||||
28
include/render/gui/CastleTooltip.h
Normal file
28
include/render/gui/CastleTooltip.h
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "GuiWidget.h"
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
namespace game {
|
||||||
|
|
||||||
|
class TeamCastle;
|
||||||
|
|
||||||
|
} // namespace game
|
||||||
|
|
||||||
|
namespace gui {
|
||||||
|
|
||||||
|
class CastleTooltip : public GuiWidget {
|
||||||
|
private:
|
||||||
|
const game::TeamCastle* m_Castle;
|
||||||
|
public:
|
||||||
|
CastleTooltip(client::Client* client);
|
||||||
|
|
||||||
|
virtual void render();
|
||||||
|
|
||||||
|
void setCastle(const game::TeamCastle* castle) { m_Castle = castle; }
|
||||||
|
bool isShown() { return m_Castle != nullptr; }
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace gui
|
||||||
|
} // namespace td
|
||||||
@@ -10,15 +10,17 @@ namespace gui {
|
|||||||
|
|
||||||
class GuiManager {
|
class GuiManager {
|
||||||
private:
|
private:
|
||||||
std::vector<std::shared_ptr<GuiWidget>> m_Widgets;
|
std::vector<std::unique_ptr<GuiWidget>> m_Widgets;
|
||||||
public:
|
public:
|
||||||
|
GuiManager(){}
|
||||||
|
|
||||||
void renderWidgets() {
|
void renderWidgets() {
|
||||||
for (auto widget : m_Widgets) {
|
for (auto& widget : m_Widgets) {
|
||||||
widget->render();
|
widget->render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void addWidgets(const std::shared_ptr<GuiWidget>& widget) {
|
void addWidget(std::unique_ptr<GuiWidget>&& widget) {
|
||||||
m_Widgets.push_back(std::move(widget));
|
m_Widgets.push_back(std::move(widget));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ public:
|
|||||||
virtual void render();
|
virtual void render();
|
||||||
|
|
||||||
void setMob(const game::Mob* mob) { m_Mob = mob; }
|
void setMob(const game::Mob* mob) { m_Mob = mob; }
|
||||||
|
bool isShown() { return m_Mob != nullptr; }
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace gui
|
} // namespace gui
|
||||||
|
|||||||
@@ -9,6 +9,8 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include "render/gui/GuiManager.h"
|
||||||
|
|
||||||
struct SDL_Window;
|
struct SDL_Window;
|
||||||
typedef void* SDL_GLContext;
|
typedef void* SDL_GLContext;
|
||||||
|
|
||||||
@@ -20,15 +22,6 @@ class Client;
|
|||||||
|
|
||||||
} // namespace client
|
} // namespace client
|
||||||
|
|
||||||
namespace gui {
|
|
||||||
|
|
||||||
class MainMenu;
|
|
||||||
class GameMenu;
|
|
||||||
class FrameMenu;
|
|
||||||
class UpdateMenu;
|
|
||||||
|
|
||||||
} // namespace gui
|
|
||||||
|
|
||||||
namespace render {
|
namespace render {
|
||||||
|
|
||||||
class Renderer;
|
class Renderer;
|
||||||
@@ -38,11 +31,8 @@ private:
|
|||||||
SDL_Window* m_Window;
|
SDL_Window* m_Window;
|
||||||
SDL_GLContext m_GlContext;
|
SDL_GLContext m_GlContext;
|
||||||
td::render::Renderer* m_Renderer;
|
td::render::Renderer* m_Renderer;
|
||||||
|
td::gui::GuiManager m_GuiManager;
|
||||||
std::unique_ptr<td::client::Client> m_Client;
|
std::unique_ptr<td::client::Client> m_Client;
|
||||||
std::unique_ptr<td::gui::MainMenu> m_MainMenu;
|
|
||||||
std::unique_ptr<td::gui::GameMenu> m_GameMenu;
|
|
||||||
std::unique_ptr<td::gui::FrameMenu> m_FrameMenu;
|
|
||||||
std::unique_ptr<td::gui::UpdateMenu> m_UpdateMenu;
|
|
||||||
public:
|
public:
|
||||||
TowerGui(SDL_Window* wndow, SDL_GLContext glContext, td::render::Renderer* renderer);
|
TowerGui(SDL_Window* wndow, SDL_GLContext glContext, td::render::Renderer* renderer);
|
||||||
~TowerGui();
|
~TowerGui();
|
||||||
|
|||||||
@@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
#include "misc/DataBuffer.h"
|
#include "misc/DataBuffer.h"
|
||||||
|
|
||||||
#include <ctime>
|
#define TD_VERSION "alpha-0.1.2"
|
||||||
|
|
||||||
#define TD_VERSION "alpha-0.0.4"
|
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace utils {
|
namespace utils {
|
||||||
|
|||||||
@@ -20,8 +20,10 @@ extern "C"
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
int main(int argc, const char* args[]) {
|
int main(int argc, const char* args[]) {
|
||||||
|
#if !defined(NDEBUG)
|
||||||
// setup signal handling
|
// setup signal handling
|
||||||
backward::SignalHandling sh;
|
backward::SignalHandling sh;
|
||||||
|
#endif
|
||||||
|
|
||||||
// remove the outdated binary
|
// remove the outdated binary
|
||||||
td::utils::Updater::removeOldFile();
|
td::utils::Updater::removeOldFile();
|
||||||
|
|||||||
@@ -17,5 +17,21 @@ void Game::tick(std::uint64_t delta) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Player* Game::getPlayerById(PlayerID id) {
|
||||||
|
auto it = m_Players.find(id);
|
||||||
|
|
||||||
|
if (it == m_Players.end()) return nullptr;
|
||||||
|
|
||||||
|
return &it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Player* Game::getPlayerById(PlayerID id) const {
|
||||||
|
auto it = m_Players.find(id);
|
||||||
|
|
||||||
|
if (it == m_Players.end()) return nullptr;
|
||||||
|
|
||||||
|
return &it->second;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace game
|
} // namespace game
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#include "game/Mobs.h"
|
#include "game/Mobs.h"
|
||||||
|
#include "game/Player.h"
|
||||||
|
#include "game/World.h"
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@@ -18,7 +20,7 @@ EffectDuration& Mob::getEffect(EffectType effectType) {
|
|||||||
return *std::find_if(m_Effects.begin(), m_Effects.end(), [&effectType](EffectDuration effect) { return effect.type == effectType;});
|
return *std::find_if(m_Effects.begin(), m_Effects.end(), [&effectType](EffectDuration effect) { return effect.type == effectType;});
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mob::addEffect(EffectType effectType, float durationSec, const Tower* tower) {
|
void Mob::addEffect(EffectType effectType, float durationSec, Tower* tower) {
|
||||||
if (isImmuneTo(effectType))
|
if (isImmuneTo(effectType))
|
||||||
return;
|
return;
|
||||||
if (hasEffect(effectType)) {
|
if (hasEffect(effectType)) {
|
||||||
@@ -30,11 +32,176 @@ void Mob::addEffect(EffectType effectType, float durationSec, const Tower* tower
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mob::tick(std::uint64_t delta) {
|
void Mob::attackCastle(std::uint64_t delta, World* world) {
|
||||||
updateEffects(delta);
|
if(!hasReachedEnemyCastle()) return;
|
||||||
|
|
||||||
|
if (m_AttackTimer.update(delta)) {
|
||||||
|
world->getMobNotifier().notifyListeners(&MobListener::OnMobCastleDamage, this, m_CastleTarget, getStats()->getDamage());
|
||||||
|
m_AttackTimer.applyCooldown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mob::updateEffects(std::uint64_t delta) {
|
void Mob::walk(std::uint64_t delta, World* world) {
|
||||||
|
float mobWalkSpeed = getStats()->getMovementSpeed();
|
||||||
|
|
||||||
|
float walkAmount = mobWalkSpeed * ((float)delta / 1000.0f);
|
||||||
|
|
||||||
|
if (hasEffect(EffectType::Slowness))
|
||||||
|
walkAmount *= 0.70; // walk 30% slower
|
||||||
|
|
||||||
|
switch (getDirection()) {
|
||||||
|
case Direction::NegativeX: {
|
||||||
|
setCenterX(getCenterX() - walkAmount);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Direction::PositiveX: {
|
||||||
|
setCenterX(getCenterX() + walkAmount);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Direction::NegativeY: {
|
||||||
|
setCenterY(getCenterY() - walkAmount);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Direction::PositiveY: {
|
||||||
|
setCenterY(getCenterY() + walkAmount);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mob::move(std::uint64_t delta, World* world) {
|
||||||
|
TilePtr tile = world->getTile(getCenter().getX(), getCenter().getY());
|
||||||
|
|
||||||
|
if (tile != nullptr && tile->getType() == TileType::Walk) {
|
||||||
|
WalkableTilePtr walkTile = std::static_pointer_cast<WalkableTile>(tile);
|
||||||
|
changeDirection(*walkTile, world);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasReachedEnemyCastle()) return;
|
||||||
|
|
||||||
|
walk(delta, world);
|
||||||
|
|
||||||
|
TeamColor mobTeam = world->getPlayerById(getSender())->getTeamColor();
|
||||||
|
|
||||||
|
TeamCastle* enemyCastle = nullptr;
|
||||||
|
|
||||||
|
if (mobTeam == TeamColor::Red) {
|
||||||
|
enemyCastle = &world->getBlueTeam().getCastle();
|
||||||
|
} else if (mobTeam == TeamColor::Blue) {
|
||||||
|
enemyCastle = &world->getRedTeam().getCastle();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isTouchingCastle(*enemyCastle)) {
|
||||||
|
moveBack(*enemyCastle, world);
|
||||||
|
setMobReachedCastle(enemyCastle);
|
||||||
|
|
||||||
|
world->getMobNotifier().notifyListeners(&MobListener::OnMobTouchCastle, this, enemyCastle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mob::moveBack(const TeamCastle& enemyCastle, World* world) {
|
||||||
|
switch (getDirection()) {
|
||||||
|
case Direction::NegativeX: {
|
||||||
|
setCenterX(enemyCastle.getBottomRight().getX() + getWidth() / 2.0f);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Direction::PositiveX: {
|
||||||
|
setCenterX(enemyCastle.getTopLeft().getX() - getWidth() / 2.0f);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Direction::NegativeY: {
|
||||||
|
setCenterY(enemyCastle.getBottomRight().getY() + getHeight() / 2.0f);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case Direction::PositiveY: {
|
||||||
|
setCenterY(enemyCastle.getTopLeft().getY() - getHeight() / 2.0f);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mob::changeDirection(const WalkableTile& tile, World* world) {
|
||||||
|
if (getDirection() == tile.direction) return;
|
||||||
|
|
||||||
|
float tileX = static_cast<float>(static_cast<std::int32_t>(getCenterX()));
|
||||||
|
float tileY = static_cast<float>(static_cast<std::int32_t>(getCenterY()));
|
||||||
|
|
||||||
|
switch (getDirection()) {
|
||||||
|
|
||||||
|
case Direction::PositiveY: {
|
||||||
|
if (tile.direction == Direction::NegativeX) {
|
||||||
|
if (getTileY() > getTileX()) {
|
||||||
|
setCenterY(tileY + getTileX());
|
||||||
|
setDirection(tile.direction);
|
||||||
|
}
|
||||||
|
} else { // tile->direction = Direction::PositiveX
|
||||||
|
if (getTileY() > 1 - getTileX()) {
|
||||||
|
setCenterY(tileY + (1 - getTileX()));
|
||||||
|
setDirection(tile.direction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Direction::NegativeY: {
|
||||||
|
if (tile.direction == Direction::PositiveX) {
|
||||||
|
if (getTileY() < getTileX()) {
|
||||||
|
setCenterY(tileY + getTileX());
|
||||||
|
setDirection(tile.direction);
|
||||||
|
}
|
||||||
|
} else { // tile.direction = Direction::NegativeX
|
||||||
|
if (getTileY() < 1 - getTileX()) {
|
||||||
|
setCenterY(tileY + (1 - getTileX()));
|
||||||
|
setDirection(tile.direction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Direction::PositiveX: {
|
||||||
|
if (tile.direction == Direction::NegativeY) {
|
||||||
|
if (getTileX() > getTileY()) {
|
||||||
|
setCenterX(tileX + getTileY());
|
||||||
|
setDirection(tile.direction);
|
||||||
|
}
|
||||||
|
} else { // tile.direction = Direction::PositiveY
|
||||||
|
if (getTileX() > 1 - getTileY()) {
|
||||||
|
setCenterX(tileX + (1 - getTileY()));
|
||||||
|
setDirection(tile.direction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Direction::NegativeX: {
|
||||||
|
if (tile.direction == Direction::PositiveY) {
|
||||||
|
if (getTileX() < getTileY()) {
|
||||||
|
setCenterX(tileX + getTileY());
|
||||||
|
setDirection(tile.direction);
|
||||||
|
}
|
||||||
|
} else { // tile.direction = Direction::NegativeY
|
||||||
|
if (getTileX() < 1 - getTileY()) {
|
||||||
|
setCenterX(tileX + (1 - getTileY()));
|
||||||
|
setDirection(tile.direction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Mob::isTouchingCastle(const TeamCastle& enemyCastle) const {
|
||||||
|
return enemyCastle.collidesWith(*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mob::tick(std::uint64_t delta, World* world) {
|
||||||
|
updateEffects(delta, world);
|
||||||
|
move(delta, world);
|
||||||
|
attackCastle(delta, world);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Mob::updateEffects(std::uint64_t delta, World* world) {
|
||||||
float deltaSec = (float)delta / 1000.0f;
|
float deltaSec = (float)delta / 1000.0f;
|
||||||
for (std::size_t i = 0; i < m_Effects.size(); i++) {
|
for (std::size_t i = 0; i < m_Effects.size(); i++) {
|
||||||
EffectDuration& effect = m_Effects[i];
|
EffectDuration& effect = m_Effects[i];
|
||||||
@@ -63,12 +230,12 @@ void Mob::updateEffects(std::uint64_t delta) {
|
|||||||
}
|
}
|
||||||
if (hasEffect(EffectType::Fire)) {
|
if (hasEffect(EffectType::Fire)) {
|
||||||
if (m_EffectFireTimer.update(delta)) {
|
if (m_EffectFireTimer.update(delta)) {
|
||||||
damage(3, getEffect(EffectType::Fire).tower);
|
world->getMobNotifier().notifyListeners(&MobListener::OnMobDamage, this, 3.0f, getEffect(EffectType::Fire).tower);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (hasEffect(EffectType::Poison)) {
|
if (hasEffect(EffectType::Poison)) {
|
||||||
if (m_EffectPoisonTimer.update(delta)) {
|
if (m_EffectPoisonTimer.update(delta)) {
|
||||||
damage(1, getEffect(EffectType::Poison).tower);
|
world->getMobNotifier().notifyListeners(&MobListener::OnMobDamage, this, 1.0f, getEffect(EffectType::Poison).tower);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (hasEffect(EffectType::Heal)) {
|
if (hasEffect(EffectType::Heal)) {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
namespace td {
|
namespace td {
|
||||||
namespace game {
|
namespace game {
|
||||||
|
|
||||||
Team::Team(TeamColor color) : m_Color(color) {}
|
Team::Team(TeamColor color) : m_Color(color), m_TeamCastle(this) {}
|
||||||
|
|
||||||
void Team::addPlayer(Player* newPlayer) {
|
void Team::addPlayer(Player* newPlayer) {
|
||||||
m_Players.push_back(newPlayer);
|
m_Players.push_back(newPlayer);
|
||||||
|
|||||||
@@ -213,7 +213,7 @@ void ArcherTower::tick(std::uint64_t delta, World* world) {
|
|||||||
std::uint8_t arrows = explosiveArrows ? 2 : getLevel().getLevel();
|
std::uint8_t arrows = explosiveArrows ? 2 : getLevel().getLevel();
|
||||||
for (MobPtr mob : world->getMobList()) {
|
for (MobPtr mob : world->getMobList()) {
|
||||||
if (isMobInRange(mob)) {
|
if (isMobInRange(mob)) {
|
||||||
world->notifyListeners(&WorldListener::OnArcherTowerShot, mob, this);
|
world->getWorldNotifier().notifyListeners(&WorldListener::OnArcherTowerShot, mob, this);
|
||||||
m_Timer.applyCooldown();
|
m_Timer.applyCooldown();
|
||||||
arrowsShot++;
|
arrowsShot++;
|
||||||
if (arrowsShot >= arrows)
|
if (arrowsShot >= arrows)
|
||||||
@@ -229,8 +229,8 @@ void IceTower::tick(std::uint64_t delta, World* world) {
|
|||||||
for (MobPtr mob : world->getMobList()) {
|
for (MobPtr mob : world->getMobList()) {
|
||||||
if (isMobInRange(mob)) {
|
if (isMobInRange(mob)) {
|
||||||
mob->addEffect(EffectType::Slowness, 1, this); // slowness for 1s every second
|
mob->addEffect(EffectType::Slowness, 1, this); // slowness for 1s every second
|
||||||
if(damage > 0)
|
if (damage > 0)
|
||||||
world->notifyListeners(&WorldListener::OnMobDamage, mob, damage, this);
|
world->getMobNotifier().notifyListeners(&MobListener::OnMobDamage, mob.get(), damage, this);
|
||||||
m_Timer.applyCooldown();
|
m_Timer.applyCooldown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -253,7 +253,7 @@ void PoisonTower::tick(std::uint64_t delta, World* world) {
|
|||||||
for (MobPtr mob : world->getMobList()) {
|
for (MobPtr mob : world->getMobList()) {
|
||||||
if (isMobInRange(mob)) {
|
if (isMobInRange(mob)) {
|
||||||
if (getLevel().getPath() == TowerPath::Bottom) {
|
if (getLevel().getPath() == TowerPath::Bottom) {
|
||||||
world->notifyListeners(&WorldListener::OnMobDamage, mob, getStats()->getDamage(), this);
|
world->getMobNotifier().notifyListeners(&MobListener::OnMobDamage, mob.get(), getStats()->getDamage(), this);
|
||||||
} else {
|
} else {
|
||||||
float durationSec;
|
float durationSec;
|
||||||
switch (getLevel().getLevel()) {
|
switch (getLevel().getLevel()) {
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ namespace td {
|
|||||||
namespace game {
|
namespace game {
|
||||||
|
|
||||||
World::World(Game* game) : m_Game(game) {
|
World::World(Game* game) : m_Game(game) {
|
||||||
bindListener(this);
|
getWorldNotifier().bindListener(this);
|
||||||
|
getMobNotifier().bindListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
TilePtr World::getTile(std::int32_t x, std::int32_t y) const {
|
TilePtr World::getTile(std::int32_t x, std::int32_t y) const {
|
||||||
@@ -36,6 +37,7 @@ bool World::loadMap(const protocol::WorldBeginDataPacket* worldHeader) {
|
|||||||
m_TowerPlacePalette = worldHeader->getTowerTilePalette();
|
m_TowerPlacePalette = worldHeader->getTowerTilePalette();
|
||||||
m_WalkablePalette = worldHeader->getWalkableTileColor();
|
m_WalkablePalette = worldHeader->getWalkableTileColor();
|
||||||
m_DecorationPalette = worldHeader->getDecorationPalette();
|
m_DecorationPalette = worldHeader->getDecorationPalette();
|
||||||
|
m_Background = worldHeader->getBackgroundColor();
|
||||||
|
|
||||||
getRedTeam().getSpawn() = worldHeader->getRedSpawn();
|
getRedTeam().getSpawn() = worldHeader->getRedSpawn();
|
||||||
getBlueTeam().getSpawn() = worldHeader->getBlueSpawn();
|
getBlueTeam().getSpawn() = worldHeader->getBlueSpawn();
|
||||||
@@ -43,7 +45,10 @@ bool World::loadMap(const protocol::WorldBeginDataPacket* worldHeader) {
|
|||||||
m_SpawnColorPalette = worldHeader->getSpawnPalette();
|
m_SpawnColorPalette = worldHeader->getSpawnPalette();
|
||||||
|
|
||||||
getRedTeam().getCastle() = worldHeader->getRedCastle();
|
getRedTeam().getCastle() = worldHeader->getRedCastle();
|
||||||
|
getRedTeam().getCastle().setTeam(&getRedTeam());
|
||||||
|
|
||||||
getBlueTeam().getCastle() = worldHeader->getBlueCastle();
|
getBlueTeam().getCastle() = worldHeader->getBlueCastle();
|
||||||
|
getBlueTeam().getCastle().setTeam(&getBlueTeam());
|
||||||
|
|
||||||
m_TilePalette = worldHeader->getTilePalette();
|
m_TilePalette = worldHeader->getTilePalette();
|
||||||
|
|
||||||
@@ -101,7 +106,8 @@ bool World::saveMap(const std::string& fileName) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void World::tick(std::uint64_t delta) {
|
void World::tick(std::uint64_t delta) {
|
||||||
moveMobs(delta);
|
if (m_Game->getGameState() != GameState::Game) return;
|
||||||
|
|
||||||
tickMobs(delta);
|
tickMobs(delta);
|
||||||
for (TowerPtr tower : m_Towers) {
|
for (TowerPtr tower : m_Towers) {
|
||||||
tower->tick(delta, this);
|
tower->tick(delta, this);
|
||||||
@@ -114,6 +120,7 @@ void World::spawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID send
|
|||||||
mob->setCenter({ x, y });
|
mob->setCenter({ x, y });
|
||||||
mob->setDirection(dir);
|
mob->setDirection(dir);
|
||||||
m_Mobs.push_back(mob);
|
m_Mobs.push_back(mob);
|
||||||
|
getMobNotifier().notifyListeners(&MobListener::OnMobSpawn, mob.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
TowerPtr World::placeTowerAt(TowerID id, TowerType type, std::int32_t x, std::int32_t y, PlayerID builder) {
|
TowerPtr World::placeTowerAt(TowerID id, TowerType type, std::int32_t x, std::int32_t y, PlayerID builder) {
|
||||||
@@ -135,161 +142,7 @@ TowerPtr World::removeTower(TowerID towerId) {
|
|||||||
|
|
||||||
void World::tickMobs(std::uint64_t delta) {
|
void World::tickMobs(std::uint64_t delta) {
|
||||||
for (MobPtr mob : m_Mobs) {
|
for (MobPtr mob : m_Mobs) {
|
||||||
mob->tick(delta);
|
mob->tick(delta, this);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void World::changeMobDirection(MobPtr mob, WalkableTilePtr tile) {
|
|
||||||
if (mob->getDirection() == tile->direction) return;
|
|
||||||
|
|
||||||
float tileX = static_cast<float>(static_cast<std::int32_t>(mob->getCenterX()));
|
|
||||||
float tileY = static_cast<float>(static_cast<std::int32_t>(mob->getCenterY()));
|
|
||||||
|
|
||||||
switch (mob->getDirection()) {
|
|
||||||
|
|
||||||
case Direction::PositiveY: {
|
|
||||||
if (tile->direction == Direction::NegativeX) {
|
|
||||||
if (mob->getTileY() > mob->getTileX()) {
|
|
||||||
mob->setCenterY(tileY + mob->getTileX());
|
|
||||||
mob->setDirection(tile->direction);
|
|
||||||
}
|
|
||||||
} else { // tile->direction = Direction::PositiveX
|
|
||||||
if (mob->getTileY() > 1 - mob->getTileX()) {
|
|
||||||
mob->setCenterY(tileY + (1 - mob->getTileX()));
|
|
||||||
mob->setDirection(tile->direction);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Direction::NegativeY: {
|
|
||||||
if (tile->direction == Direction::PositiveX) {
|
|
||||||
if (mob->getTileY() < mob->getTileX()) {
|
|
||||||
mob->setCenterY(tileY + mob->getTileX());
|
|
||||||
mob->setDirection(tile->direction);
|
|
||||||
}
|
|
||||||
} else { // tile->direction = Direction::NegativeX
|
|
||||||
if (mob->getTileY() < 1 - mob->getTileX()) {
|
|
||||||
mob->setCenterY(tileY + (1 - mob->getTileX()));
|
|
||||||
mob->setDirection(tile->direction);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Direction::PositiveX: {
|
|
||||||
if (tile->direction == Direction::NegativeY) {
|
|
||||||
if (mob->getTileX() > mob->getTileY()) {
|
|
||||||
mob->setCenterX(tileX + mob->getTileY());
|
|
||||||
mob->setDirection(tile->direction);
|
|
||||||
}
|
|
||||||
} else { // tile->direction = Direction::PositiveY
|
|
||||||
if (mob->getTileX() > 1 - mob->getTileY()) {
|
|
||||||
mob->setCenterX(tileX + (1 - mob->getTileY()));
|
|
||||||
mob->setDirection(tile->direction);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Direction::NegativeX: {
|
|
||||||
if (tile->direction == Direction::PositiveY) {
|
|
||||||
if (mob->getTileX() < mob->getTileY()) {
|
|
||||||
mob->setCenterX(tileX + mob->getTileY());
|
|
||||||
mob->setDirection(tile->direction);
|
|
||||||
}
|
|
||||||
} else { // tile->direction = Direction::NegativeY
|
|
||||||
if (mob->getTileX() < 1 - mob->getTileY()) {
|
|
||||||
mob->setCenterX(tileX + (1 - mob->getTileY()));
|
|
||||||
mob->setDirection(tile->direction);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void World::moveMobs(std::uint64_t delta) {
|
|
||||||
for (MobPtr mob : m_Mobs) {
|
|
||||||
TilePtr tile = getTile(mob->getCenter().getX(), mob->getCenter().getY());
|
|
||||||
|
|
||||||
if (tile != nullptr && tile->getType() == TileType::Walk) {
|
|
||||||
WalkableTilePtr walkTile = std::static_pointer_cast<WalkableTile>(tile);
|
|
||||||
changeMobDirection(mob, walkTile);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mob->hasReachedEnemyCastle()) continue;
|
|
||||||
|
|
||||||
moveMob(mob, delta);
|
|
||||||
|
|
||||||
TeamColor mobTeam = m_Game->getPlayerById(mob->getSender()).getTeamColor();
|
|
||||||
|
|
||||||
const TeamCastle* enemyCastle = nullptr;
|
|
||||||
|
|
||||||
if (mobTeam == TeamColor::Red) {
|
|
||||||
enemyCastle = &getBlueTeam().getCastle();
|
|
||||||
} else if (mobTeam == TeamColor::Blue) {
|
|
||||||
enemyCastle = &getRedTeam().getCastle();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isMobTouchingCastle(mob, *enemyCastle)) {
|
|
||||||
moveBackMob(mob, *enemyCastle);
|
|
||||||
mob->setMobReachedCastle();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void World::moveMob(MobPtr mob, std::uint64_t delta) {
|
|
||||||
float mobWalkSpeed = mob->getStats()->getMovementSpeed();
|
|
||||||
|
|
||||||
float walkAmount = mobWalkSpeed * ((float)delta / 1000.0f);
|
|
||||||
|
|
||||||
if (mob->hasEffect(EffectType::Slowness))
|
|
||||||
walkAmount *= 0.70; // walk 30% slower
|
|
||||||
|
|
||||||
switch (mob->getDirection()) {
|
|
||||||
case Direction::NegativeX: {
|
|
||||||
mob->setCenterX(mob->getCenterX() - walkAmount);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case Direction::PositiveX: {
|
|
||||||
mob->setCenterX(mob->getCenterX() + walkAmount);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case Direction::NegativeY: {
|
|
||||||
mob->setCenterY(mob->getCenterY() - walkAmount);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case Direction::PositiveY: {
|
|
||||||
mob->setCenterY(mob->getCenterY() + walkAmount);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool World::isMobTouchingCastle(MobPtr mob, const TeamCastle& enemyCastle) const {
|
|
||||||
return enemyCastle.collidesWith(*mob);
|
|
||||||
}
|
|
||||||
|
|
||||||
void World::moveBackMob(MobPtr mob, const TeamCastle& enemyCastle) {
|
|
||||||
switch (mob->getDirection()) {
|
|
||||||
case Direction::NegativeX: {
|
|
||||||
mob->setCenterX(enemyCastle.getBottomRight().getX() + mob->getWidth() / 2.0f);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case Direction::PositiveX: {
|
|
||||||
mob->setCenterX(enemyCastle.getTopLeft().getX() - mob->getWidth() / 2.0f);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case Direction::NegativeY: {
|
|
||||||
mob->setCenterY(enemyCastle.getBottomRight().getY() + mob->getHeight() / 2.0f);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case Direction::PositiveY: {
|
|
||||||
mob->setCenterY(enemyCastle.getTopLeft().getY() - mob->getHeight() / 2.0f);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,19 +222,11 @@ bool World::CanPlaceBigTower(const glm::vec2& worldPos, PlayerID playerID) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
void World::cleanDeadMobs() {
|
void World::cleanDeadMobs() {
|
||||||
|
// safely remove mobs when unused
|
||||||
for (std::size_t i = 0; i < m_Mobs.size(); i++) {
|
for (std::size_t i = 0; i < m_Mobs.size(); i++) {
|
||||||
MobPtr mob = m_Mobs[i];
|
MobPtr mob = m_Mobs[i];
|
||||||
if (mob->isDead()) {
|
if (mob->isDead()) {
|
||||||
if (mob->OnDeath(this)) {
|
m_Mobs.erase(m_Mobs.begin() + i);
|
||||||
//reward players
|
|
||||||
Player& sender = m_Game->getPlayerById(mob->getSender());
|
|
||||||
sender.addExp(mob->getStats()->getExpReward());
|
|
||||||
|
|
||||||
Player& killer = m_Game->getPlayerById(mob->getLastDamageTower()->getBuilder());
|
|
||||||
killer.addGold(mob->getStats()->getMoneyCost());
|
|
||||||
|
|
||||||
m_Mobs.erase(m_Mobs.begin() + i);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -413,14 +258,14 @@ void World::OnArcherTowerShot(MobPtr target, ArcherTower* shooter) {
|
|||||||
bool fireArrows = shooter->getLevel().getPath() == TowerPath::Bottom;
|
bool fireArrows = shooter->getLevel().getPath() == TowerPath::Bottom;
|
||||||
bool explosiveArrows = shooter->getLevel().getLevel() == 4 && fireArrows;
|
bool explosiveArrows = shooter->getLevel().getLevel() == 4 && fireArrows;
|
||||||
|
|
||||||
notifyListeners(&WorldListener::OnArrowShot, target, fireArrows, shooter);
|
getWorldNotifier().notifyListeners(&WorldListener::OnArrowShot, target, fireArrows, shooter);
|
||||||
if (explosiveArrows) {
|
if (explosiveArrows) {
|
||||||
notifyListeners(&WorldListener::OnExplosion, utils::shape::Circle{ target->getCenterX(), target->getCenterY(), ArcherTower::ExplosionRadius }, shooter->getStats()->getDamage(), shooter);
|
getWorldNotifier().notifyListeners(&WorldListener::OnExplosion, utils::shape::Circle{ target->getCenterX(), target->getCenterY(), ArcherTower::ExplosionRadius }, shooter->getStats()->getDamage(), shooter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::OnArrowShot(MobPtr target, bool fireArrow, Tower* shooter) {
|
void World::OnArrowShot(MobPtr target, bool fireArrow, Tower* shooter) {
|
||||||
notifyListeners(&WorldListener::OnMobDamage, target, shooter->getStats()->getDamage(), shooter);
|
getMobNotifier().notifyListeners(&MobListener::OnMobDamage, target.get(), shooter->getStats()->getDamage(), shooter);
|
||||||
if (fireArrow) {
|
if (fireArrow) {
|
||||||
target->addEffect(EffectType::Fire, ArcherTower::FireDurationSec, shooter);
|
target->addEffect(EffectType::Fire, ArcherTower::FireDurationSec, shooter);
|
||||||
}
|
}
|
||||||
@@ -431,15 +276,22 @@ void World::OnExplosion(utils::shape::Circle explosion, float centerDamage, Towe
|
|||||||
if (mob->isAlive() && mob->collidesWith(explosion)) {
|
if (mob->isAlive() && mob->collidesWith(explosion)) {
|
||||||
// linear distance damage reduction
|
// linear distance damage reduction
|
||||||
float explosionDamage = mob->distance(explosion) / explosion.getRadius() * centerDamage;
|
float explosionDamage = mob->distance(explosion) / explosion.getRadius() * centerDamage;
|
||||||
notifyListeners(&WorldListener::OnMobDamage, mob, explosionDamage, shooter);
|
getMobNotifier().notifyListeners(&MobListener::OnMobDamage, mob.get(), explosionDamage, shooter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::OnMobDamage(MobPtr target, float damage, Tower* source) {
|
void World::OnMobCastleDamage(Mob* damager, TeamCastle* enemyCastle, float damage) {
|
||||||
|
enemyCastle->damage(damage);
|
||||||
|
if (enemyCastle->getLife() <= 0) {
|
||||||
|
m_Game->notifyListeners(&GameListener::OnGameEnd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void World::OnMobDamage(Mob* target, float damage, Tower* source) {
|
||||||
target->damage(damage, source);
|
target->damage(damage, source);
|
||||||
if (target->isDead()) {
|
if (target->isDead()) {
|
||||||
notifyListeners(&WorldListener::OnMobDead, target);
|
getMobNotifier().notifyListeners(&MobListener::OnMobDie, target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -467,5 +319,13 @@ const Team& World::getTeam(TeamColor team) const {
|
|||||||
return m_Game->getTeam(team);
|
return m_Game->getTeam(team);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Player* World::getPlayerById(PlayerID id) const {
|
||||||
|
return m_Game->getPlayerById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
const TeamList& World::getTeams() const {
|
||||||
|
return m_Game->getTeams();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace game
|
} // namespace game
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -40,13 +40,19 @@ void Client::tick(std::uint64_t delta) {
|
|||||||
m_Connected = m_Connexion.updateSocket();
|
m_Connected = m_Connexion.updateSocket();
|
||||||
if (!m_Connected) {
|
if (!m_Connected) {
|
||||||
std::cout << "Disconnected ! (Reason : " << m_Connexion.getDisconnectReason() << ")\n";
|
std::cout << "Disconnected ! (Reason : " << m_Connexion.getDisconnectReason() << ")\n";
|
||||||
|
reset();
|
||||||
} else {
|
} else {
|
||||||
m_Game.tick(delta);
|
m_Game->tick(delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::render() {
|
void Client::render() {
|
||||||
m_Game.renderWorld();
|
m_Game->renderWorld();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::reset() {
|
||||||
|
m_Game.reset(0);
|
||||||
|
m_Game = std::make_unique<ClientGame>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::sendMobs(const std::vector<protocol::MobSend>& mobSends) {
|
void Client::sendMobs(const std::vector<protocol::MobSend>& mobSends) {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
namespace td {
|
namespace td {
|
||||||
namespace client {
|
namespace client {
|
||||||
|
|
||||||
ClientConnexion::ClientConnexion() : Connexion(&m_Dispatcher) {
|
ClientConnexion::ClientConnexion() : Connexion(&m_Dispatcher), m_ServerTPS(0) {
|
||||||
registerHandlers();
|
registerHandlers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ void ClientGame::HandlePacket(const protocol::UpdateMoneyPacket* packet) {
|
|||||||
|
|
||||||
void ClientGame::HandlePacket(const protocol::DisconnectPacket* packet) {
|
void ClientGame::HandlePacket(const protocol::DisconnectPacket* packet) {
|
||||||
m_GameState = game::GameState::Disconnected;
|
m_GameState = game::GameState::Disconnected;
|
||||||
|
m_Renderer->setBackgroundColor({ 0, 0, 0 });
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientGame::HandlePacket(const protocol::WorldDataPacket* packet) {
|
void ClientGame::HandlePacket(const protocol::WorldDataPacket* packet) {
|
||||||
|
|||||||
@@ -17,6 +17,11 @@ WorldClient::WorldClient(ClientGame* game) : game::World(game), protocol::Packet
|
|||||||
|
|
||||||
void WorldClient::HandlePacket(const protocol::WorldBeginDataPacket* packet) {
|
void WorldClient::HandlePacket(const protocol::WorldBeginDataPacket* packet) {
|
||||||
loadMap(packet);
|
loadMap(packet);
|
||||||
|
if (m_Game->getGameState() == game::GameState::Game) {
|
||||||
|
const game::Color& backgroundColor = getBackgroundColor();
|
||||||
|
m_Game->getRenderer()->setBackgroundColor({ static_cast<float>(backgroundColor.r / 255.0f), static_cast<float>(backgroundColor.g / 255.0f),
|
||||||
|
static_cast<float>(backgroundColor.b / 255.0f) });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldClient::HandlePacket(const protocol::WorldDataPacket* packet) {
|
void WorldClient::HandlePacket(const protocol::WorldDataPacket* packet) {
|
||||||
@@ -37,14 +42,14 @@ void WorldClient::HandlePacket(const protocol::UpgradeTowerPacket* packet) {
|
|||||||
void WorldClient::HandlePacket(const protocol::WorldAddTowerPacket* packet) {
|
void WorldClient::HandlePacket(const protocol::WorldAddTowerPacket* packet) {
|
||||||
game::TowerPtr newTower = placeTowerAt(packet->getTowerID(), packet->getTowerType(), packet->getTowerX(), packet->getTowerY(), packet->getBuilder());
|
game::TowerPtr newTower = placeTowerAt(packet->getTowerID(), packet->getTowerType(), packet->getTowerX(), packet->getTowerY(), packet->getBuilder());
|
||||||
|
|
||||||
notifyListeners(&WorldListener::OnTowerAdd, newTower);
|
getWorldNotifier().notifyListeners(&WorldListener::OnTowerAdd, newTower);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldClient::HandlePacket(const protocol::RemoveTowerPacket* packet) {
|
void WorldClient::HandlePacket(const protocol::RemoveTowerPacket* packet) {
|
||||||
game::TowerPtr tower = removeTower(packet->getTowerID());
|
game::TowerPtr tower = removeTower(packet->getTowerID());
|
||||||
|
|
||||||
if (tower != nullptr) {
|
if (tower != nullptr) {
|
||||||
notifyListeners(&WorldListener::OnTowerRemove, tower);
|
getWorldNotifier().notifyListeners(&WorldListener::OnTowerRemove, tower);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,10 +37,8 @@ void Lobby::tick() {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
if (utils::getTime() - m_StartTimerTime >= LobbyWaitingTime) {
|
if (utils::getTime() - m_StartTimerTime >= LobbyWaitingTime) {
|
||||||
protocol::UpdateGameStatePacket packet(game::GameState::Game);
|
m_Server->getGame().notifyListeners(&game::GameListener::OnGameBegin);
|
||||||
m_Server->broadcastPacket(&packet);
|
|
||||||
m_GameStarted = true;
|
m_GameStarted = true;
|
||||||
m_Server->lauchGame();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,8 +9,9 @@ Server::Server(const std::string& worldFilePath) : m_ServerRunning(false) {
|
|||||||
m_Game.getWorld()->loadMapFromFile(worldFilePath);
|
m_Game.getWorld()->loadMapFromFile(worldFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::lauchGame() {
|
Server::~Server() {
|
||||||
m_Game.startGame();
|
if (m_Thread.joinable())
|
||||||
|
m_Thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::startThread() {
|
void Server::startThread() {
|
||||||
@@ -29,13 +30,16 @@ void Server::startThread() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
clean();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Server::close() {
|
||||||
|
stopThread();
|
||||||
|
}
|
||||||
|
|
||||||
void Server::stopThread() {
|
void Server::stopThread() {
|
||||||
m_ServerRunning = false;
|
m_ServerRunning = false;
|
||||||
if (m_Thread.joinable())
|
|
||||||
m_Thread.join();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Server::start(std::uint16_t port) {
|
bool Server::start(std::uint16_t port) {
|
||||||
@@ -54,19 +58,24 @@ bool Server::start(std::uint16_t port) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::stop() {
|
void Server::clean() {
|
||||||
if (!m_ServerRunning)
|
|
||||||
return;
|
|
||||||
stopThread();
|
|
||||||
|
|
||||||
protocol::DisconnectPacket packet("Server closed");
|
|
||||||
broadcastPacket(&packet);
|
|
||||||
|
|
||||||
m_Listener.close();
|
m_Listener.close();
|
||||||
m_Listener.destroy();
|
m_Listener.destroy();
|
||||||
|
|
||||||
m_Connections.clear();
|
m_Connections.clear();
|
||||||
getPlayers().clear();
|
getPlayers().clear();
|
||||||
|
|
||||||
|
std::cout << "Server successfully stopped !\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Server::stop() {
|
||||||
|
if (!m_ServerRunning)
|
||||||
|
return;
|
||||||
|
|
||||||
|
protocol::DisconnectPacket packet("Server closed");
|
||||||
|
broadcastPacket(&packet);
|
||||||
|
|
||||||
|
stopThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::tick(std::uint64_t delta) {
|
void Server::tick(std::uint64_t delta) {
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ void ServerConnexion::HandlePacket(const protocol::PlaceTowerPacket* packet) {
|
|||||||
|
|
||||||
game::TowerPtr tower = world->placeTowerAt(towerType, packet->getTowerX(), packet->getTowerY(), m_ID);
|
game::TowerPtr tower = world->placeTowerAt(towerType, packet->getTowerX(), packet->getTowerY(), m_ID);
|
||||||
|
|
||||||
world->notifyListeners(&game::WorldListener::OnTowerAdd, tower);
|
world->getWorldNotifier().notifyListeners(&game::WorldListener::OnTowerAdd, tower);
|
||||||
|
|
||||||
protocol::WorldAddTowerPacket addTowerPacket(tower->getID(), packet->getTowerX(), packet->getTowerY(), packet->getTowerType(), m_ID);
|
protocol::WorldAddTowerPacket addTowerPacket(tower->getID(), packet->getTowerX(), packet->getTowerY(), packet->getTowerType(), m_ID);
|
||||||
m_Server->broadcastPacket(&addTowerPacket);
|
m_Server->broadcastPacket(&addTowerPacket);
|
||||||
|
|||||||
@@ -5,13 +5,17 @@ namespace td {
|
|||||||
namespace server {
|
namespace server {
|
||||||
|
|
||||||
ServerGame::ServerGame(server::Server* server) : game::Game(&m_ServerWorld), m_Server(server), m_ServerWorld(server, this) {
|
ServerGame::ServerGame(server::Server* server) : game::Game(&m_ServerWorld), m_Server(server), m_ServerWorld(server, this) {
|
||||||
|
bindListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerGame::tick(std::uint64_t delta) {
|
void ServerGame::tick(std::uint64_t delta) {
|
||||||
if (m_GameState == game::GameState::Game) {
|
if (m_GameState == game::GameState::Game) {
|
||||||
Game::tick(delta);
|
Game::tick(delta);
|
||||||
updatePlayerStats();
|
updatePlayerStats();
|
||||||
|
} else if (m_GameState == game::GameState::EndGame) {
|
||||||
|
if (m_EndGameCooldown.update(delta)) {
|
||||||
|
notifyListeners(&game::GameListener::OnGameClose);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,5 +74,31 @@ void ServerGame::balanceTeams() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ServerGame::OnGameStateUpdate(game::GameState newState) {
|
||||||
|
setGameState(newState);
|
||||||
|
protocol::UpdateGameStatePacket packet(newState);
|
||||||
|
m_Server->broadcastPacket(&packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerGame::OnGameBegin() {
|
||||||
|
notifyListeners(&game::GameListener::OnGameStateUpdate, game::GameState::Game);
|
||||||
|
startGame();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerGame::OnGameEnd() {
|
||||||
|
notifyListeners(&game::GameListener::OnGameStateUpdate, game::GameState::EndGame);
|
||||||
|
m_EndGameCooldown.applyCooldown();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ServerGame::OnGameClose() {
|
||||||
|
notifyListeners(&game::GameListener::OnGameStateUpdate, game::GameState::Closed);
|
||||||
|
// Disconnect clients
|
||||||
|
protocol::DisconnectPacket packet("Game finished");
|
||||||
|
m_Server->broadcastPacket(&packet);
|
||||||
|
|
||||||
|
// Closing server
|
||||||
|
m_Server->close();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace game
|
} // namespace game
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -37,8 +37,8 @@ void ServerWorld::spawnMobs(game::MobType type, std::uint8_t level, game::Player
|
|||||||
float minSpawnY = spawnCenterY - spawnHeight / 2.0f + mobSize.y / 2.0f;
|
float minSpawnY = spawnCenterY - spawnHeight / 2.0f + mobSize.y / 2.0f;
|
||||||
float maxSpawnY = spawnCenterY + spawnHeight / 2.0f - mobSize.y / 2.0f;
|
float maxSpawnY = spawnCenterY + spawnHeight / 2.0f - mobSize.y / 2.0f;
|
||||||
|
|
||||||
float mobX = utils::getRandomReal<float>(minSpawnX, maxSpawnX);
|
float mobX = utils::getRandomReal<float>(minSpawnX + MobSpawnBorder, maxSpawnX - MobSpawnBorder);
|
||||||
float mobY = utils::getRandomReal<float>(minSpawnY, maxSpawnY);
|
float mobY = utils::getRandomReal<float>(minSpawnY + MobSpawnBorder, maxSpawnY - MobSpawnBorder);
|
||||||
|
|
||||||
spawnMobAt(m_CurrentMobID, type, level, sender, mobX, mobY, enemyMobSpawn->getDirection());
|
spawnMobAt(m_CurrentMobID, type, level, sender, mobX, mobY, enemyMobSpawn->getDirection());
|
||||||
|
|
||||||
@@ -55,5 +55,16 @@ game::TowerPtr ServerWorld::placeTowerAt(game::TowerType type, std::int32_t x, s
|
|||||||
return tower;
|
return tower;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ServerWorld::OnMobDie(game::Mob* mob) {
|
||||||
|
if (mob->OnDeath(this)) { // check if the mob is actually dead (slimes ...)
|
||||||
|
//reward players
|
||||||
|
game::Player* sender = m_Game->getPlayerById(mob->getSender());
|
||||||
|
sender->addExp(mob->getStats()->getExpReward());
|
||||||
|
|
||||||
|
game::Player* killer = m_Game->getPlayerById(mob->getLastDamageTower()->getBuilder());
|
||||||
|
killer->addGold(mob->getStats()->getMoneyCost());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace server
|
} // namespace server
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -91,14 +91,16 @@ DataBuffer WorldBeginDataPacket::SerializeCustom() const {
|
|||||||
|
|
||||||
memcpy((std::uint8_t*)data.data() + bufferSize, decoTilePalette.data(), decoTilePalette.size() * sizeof(game::Color));
|
memcpy((std::uint8_t*)data.data() + bufferSize, decoTilePalette.data(), decoTilePalette.size() * sizeof(game::Color));
|
||||||
|
|
||||||
|
data << m_Header.m_Background;
|
||||||
|
|
||||||
const game::Spawn& redSpawn = m_Header.m_RedSpawn, blueSpawn = m_Header.m_BlueSpawn;
|
const game::Spawn& redSpawn = m_Header.m_RedSpawn, blueSpawn = m_Header.m_BlueSpawn;
|
||||||
const game::TeamCastle& redCastle = m_Header.m_RedCastle, blueCastle = m_Header.m_BlueCastle;
|
const game::TeamCastle& redCastle = m_Header.m_RedCastle, blueCastle = m_Header.m_BlueCastle;
|
||||||
|
|
||||||
data << redSpawn << redCastle;
|
data << redSpawn << static_cast<utils::shape::Rectangle>(redCastle);
|
||||||
data << blueSpawn << blueCastle;
|
data << blueSpawn << static_cast<utils::shape::Rectangle>(blueCastle);
|
||||||
|
|
||||||
// tile palette
|
// tile palette
|
||||||
data << m_Header.m_TilePalette.size();
|
data << static_cast<std::uint64_t>(m_Header.m_TilePalette.size());
|
||||||
|
|
||||||
for (game::TilePtr tile : m_Header.m_TilePalette) {
|
for (game::TilePtr tile : m_Header.m_TilePalette) {
|
||||||
data << tile;
|
data << tile;
|
||||||
@@ -123,14 +125,16 @@ DataBuffer WorldBeginDataPacket::Serialize() const {
|
|||||||
|
|
||||||
memcpy((std::uint8_t*)data.data() + bufferSize, decoTilePalette.data(), decoTilePalette.size() * sizeof(game::Color));
|
memcpy((std::uint8_t*)data.data() + bufferSize, decoTilePalette.data(), decoTilePalette.size() * sizeof(game::Color));
|
||||||
|
|
||||||
|
data << m_Header.m_World->getBackgroundColor();
|
||||||
|
|
||||||
const game::Spawn& redSpawn = m_Header.m_World->getRedTeam().getSpawn(), blueSpawn = m_Header.m_World->getBlueTeam().getSpawn();
|
const game::Spawn& redSpawn = m_Header.m_World->getRedTeam().getSpawn(), blueSpawn = m_Header.m_World->getBlueTeam().getSpawn();
|
||||||
const game::TeamCastle& redCastle = m_Header.m_World->getRedTeam().getCastle(), blueCastle = m_Header.m_World->getBlueTeam().getCastle();
|
const game::TeamCastle& redCastle = m_Header.m_World->getRedTeam().getCastle(), blueCastle = m_Header.m_World->getBlueTeam().getCastle();
|
||||||
|
|
||||||
data << redSpawn << redCastle;
|
data << redSpawn << static_cast<utils::shape::Rectangle>(redCastle);
|
||||||
data << blueSpawn << blueCastle;
|
data << blueSpawn << static_cast<utils::shape::Rectangle>(blueCastle);
|
||||||
|
|
||||||
// tile palette
|
// tile palette
|
||||||
data << m_Header.m_World->getTilePalette().size();
|
data << static_cast<std::uint64_t>(m_Header.m_World->getTilePalette().size());
|
||||||
|
|
||||||
for (game::TilePtr tile : m_Header.m_World->getTilePalette()) {
|
for (game::TilePtr tile : m_Header.m_World->getTilePalette()) {
|
||||||
data << tile;
|
data << tile;
|
||||||
@@ -155,8 +159,15 @@ void WorldBeginDataPacket::Deserialize(DataBuffer& data) {
|
|||||||
|
|
||||||
data.SetReadOffset(data.GetReadOffset() + decoPalletteSizeByte);
|
data.SetReadOffset(data.GetReadOffset() + decoPalletteSizeByte);
|
||||||
|
|
||||||
data >> m_Header.m_RedSpawn >> m_Header.m_RedCastle;
|
data >> m_Header.m_Background;
|
||||||
data >> m_Header.m_BlueSpawn >> m_Header.m_BlueCastle;
|
|
||||||
|
utils::shape::Rectangle redCastle, blueCastle;
|
||||||
|
|
||||||
|
data >> m_Header.m_RedSpawn >> redCastle;
|
||||||
|
data >> m_Header.m_BlueSpawn >> blueCastle;
|
||||||
|
|
||||||
|
m_Header.m_RedCastle.setShape(redCastle);
|
||||||
|
m_Header.m_BlueCastle.setShape(blueCastle);
|
||||||
|
|
||||||
std::uint64_t tilePaletteSize;
|
std::uint64_t tilePaletteSize;
|
||||||
data >> tilePaletteSize;
|
data >> tilePaletteSize;
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
namespace td {
|
namespace td {
|
||||||
namespace render {
|
namespace render {
|
||||||
|
|
||||||
Renderer::Renderer() {
|
Renderer::Renderer() : m_BackgroundColor(0, 0, 0) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ void Renderer::updateIsometricFade() {
|
|||||||
|
|
||||||
void Renderer::prepare() {
|
void Renderer::prepare() {
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
glClearColor(0, 0, 0, 0);
|
glClearColor(m_BackgroundColor.r, m_BackgroundColor.g, m_BackgroundColor.b, 0);
|
||||||
updateIsometricFade();
|
updateIsometricFade();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,8 @@ WorldRenderer::WorldRenderer(game::World* world, client::ClientGame* client) : m
|
|||||||
m_Renderer->setCamMovement({});
|
m_Renderer->setCamMovement({});
|
||||||
m_TowerPlacePopup = std::make_unique<gui::TowerPlacePopup>(m_Client->getClient());
|
m_TowerPlacePopup = std::make_unique<gui::TowerPlacePopup>(m_Client->getClient());
|
||||||
m_MobTooltip = std::make_unique<gui::MobTooltip>(m_Client->getClient());
|
m_MobTooltip = std::make_unique<gui::MobTooltip>(m_Client->getClient());
|
||||||
m_Client->getWorld().bindListener(this);
|
m_CastleTooltip = std::make_unique<gui::CastleTooltip>(m_Client->getClient());
|
||||||
|
m_Client->getWorldClient().getWorldNotifier().bindListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WorldRenderer::updateCursorPos() {
|
void WorldRenderer::updateCursorPos() {
|
||||||
@@ -100,6 +101,8 @@ void WorldRenderer::renderTowers() const {
|
|||||||
void WorldRenderer::renderTileSelect() const {
|
void WorldRenderer::renderTileSelect() const {
|
||||||
if (ImGui::IsAnyItemHovered()) return;
|
if (ImGui::IsAnyItemHovered()) return;
|
||||||
|
|
||||||
|
if(m_MobTooltip->isShown() || m_CastleTooltip->isShown()) return;
|
||||||
|
|
||||||
Renderer::Model tileSelectModel;
|
Renderer::Model tileSelectModel;
|
||||||
tileSelectModel.vao = m_SelectTileVao.get();
|
tileSelectModel.vao = m_SelectTileVao.get();
|
||||||
tileSelectModel.positon = { (int)m_CursorPos.x, (int)m_CursorPos.y };
|
tileSelectModel.positon = { (int)m_CursorPos.x, (int)m_CursorPos.y };
|
||||||
@@ -119,7 +122,7 @@ void WorldRenderer::render() {
|
|||||||
renderMobs();
|
renderMobs();
|
||||||
renderTowers();
|
renderTowers();
|
||||||
renderTileSelect();
|
renderTileSelect();
|
||||||
renderMobTooltip();
|
renderTooltips();
|
||||||
renderPopups();
|
renderPopups();
|
||||||
detectClick();
|
detectClick();
|
||||||
}
|
}
|
||||||
@@ -128,6 +131,11 @@ WorldRenderer::~WorldRenderer() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WorldRenderer::renderTooltips() const {
|
||||||
|
renderMobTooltip();
|
||||||
|
renderCastleTooltip();
|
||||||
|
}
|
||||||
|
|
||||||
void WorldRenderer::moveCam(float relativeX, float relativeY, float aspectRatio) {
|
void WorldRenderer::moveCam(float relativeX, float relativeY, float aspectRatio) {
|
||||||
if (m_WorldVao == nullptr)
|
if (m_WorldVao == nullptr)
|
||||||
return;
|
return;
|
||||||
@@ -245,6 +253,13 @@ void WorldRenderer::renderMobTooltip() const {
|
|||||||
m_MobTooltip->render();
|
m_MobTooltip->render();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WorldRenderer::renderCastleTooltip() const {
|
||||||
|
if (ImGui::IsAnyItemHovered()) return;
|
||||||
|
|
||||||
|
detectCastleHovering();
|
||||||
|
m_CastleTooltip->render();
|
||||||
|
}
|
||||||
|
|
||||||
void WorldRenderer::detectMobHovering() const {
|
void WorldRenderer::detectMobHovering() const {
|
||||||
glm::vec2 cursorWorldPos = getCursorWorldPos();
|
glm::vec2 cursorWorldPos = getCursorWorldPos();
|
||||||
for (game::MobPtr mob : m_World->getMobList()) {
|
for (game::MobPtr mob : m_World->getMobList()) {
|
||||||
@@ -256,6 +271,17 @@ void WorldRenderer::detectMobHovering() const {
|
|||||||
m_MobTooltip->setMob(nullptr);
|
m_MobTooltip->setMob(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WorldRenderer::detectCastleHovering() const {
|
||||||
|
glm::vec2 cursorWorldPos = getCursorWorldPos();
|
||||||
|
for (const game::Team& team : m_World->getTeams()) {
|
||||||
|
if (team.getCastle().collidesWith({ cursorWorldPos.x, cursorWorldPos.y })) {
|
||||||
|
m_CastleTooltip->setCastle(&team.getCastle());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_CastleTooltip->setCastle(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
void WorldRenderer::OnTowerAdd(game::TowerPtr tower) {
|
void WorldRenderer::OnTowerAdd(game::TowerPtr tower) {
|
||||||
WorldLoader::RenderData renderData = WorldLoader::loadTowerModel(tower);
|
WorldLoader::RenderData renderData = WorldLoader::loadTowerModel(tower);
|
||||||
m_TowersCache.addData(tower->getID(), renderData.positions, renderData.colors);
|
m_TowersCache.addData(tower->getID(), renderData.positions, renderData.colors);
|
||||||
|
|||||||
27
src/render/gui/CastleTooltip.cpp
Normal file
27
src/render/gui/CastleTooltip.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include "render/gui/CastleTooltip.h"
|
||||||
|
#include "render/gui/imgui/imgui.h"
|
||||||
|
|
||||||
|
#include "render/WorldRenderer.h"
|
||||||
|
|
||||||
|
#include "game/client/Client.h"
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace gui {
|
||||||
|
|
||||||
|
CastleTooltip::CastleTooltip(client::Client* client) : GuiWidget(client) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void CastleTooltip::render() {
|
||||||
|
if (m_Castle == nullptr) return;
|
||||||
|
|
||||||
|
ImGui::BeginTooltip();
|
||||||
|
ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::getImGuiTeamColor(m_Castle->getTeam()->getColor()));
|
||||||
|
ImGui::Text("Castle : ");
|
||||||
|
ImGui::PopStyleColor();
|
||||||
|
ImGui::Text("\tCastle HP : %i/%i", static_cast<int>(m_Castle->getLife()), game::TeamCastle::CastleMaxLife);
|
||||||
|
ImGui::EndTooltip();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace gui
|
||||||
|
} // namespace td
|
||||||
@@ -14,6 +14,8 @@ GameMenu::GameMenu(client::Client* client) : GuiWidget(client), m_SummonMenu(std
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GameMenu::render() {
|
void GameMenu::render() {
|
||||||
|
if(!m_Client->isConnected()) return;
|
||||||
|
|
||||||
if (getClient()->getGame().getGameState() == td::game::GameState::Lobby) {
|
if (getClient()->getGame().getGameState() == td::game::GameState::Lobby) {
|
||||||
ImGui::Begin("Lobby");
|
ImGui::Begin("Lobby");
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,12 @@ MainMenu::~MainMenu() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MainMenu::render() {
|
void MainMenu::render() {
|
||||||
|
if (m_Server != nullptr && !m_Server->isRunning()) {
|
||||||
|
m_Server.reset(0); // destroying server if it stoped
|
||||||
|
}
|
||||||
|
|
||||||
|
if(m_Client->isConnected()) return;
|
||||||
|
|
||||||
ImGui::Begin("Main Menu");
|
ImGui::Begin("Main Menu");
|
||||||
if (ImGui::Button("Rejoindre une partie##join")) {
|
if (ImGui::Button("Rejoindre une partie##join")) {
|
||||||
ImGui::OpenPopup("Rejoindre une partie##join_popup");
|
ImGui::OpenPopup("Rejoindre une partie##join_popup");
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ MobTooltip::MobTooltip(client::Client* client) : GuiWidget(client) {
|
|||||||
void MobTooltip::render() {
|
void MobTooltip::render() {
|
||||||
if (m_Mob == nullptr) return;
|
if (m_Mob == nullptr) return;
|
||||||
|
|
||||||
const game::Player& sender = getClient()->getGame().getPlayerById(m_Mob->getSender());
|
// TODO: add sender null check
|
||||||
|
|
||||||
|
const game::Player* sender = getClient()->getGame().getPlayerById(m_Mob->getSender());
|
||||||
ImGui::BeginTooltip();
|
ImGui::BeginTooltip();
|
||||||
ImGui::Text("Sender :");
|
ImGui::Text("Sender :");
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::getImGuiTeamColor(sender.getTeamColor()));
|
ImGui::PushStyleColor(ImGuiCol_Text, render::WorldRenderer::getImGuiTeamColor(sender->getTeamColor()));
|
||||||
ImGui::Text("%s", sender.getName().c_str());
|
ImGui::Text("%s", sender->getName().c_str());
|
||||||
ImGui::PopStyleColor();
|
ImGui::PopStyleColor();
|
||||||
ImGui::Text("Mob HP : %.1f/%i", m_Mob->getHealth(), m_Mob->getStats()->getMaxLife());
|
ImGui::Text("Mob HP : %.1f/%i", m_Mob->getHealth(), m_Mob->getStats()->getMaxLife());
|
||||||
ImGui::Text("Mob Type : %s", game::MobFactory::getMobName(m_Mob->getType()).c_str());
|
ImGui::Text("Mob Type : %s", game::MobFactory::getMobName(m_Mob->getType()).c_str());
|
||||||
|
|||||||
@@ -22,10 +22,10 @@ namespace td {
|
|||||||
namespace render {
|
namespace render {
|
||||||
|
|
||||||
void TowerGui::initWidgets() {
|
void TowerGui::initWidgets() {
|
||||||
m_MainMenu = std::make_unique<td::gui::MainMenu>(m_Client.get());
|
m_GuiManager.addWidget(std::make_unique<td::gui::MainMenu>(m_Client.get()));
|
||||||
m_GameMenu = std::make_unique<td::gui::GameMenu>(m_Client.get());
|
m_GuiManager.addWidget(std::make_unique<td::gui::GameMenu>(m_Client.get()));
|
||||||
m_FrameMenu = std::make_unique<td::gui::FrameMenu>(m_Client.get());
|
m_GuiManager.addWidget(std::make_unique<td::gui::FrameMenu>(m_Client.get()));
|
||||||
m_UpdateMenu = std::make_unique<td::gui::UpdateMenu>(m_Client.get());
|
m_GuiManager.addWidget(std::make_unique<td::gui::UpdateMenu>(m_Client.get()));
|
||||||
}
|
}
|
||||||
|
|
||||||
TowerGui::TowerGui(SDL_Window* sdl_window, SDL_GLContext glContext, td::render::Renderer* renderer) : m_Window(sdl_window),
|
TowerGui::TowerGui(SDL_Window* sdl_window, SDL_GLContext glContext, td::render::Renderer* renderer) : m_Window(sdl_window),
|
||||||
@@ -69,13 +69,8 @@ void TowerGui::render() {
|
|||||||
beginFrame();
|
beginFrame();
|
||||||
|
|
||||||
m_Client->render();
|
m_Client->render();
|
||||||
if (m_Client->isConnected())
|
|
||||||
m_GameMenu->render();
|
|
||||||
else
|
|
||||||
m_MainMenu->render();
|
|
||||||
|
|
||||||
m_FrameMenu->render();
|
m_GuiManager.renderWidgets();
|
||||||
m_UpdateMenu->render();
|
|
||||||
|
|
||||||
endFrame();
|
endFrame();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,3 @@
|
|||||||
/*
|
|
||||||
* EntityShader.cpp
|
|
||||||
*
|
|
||||||
* Created on: 4 nov. 2020
|
|
||||||
* Author: simon
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "render/shaders/EntityShader.h"
|
#include "render/shaders/EntityShader.h"
|
||||||
|
|
||||||
#ifdef __ANDROID__
|
#ifdef __ANDROID__
|
||||||
|
|||||||
@@ -38,6 +38,9 @@ target("TowerDefense")
|
|||||||
if is_os("linux") then
|
if is_os("linux") then
|
||||||
add_links("dw")
|
add_links("dw")
|
||||||
end
|
end
|
||||||
|
if is_os("windows") then
|
||||||
|
add_links("dbghelp", "psapi", "kernel32", "msvcr90")
|
||||||
|
end
|
||||||
add_cxflags("-pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default -Wundef -Wno-unused")
|
add_cxflags("-pedantic -Wall -Wextra -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 -Wswitch-default -Wundef -Wno-unused")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user