From 33bd683c9d49704eb49db3b98a57b052444a978f Mon Sep 17 00:00:00 2001 From: jonylu7 Date: Wed, 8 May 2024 19:05:53 +0800 Subject: [PATCH 1/3] spawn --- CMakeLists.txt | 2 +- src/Mechanics/AvatarManager.cpp | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 02f71839..231d23b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,7 +113,7 @@ set(SRC_FILES ${SRC_DIR}/Map/MapbinReader.cpp ${SRC_DIR}/Map/Map.cpp - ${SRC_DIR}/Mechanics/FindValidPathToDest.cpp + ${SRC_DIR}/Mechanics/AvatarNavigator.cpp ${SRC_DIR}/Mechanics/GameObjectID.cpp ${SRC_DIR}/Mechanics/CursorSelection.cpp ${SRC_DIR}/Mechanics/AvatarManager.cpp diff --git a/src/Mechanics/AvatarManager.cpp b/src/Mechanics/AvatarManager.cpp index 70865493..a35f7928 100644 --- a/src/Mechanics/AvatarManager.cpp +++ b/src/Mechanics/AvatarManager.cpp @@ -34,15 +34,18 @@ void AvatarManager::giveOrderToAvatar(std::shared_ptr unit) { } } -void AvatarManager::updateTileWhileAvatarMoving(std::shared_ptr unit) { - if (unit->ifArrivedAtNextCell()) { - m_Map->removeAvatarsByCellPosition(unit, unitArrayAndLocation[unit]); - m_Map->setAvatarByCellPosition(unit, unit->getCurrentCell()); - unitArrayAndLocation[unit] = unit->getCurrentCell(); +void AvatarManager::updateTileWhileAvatarMoving( + std::shared_ptr avatar) { + if (avatar->ifArrivedAtNextCell()) { + m_Map->removeAvatarsByCellPosition(avatar, + unitArrayAndLocation[avatar]); + m_Map->setAvatarByCellPosition(avatar, avatar->getCurrentCell()); + unitArrayAndLocation[avatar] = avatar->getCurrentCell(); } } void AvatarManager::AppendAvatar(std::shared_ptr newAvatar) { m_AvatarArray.push_back(newAvatar); unitArrayAndLocation[newAvatar] = newAvatar->getCurrentCell(); + m_Map->setAvatarByCellPosition(newAvatar, newAvatar->getCurrentCell()); } From 2794d6bb1c6ebfe1e1a8b222920d3f3b965d4322 Mon Sep 17 00:00:00 2001 From: jonylu7 Date: Thu, 9 May 2024 10:09:54 +0800 Subject: [PATCH 2/3] broken --- CMakeLists.txt | 4 +-- include/Avatar/Avatar.hpp | 4 ++- include/Avatar/Moving.hpp | 13 +++----- include/Map/Tile.hpp | 3 +- src/Avatar/Avatar.cpp | 32 +++++++++++++------ .../{PathFindingUnit.cpp => Moving.cpp} | 12 ++++--- src/Mechanics/AvatarManager.cpp | 28 ++++++++-------- 7 files changed, 57 insertions(+), 39 deletions(-) rename src/Avatar/{PathFindingUnit.cpp => Moving.cpp} (94%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 231d23b7..449c9ed6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,7 +101,7 @@ set(SRC_FILES ${SRC_DIR}/Avatar/Avatar.cpp ${SRC_DIR}/Avatar/Hunter.cpp ${SRC_DIR}/Avatar/PathUtility.cpp - ${SRC_DIR}/Avatar/PathFindingUnit.cpp + ${SRC_DIR}/Avatar/Moving.cpp ${SRC_DIR}/Scene/DefaultScene.cpp ${SRC_DIR}/Camera.cpp @@ -201,7 +201,7 @@ set(INCLUDE_FILES ${INCLUDE_DIR}/Structure/WarFactory.hpp ${INCLUDE_DIR}/Structure/ADVPowerPlants.hpp - ${INCLUDE_DIR}/Avatar/PathfindingUnit.hpp + ${INCLUDE_DIR}/Avatar/Moving.hpp ${INCLUDE_DIR}/Avatar/Avatar.hpp ${INCLUDE_DIR}/Avatar/WayPointUnit.hpp ${INCLUDE_DIR}/Avatar/Runner.hpp diff --git a/include/Avatar/Avatar.hpp b/include/Avatar/Avatar.hpp index 07c80463..fd5cbb99 100644 --- a/include/Avatar/Avatar.hpp +++ b/include/Avatar/Avatar.hpp @@ -27,7 +27,9 @@ class Avatar : public Moving, ~Avatar() override{}; virtual void Start(glm::vec2 spawnlocationcell); - void aliveUpdate(); + void noOrderUpdate(); + void spawnedUpdate(); + void moveUpdate(); void deadUpdate(); void attackUpdate(); diff --git a/include/Avatar/Moving.hpp b/include/Avatar/Moving.hpp index 57411deb..8db0fd5e 100644 --- a/include/Avatar/Moving.hpp +++ b/include/Avatar/Moving.hpp @@ -25,9 +25,7 @@ class Moving { MoveDirection m_CurrentDir = MoveDirection::IDLE; float m_MovementSpeed = 1.F; - int m_MoveDistance = 0; - - bool b_NewDestinationIsSetted = false; + glm::vec2 m_PrevCell; public: enum class AvatarStandingCorner { @@ -56,16 +54,13 @@ class Moving { void setMovePath(std::deque movepath) { m_MovePath = movepath; - setNewDestinationIsSetted(false); + m_CurrentDir = m_MovePath.front(); + m_MovePath.pop_front(); + m_PrevCell = getCurrentCell(); } - bool ifNewDestionationIsSetted() { return b_NewDestinationIsSetted; } - void setMovementSpeed(float speed) { m_MovementSpeed = speed; } - void setNewDestinationIsSetted(bool value) { - b_NewDestinationIsSetted = value; - } glm::vec2 getDestinationCell() { return MapUtil::CellCoordToGlobal(m_DestinationLocation); } diff --git a/include/Map/Tile.hpp b/include/Map/Tile.hpp index 628ce966..23d1cc60 100644 --- a/include/Map/Tile.hpp +++ b/include/Map/Tile.hpp @@ -59,7 +59,8 @@ class TileClass { std::vector> getAvatars() { return m_Avatars; } std::shared_ptr getStructure() { return m_Structure; } bool ifStrucutreExists() { - if (m_Structure == std::make_shared()) { + if (*m_Structure->getHealth()->getLivingStatus() == + LivingStatus::NOT_BORN_YET) { return false; } else { return true; diff --git a/src/Avatar/Avatar.cpp b/src/Avatar/Avatar.cpp index 559cae29..a58620f7 100644 --- a/src/Avatar/Avatar.cpp +++ b/src/Avatar/Avatar.cpp @@ -16,39 +16,53 @@ void Avatar::Update() { case (LivingStatus::ALIVE): whenSelected(); - aliveUpdate(); if (m_AvatarOrder == AvatarOrderType::OPEN_FIRE) { } else if (m_AvatarOrder == AvatarOrderType::MOVE) { - + moveUpdate(); } else if (m_AvatarOrder == AvatarOrderType::NO_ORDER) { + noOrderUpdate(); } else if (m_AvatarOrder == AvatarOrderType::TAKEN_DAMAGE) { + } else if (m_AvatarOrder == AvatarOrderType::SPAWNED) { + spawnedUpdate(); } break; } } -void Avatar::aliveUpdate() { - moveToNextCell(); +void Avatar::noOrderUpdate() { + m_CurrentDir = MoveDirection::IDLE; + SetVisible(true); + m_Transform.translation = getCurrentLocation(); + + Draw(); +} + +void Avatar::spawnedUpdate() { + SetVisible(true); + m_Transform.translation = getCurrentLocation(); + + Draw(); +} +void Avatar::moveUpdate() { + if (ifArrivedAtNextCell()) { if (m_MovePath.size() >= 1) { m_CurrentDir = m_MovePath.front(); m_MovePath.pop_front(); } else { m_CurrentDir = MoveDirection::IDLE; + m_AvatarOrder = AvatarOrderType::NO_ORDER; } } + moveToNextCell(); SetVisible(true); m_Transform.translation = getCurrentLocation(); Draw(); - - printf("Avatar cell={%d,%d}\n", getCurrentLocation().x, - getCurrentLocation().y); - printf("-----------avatar------------------\n"); } void Avatar::Start(glm::vec2 spawnlocationcell) { // destination = Barrack's @@ -58,7 +72,7 @@ void Avatar::Start(glm::vec2 spawnlocationcell) { // destination = Barrack's // setSpriteSheet(); SetVisible(true); setMovementSpeed(4); - m_AvatarOrder = AvatarOrderType::MOVE; + m_AvatarOrder = AvatarOrderType::SPAWNED; m_CurrentLocation = MapUtil::CellCoordToGlobal(spawnlocationcell); m_Transform.scale = {1, 1}; getHealth()->setLivingStatus( diff --git a/src/Avatar/PathFindingUnit.cpp b/src/Avatar/Moving.cpp similarity index 94% rename from src/Avatar/PathFindingUnit.cpp rename to src/Avatar/Moving.cpp index 64671124..c7f2f107 100644 --- a/src/Avatar/PathFindingUnit.cpp +++ b/src/Avatar/Moving.cpp @@ -57,14 +57,18 @@ void Moving::moveToNextCell() { break; } + case MoveDirection::IDLE: { + return; + } } } bool Moving::ifArrivedAtNextCell() { - // change this to check - // previous cell - // getCurrentCell() != previousCell() - return false; + if (m_PrevCell != getCurrentCell()) { + return true; + } else { + return false; + } } void Moving::moveToCellCorner(AvatarStandingCorner corner) { float quaterHeight = CELL_SIZE.y / 4; diff --git a/src/Mechanics/AvatarManager.cpp b/src/Mechanics/AvatarManager.cpp index a35f7928..b17fe66a 100644 --- a/src/Mechanics/AvatarManager.cpp +++ b/src/Mechanics/AvatarManager.cpp @@ -17,20 +17,22 @@ void AvatarManager::Update() { } void AvatarManager::giveOrderToAvatar(std::shared_ptr unit) { + if (Util::Input::IsKeyDown(Util::Keycode::MOUSE_RB)) { + auto dest = Util::Input::GetCursorPosition(); + auto queue = m_Navigator->findPath( + unit->getCurrentCell(), MapUtil::GlobalCoordToCellCoord( + MapUtil::ScreenToGlobalCoord(dest))); + // unit + unit->setMovePath(queue); - auto queue = m_Navigator->findPath(unit->getCurrentCell(), - unit->getDestinationCell()); - // unit - unit->setMovePath(queue); - unit->setNewDestinationIsSetted(false); - - if (m_Map->getTileByCellPosition(unit->getDestinationCell()) - ->ifEnemyAtTile()) { - m_NemesisManager->addNemesis( - unit, m_Map->getTileByCellPosition(unit->getDestinationCell()) - ->getAvatars()[0]); - } else { - unit->setAvatarOrder(AvatarOrderType::MOVE); + if (m_Map->getTileByCellPosition(unit->getDestinationCell()) + ->ifEnemyAtTile()) { + m_NemesisManager->addNemesis( + unit, m_Map->getTileByCellPosition(unit->getDestinationCell()) + ->getAvatars()[0]); + } else { + unit->setAvatarOrder(AvatarOrderType::MOVE); + } } } From 72147b4b36ad99a3108d8b10d9fda5126b384eed Mon Sep 17 00:00:00 2001 From: jonylu7 Date: Fri, 10 May 2024 09:44:13 +0800 Subject: [PATCH 3/3] attack and damage --- include/Avatar/Avatar.hpp | 4 ++++ include/Mechanics/NemesisManager.hpp | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/Avatar/Avatar.hpp b/include/Avatar/Avatar.hpp index fd5cbb99..66ca64dd 100644 --- a/include/Avatar/Avatar.hpp +++ b/include/Avatar/Avatar.hpp @@ -66,6 +66,10 @@ class Avatar : public Moving, m_Health = health; } + std::shared_ptr getAttackAndDamager() { + return m_AttackAndDamage; + } + protected: std::shared_ptr m_AvatarSpriteSheet = std::make_shared(); diff --git a/include/Mechanics/NemesisManager.hpp b/include/Mechanics/NemesisManager.hpp index 36eddc5a..ccc13e2d 100644 --- a/include/Mechanics/NemesisManager.hpp +++ b/include/Mechanics/NemesisManager.hpp @@ -46,7 +46,7 @@ class NemesisManager { if (ifNemesisWithinWeaponRange(hunter)) { hunter->setAvatarOrder(AvatarOrderType::OPEN_FIRE); prey->setAvatarOrder(AvatarOrderType::TAKEN_DAMAGE); - hunter->openFireToTarget(prey); + hunter->getAttackAndDamager()->openFireToTarget(prey); } if (*pair.second->getHealth()->getLivingStatus() ==