Skip to content

Commit

Permalink
failed to fix gameobject
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed May 3, 2024
1 parent e1939be commit 8faba1f
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 87 deletions.
34 changes: 23 additions & 11 deletions include/Avatar/AttackAndDamageUnit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand All @@ -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<AttackAndDamageUnit> target) {
void attackUpdate(std::shared_ptr<AttackAndDamageUnit> 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;
Expand Down
10 changes: 6 additions & 4 deletions include/Avatar/Avatar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
#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"
#include "Map/MapUtility.hpp"
#include "Mechanics/GameObjectID.hpp"
#include "Selectable.hpp"

class Avatar : public PathfindingUnit,
class Avatar : public Moving,
public AttackAndDamageUnit,
public Util::GameObject,
public Selectable,
Expand All @@ -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) +
Expand Down Expand Up @@ -58,7 +60,7 @@ class Avatar : public PathfindingUnit,
virtual void Update() override;

protected:
std::shared_ptr<Avatar> m_Nemesis;
// std::shared_ptr<Avatar> m_Nemesis = std::make_shared<Avatar>();
std::shared_ptr<Util::Image> m_Image;
std::shared_ptr<SpriteSheet> m_AvatarSpriteSheet =
std::make_shared<SpriteSheet>();
Expand Down
12 changes: 6 additions & 6 deletions include/Avatar/PathfindingUnit.hpp → include/Avatar/Moving.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -14,7 +14,7 @@

#define SPEED 1

class PathfindingUnit {
class Moving {
protected:
std::deque<MoveDirection> m_MovePath;

Expand All @@ -39,8 +39,8 @@ class PathfindingUnit {
LOWER_RIGHT
};

PathfindingUnit(){};
virtual ~PathfindingUnit(){};
Moving(){};
virtual ~Moving(){};

glm::vec2 getCurrentCell() {
return MapUtil::GlobalCoordToCellCoord(getCurrentLocation());
Expand Down Expand Up @@ -70,4 +70,4 @@ class PathfindingUnit {
m_DestinationCell = destination;
}
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_PATHFINDINGUNIT_HPP
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_MOVING_HPP
13 changes: 10 additions & 3 deletions include/Avatar/Weapon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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;
Expand Down
71 changes: 34 additions & 37 deletions include/Mechanics/GameObjectID.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,62 @@
#define PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTID_HPP
#include "House.hpp"
#include <string>
#include <unordered_map>
#include <vector>

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<int, unsigned int> 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; }

Expand All @@ -87,7 +83,8 @@ class GameObjectID : public House {

private:
UnitType m_UnitType;
unsigned int m_Number;
int m_Number = 0;
static std::vector<int> m_OccupiedID;
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_GAMEOBJECTID_HPP
2 changes: 1 addition & 1 deletion include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
7 changes: 6 additions & 1 deletion src/Avatar/Avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {}
8 changes: 4 additions & 4 deletions src/Avatar/PathFindingUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 3 additions & 20 deletions src/Mechanics/GameObjectID.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
//
// Created by 盧威任 on 3/13/24.
// Created by 盧威任 on 5/3/24.
//
#include "Mechanics/GameObjectID.hpp"

std::unordered_map<int, unsigned int> 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<int> 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};

0 comments on commit 8faba1f

Please sign in to comment.