feat: improved tower place + tower upgrade

This commit is contained in:
2021-11-05 17:35:39 +01:00
parent 524af9ad5f
commit f863fb4942
6 changed files with 87 additions and 32 deletions

View File

@@ -125,6 +125,7 @@ typedef std::shared_ptr<Tower> TowerPtr;
namespace TowerFactory { namespace TowerFactory {
TowerPtr createTower(TowerType type, TowerID id, std::int32_t x, std::int32_t y, PlayerID builder); TowerPtr createTower(TowerType type, TowerID id, std::int32_t x, std::int32_t y, PlayerID builder);
std::string getTowerName(TowerType type);
} // namespace TowerFactory } // namespace TowerFactory

View File

@@ -163,7 +163,7 @@ public:
bool CanPlaceLittleTower(const glm::vec2& worldPos, PlayerID player) const; bool CanPlaceLittleTower(const glm::vec2& worldPos, PlayerID player) const;
bool CanPlaceBigTower(const glm::vec2& worldPos, PlayerID player) const; bool CanPlaceBigTower(const glm::vec2& worldPos, PlayerID player) const;
TowerPtr GetTower(const glm::vec2& position); // returns null if no tower is here TowerPtr getTower(const glm::vec2& position) const; // returns null if no tower is here
const std::unordered_map<ChunkCoord, ChunkPtr>& getChunks() const { return m_Chunks; } const std::unordered_map<ChunkCoord, ChunkPtr>& getChunks() const { return m_Chunks; }

View File

@@ -28,7 +28,7 @@ private:
glm::vec2 m_HoldCursorPos; glm::vec2 m_HoldCursorPos;
float m_Zoom; float m_Zoom;
float m_CamSensibility = 1; float m_CamSensibility = 1;
bool m_TowerPlacePopupOpened = false; bool m_PopupOpened = false;
VertexCache m_TowersCache; VertexCache m_TowersCache;
public: public:
WorldRenderer(game::World* world, client::ClientGame* client); WorldRenderer(game::World* world, client::ClientGame* client);
@@ -53,6 +53,8 @@ private:
void renderMobs() const; void renderMobs() const;
void renderTileSelect() const; void renderTileSelect() const;
void renderPopups() const; void renderPopups() const;
void renderTowerPlacePopup() const;
void renderTowerUpgradePopup() const;
void renderMobTooltip() const; void renderMobTooltip() const;
void detectClick(); void detectClick();
glm::vec2 getCursorWorldPos() const; glm::vec2 getCursorWorldPos() const;

View File

@@ -160,6 +160,35 @@ TowerPtr createTower(TowerType type, TowerID id, std::int32_t x, std::int32_t y,
return towerFactory.at(type)(id, x, y, builder); return towerFactory.at(type)(id, x, y, builder);
} }
std::string getTowerName(TowerType type) {
switch (type) {
case TowerType::Archer:
return "Archer";
case TowerType::Artillery:
return "Artillery";
case TowerType::Ice:
return "Ice";
case TowerType::Mage:
return "Mage";
case TowerType::Poison:
return "Poison";
case TowerType::Quake:
return "Quake";
case TowerType::Sorcerer:
return "Sorcerer";
case TowerType::Zeus:
return "Zeus";
case TowerType::Leach:
return "Leach";
case TowerType::Necromancer:
return "Necromancer";
case TowerType::Turret:
return "Turret";
}
}
} // namespace TowerFactory } // namespace TowerFactory

View File

