diff --git a/assets/sprites/.DS_Store b/assets/sprites/.DS_Store index ea923d77..4cd0e843 100644 Binary files a/assets/sprites/.DS_Store and b/assets/sprites/.DS_Store differ diff --git a/assets/sprites/mech.png b/assets/sprites/mech.png deleted file mode 100644 index 1c49b5ce..00000000 Binary files a/assets/sprites/mech.png and /dev/null differ diff --git a/assets/sprites/mech_dead.png b/assets/sprites/mech_dead.png new file mode 100644 index 00000000..fe0853cd Binary files /dev/null and b/assets/sprites/mech_dead.png differ diff --git a/assets/sprites/mech_open_fire.png b/assets/sprites/mech_open_fire.png new file mode 100644 index 00000000..d66eefba Binary files /dev/null and b/assets/sprites/mech_open_fire.png differ diff --git a/assets/sprites/mech_taken_damage.png b/assets/sprites/mech_taken_damage.png new file mode 100644 index 00000000..96ffd139 Binary files /dev/null and b/assets/sprites/mech_taken_damage.png differ diff --git a/include/Avatar/Avatar.hpp b/include/Avatar/Avatar.hpp index b5a12e47..525d6033 100644 --- a/include/Avatar/Avatar.hpp +++ b/include/Avatar/Avatar.hpp @@ -15,30 +15,28 @@ #include "Selectable.hpp" #include "Util/Image.hpp" -class Avatar : public Moving, - public AttackAndDamage, +class Avatar : public AttackAndDamage, + public AvatarOrder, public Util::GameObject, public Selectable, - public AvatarOrder, public IHealthable { public: Avatar(){}; Avatar(UnitType unit, HouseType house) - : m_ID(GameObjectID(UnitType::NONE, HouseType::MY)){}; + : m_ID(GameObjectID(UnitType::NONE, house)){}; ~Avatar() override{}; virtual void Start(glm::vec2 spawnlocationcell); - void noOrderUpdate(); + void noorderUpdate(); void spawnedUpdate(); - void finishedmovingUpdate(); void moveUpdate(); void deadUpdate(); void attackUpdate(); float getDistance(glm::vec2 cell) { - return sqrt(pow(cell.x - getCurrentCell().x, 2) + - pow(cell.y - getCurrentCell().y, 2)); + return sqrt(pow(cell.x - m_Moving->getCurrentCell().x, 2) + + pow(cell.y - m_Moving->getCurrentCell().y, 2)); } void setSpriteSheet() { @@ -70,9 +68,13 @@ class Avatar : public Moving, m_Health = health; } + void DrawAvatar(); + +public: std::shared_ptr getAttackAndDamager() { return m_AttackAndDamage; } + std::shared_ptr getMoving() { return m_Moving; } protected: std::shared_ptr m_AvatarSpriteSheet = @@ -80,6 +82,8 @@ class Avatar : public Moving, std::shared_ptr m_SpriteSheetAnimation = std::make_shared(); + // moving + std::shared_ptr m_Moving = std::make_shared(); // health std::shared_ptr m_Health = std::make_shared(); // attack and damage diff --git a/include/Avatar/Infantry.hpp b/include/Avatar/Infantry.hpp index bc84e8cb..c1b7bf7f 100644 --- a/include/Avatar/Infantry.hpp +++ b/include/Avatar/Infantry.hpp @@ -9,15 +9,15 @@ class Infantry : public Avatar { public: Infantry() : Avatar() { - setMovementSpeed(4), + getMoving()->setMovementSpeed(4), m_Health = std::make_shared( std::make_shared(LivingStatus::NOT_BORN_YET), 100, 0.5); } Infantry(HouseType house) - : Avatar(UnitType::INFANTRY,house){ + : Avatar(UnitType::INFANTRY, house) { // setHp(50); - setMovementSpeed(4); + getMoving()->setMovementSpeed(4); } private: diff --git a/include/Avatar/Moving.hpp b/include/Avatar/Moving.hpp index 8db0fd5e..2d731c68 100644 --- a/include/Avatar/Moving.hpp +++ b/include/Avatar/Moving.hpp @@ -20,7 +20,6 @@ class Moving { std::vector m_lineVector; glm::vec2 m_CurrentLocation; - glm::vec2 m_DestinationLocation; MoveDirection m_CurrentDir = MoveDirection::IDLE; @@ -37,7 +36,7 @@ class Moving { }; Moving(){}; - virtual ~Moving(){}; + ~Moving(){}; glm::vec2 getCurrentCell() { return MapUtil::GlobalCoordToCellCoord(getCurrentLocation()); @@ -47,6 +46,25 @@ class Moving { m_CurrentLocation = location; } MoveDirection getCurrentDir() { return m_CurrentDir; } + void setCurrentDir(MoveDirection dir) { m_CurrentDir = dir; } + + void moveUpdate() { + if (ifArrivedAtNextCell()) { + m_PrevCell = getCurrentCell(); + if (!m_MovePath.empty()) { + setCurrentDir(m_MovePath.front()); + m_MovePath.pop_front(); + } else { + finishedmovingUpdate(); + } + } + moveToNextCell(); + } + + void finishedmovingUpdate() { + moveToCellCorner(AvatarStandingCorner::CENTER); + setCurrentDir(MoveDirection::IDLE); + } void moveToNextCell(); void moveToCellCorner(AvatarStandingCorner corner); @@ -59,13 +77,14 @@ class Moving { m_PrevCell = getCurrentCell(); } - void setMovementSpeed(float speed) { m_MovementSpeed = speed; } - - glm::vec2 getDestinationCell() { - return MapUtil::CellCoordToGlobal(m_DestinationLocation); - } - void setDestinationCell(glm::vec2 destination) { - m_DestinationLocation = MapUtil::GlobalCoordToCellCoord(destination); + bool ifMovePathEmpty() { + if (m_MovePath.empty()) { + return true; + } else { + return false; + } } + + void setMovementSpeed(float speed) { m_MovementSpeed = speed; } }; #endif // PRACTICALTOOLSFORSIMPLEDESIGN_MOVING_HPP diff --git a/include/Avatar/Weapon.hpp b/include/Avatar/Weapon.hpp index c4081c4a..5c66f555 100644 --- a/include/Avatar/Weapon.hpp +++ b/include/Avatar/Weapon.hpp @@ -14,12 +14,18 @@ enum class WeaponType { Art_105mm, Art_120mm, Nuke, - Grenade + Grenade, + NONE }; class Weapon { public: - Weapon() {} + Weapon() + : m_FireRange(1), + m_SoftAttack(10), + m_HardAttack(10), + m_FireRateInMs(10), + m_Type(WeaponType::NONE) {} Weapon(float firerate, float firerange, float softattack, float hardattack, WeaponType weapontype) : m_FireRange(firerange), diff --git a/include/Mechanics/AvatarManager.hpp b/include/Mechanics/AvatarManager.hpp index 7b356d93..518a7fbf 100644 --- a/include/Mechanics/AvatarManager.hpp +++ b/include/Mechanics/AvatarManager.hpp @@ -29,9 +29,10 @@ class AvatarManager { std::vector> getAvatarArray() { return m_AvatarArray; } - void forceMove(std::shared_ptr unit,glm::vec2 cell); + void forceMove(std::shared_ptr unit, glm::vec2 cell); + protected: - void giveOrderToAvatar(std::shared_ptr unit); + void giveOrderToMyAvatar(std::shared_ptr unit); void updateTileWhileAvatarMoving(std::shared_ptr unit); diff --git a/include/Mechanics/NemesisManager.hpp b/include/Mechanics/NemesisManager.hpp index e554bcde..a6e3fab9 100644 --- a/include/Mechanics/NemesisManager.hpp +++ b/include/Mechanics/NemesisManager.hpp @@ -33,8 +33,10 @@ class NemesisManager { if (ifAvatarHasNemesis(hunter) == false) { return false; } - if (hunter->getDistance(m_Nemesis[hunter]->getCurrentCell()) <= - hunter->getWeapon()->getFireRange()) // check with in range + if (hunter->getDistance( + m_Nemesis[hunter]->getMoving()->getCurrentCell()) <= + hunter->getWeapon()->getFireRange() * + CELL_SIZE.x) // check with in range { return true; } else { diff --git a/include/Mechanics/UnitManager.hpp b/include/Mechanics/UnitManager.hpp index 2007280a..b945048f 100644 --- a/include/Mechanics/UnitManager.hpp +++ b/include/Mechanics/UnitManager.hpp @@ -91,8 +91,6 @@ class UnitManager : public Player { return; } - - avatar->Start(m_StructureManager->getStructureArray() ->getPlayerBarrackCell()); // avatar @@ -109,11 +107,6 @@ class UnitManager : public Player { void spawn(std::shared_ptr m_Map, UnitType unit, HouseType house, glm::vec2 cellPos) { // 缺檢查敵方擁有建築的位置,並重生在該處 - if (house == HouseType::ENEMY) { - // m_Enemy->addUnitConstructCount(unit, 1); - } else { - // m_Player->setUnitConstructCount(unit, 1); - } switch (unit) { case UnitType::BARRACKS: { auto structure = std::make_shared(house); diff --git a/src/Avatar/Avatar.cpp b/src/Avatar/Avatar.cpp index 30ea5564..ab73d129 100644 --- a/src/Avatar/Avatar.cpp +++ b/src/Avatar/Avatar.cpp @@ -9,7 +9,12 @@ void Avatar::whenSelected() { } void Avatar::Update() { + + if (getMoving()->ifMovePathEmpty()) { + m_AvatarOrder = AvatarOrderType::NO_ORDER; + } switch (*m_Health->getLivingStatus()) { + DrawAvatar(); case (LivingStatus::DEAD): SetVisible(false); break; @@ -18,12 +23,14 @@ void Avatar::Update() { whenSelected(); if (m_AvatarOrder == AvatarOrderType::OPEN_FIRE) { - + // open fire } else if (m_AvatarOrder == AvatarOrderType::MOVE) { - moveUpdate(); + m_Moving->moveUpdate(); } else if (m_AvatarOrder == AvatarOrderType::NO_ORDER) { - noOrderUpdate(); + noorderUpdate(); } else if (m_AvatarOrder == AvatarOrderType::TAKEN_DAMAGE) { + + // takendamage } else if (m_AvatarOrder == AvatarOrderType::SPAWNED) { spawnedUpdate(); } @@ -32,39 +39,15 @@ void Avatar::Update() { } } -void Avatar::noOrderUpdate() { - m_CurrentDir = MoveDirection::IDLE; +void Avatar::noorderUpdate() { + getMoving()->setCurrentDir(MoveDirection::IDLE); SetVisible(true); - m_Transform.translation = getCurrentLocation(); - - Draw(); + m_Transform.translation = getMoving()->getCurrentLocation(); } void Avatar::spawnedUpdate() { SetVisible(true); - m_Transform.translation = getCurrentLocation(); - - Draw(); -} -void Avatar::moveUpdate() { - - if (ifArrivedAtNextCell()) { - m_PrevCell = getCurrentCell(); - if (!m_MovePath.empty()) { - m_CurrentDir = m_MovePath.front(); - m_MovePath.pop_front(); - } else { - finishedmovingUpdate(); - m_CurrentDir = MoveDirection::IDLE; - m_AvatarOrder = AvatarOrderType::NO_ORDER; - } - } - moveToNextCell(); - - SetVisible(true); - m_Transform.translation = getCurrentLocation(); - - Draw(); + m_Transform.translation = getMoving()->getCurrentLocation(); } void Avatar::Start(glm::vec2 spawnlocationcell) { // destination = Barrack's @@ -73,9 +56,10 @@ void Avatar::Start(glm::vec2 spawnlocationcell) { // destination = Barrack's this->SetDrawable(customizeImage()); // setSpriteSheet(); SetVisible(true); - setMovementSpeed(4); + getMoving()->setMovementSpeed(4); m_AvatarOrder = AvatarOrderType::SPAWNED; - m_CurrentLocation = MapUtil::CellCoordToGlobal(spawnlocationcell); + getMoving()->getCurrentLocation() = + MapUtil::CellCoordToGlobal(spawnlocationcell); m_Transform.scale = {1, 1}; getHealth()->setLivingStatus( std::make_shared(LivingStatus::ALIVE)); @@ -124,9 +108,17 @@ void Avatar::DEBUG_printCurrentMoveDirection(MoveDirection Dir) { } } -void Avatar::finishedmovingUpdate() { - Moving::moveToCellCorner(AvatarStandingCorner::CENTER); - m_Transform.translation = getCurrentLocation(); - +void Avatar::DrawAvatar() { + m_Transform.translation = getMoving()->getCurrentLocation(); + if (m_AvatarOrder == AvatarOrderType::OPEN_FIRE) { + this->SetDrawable(std::make_shared( + "../assets/sprites/mech_open_fire.png")); + } else if (m_AvatarOrder == AvatarOrderType::TAKEN_DAMAGE) { + this->SetDrawable(std::make_shared( + "../assets/sprites/mech_taken_damage.png")); + } else { + this->SetDrawable( + std::make_shared("../assets/sprites/mech_single.png")); + } Draw(); } diff --git a/src/Avatar/Hunter.cpp b/src/Avatar/Hunter.cpp index bdc4af69..338bc324 100644 --- a/src/Avatar/Hunter.cpp +++ b/src/Avatar/Hunter.cpp @@ -5,17 +5,16 @@ #include "Avatar/Hunter.hpp" void Hunter::customizeUpdate() { - glm::vec2 targetCell = m_target->getCurrentCell(); + glm::vec2 targetCell = m_target->getMoving()->getCurrentCell(); if (getDistance(targetCell) > ATTACK_RANGE - 1 && - lastTargetCell != m_target->getCurrentCell()) { + lastTargetCell != m_target->getMoving()->getCurrentCell()) { // glm::vec2 nextCell = // getNextCellByCurrent(getDirByRelativeCells(getCurrentCell(),targetCell),getCurrentCell()); - setDestinationCell(m_target->getCurrentCell()); - lastTargetCell = m_target->getCurrentCell(); + + lastTargetCell = m_target->getMoving()->getCurrentCell(); // setNewDestination(nextCell); } else if (getDistance(targetCell) < ATTACK_RANGE - 1) { - setDestinationCell(getCurrentCell()); - lastTargetCell = getCurrentCell(); + lastTargetCell = getMoving()->getCurrentCell(); attack(m_target); } } diff --git a/src/Avatar/Runner.cpp b/src/Avatar/Runner.cpp index 3fd11c99..b2edb930 100644 --- a/src/Avatar/Runner.cpp +++ b/src/Avatar/Runner.cpp @@ -6,30 +6,30 @@ void Runner::setBeingChase(std::shared_ptr hunter) { b_beingChase = true; m_hunter = hunter; - lastTargetCell = getCurrentCell(); + lastTargetCell = m_Moving->getCurrentCell(); } void Runner::customizeUpdate() { if (b_beingChase && *m_hunter->getHealth()->getLivingStatus() == LivingStatus::ALIVE) { - glm::vec2 hunterCell = m_hunter->getCurrentCell(); + glm::vec2 hunterCell = m_hunter->getMoving()->getCurrentCell(); if (getDistance(hunterCell) <= ATTACK_RANGE - 1 && - lastTargetCell == getCurrentCell()) { + lastTargetCell == getMoving()->getCurrentCell()) { edgeCount = 0; - MoveDirection Dir = oppositeDir(PathUtility::getDirByRelativeCells( - getCurrentCell(), hunterCell), - runMode::LIDL_RANDOM); + MoveDirection Dir = + oppositeDir(PathUtility::getDirByRelativeCells( + getMoving()->getCurrentCell(), hunterCell), + runMode::LIDL_RANDOM); DEBUG_printCurrentMoveDirection(Dir); - glm::vec2 nextCell = - getNextCellByCurrentPlus3(Dir, getCurrentCell(), 3, 1); + glm::vec2 nextCell = getNextCellByCurrentPlus3( + Dir, getMoving()->getCurrentCell(), 3, 1); while (nextCell.x < 0 || nextCell.y < 0) { edgeCount += rand() % 2 + 1; Dir = findNewDir(Dir, edgeCount); DEBUG_printCurrentMoveDirection(Dir); - nextCell = - getNextCellByCurrentPlus3(Dir, getCurrentCell(), 1, 3); + nextCell = getNextCellByCurrentPlus3( + Dir, getMoving()->getCurrentCell(), 1, 3); } lastTargetCell = nextCell; - setDestinationCell(nextCell); } } else { b_beingChase = false; diff --git a/src/Mechanics/AvatarManager.cpp b/src/Mechanics/AvatarManager.cpp index a1cf3cdf..54cade30 100644 --- a/src/Mechanics/AvatarManager.cpp +++ b/src/Mechanics/AvatarManager.cpp @@ -5,54 +5,67 @@ #include "Mechanics/AvatarManager.hpp" void AvatarManager::Update() { + m_NemesisManager->Update(); for (auto unit : m_AvatarArray) { unit->Update(); if (unit->getAvatarOrder() == AvatarOrderType::MOVE) { updateTileWhileAvatarMoving(unit); } if (unit->getSelected()) { - giveOrderToAvatar(unit); + giveOrderToMyAvatar(unit); } } } -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); +void AvatarManager::giveOrderToMyAvatar(std::shared_ptr unit) { + if (unit->getID().getHouseType() == HouseType::MY) { + if (Util::Input::IsKeyDown(Util::Keycode::MOUSE_RB)) { + auto dest = Util::Input::GetCursorPosition(); + auto queue = + m_Navigator->findPath(unit->getMoving()->getCurrentCell(), + MapUtil::GlobalCoordToCellCoord( + MapUtil::ScreenToGlobalCoord(dest))); + // unit + unit->getMoving()->setMovePath(queue); - 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(MapUtil::GlobalCoordToCellCoord( + MapUtil::ScreenToGlobalCoord(dest))) + ->ifEnemyAtTile()) { + unit->setAvatarOrder(AvatarOrderType::MOVE); + m_NemesisManager->addNemesis( + unit, + m_Map + ->getTileByCellPosition(MapUtil::GlobalCoordToCellCoord( + MapUtil::ScreenToGlobalCoord(dest))) + ->getAvatars()[0]); + } else { + unit->setAvatarOrder(AvatarOrderType::MOVE); + } } } } -void AvatarManager::forceMove(std::shared_ptr unit,glm::vec2 cell){ - unit->setAvatarOrder(AvatarOrderType::MOVE); - auto queue = m_Navigator->findPath(unit->getCurrentCell(), cell); - unit->setMovePath(queue); - } +void AvatarManager::forceMove(std::shared_ptr unit, glm::vec2 cell) { + unit->setAvatarOrder(AvatarOrderType::MOVE); + auto queue = + m_Navigator->findPath(unit->getMoving()->getCurrentCell(), cell); + unit->getMoving()->setMovePath(queue); +} void AvatarManager::updateTileWhileAvatarMoving( std::shared_ptr avatar) { - if (avatar->ifArrivedAtNextCell()) { + if (avatar->getMoving()->ifArrivedAtNextCell()) { m_Map->removeAvatarsByCellPosition(avatar, unitArrayAndLocation[avatar]); - m_Map->setAvatarByCellPosition(avatar, avatar->getCurrentCell()); - unitArrayAndLocation[avatar] = avatar->getCurrentCell(); + m_Map->setAvatarByCellPosition(avatar, + avatar->getMoving()->getCurrentCell()); + unitArrayAndLocation[avatar] = avatar->getMoving()->getCurrentCell(); } } void AvatarManager::AppendAvatar(std::shared_ptr newAvatar) { m_AvatarArray.push_back(newAvatar); - unitArrayAndLocation[newAvatar] = newAvatar->getCurrentCell(); - m_Map->setAvatarByCellPosition(newAvatar, newAvatar->getCurrentCell()); + unitArrayAndLocation[newAvatar] = newAvatar->getMoving()->getCurrentCell(); + m_Map->setAvatarByCellPosition(newAvatar, + newAvatar->getMoving()->getCurrentCell()); } diff --git a/src/Scene/TutorialScene.cpp b/src/Scene/TutorialScene.cpp index 1f7ebe72..a8b55df6 100644 --- a/src/Scene/TutorialScene.cpp +++ b/src/Scene/TutorialScene.cpp @@ -26,6 +26,9 @@ void TutorialScene::Start() { */ m_GameObjectManager->spawn(m_Map, UnitType::INFANTRY, HouseType::MY, {5, 5}); + // combat test + m_EnemyObjectManager->spawn(m_Map, UnitType::INFANTRY, HouseType::ENEMY, + {6, 6}); stageStart(); } @@ -89,7 +92,7 @@ void TutorialScene::stageUpdate() { for (auto i : m_GameObjectManager->getAvatarManager()->getAvatarArray()) { if (i->getHouseType() == HouseType::MY) { - if (m_cellProp->isOverlaps(i->getCurrentCell()) || + if (m_cellProp->isOverlaps(i->getMoving()->getCurrentCell()) || Util::Input::IsKeyPressed(Util::Keycode::DEBUG_KEY)) { // change next stage's text&prop here m_Text->SetDrawable(std::make_unique( @@ -139,7 +142,7 @@ void TutorialScene::stageUpdate() { for (auto i : m_GameObjectManager->getAvatarManager()->getAvatarArray()) { if (i->getHouseType() == HouseType::MY) { - if (m_cellProp->isOverlaps(i->getCurrentCell())) { + if (m_cellProp->isOverlaps(i->getMoving()->getCurrentCell())) { avatarCount++; } } diff --git a/src/UI/UI.cpp b/src/UI/UI.cpp index beb9538d..59654d8f 100644 --- a/src/UI/UI.cpp +++ b/src/UI/UI.cpp @@ -442,7 +442,7 @@ std::shared_ptr UIClass::getUnitFromUI() { Avatar->Start({m_barrackCell.x + 1, m_barrackCell.y - 1}); - Avatar->setDestinationCell(m_barrackTargetCell); + // Avatar->setDestinationCell(m_barrackTargetCell); } printf("(UI)return to GOM success\n"); return Avatar;