Skip to content

Commit

Permalink
Health
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed May 5, 2024
1 parent 6798cf6 commit d7e62f9
Show file tree
Hide file tree
Showing 20 changed files with 218 additions and 117 deletions.
45 changes: 45 additions & 0 deletions include/Avatar/AttackAndDamage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Created by 盧威任 on 3/22/24.
//

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_ATTACKANDDAMAGE_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_ATTACKANDDAMAGE_HPP
#include "Unit/IHealthable.hpp"
#include "Util/Time.hpp"
#include "Weapon.hpp"

class AttackAndDamage {
public:
AttackAndDamage() {}
virtual ~AttackAndDamage() {}

void damageTargetWithWeapon(std::shared_ptr<IHealthable> target,
std::shared_ptr<Weapon> weapon) {
auto targethealth = target->getHealth();
targethealth->addHP(-1 * (100 - targethealth->getArmorRate()) *
(1 / 100) * m_Weapon->getSoftAttack() +
targethealth->getArmorRate() * (1 / 100) *
m_Weapon->getHardAttack());
}

void openFireToTarget(std::shared_ptr<IHealthable> target) {
// cd time
m_DeltaTime += m_Time.GetDeltaTime();
if (m_Weapon->getIntervalInS() <= m_DeltaTime) {
m_DeltaTime = 0;
damageTargetWithWeapon(target, m_Weapon);
}
}

std::shared_ptr<Weapon> getWeapon() { return m_Weapon; }
void setWeapon(std::shared_ptr<Weapon> weapon) { m_Weapon = weapon; }

private:
float m_DeltaTime = 0;

protected:
Util::Time m_Time;
std::shared_ptr<Weapon> m_Weapon = std::make_shared<Weapon>();
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_ATTACKANDDAMAGE_HPP
68 changes: 0 additions & 68 deletions include/Avatar/AttackAndDamageUnit.hpp

This file was deleted.

19 changes: 15 additions & 4 deletions include/Avatar/Avatar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_DUMMY_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_DUMMY_HPP
#include "Avatar/AttackAndDamageUnit.hpp"
#include "Avatar/AttackAndDamage.hpp"
#include "Avatar/AvatarOrder.hpp"
#include "Avatar/Moving.hpp"
#include "Avatar/PathUtility.hpp"
Expand All @@ -16,11 +16,11 @@
#include "Selectable.hpp"

class Avatar : public Moving,
public AttackAndDamageUnit,
public AttackAndDamage,
public Util::GameObject,
public Selectable,
public GameObjectID,
public AvatarOrder {
public AvatarOrder,
public IHealthable {

public:
Avatar(){};
Expand Down Expand Up @@ -59,13 +59,24 @@ class Avatar : public Moving,

virtual void Update() override;

std::shared_ptr<Health> getHealth() override { return m_Health; }
void setHealth(std::shared_ptr<Health> health) override {
m_Health = health;
}

protected:
// 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>();
std::shared_ptr<Util::SpriteSheetAnimation> m_SpriteSheetAnimation =
std::make_shared<Util::SpriteSheetAnimation>();

// health
std::shared_ptr<Health> m_Health = std::make_shared<Health>();
// attack and damage
std::shared_ptr<AttackAndDamage> m_AttackAndDamage =
std::make_shared<AttackAndDamage>();

private:
bool b_justStarted = true;
Expand Down
6 changes: 4 additions & 2 deletions include/Avatar/Infantry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class Infantry : public Avatar {
public:
Infantry()
: Avatar() {
// setHp(50);
setMovementSpeed(4);
setMovementSpeed(4),
m_Health = std::make_shared<Health>(
std::make_shared<LivingStatus>(LivingStatus::NOT_BORN_YET), 100,
0.5);
}

private:
Expand Down
1 change: 0 additions & 1 deletion include/Avatar/Moving.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Moving {
std::deque<MoveDirection> m_MovePath;

std::vector<Line> m_lineVector;
float defaultZIndex = 15;
glm::vec2 m_CurrentLocation;
glm::vec2 m_DestinationCell;

Expand Down
6 changes: 4 additions & 2 deletions include/Mechanics/NemesisManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ class NemesisManager {
pair.first->openFireToTarget(pair.second);
}

if (pair.second->getLivingStatus() == LivingStatus::DEAD) {
if (*pair.second->getHealth()->getLivingStatus() ==
LivingStatus::DEAD) {
removeNemesis(pair.first);
pair.first->setAvatarOrder(AvatarOrderType::NO_ORDER);
pair.second->setAvatarOrder(AvatarOrderType::NO_ORDER);
}

if (pair.first->getLivingStatus() == LivingStatus::DEAD) {
if (*pair.first->getHealth()->getLivingStatus() ==
LivingStatus::DEAD) {
removeNemesis(pair.first);
pair.first->setAvatarOrder(AvatarOrderType::NO_ORDER);
pair.second->setAvatarOrder(AvatarOrderType::NO_ORDER);
Expand Down
11 changes: 6 additions & 5 deletions include/Structure/AdvencePowerPlants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#include "Structure.hpp"
class ADVPowerPlants : public Structure {
public:
ADVPowerPlants(float electricPower = 200.F,
float buildingTime = 25.F * CHEAT,
float buildingCost = 500.F, float buildingHp = 700.F,
HouseType house = HouseType::NONE)
ADVPowerPlants(
float electricPower = 200.F, float buildingTime = 25.F * CHEAT,
float buildingCost = 500.F, float buildingHp = 700.F,
HouseType house = HouseType::NONE,
std::shared_ptr<Health> health = std::make_shared<Health>(100, 0.7))
: Structure(electricPower, buildingTime, buildingCost, buildingHp,
GameObjectID(UnitType::ADV_POWER_PLANT, house)){
GameObjectID(UnitType::ADV_POWER_PLANT, house), health){
// SetDrawable(
// std::make_unique<Util::Image>("../assets/sprites/PowerPlants.png"));
};
Expand Down
6 changes: 4 additions & 2 deletions include/Structure/PowerPlants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ class PowerPlants : public Structure {
public:
PowerPlants(float electricPower = 100.F, float buildingTime = 15.F * CHEAT,
float buildingCost = 300.F, float buildingHp = 400.F,
HouseType house = HouseType::NONE)
HouseType house = HouseType::NONE,
std::shared_ptr<Health> health = std::make_shared<Health>(100,
0.5))
: Structure(electricPower, buildingTime, buildingCost, buildingHp,
GameObjectID(UnitType::POWER_PLANT, house)){
GameObjectID(UnitType::POWER_PLANT, house), health){

};
void SetSpriteSheet() override {
Expand Down
35 changes: 23 additions & 12 deletions include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_HPP
#include "Avatar/AttackAndDamage.hpp"
#include "Display/Image.hpp"
#include "Display/SpriteSheet.hpp"
#include "Display/SpriteSheetAnimation.hpp"
#include "HighLight.h"
#include "Mechanics//GameObjectID.hpp"
#include "Mechanics/GameObjectID.hpp"
#include "Selectable.hpp"

#include "Avatar/AttackAndDamageUnit.hpp"
#include "Display/Image.hpp"
#include "Display/SpriteSheetAnimation.hpp"
#include "Structure/StructureOrder.hpp"

#include "Unit/Health.hpp"
#include "Unit/IHealthable.hpp"

#include "Util/GameObject.hpp"
#include "Util/Input.hpp"
#include "Util/TransformUtils.hpp"
Expand All @@ -24,25 +27,24 @@
class Structure : public Util::GameObject,
public Selectable,
public StructureOrder,
public AttackAndDamageUnit {
public IHealthable {

public:
Structure()
: m_ElectricPower(0.F),
m_BuildingTime(0.F),
m_BuildingCost(0.F),
Selectable(),
m_ID(GameObjectID(UnitType::NONE, HouseType::NONE)) {
m_LivingStatus = LivingStatus::NOT_BORN_YET;
};
m_ID(GameObjectID(UnitType::NONE, HouseType::NONE)) {}

Structure(float electricPower, float buildingTime, float buildingCost,
float buildingHp, GameObjectID id)
float buildingHp, GameObjectID id, std::shared_ptr<Health> health)
: m_ElectricPower(electricPower),
m_BuildingTime(buildingTime),
m_BuildingCost(buildingCost),
Selectable(),
m_ID(id) {
m_ID(id),
m_Health(health) {
m_Transform.scale = {1, 1};
// this->SetZIndex(DEFAULT_ZINDEX);
};
Expand Down Expand Up @@ -92,6 +94,11 @@ class Structure : public Util::GameObject,

GameObjectID getID() { return m_ID; }

std::shared_ptr<Health> getHealth() override { return m_Health; }
void setHealth(std::shared_ptr<Health> health) override {
m_Health = health;
}

protected:
float m_ElectricPower;
float m_BuildingTime;
Expand All @@ -104,11 +111,15 @@ class Structure : public Util::GameObject,
std::shared_ptr<SpriteSheet> m_StructureSpriteSheet =
std::make_shared<SpriteSheet>();
std::shared_ptr<Util::SpriteSheetAnimation> m_SpriteSheetAnimation =
std::make_shared<Util::SpriteSheetAnimation>();
std::make_shared<Util::SpriteSheetAnimation>(m_StructureSpriteSheet,
false, 1);
glm::vec2 m_DrawLocation = {m_ObjectLocation.x + CELL_SIZE.x,
m_ObjectLocation.y + CELL_SIZE.y};
glm::vec2 m_ObjectLocation = {100, 100};
std::vector<glm::vec2> m_RelativeOccupiedArea = {{0, 0}};
std::shared_ptr<Health> m_Health = std::make_shared<Health>();
std::shared_ptr<AttackAndDamage> m_AttackAndDamage =
std::make_shared<AttackAndDamage>();
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTURE_HPP
14 changes: 8 additions & 6 deletions include/Structure/WayPointStructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ class WayPointStructure : public Structure, public IWayPointStructure {
std::vector<Line> m_lineVector;

public:
WayPointStructure(float electricPower = -10.F, float buildingTime = 10.F,
float buildingCost = 20.F, float buildingHp = 90.F,
GameObjectID id = GameObjectID(UnitType::null,
HouseType::NONE))
: Structure(electricPower, buildingTime, buildingCost, buildingHp, id) {
WayPointStructure(
float electricPower = -10.F, float buildingTime = 10.F,
float buildingCost = 20.F, float buildingHp = 90.F,
GameObjectID id = GameObjectID(UnitType::null, HouseType::NONE),
std::shared_ptr<Health> health = std::make_shared<Health>())
: Structure(electricPower, buildingTime, buildingCost, buildingHp, id,
health) {
m_Transform.scale = {2.f, 2.f};
};
virtual ~WayPointStructure(){};
Expand All @@ -39,7 +41,7 @@ class WayPointStructure : public Structure, public IWayPointStructure {

void Update() override {

switch (m_LivingStatus) {
switch (*Structure::getHealth()->getLivingStatus()) {
case LivingStatus::NOT_BORN_YET: {
this->updateInvinsible();
break;
Expand Down
Loading

0 comments on commit d7e62f9

Please sign in to comment.