@@ -202,7 +202,7 @@ bool World::CanPlaceLittleTower(const glm::vec2& worldPos, PlayerID playerID) co
for (int x = -1; x < 2; x++) { for (int x = -1; x < 2; x++) {
for (int y = -1; y < 2; y++) { for (int y = -1; y < 2; y++) {
game::TilePtr adjacentTile = getTile(worldPos.x + x, worldPos.y + y); game::TilePtr adjacentTile = getTile(worldPos.x + x, worldPos.y + y);
if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower) { if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower || getTower({worldPos.x + x, worldPos.y + y}) != nullptr) {
return false; return false;
} }
} }
@@ -214,6 +214,8 @@ bool World::CanPlaceLittleTower(const glm::vec2& worldPos, PlayerID playerID) co
} }
bool World::CanPlaceBigTower(const glm::vec2& worldPos, PlayerID playerID) const { bool World::CanPlaceBigTower(const glm::vec2& worldPos, PlayerID playerID) const {
if(!CanPlaceLittleTower(worldPos, playerID)) return false;
TilePtr tile = getTile(worldPos.x, worldPos.y); TilePtr tile = getTile(worldPos.x, worldPos.y);
const Player& player = m_Game->getPlayers()[playerID]; const Player& player = m_Game->getPlayers()[playerID];
@@ -228,7 +230,7 @@ bool World::CanPlaceBigTower(const glm::vec2& worldPos, PlayerID playerID) const
for (int x = -2; x < 3; x++) { for (int x = -2; x < 3; x++) {
for (int y = -2; y < 3; y++) { for (int y = -2; y < 3; y++) {
game::TilePtr adjacentTile = getTile(worldPos.x + x, worldPos.y + y); game::TilePtr adjacentTile = getTile(worldPos.x + x, worldPos.y + y);
if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower) { if (adjacentTile == nullptr || adjacentTile->getType() != game::TileType::Tower || getTower({worldPos.x + x, worldPos.y + y}) != nullptr) {
return false; return false;
} }
} }
@@ -257,7 +259,7 @@ void World::cleanDeadMobs() {
} }
} }
TowerPtr World::GetTower(const glm::vec2& position) { TowerPtr World::getTower(const glm::vec2& position) const{
for (TowerPtr tower : m_Towers) { for (TowerPtr tower : m_Towers) {
if (tower->getSize() == TowerSize::Big) { if (tower->getSize() == TowerSize::Big) {
if (tower->getX() - 2 <= position.x && tower->getX() + 3 >= position.x && if (tower->getX() - 2 <= position.x && tower->getX() + 3 >= position.x &&

View File

@@ -54,10 +54,10 @@ void WorldRenderer::update() {
} }
updateCursorPos(); updateCursorPos();
if (ImGui::IsMouseClicked(0)) { if (ImGui::IsMouseClicked(0)) {
if (!m_TowerPlacePopupOpened) { if (!m_PopupOpened) {
m_HoldCursorPos = { io.MousePos.x, io.MousePos.y }; m_HoldCursorPos = { io.MousePos.x, io.MousePos.y };
} else { } else {
m_TowerPlacePopupOpened = false; m_PopupOpened = false;
} }
} }
} }
@@ -88,6 +88,11 @@ void WorldRenderer::renderTileSelect() const {
m_Renderer->renderModel(tileSelectModel); m_Renderer->renderModel(tileSelectModel);
} }
void WorldRenderer::renderPopups() const {
renderTowerPlacePopup();
renderTowerUpgradePopup();
}
void WorldRenderer::render() { void WorldRenderer::render() {
if (m_WorldVao == nullptr) if (m_WorldVao == nullptr)
return; return;
@@ -126,9 +131,13 @@ void WorldRenderer::changeZoom(float zoomStep) {
} }
void WorldRenderer::click() { void WorldRenderer::click() {
if (m_Client->getWorld().CanPlaceLittleTower(getClickWorldPos(), m_Client->getPlayer()->getID())) { const game::TowerPtr tower = m_Client->getWorld().getTower(getClickWorldPos());
if(tower != nullptr){ // there is a tower here
ImGui::OpenPopup("TowerUpgrade");
m_PopupOpened = true;
}else if (m_Client->getWorld().CanPlaceLittleTower(getClickWorldPos(), m_Client->getPlayer()->getID())) {
ImGui::OpenPopup("TowerPlace"); ImGui::OpenPopup("TowerPlace");
m_TowerPlacePopupOpened = true; m_PopupOpened = true;
} }
} }
@@ -137,7 +146,7 @@ void WorldRenderer::setCamPos(float camX, float camY) {
m_Renderer->setCamPos(m_CamPos); m_Renderer->setCamPos(m_CamPos);
} }
void WorldRenderer::renderPopups() const { void WorldRenderer::renderTowerPlacePopup() const {
if (ImGui::BeginPopup("TowerPlace")) { if (ImGui::BeginPopup("TowerPlace")) {
for (int i = 0; i < (int)game::TowerType::TowerCount; i++) { for (int i = 0; i < (int)game::TowerType::TowerCount; i++) {
game::TowerType towerType = game::TowerType(i); game::TowerType towerType = game::TowerType(i);
@@ -157,6 +166,18 @@ void WorldRenderer::renderPopups() const {
} }
} }
void WorldRenderer::renderTowerUpgradePopup() const {
if (ImGui::BeginPopup("TowerUpgrade")) {
game::TowerPtr tower = m_Client->getWorld().getTower(getClickWorldPos());
if(tower == nullptr){
ImGui::EndPopup();
return;
}
ImGui::Text("Tower : %s", game::TowerFactory::getTowerName(tower->getType()).c_str());
ImGui::EndPopup();
}
}
void WorldRenderer::detectClick() { void WorldRenderer::detectClick() {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
if (ImGui::IsMouseReleased(0) && !ImGui::IsAnyItemHovered() && !ImGui::IsAnyItemFocused()) { if (ImGui::IsMouseReleased(0) && !ImGui::IsAnyItemHovered() && !ImGui::IsAnyItemFocused()) {
@@ -176,27 +197,27 @@ void WorldRenderer::renderMobTooltip() const {
if (cursorWorldPos.x > mobCenterX - 0.5f && cursorWorldPos.x < mobCenterX + 0.5f if (cursorWorldPos.x > mobCenterX - 0.5f && cursorWorldPos.x < mobCenterX + 0.5f
&& cursorWorldPos.y > mobCenterY - 0.5f && cursorWorldPos.y < mobCenterY + 0.5f) { && cursorWorldPos.y > mobCenterY - 0.5f && cursorWorldPos.y < mobCenterY + 0.5f) {
const game::Player& sender = m_Client->getPlayerById(mob->getSender()); const game::Player& sender = m_Client->getPlayerById(mob->getSender());
ImGui::BeginTooltip(); ImGui::BeginTooltip();
ImGui::Text("Sender :"); ImGui::Text("Sender :");
ImGui::SameLine(); ImGui::SameLine();
ImGui::PushStyleColor(ImGuiCol_Text, getImGuiTeamColor(sender.getTeamColor())); ImGui::PushStyleColor(ImGuiCol_Text, 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", mob->getHealth(), mob->getStats()->getMaxLife()); ImGui::Text("Mob HP : %.1f/%i", mob->getHealth(), mob->getStats()->getMaxLife());
ImGui::Text("Mob Type : %s", game::MobFactory::getMobName(mob->getType()).c_str()); ImGui::Text("Mob Type : %s", game::MobFactory::getMobName(mob->getType()).c_str());
ImGui::Text("Mob Level : %i", mob->getLevel()); ImGui::Text("Mob Level : %i", mob->getLevel());
ImGui::NewLine(); ImGui::NewLine();
ImGui::Text("Mob Stats :"); ImGui::Text("Mob Stats :");
ImGui::Text("\tMax health : %i", mob->getStats()->getMaxLife()); ImGui::Text("\tMax health : %i", mob->getStats()->getMaxLife());
ImGui::Text("\tSpeed : %.1f", mob->getStats()->getMovementSpeed()); ImGui::Text("\tSpeed : %.1f", mob->getStats()->getMovementSpeed());
ImGui::Text("\tDamage : %.1f", mob->getStats()->getDamage()); ImGui::Text("\tDamage : %.1f", mob->getStats()->getDamage());
ImGui::Text("\tMoney cost : %i", mob->getStats()->getMoneyCost()); ImGui::Text("\tMoney cost : %i", mob->getStats()->getMoneyCost());
ImGui::Text("\tEXP cost : %i", mob->getStats()->getExpCost()); ImGui::Text("\tEXP cost : %i", mob->getStats()->getExpCost());
ImGui::Text("\tEXP reward : %i", mob->getStats()->getExpReward()); ImGui::Text("\tEXP reward : %i", mob->getStats()->getExpReward());
ImGui::EndTooltip(); ImGui::EndTooltip();
break; break;
} }
} }
} }