diff --git a/include/Avatar/AttackAndDamage.hpp b/include/Avatar/AttackAndDamage.hpp index 55f3b832..f98daba6 100644 --- a/include/Avatar/AttackAndDamage.hpp +++ b/include/Avatar/AttackAndDamage.hpp @@ -4,7 +4,7 @@ #ifndef PRACTICALTOOLSFORSIMPLEDESIGN_ATTACKANDDAMAGE_HPP #define PRACTICALTOOLSFORSIMPLEDESIGN_ATTACKANDDAMAGE_HPP -#include "Unit/Huntable.hpp" +#include "Unit/IHealthable.hpp" #include "Util/Time.hpp" #include "Weapon.hpp" @@ -13,7 +13,7 @@ class AttackAndDamage { AttackAndDamage() {} virtual ~AttackAndDamage() {} - void damageTargetWithWeapon(std::shared_ptr target, + void damageTargetWithWeapon(std::shared_ptr target, std::shared_ptr weapon) { auto targethealth = target->getHealth(); auto damage = @@ -22,7 +22,7 @@ class AttackAndDamage { targethealth->addHP(-1 * damage); } - void openFireToTarget(std::shared_ptr target) { + void openFireToTarget(std::shared_ptr target) { // cd time m_DeltaTime += m_Time.GetDeltaTime(); if (m_Weapon->getIntervalInS() <= m_DeltaTime) { diff --git a/include/Avatar/Avatar.hpp b/include/Avatar/Avatar.hpp index 3bc07483..6c68c95c 100644 --- a/include/Avatar/Avatar.hpp +++ b/include/Avatar/Avatar.hpp @@ -13,10 +13,9 @@ #include "Map/MapUtility.hpp" #include "Mechanics/GameObjectID.hpp" #include "Selectable.hpp" -#include "Unit/Huntable.hpp" #include "Util/Image.hpp" -class Avatar : public Util::GameObject, public Selectable, public Huntable { +class Avatar : public Util::GameObject, public Selectable, public IHealthable { public: Avatar(){}; @@ -69,20 +68,11 @@ class Avatar : public Util::GameObject, public Selectable, public Huntable { std::shared_ptr getMoving() { return m_Moving; } std::shared_ptr getAvatarOrder() { return m_Order; } - void setHealth(std::shared_ptr health) { m_Health = health; } - -public: - // huntable std::shared_ptr getHealth() override { return m_Health; } - void setOrderNoOrder() override { - m_Order->setAvatarOrder(AvatarOrderType::NO_ORDER); - }; - void setOrderTakenDamage() override { - m_Order->setAvatarOrder(AvatarOrderType::TAKEN_DAMAGE); - }; - glm::vec2 getCurrentLocationInCell() override { - return getMoving()->getCurrentCell(); - }; + + void setHealth(std::shared_ptr health) override { + m_Health = health; + } protected: std::shared_ptr m_AvatarSpriteSheet = diff --git a/include/Avatar/Moving.hpp b/include/Avatar/Moving.hpp index a70a9b9a..e4585202 100644 --- a/include/Avatar/Moving.hpp +++ b/include/Avatar/Moving.hpp @@ -71,13 +71,8 @@ class Moving { bool ifArrivedAtNextCell(); void setMovePath(std::deque movepath) { - if (movepath.empty()) { - m_MovePath = {MoveDirection::IDLE}; - m_CurrentDir = MoveDirection::IDLE; - } else { - m_MovePath = movepath; - m_CurrentDir = m_MovePath.front(); - } + m_MovePath = movepath; + m_CurrentDir = m_MovePath.front(); m_PrevCell = getCurrentCell(); } diff --git a/include/Map/Tile.hpp b/include/Map/Tile.hpp index 098558d0..5c833e4a 100644 --- a/include/Map/Tile.hpp +++ b/include/Map/Tile.hpp @@ -58,7 +58,7 @@ class TileClass { std::vector> getAvatars() { return m_Avatars; } std::shared_ptr getStructure() { return m_Structure; } - bool ifStructureExists() { + bool ifStrucutreExists() { if (*m_Structure->getHealth()->getLivingStatus() == LivingStatus::NOT_BORN_YET) { return false; diff --git a/include/Mechanics/BuiltStructure.hpp b/include/Mechanics/BuiltStructure.hpp index 0665a805..2759c33f 100644 --- a/include/Mechanics/BuiltStructure.hpp +++ b/include/Mechanics/BuiltStructure.hpp @@ -25,14 +25,14 @@ class BuiltStructure { if (ifCanBuildStructureAtTile(newstruct) == true) { m_BuiltStructure.push_back(newstruct); m_Map->builtStructureByCellPosition(newstruct, coords); - newstruct->getStructureOrder()->setStructureOrder( + newstruct->getStructureOrder()->setStructOrder( StructureOrderType::BUILT); } } } else { m_BuiltStructure.push_back(newstruct); m_Map->builtStructureByCellPosition(newstruct, coords); - newstruct->getStructureOrder()->setStructureOrder( + newstruct->getStructureOrder()->setStructOrder( StructureOrderType::BUILT); } } diff --git a/include/Mechanics/NemesisManager.hpp b/include/Mechanics/NemesisManager.hpp index 9b0d4a56..67cc0736 100644 --- a/include/Mechanics/NemesisManager.hpp +++ b/include/Mechanics/NemesisManager.hpp @@ -12,7 +12,7 @@ class NemesisManager { NemesisManager() {} ~NemesisManager() {} void addNemesis(std::shared_ptr avatar, - std::shared_ptr nemesis) { + std::shared_ptr nemesis) { m_Nemesis[avatar] = nemesis; } @@ -34,7 +34,7 @@ class NemesisManager { return false; } if (hunter->getDistance( - m_Nemesis[hunter]->getCurrentLocationInCell()) <= + m_Nemesis[hunter]->getMoving()->getCurrentCell()) <= hunter->getAttackAndDamage()->getWeapon()->getFireRange() * CELL_SIZE.x) // check with in range { @@ -52,16 +52,18 @@ class NemesisManager { if (ifNemesisWithinWeaponRange(hunter)) { hunter->getAvatarOrder()->setAvatarOrder( AvatarOrderType::OPEN_FIRE); - prey->setOrderTakenDamage(); + prey->getAvatarOrder()->setAvatarOrder( + AvatarOrderType::TAKEN_DAMAGE); hunter->getAttackAndDamage()->openFireToTarget(prey); } - if (pair.second->getHealth()->ifDead()) { + if (*pair.second->getHealth()->getLivingStatus() == + LivingStatus::DEAD) { removeNemesis(hunter); hunter->getAvatarOrder()->setAvatarOrder( AvatarOrderType::NO_ORDER); - prey->setOrderNoOrder(); - + prey->getAvatarOrder()->setAvatarOrder( + AvatarOrderType::NO_ORDER); break; } @@ -70,14 +72,15 @@ class NemesisManager { removeNemesis(hunter); hunter->getAvatarOrder()->setAvatarOrder( AvatarOrderType::NO_ORDER); - prey->setOrderNoOrder(); + prey->getAvatarOrder()->setAvatarOrder( + AvatarOrderType::NO_ORDER); break; } } } private: - std::unordered_map, std::shared_ptr> + std::unordered_map, std::shared_ptr> m_Nemesis; }; #endif // PRACTICALTOOLSFORSIMPLEDESIGN_NEMESISMANAGER_HPP diff --git a/include/Mechanics/StructureManager.hpp b/include/Mechanics/StructureManager.hpp index 84b2c75d..60921b2b 100644 --- a/include/Mechanics/StructureManager.hpp +++ b/include/Mechanics/StructureManager.hpp @@ -27,7 +27,7 @@ class StructureManager { newstruct->Start(); m_StructSelectingConstructionSite = newstruct; - newstruct->getStructureOrder()->setStructureOrder( + newstruct->getStructureOrder()->setStructOrder( StructureOrderType::SELECTING_SITE); } diff --git a/include/Mechanics/UnitManager.hpp b/include/Mechanics/UnitManager.hpp index 692f7ada..eb871591 100644 --- a/include/Mechanics/UnitManager.hpp +++ b/include/Mechanics/UnitManager.hpp @@ -110,40 +110,35 @@ class UnitManager : public Player { switch (unit) { case UnitType::BARRACKS: { auto structure = std::make_shared(house); - auto globalPos = MapUtil::CellCoordToGlobal(cellPos); - structure->Start(globalPos); + structure->Start(cellPos); m_StructureManager->getStructureArray()->buildNewStructure( structure, true); break; } case UnitType::ORE_REF: { auto structure = std::make_shared(house); - auto globalPos = MapUtil::CellCoordToGlobal(cellPos); - structure->Start(globalPos); + structure->Start(cellPos); m_StructureManager->getStructureArray()->buildNewStructure( structure, true); break; } case UnitType::POWER_PLANT: { auto structure = std::make_shared(house); - auto globalPos = MapUtil::CellCoordToGlobal(cellPos); - structure->Start(globalPos); + structure->Start(cellPos); m_StructureManager->getStructureArray()->buildNewStructure( structure, true); break; } case UnitType::WAR_FACT: { auto structure = std::make_shared(house); - auto globalPos = MapUtil::CellCoordToGlobal(cellPos); - structure->Start(globalPos); + structure->Start(cellPos); m_StructureManager->getStructureArray()->buildNewStructure( structure, true); break; } case UnitType::ADV_POWER_PLANT: { auto structure = std::make_shared(house); - auto globalPos = MapUtil::CellCoordToGlobal(cellPos); - structure->Start(globalPos); + structure->Start(cellPos); m_StructureManager->getStructureArray()->buildNewStructure( structure, true); break; diff --git a/include/Structure/Structure.hpp b/include/Structure/Structure.hpp index b342c419..47d94eb0 100644 --- a/include/Structure/Structure.hpp +++ b/include/Structure/Structure.hpp @@ -13,9 +13,10 @@ #include "Selectable.hpp" #include "Structure/StructureOrder.hpp" #include "Unit/Health.hpp" -#include "Unit/Huntable.hpp" -#include "Util/GameObject.hpp" +#include "Unit/IHealthable.hpp" #include "Util/Image.hpp" + +#include "Util/GameObject.hpp" #include "Util/Input.hpp" #include "Util/TransformUtils.hpp" #include "glm/glm.hpp" @@ -23,7 +24,9 @@ #define CHEAT 0.01 #define INTERVAL 50 -class Structure : public Util::GameObject, public Selectable, public Huntable { +class Structure : public Util::GameObject, + public Selectable, + public IHealthable { public: Structure() @@ -94,25 +97,16 @@ class Structure : public Util::GameObject, public Selectable, public Huntable { public: GameObjectID getID() { return m_ID; } - void setHealth(std::shared_ptr health) { m_Health = health; } + std::shared_ptr getHealth() override { return m_Health; } + void setHealth(std::shared_ptr health) override { + m_Health = health; + } std::shared_ptr getStructureOrder() { return m_Order; } + std::shared_ptr getAttackAndDamage() { return m_AttackAndDamage; } -public: - // huntable - std::shared_ptr getHealth() override { return m_Health; } - void setOrderNoOrder() override { - m_Order->setStructureOrder(StructureOrderType::NO_ORDER); - }; - void setOrderTakenDamage() override { - m_Order->setStructureOrder(StructureOrderType::TAKEN_DAMAGE); - }; - glm::vec2 getCurrentLocationInCell() override { - return getAbsoluteOccupiedArea()[0]; - }; - protected: float m_ElectricPower; float m_BuildingTime; diff --git a/include/Structure/StructureOrder.hpp b/include/Structure/StructureOrder.hpp index fb8a9bb9..a8a58c02 100644 --- a/include/Structure/StructureOrder.hpp +++ b/include/Structure/StructureOrder.hpp @@ -9,8 +9,7 @@ enum class StructureOrderType { CONSTRUCTED, SELECTING_SITE, BUILT, - NO_ORDER, - TAKEN_DAMAGE + NO_ORDER }; class StructureOrder { public: @@ -19,7 +18,7 @@ class StructureOrder { StructureOrderType getStructureOrderType() { return m_StructOrder; } - void setStructureOrder(StructureOrderType structorder) { + void setStructOrder(StructureOrderType structorder) { m_StructOrder = structorder; } diff --git a/include/Unit/Huntable.hpp b/include/Unit/Huntable.hpp deleted file mode 100644 index 02d87b0e..00000000 --- a/include/Unit/Huntable.hpp +++ /dev/null @@ -1,20 +0,0 @@ -// -// Created by 盧威任 on 5/20/24. -// - -#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_UNIT_HPP -#define PRACTICALTOOLSFORSIMPLEDESIGN_UNIT_HPP -#include "Unit/Health.hpp" -#include "pch.hpp" -class Huntable { -public: - Huntable() {} - virtual ~Huntable() {} - -public: - virtual void setOrderNoOrder() = 0; - virtual void setOrderTakenDamage() = 0; - virtual glm::vec2 getCurrentLocationInCell() = 0; - virtual std::shared_ptr getHealth() = 0; -}; -#endif // PRACTICALTOOLSFORSIMPLEDESIGN_UNIT_HPP diff --git a/include/Unit/IHealthable.hpp b/include/Unit/IHealthable.hpp new file mode 100644 index 00000000..c5722c71 --- /dev/null +++ b/include/Unit/IHealthable.hpp @@ -0,0 +1,14 @@ +// +// Created by 盧威任 on 5/5/24. +// + +#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_IHEALTHABLE_HPP +#define PRACTICALTOOLSFORSIMPLEDESIGN_IHEALTHABLE_HPP +#include "Health.hpp" +#include +class IHealthable { +public: + virtual std::shared_ptr getHealth() = 0; + virtual void setHealth(std::shared_ptr health) = 0; +}; +#endif // PRACTICALTOOLSFORSIMPLEDESIGN_IHEALTHABLE_HPP diff --git a/src/Mechanics/AvatarManager.cpp b/src/Mechanics/AvatarManager.cpp index cd1ec372..1a3db282 100644 --- a/src/Mechanics/AvatarManager.cpp +++ b/src/Mechanics/AvatarManager.cpp @@ -49,27 +49,13 @@ void AvatarManager::giveOrderToMyAvatar(std::shared_ptr unit) { MapUtil::ScreenToGlobalCoord(dest))) ->ifEnemyAtTile()) { unit->getAvatarOrder()->setAvatarOrder(AvatarOrderType::MOVE); - if (m_Map + m_NemesisManager->addNemesis( + unit, + m_Map ->getTileByCellPosition(MapUtil::GlobalCoordToCellCoord( MapUtil::ScreenToGlobalCoord(dest))) - ->ifStructureExists()) { - m_NemesisManager->addNemesis( - unit, m_Map - ->getTileByCellPosition( - MapUtil::GlobalCoordToCellCoord( - MapUtil::ScreenToGlobalCoord(dest))) - ->getStructure()); - } else { - m_NemesisManager->addNemesis( - unit, m_Map - ->getTileByCellPosition( - MapUtil::GlobalCoordToCellCoord( - MapUtil::ScreenToGlobalCoord(dest))) - ->getAvatars()[0]); - } - + ->getAvatars()[0]); } else { - m_NemesisManager->removeNemesis(unit); unit->getAvatarOrder()->setAvatarOrder(AvatarOrderType::MOVE); } } diff --git a/src/Mechanics/CursorSelection.cpp b/src/Mechanics/CursorSelection.cpp index c1db8c88..d082f420 100644 --- a/src/Mechanics/CursorSelection.cpp +++ b/src/Mechanics/CursorSelection.cpp @@ -28,7 +28,7 @@ void CursorSelection::cursorSelect() { for (int i = min_y_cell; i <= max_y_cell; i++) { for (int j = min_x_cell; j <= max_x_cell; j++) { auto tile = m_Map->getTileByCellPosition(glm::vec2(j, i)); - if (tile->ifStructureExists()) { + if (tile->ifStrucutreExists()) { auto structure = tile->getStructure(); Append(structure); diff --git a/src/Scene/TutorialScene.cpp b/src/Scene/TutorialScene.cpp index de38b4df..a8b55df6 100644 --- a/src/Scene/TutorialScene.cpp +++ b/src/Scene/TutorialScene.cpp @@ -29,8 +29,6 @@ void TutorialScene::Start() { // combat test m_EnemyObjectManager->spawn(m_Map, UnitType::INFANTRY, HouseType::ENEMY, {6, 6}); - m_EnemyObjectManager->spawn(m_Map, UnitType::BARRACKS, HouseType::ENEMY, - {10, 10}); stageStart(); } diff --git a/src/Structure/Barracks.cpp b/src/Structure/Barracks.cpp index 565e4977..95f28481 100644 --- a/src/Structure/Barracks.cpp +++ b/src/Structure/Barracks.cpp @@ -20,7 +20,7 @@ void Barracks::Start() { m_HighLight.SetHLScale(this->GetTransform().scale); whenSelected(); // State - getStructureOrder()->setStructureOrder(StructureOrderType::CONSTRUCTED); + getStructureOrder()->setStructOrder(StructureOrderType::CONSTRUCTED); // health Structure::getHealth()->setLivingStatus( std::make_shared(LivingStatus::ALIVE)); @@ -47,7 +47,7 @@ void Barracks::Start(glm::vec2 location) { SetVisible(true); m_SpriteSheetAnimation->initSpriteSheetAnimation(m_StructureSpriteSheet, false, INTERVAL, false); - getStructureOrder()->setStructureOrder(StructureOrderType::BUILT); + getStructureOrder()->setStructOrder(StructureOrderType::BUILT); Structure::getHealth()->setLivingStatus( std::make_shared(LivingStatus::ALIVE)); } diff --git a/src/Structure/OreRefinery.cpp b/src/Structure/OreRefinery.cpp index 979b36bc..b6bd1020 100644 --- a/src/Structure/OreRefinery.cpp +++ b/src/Structure/OreRefinery.cpp @@ -20,7 +20,7 @@ void OreRefinery::Start() { m_HighLight.SetHLScale(this->GetTransform().scale); whenSelected(); // State - getStructureOrder()->setStructureOrder( + getStructureOrder()->setStructOrder( StructureOrderType::NOT_CONSTRUCTED_YET); Structure::getHealth()->setLivingStatus( std::make_shared(LivingStatus::ALIVE)); @@ -47,7 +47,7 @@ void OreRefinery::Start(glm::vec2 location) { SetVisible(true); m_SpriteSheetAnimation->initSpriteSheetAnimation(m_StructureSpriteSheet, false, INTERVAL, false); - getStructureOrder()->setStructureOrder(StructureOrderType::BUILT); + getStructureOrder()->setStructOrder(StructureOrderType::BUILT); Structure::getHealth()->setLivingStatus( std::make_shared(LivingStatus::ALIVE)); } diff --git a/src/Structure/Structure.cpp b/src/Structure/Structure.cpp index af8dbfdb..df404de3 100644 --- a/src/Structure/Structure.cpp +++ b/src/Structure/Structure.cpp @@ -11,7 +11,7 @@ void Structure::Start() { if (this->m_ID.getUnitType() == UnitType::NONE) { Structure::getHealth()->setLivingStatus( std::make_shared(LivingStatus::NOT_BORN_YET)); - getStructureOrder()->setStructureOrder(StructureOrderType::NO_ORDER); + getStructureOrder()->setStructOrder(StructureOrderType::NO_ORDER); } else { m_Transform.scale = {2.f, 2.f}; m_HighLight.SetDrawable( @@ -23,8 +23,7 @@ void Structure::Start() { SetSpriteSheet(); Structure::getHealth()->setLivingStatus( std::make_shared(LivingStatus::ALIVE)); - getStructureOrder()->setStructureOrder( - StructureOrderType::SELECTING_SITE); + getStructureOrder()->setStructOrder(StructureOrderType::SELECTING_SITE); } } void Structure::Start(glm::vec2 location) { @@ -38,12 +37,12 @@ void Structure::Start(glm::vec2 location) { SetSpriteSheet(); Structure::getHealth()->setLivingStatus( std::make_shared(LivingStatus::ALIVE)); - getStructureOrder()->setStructureOrder(StructureOrderType::SELECTING_SITE); + getStructureOrder()->setStructOrder(StructureOrderType::SELECTING_SITE); this->SetObjectLocation(location); SetVisible(true); m_SpriteSheetAnimation->initSpriteSheetAnimation(m_StructureSpriteSheet, false, INTERVAL, false); - getStructureOrder()->setStructureOrder(StructureOrderType::BUILT); + getStructureOrder()->setStructOrder(StructureOrderType::BUILT); Structure::getHealth()->setLivingStatus( std::make_shared(LivingStatus::ALIVE)); } @@ -62,9 +61,6 @@ void Structure::Update() { } else if (getStructureOrder()->getStructureOrderType() == StructureOrderType::BUILT) { this->updateFixed(); - } else if (getStructureOrder()->getStructureOrderType() == - StructureOrderType::TAKEN_DAMAGE) { - this->updateFixed(); } break; case LivingStatus::DEAD: diff --git a/src/Structure/WarFactory.cpp b/src/Structure/WarFactory.cpp index 2e7f32fd..32457ade 100644 --- a/src/Structure/WarFactory.cpp +++ b/src/Structure/WarFactory.cpp @@ -44,7 +44,7 @@ void WarFactory::Start(glm::vec2 location) { SetVisible(true); m_SpriteSheetAnimation->initSpriteSheetAnimation(m_StructureSpriteSheet, false, INTERVAL, false); - getStructureOrder()->setStructureOrder(StructureOrderType::BUILT); + getStructureOrder()->setStructOrder(StructureOrderType::BUILT); Structure::getHealth()->setLivingStatus( std::make_shared(LivingStatus::ALIVE)); }