From 8faba1f15b3303b4df842df4dfc1686b384e5cfa Mon Sep 17 00:00:00 2001 From: jonylu7 Date: Fri, 3 May 2024 09:48:30 +0800 Subject: [PATCH] failed to fix gameobject --- include/Avatar/AttackAndDamageUnit.hpp | 34 ++++++--- include/Avatar/Avatar.hpp | 10 +-- .../{PathfindingUnit.hpp => Moving.hpp} | 12 ++-- include/Avatar/Weapon.hpp | 13 +++- include/Mechanics/GameObjectID.hpp | 71 +++++++++---------- include/Structure/Structure.hpp | 2 +- src/Avatar/Avatar.cpp | 7 +- src/Avatar/PathFindingUnit.cpp | 8 +-- src/Mechanics/GameObjectID.cpp | 23 +----- 9 files changed, 93 insertions(+), 87 deletions(-) rename include/Avatar/{PathfindingUnit.hpp => Moving.hpp} (86%) diff --git a/include/Avatar/AttackAndDamageUnit.hpp b/include/Avatar/AttackAndDamageUnit.hpp index 60b54ba3..7a3a2a7c 100644 --- a/include/Avatar/AttackAndDamageUnit.hpp +++ b/include/Avatar/AttackAndDamageUnit.hpp @@ -4,18 +4,19 @@ #ifndef PRACTICALTOOLSFORSIMPLEDESIGN_ATTACKANDDAMAGEUNIT_HPP #define PRACTICALTOOLSFORSIMPLEDESIGN_ATTACKANDDAMAGEUNIT_HPP +#include "Util/Time.hpp" #include "Weapon.hpp" -class AttackAndDamageUnit { -public: - enum class LivingStatus { - DEAD, - ALIVE, - NOT_BORN_YET, +enum class LivingStatus { + DEAD, + ALIVE, + NOT_BORN_YET, - REGRET_ABOUT_LIVING, // just a joke - }; + REGRET_ABOUT_LIVING, // just a joke, or not +}; +class AttackAndDamageUnit { +public: AttackAndDamageUnit() {} virtual ~AttackAndDamageUnit() {} @@ -32,20 +33,31 @@ class AttackAndDamageUnit { m_HP -= (100 - m_ArmorRate) * (1 / 100) * softattack + m_ArmorRate * (1 / 100) * hardattack; + if (m_HP <= 0) { + m_LivingStatus = LivingStatus::DEAD; + } } - void forceAttackUnit(std::shared_ptr target) { + void attackUpdate(std::shared_ptr target) { // cd time - target->takeDamage(m_Weapon.getSoftAttack(), m_Weapon.getHardAttack()); - return; + m_DeltaTime += m_Time.GetDeltaTime(); + if (m_Weapon.getIntervalInS() <= m_DeltaTime) { + m_DeltaTime = 0; + target->takeDamage(m_Weapon.getSoftAttack(), + m_Weapon.getHardAttack()); + } } LivingStatus getLivingStatus() const { return m_LivingStatus; }; void setLivingStatus(LivingStatus status) { m_LivingStatus = status; }; +private: + float m_DeltaTime = 0; + protected: LivingStatus m_LivingStatus = LivingStatus::NOT_BORN_YET; + Util::Time m_Time; int m_HP; Weapon m_Weapon; float m_ArmorRate; diff --git a/include/Avatar/Avatar.hpp b/include/Avatar/Avatar.hpp index be3b64a0..d3d5ad6b 100644 --- a/include/Avatar/Avatar.hpp +++ b/include/Avatar/Avatar.hpp @@ -6,8 +6,8 @@ #define PRACTICALTOOLSFORSIMPLEDESIGN_DUMMY_HPP #include "Avatar/AttackAndDamageUnit.hpp" #include "Avatar/AvatarOrder.hpp" +#include "Avatar/Moving.hpp" #include "Avatar/PathUtility.hpp" -#include "Avatar/PathfindingUnit.hpp" #include "Display/Image.hpp" #include "Display/SpriteSheet.hpp" #include "Display/SpriteSheetAnimation.hpp" @@ -15,7 +15,7 @@ #include "Mechanics/GameObjectID.hpp" #include "Selectable.hpp" -class Avatar : public PathfindingUnit, +class Avatar : public Moving, public AttackAndDamageUnit, public Util::GameObject, public Selectable, @@ -27,7 +27,9 @@ class Avatar : public PathfindingUnit, ~Avatar() override{}; virtual void Start(glm::vec2 destination); - virtual void aliveUpdate(); + void aliveUpdate(); + void deadUpdate(); + void attackUpdate(); float getDistance(glm::vec2 cell) { return sqrt(pow(cell.x - getCurrentCell().x, 2) + @@ -58,7 +60,7 @@ class Avatar : public PathfindingUnit, virtual void Update() override; protected: - std::shared_ptr m_Nemesis; + // std::shared_ptr m_Nemesis = std::make_shared(); std::shared_ptr m_Image; std::shared_ptr m_AvatarSpriteSheet = std::make_shared(); diff --git a/include/Avatar/PathfindingUnit.hpp b/include/Avatar/Moving.hpp similarity index 86% rename from include/Avatar/PathfindingUnit.hpp rename to include/Avatar/Moving.hpp index 9840d0a9..094a1a79 100644 --- a/include/Avatar/PathfindingUnit.hpp +++ b/include/Avatar/Moving.hpp @@ -2,8 +2,8 @@ // Created by nudle on 2024/3/15. // -#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_PATHFINDINGUNIT_HPP -#define PRACTICALTOOLSFORSIMPLEDESIGN_PATHFINDINGUNIT_HPP +#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_MOVING_HPP +#define PRACTICALTOOLSFORSIMPLEDESIGN_MOVING_HPP #include "Avatar/PathUtility.hpp" #include "Display/Grid.hpp" #include "Display/Line.hpp" @@ -14,7 +14,7 @@ #define SPEED 1 -class PathfindingUnit { +class Moving { protected: std::deque m_MovePath; @@ -39,8 +39,8 @@ class PathfindingUnit { LOWER_RIGHT }; - PathfindingUnit(){}; - virtual ~PathfindingUnit(){}; + Moving(){}; + virtual ~Moving(){}; glm::vec2 getCurrentCell() { return MapUtil::GlobalCoordToCellCoord(getCurrentLocation()); @@ -70,4 +70,4 @@ class PathfindingUnit { m_DestinationCell = destination; } }; -#endif // PRACTICALTOOLSFORSIMPLEDESIGN_PATHFINDINGUNIT_HPP +#endif // PRACTICALTOOLSFORSIMPLEDESIGN_MOVING_HPP diff --git a/include/Avatar/Weapon.hpp b/include/Avatar/Weapon.hpp index 4a669c7f..dcf7e317 100644 --- a/include/Avatar/Weapon.hpp +++ b/include/Avatar/Weapon.hpp @@ -24,9 +24,16 @@ class Weapon { WeaponType weapon) {} ~Weapon() {} // Getter and setter methods for m_fireRate - static float getFireRate() { return m_FireRate; } + static float getFireRateInM() { return m_FireRateInMs; } - static void setFireRate(float fireRate) { m_FireRate = fireRate; } + static void setFireRateInM(float fireRateInM) { + m_FireRateInMs = fireRateInM; + } + + static float getIntervalInS() { + auto r = m_FireRateInMs / 60; + return (1 / r); + } // Getter and setter methods for m_fireRange static float getFireRange() { return m_FireRange; } @@ -49,7 +56,7 @@ class Weapon { void setType(WeaponType type) { m_Type = type; } private: - static float m_FireRate; + static float m_FireRateInMs; static float m_FireRange; static float m_SoftAttack; static float m_HardAttack; diff --git a/include/Mechanics/GameObjectID.hpp b/include/Mechanics/GameObjectID.hpp index 7e4f7d3f..7f93a2b2 100644 --- a/include/Mechanics/GameObjectID.hpp +++ b/include/Mechanics/GameObjectID.hpp @@ -6,66 +6,62 @@ #define PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTID_HPP #include "House.hpp" #include -#include +#include enum class UnitType { // buildings - POWER_PLANT, - BARRACKS, - ORE_REF, - WAR_FACT, - ADV_POWER_PLANT, + POWER_PLANT, // 0 + BARRACKS, // 1 + ORE_REF, // 2 + WAR_FACT, // 3 + ADV_POWER_PLANT, // 4 // defense - SANDBAGS, - PILLBOX, - TURRET, + SANDBAGS, // 5 + PILLBOX, // 6 + TURRET, // 7 // Troopers - INFANTRY, + INFANTRY, // 8 // Vehicles - TRUCK, + TRUCK, // 9 // null - null, + null, // 10 // tile - TILE_BEACH, - TILE_BRIDGE, - TILE_CLEAR, - TILE_RIVER, - TILE_ROAD, - TILE_ROUGH, - TILE_WATER, - TILE_ROCK, - TILE_TREE, + TILE_BEACH, // 11 + TILE_BRIDGE, // 12 + TILE_CLEAR, // 13 + TILE_RIVER, // 14 + TILE_ROAD, // 15 + TILE_ROUGH, // 16 + TILE_WATER, // 17 + TILE_ROCK, // 18 + TILE_TREE, // 19 // overlays - OVERLAY_GEMS, - OVERLAY_ORE, + OVERLAY_GEMS, // 20 + OVERLAY_ORE, // 21 // NONE - NONE -}; - -class OccupiedID { -private: - static std::unordered_map m_OccupiedID; - -public: - static unsigned int getNewestID(UnitType type); + NONE // 22 }; class GameObjectID : public House { public: GameObjectID() - : m_UnitType(UnitType::null), - m_Number(0) {} + : m_UnitType(UnitType::NONE) { + m_Number = 0; + } GameObjectID(UnitType type, HouseType house) : m_UnitType(type), - m_Number(OccupiedID::getNewestID(type)), - House(house) {} + House(house) { + m_Number = 0; + } ~GameObjectID() {} + int getNewestID(UnitType type) { return m_OccupiedID[int(type)]++; } + int getNumber() const { return m_Number; } UnitType getUnitType() const { return m_UnitType; } @@ -87,7 +83,8 @@ class GameObjectID : public House { private: UnitType m_UnitType; - unsigned int m_Number; + int m_Number = 0; + static std::vector m_OccupiedID; }; #endif // PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTID_HPP diff --git a/include/Structure/Structure.hpp b/include/Structure/Structure.hpp index f56c8fd5..1452bd34 100644 --- a/include/Structure/Structure.hpp +++ b/include/Structure/Structure.hpp @@ -32,7 +32,7 @@ class Structure : public Util::GameObject, m_BuildingTime(0.F), m_BuildingCost(0.F), Selectable(), - m_ID(GameObjectID(UnitType::null, HouseType::NONE)) { + m_ID(GameObjectID(UnitType::NONE, HouseType::NONE)) { m_LivingStatus = LivingStatus::NOT_BORN_YET; }; diff --git a/src/Avatar/Avatar.cpp b/src/Avatar/Avatar.cpp index 59d7d59e..8f42f404 100644 --- a/src/Avatar/Avatar.cpp +++ b/src/Avatar/Avatar.cpp @@ -65,10 +65,13 @@ void Avatar::Update() { whenSelected(); aliveUpdate(); - if (m_AvatarOrder == AvatarOrderType::MOVE_ATTACK) { + if (m_AvatarOrder == AvatarOrderType::ATTACK) { // if nemesis is within weapon range // change move attack to // if nemesis is dead + } else if (m_AvatarOrder == AvatarOrderType::MOVE) { + + } else if (m_AvatarOrder == AvatarOrderType::NO_ORDER) { } break; @@ -116,3 +119,5 @@ void Avatar::Start(glm::vec2 destination) { // destination = Barrack's m_AvatarOrder = AvatarOrderType::MOVE; m_Transform.scale = {1, 1}; } + +void Avatar::deadUpdate() {} diff --git a/src/Avatar/PathFindingUnit.cpp b/src/Avatar/PathFindingUnit.cpp index 5fc793fc..64671124 100644 --- a/src/Avatar/PathFindingUnit.cpp +++ b/src/Avatar/PathFindingUnit.cpp @@ -5,9 +5,9 @@ // // Created by 盧威任 on 4/1/24. // -#include "Avatar/PathfindingUnit.hpp" +#include "Avatar/Moving.hpp" -void PathfindingUnit::moveToNextCell() { +void Moving::moveToNextCell() { switch (m_CurrentDir) { case MoveDirection::RIGHT: { m_CurrentLocation = {m_CurrentLocation.x + m_MovementSpeed, @@ -60,13 +60,13 @@ void PathfindingUnit::moveToNextCell() { } } -bool PathfindingUnit::ifArrivedAtNextCell() { +bool Moving::ifArrivedAtNextCell() { // change this to check // previous cell // getCurrentCell() != previousCell() return false; } -void PathfindingUnit::moveToCellCorner(AvatarStandingCorner corner) { +void Moving::moveToCellCorner(AvatarStandingCorner corner) { float quaterHeight = CELL_SIZE.y / 4; float quaterWidth = CELL_SIZE.x / 4; switch (corner) { diff --git a/src/Mechanics/GameObjectID.cpp b/src/Mechanics/GameObjectID.cpp index 973c781a..567bd840 100644 --- a/src/Mechanics/GameObjectID.cpp +++ b/src/Mechanics/GameObjectID.cpp @@ -1,24 +1,7 @@ // -// Created by 盧威任 on 3/13/24. +// Created by 盧威任 on 5/3/24. // #include "Mechanics/GameObjectID.hpp" -std::unordered_map OccupiedID::m_OccupiedID = { - {int(UnitType::NONE), 0}, {int(UnitType::POWER_PLANT), 0}, - {int(UnitType::BARRACKS), 0}, {int(UnitType::ORE_REF), 0}, - {int(UnitType::WAR_FACT), 0}, {int(UnitType::ADV_POWER_PLANT), 0}, - {int(UnitType::SANDBAGS), 0}, {int(UnitType::PILLBOX), 0}, - {int(UnitType::TURRET), 0}, {int(UnitType::INFANTRY), 0}, - {int(UnitType::TRUCK), 0}, {int(UnitType::null), 0}, - {int(UnitType::TILE_BEACH), 0}, {int(UnitType::TILE_BRIDGE), 0}, - {int(UnitType::TILE_CLEAR), 0}, {int(UnitType::TILE_RIVER), 0}, - {int(UnitType::TILE_ROAD), 0}, {int(UnitType::TILE_ROUGH), 0}, - {int(UnitType::TILE_WATER), 0}, {int(UnitType::TILE_ROCK), 0}, - {int(UnitType::TILE_TREE), 0}, {int(UnitType::OVERLAY_GEMS), 0}, - {int(UnitType::OVERLAY_ORE), 0}}; - -unsigned int OccupiedID::getNewestID(UnitType type) { - return 0; - // 先copy進去 - return OccupiedID::m_OccupiedID[int(type)]++; -} +std::vector GameObjectID::m_OccupiedID = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};