feat: implement basic tower placement
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
#include "game/client/ClientGame.h"
|
||||
#include "protocol/PacketDispatcher.h"
|
||||
//#include "game/Team.h"
|
||||
#include "game/client/Client.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
ClientGame::ClientGame(protocol::PacketDispatcher* dispatcher, render::Renderer* renderer): protocol::PacketHandler(dispatcher),
|
||||
game::Game(&m_WorldClient), m_Renderer(renderer), m_WorldClient(this), m_WorldRenderer(&m_WorldClient, m_Renderer){
|
||||
ClientGame::ClientGame(Client* client): protocol::PacketHandler(client->getConnexion().GetDispatcher()),
|
||||
game::Game(&m_WorldClient), m_Client(client), m_Renderer(client->getRenderer()), m_WorldClient(this),
|
||||
m_WorldRenderer(&m_WorldClient, this){
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::ConnectionInfo, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerJoin, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlayerList, this);
|
||||
@@ -111,5 +113,54 @@ void ClientGame::renderWorld(){
|
||||
}
|
||||
}
|
||||
|
||||
void ClientGame::PlaceTower(game::TowerType type, const glm::vec2& position){
|
||||
protocol::PlaceTowerPacket packet(position.x, position.y, type);
|
||||
m_Client->getConnexion().sendPacket(&packet);
|
||||
}
|
||||
|
||||
bool ClientGame::CanPlaceLittleTower(const glm::vec2& worldPos){
|
||||
game::TilePtr tile = m_WorldClient.getTile(worldPos.x, worldPos.y);
|
||||
|
||||
if(tile == nullptr){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(tile->getType() == game::TileType::Tower){
|
||||
for(int x = -1; x < 2; x++){
|
||||
for(int y = -1; y < 2; y++){
|
||||
game::TilePtr adjacentTile = m_WorldClient.getTile(worldPos.x + x, worldPos.y + y);
|
||||
if(adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ClientGame::CanPlaceBigTower(const glm::vec2& worldPos){
|
||||
game::TilePtr tile = m_WorldClient.getTile(worldPos.x, worldPos.y);
|
||||
|
||||
if(tile == nullptr){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(tile->getType() == game::TileType::Tower){
|
||||
for(int x = -2; x < 3; x++){
|
||||
for(int y = -2; y < 3; y++){
|
||||
game::TilePtr adjacentTile = m_WorldClient.getTile(worldPos.x + x, worldPos.y + y);
|
||||
if(adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
|
||||
27
src/game/client/TowersInfo.cpp
Normal file
27
src/game/client/TowersInfo.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "game/client/TowersInfo.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
static const std::map<TowerType, TowerInfo> TowerInfoConstants = {
|
||||
{TowerType::Archer, {"Archer", "Shoot projectiles", false}},
|
||||
{TowerType::Artillery, {"Artillery", "Explosion", false}},
|
||||
{TowerType::Ice, {"Ice", "Slow down enemies", false}},
|
||||
{TowerType::Leach, {"Leach", "Shoot projectiles", true}},
|
||||
{TowerType::Mage, {"Mage", "Set enemies on fire", false}},
|
||||
{TowerType::Necromancer, {"Necromancer", "Summon troops", true}},
|
||||
{TowerType::Poison, {"Poison", "Poison enemies", false}},
|
||||
{TowerType::Quake, {"Quake", "Shoot projectiles", false}},
|
||||
{TowerType::Sorcerer, {"Sorcerer", "Summon friendly troops", false}},
|
||||
{TowerType::Turret, {"Turret", "Shoot arrow very fast", true}},
|
||||
{TowerType::Zeus, {"Zeus", "Strike lightning", false}},
|
||||
};
|
||||
|
||||
const TowerInfo& getTowerInfo(TowerType type){
|
||||
return TowerInfoConstants.at(type);
|
||||
}
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
@@ -9,6 +9,7 @@ namespace client{
|
||||
WorldClient::WorldClient(ClientGame* game) : game::World(game), protocol::PacketHandler(game->GetDispatcher()), m_Game(game){
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::WorldBeginData, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::WorldData, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::WorldAddTower, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::SpawnMob, this);
|
||||
}
|
||||
|
||||
@@ -20,6 +21,10 @@ void WorldClient::HandlePacket(protocol::WorldDataPacket* packet){
|
||||
loadMap(packet);
|
||||
}
|
||||
|
||||
void WorldClient::HandlePacket(protocol::WorldAddTowerPacket* packet){
|
||||
|
||||
}
|
||||
|
||||
void WorldClient::HandlePacket(protocol::SpawnMobPacket* packet){
|
||||
spawnMobAt(packet->getMobID(), packet->getMobType(), packet->getMobLevel(), packet->getSender(),
|
||||
packet->getMobX(), packet->getMobY(), packet->getMobDirection());
|
||||
|
||||
@@ -38,6 +38,7 @@ void ServerConnexion::registerHandlers(){
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::KeepAlive, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::SelectTeam, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::Disconnect, this);
|
||||
GetDispatcher()->RegisterHandler(protocol::PacketType::PlaceTower, this);
|
||||
}
|
||||
|
||||
bool ServerConnexion::updateSocket(){
|
||||
@@ -143,6 +144,11 @@ void ServerConnexion::initConnection(){
|
||||
}
|
||||
}
|
||||
|
||||
void ServerConnexion::HandlePacket(protocol::PlaceTowerPacket* packet){
|
||||
// process packet
|
||||
protocol::WorldAddTowerPacket addTowerPacket(packet->getTowerX(), packet->getTowerY(), packet->getTowerType(), m_ID);
|
||||
m_Server->broadcastPacket(&addTowerPacket);
|
||||
}
|
||||
|
||||
ServerConnexion::~ServerConnexion(){
|
||||
if (GetDispatcher() != nullptr)
|
||||
|
||||
Reference in New Issue
Block a user