Skip to content

Commit

Permalink
Merge pull request #72 from jonylu7/combat
Browse files Browse the repository at this point in the history
interupt open fire command
  • Loading branch information
jonylu7 authored May 20, 2024
2 parents 1a082b6 + dcb7423 commit 1ecace3
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 71 deletions.
6 changes: 3 additions & 3 deletions include/Avatar/AttackAndDamage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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

Expand All @@ -13,7 +13,7 @@ class AttackAndDamage {
AttackAndDamage() {}
virtual ~AttackAndDamage() {}

void damageTargetWithWeapon(std::shared_ptr<IHealthable> target,
void damageTargetWithWeapon(std::shared_ptr<Huntable> target,
std::shared_ptr<Weapon> weapon) {
auto targethealth = target->getHealth();
auto damage =
Expand All @@ -22,7 +22,7 @@ class AttackAndDamage {
targethealth->addHP(-1 * damage);
}

void openFireToTarget(std::shared_ptr<IHealthable> target) {
void openFireToTarget(std::shared_ptr<Huntable> target) {
// cd time
m_DeltaTime += m_Time.GetDeltaTime();
if (m_Weapon->getIntervalInS() <= m_DeltaTime) {
Expand Down
20 changes: 15 additions & 5 deletions include/Avatar/Avatar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
#include "Map/MapUtility.hpp"
#include "Mechanics/GameObjectID.hpp"
#include "Selectable.hpp"
#include "Unit/Huntable.hpp"
#include "Util/Image.hpp"

class Avatar : public Util::GameObject, public Selectable, public IHealthable {
class Avatar : public Util::GameObject, public Selectable, public Huntable {

public:
Avatar(){};
Expand Down Expand Up @@ -68,11 +69,20 @@ class Avatar : public Util::GameObject, public Selectable, public IHealthable {
std::shared_ptr<Moving> getMoving() { return m_Moving; }
std::shared_ptr<AvatarOrder> getAvatarOrder() { return m_Order; }

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

void setHealth(std::shared_ptr<Health> health) override {
m_Health = health;
}
public:
// huntable
std::shared_ptr<Health> getHealth() override { return m_Health; }
void setOrderNoOrder() override {
m_Order->setAvatarOrder(AvatarOrderType::NO_ORDER);
};
void setOrderTakenDamage() override {
m_Order->setAvatarOrder(AvatarOrderType::TAKEN_DAMAGE);
};
glm::vec2 getCurrentLocationInCell() override {
return getMoving()->getCurrentCell();
};

protected:
std::shared_ptr<SpriteSheet> m_AvatarSpriteSheet =
Expand Down
9 changes: 7 additions & 2 deletions include/Avatar/Moving.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ class Moving {
bool ifArrivedAtNextCell();

void setMovePath(std::deque<MoveDirection> movepath) {
m_MovePath = movepath;
m_CurrentDir = m_MovePath.front();
if (movepath.empty()) {
m_MovePath = {MoveDirection::IDLE};
m_CurrentDir = MoveDirection::IDLE;
} else {
m_MovePath = movepath;
m_CurrentDir = m_MovePath.front();
}
m_PrevCell = getCurrentCell();
}

Expand Down
2 changes: 1 addition & 1 deletion include/Map/Tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class TileClass {

std::vector<std::shared_ptr<Avatar>> getAvatars() { return m_Avatars; }
std::shared_ptr<Structure> getStructure() { return m_Structure; }
bool ifStrucutreExists() {
bool ifStructureExists() {
if (*m_Structure->getHealth()->getLivingStatus() ==
LivingStatus::NOT_BORN_YET) {
return false;
Expand Down
4 changes: 2 additions & 2 deletions include/Mechanics/BuiltStructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class BuiltStructure {
if (ifCanBuildStructureAtTile(newstruct) == true) {
m_BuiltStructure.push_back(newstruct);
m_Map->builtStructureByCellPosition(newstruct, coords);
newstruct->getStructureOrder()->setStructOrder(
newstruct->getStructureOrder()->setStructureOrder(
StructureOrderType::BUILT);
}
}
} else {
m_BuiltStructure.push_back(newstruct);
m_Map->builtStructureByCellPosition(newstruct, coords);
newstruct->getStructureOrder()->setStructOrder(
newstruct->getStructureOrder()->setStructureOrder(
StructureOrderType::BUILT);
}
}
Expand Down
19 changes: 8 additions & 11 deletions include/Mechanics/NemesisManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NemesisManager {
NemesisManager() {}
~NemesisManager() {}
void addNemesis(std::shared_ptr<Avatar> avatar,
std::shared_ptr<Avatar> nemesis) {
std::shared_ptr<Huntable> nemesis) {
m_Nemesis[avatar] = nemesis;
}

Expand All @@ -34,7 +34,7 @@ class NemesisManager {
return false;
}
if (hunter->getDistance(
m_Nemesis[hunter]->getMoving()->getCurrentCell()) <=
m_Nemesis[hunter]->getCurrentLocationInCell()) <=
hunter->getAttackAndDamage()->getWeapon()->getFireRange() *
CELL_SIZE.x) // check with in range
{
Expand All @@ -52,18 +52,16 @@ class NemesisManager {
if (ifNemesisWithinWeaponRange(hunter)) {
hunter->getAvatarOrder()->setAvatarOrder(
AvatarOrderType::OPEN_FIRE);
prey->getAvatarOrder()->setAvatarOrder(
AvatarOrderType::TAKEN_DAMAGE);
prey->setOrderTakenDamage();
hunter->getAttackAndDamage()->openFireToTarget(prey);
}

if (*pair.second->getHealth()->getLivingStatus() ==
LivingStatus::DEAD) {
if (pair.second->getHealth()->ifDead()) {
removeNemesis(hunter);
hunter->getAvatarOrder()->setAvatarOrder(
AvatarOrderType::NO_ORDER);
prey->getAvatarOrder()->setAvatarOrder(
AvatarOrderType::NO_ORDER);
prey->setOrderNoOrder();

break;
}

Expand All @@ -72,15 +70,14 @@ class NemesisManager {
removeNemesis(hunter);
hunter->getAvatarOrder()->setAvatarOrder(
AvatarOrderType::NO_ORDER);
prey->getAvatarOrder()->setAvatarOrder(
AvatarOrderType::NO_ORDER);
prey->setOrderNoOrder();
break;
}
}
}

private:
std::unordered_map<std::shared_ptr<Avatar>, std::shared_ptr<Avatar>>
std::unordered_map<std::shared_ptr<Avatar>, std::shared_ptr<Huntable>>
m_Nemesis;
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_NEMESISMANAGER_HPP
2 changes: 1 addition & 1 deletion include/Mechanics/StructureManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class StructureManager {
newstruct->Start();
m_StructSelectingConstructionSite = newstruct;

newstruct->getStructureOrder()->setStructOrder(
newstruct->getStructureOrder()->setStructureOrder(
StructureOrderType::SELECTING_SITE);
}

Expand Down
15 changes: 10 additions & 5 deletions include/Mechanics/UnitManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,35 +110,40 @@ class UnitManager : public Player {
switch (unit) {
case UnitType::BARRACKS: {
auto structure = std::make_shared<Barracks>(house);
structure->Start(cellPos);
auto globalPos = MapUtil::CellCoordToGlobal(cellPos);
structure->Start(globalPos);
m_StructureManager->getStructureArray()->buildNewStructure(
structure, true);
break;
}
case UnitType::ORE_REF: {
auto structure = std::make_shared<OreRefinery>(house);
structure->Start(cellPos);
auto globalPos = MapUtil::CellCoordToGlobal(cellPos);
structure->Start(globalPos);
m_StructureManager->getStructureArray()->buildNewStructure(
structure, true);
break;
}
case UnitType::POWER_PLANT: {
auto structure = std::make_shared<PowerPlants>(house);
structure->Start(cellPos);
auto globalPos = MapUtil::CellCoordToGlobal(cellPos);
structure->Start(globalPos);
m_StructureManager->getStructureArray()->buildNewStructure(
structure, true);
break;
}
case UnitType::WAR_FACT: {
auto structure = std::make_shared<WarFactory>(house);
structure->Start(cellPos);
auto globalPos = MapUtil::CellCoordToGlobal(cellPos);
structure->Start(globalPos);
m_StructureManager->getStructureArray()->buildNewStructure(
structure, true);
break;
}
case UnitType::ADV_POWER_PLANT: {
auto structure = std::make_shared<ADVPowerPlants>(house);
structure->Start(cellPos);
auto globalPos = MapUtil::CellCoordToGlobal(cellPos);
structure->Start(globalPos);
m_StructureManager->getStructureArray()->buildNewStructure(
structure, true);
break;
Expand Down
28 changes: 17 additions & 11 deletions include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@
#include "Selectable.hpp"
#include "Structure/StructureOrder.hpp"
#include "Unit/Health.hpp"
#include "Unit/IHealthable.hpp"
#include "Util/Image.hpp"

#include "Unit/Huntable.hpp"
#include "Util/GameObject.hpp"
#include "Util/Image.hpp"
#include "Util/Input.hpp"
#include "Util/TransformUtils.hpp"
#include "glm/glm.hpp"
#define DEFAULT_ZINDEX 15
#define CHEAT 0.01
#define INTERVAL 50

class Structure : public Util::GameObject,
public Selectable,
public IHealthable {
class Structure : public Util::GameObject, public Selectable, public Huntable {

public:
Structure()
Expand Down Expand Up @@ -97,16 +94,25 @@ class Structure : public Util::GameObject,

public:
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;
}
void setHealth(std::shared_ptr<Health> health) { m_Health = health; }
std::shared_ptr<StructureOrder> getStructureOrder() { return m_Order; }

std::shared_ptr<AttackAndDamage> getAttackAndDamage() {
return m_AttackAndDamage;
}

public:
// huntable
std::shared_ptr<Health> getHealth() override { return m_Health; }
void setOrderNoOrder() override {
m_Order->setStructureOrder(StructureOrderType::NO_ORDER);
};
void setOrderTakenDamage() override {
m_Order->setStructureOrder(StructureOrderType::TAKEN_DAMAGE);
};
glm::vec2 getCurrentLocationInCell() override {
return getAbsoluteOccupiedArea()[0];
};

protected:
float m_ElectricPower;
float m_BuildingTime;
Expand Down
5 changes: 3 additions & 2 deletions include/Structure/StructureOrder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ enum class StructureOrderType {
CONSTRUCTED,
SELECTING_SITE,
BUILT,
NO_ORDER
NO_ORDER,
TAKEN_DAMAGE
};
class StructureOrder {
public:
Expand All @@ -18,7 +19,7 @@ class StructureOrder {

StructureOrderType getStructureOrderType() { return m_StructOrder; }

void setStructOrder(StructureOrderType structorder) {
void setStructureOrder(StructureOrderType structorder) {
m_StructOrder = structorder;
}

Expand Down
20 changes: 20 additions & 0 deletions include/Unit/Huntable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Created by 盧威任 on 5/20/24.
//

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_UNIT_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_UNIT_HPP
#include "Unit/Health.hpp"
#include "pch.hpp"
class Huntable {
public:
Huntable() {}
virtual ~Huntable() {}

public:
virtual void setOrderNoOrder() = 0;
virtual void setOrderTakenDamage() = 0;
virtual glm::vec2 getCurrentLocationInCell() = 0;
virtual std::shared_ptr<Health> getHealth() = 0;
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_UNIT_HPP
14 changes: 0 additions & 14 deletions include/Unit/IHealthable.hpp

This file was deleted.

22 changes: 18 additions & 4 deletions src/Mechanics/AvatarManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,27 @@ void AvatarManager::giveOrderToMyAvatar(std::shared_ptr<Avatar> unit) {
MapUtil::ScreenToGlobalCoord(dest)))
->ifEnemyAtTile()) {
unit->getAvatarOrder()->setAvatarOrder(AvatarOrderType::MOVE);
m_NemesisManager->addNemesis(
unit,
m_Map
if (m_Map
->getTileByCellPosition(MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(dest)))
->getAvatars()[0]);
->ifStructureExists()) {
m_NemesisManager->addNemesis(
unit, m_Map
->getTileByCellPosition(
MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(dest)))
->getStructure());
} else {
m_NemesisManager->addNemesis(
unit, m_Map
->getTileByCellPosition(
MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(dest)))
->getAvatars()[0]);
}

} else {
m_NemesisManager->removeNemesis(unit);
unit->getAvatarOrder()->setAvatarOrder(AvatarOrderType::MOVE);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mechanics/CursorSelection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void CursorSelection::cursorSelect() {
for (int i = min_y_cell; i <= max_y_cell; i++) {
for (int j = min_x_cell; j <= max_x_cell; j++) {
auto tile = m_Map->getTileByCellPosition(glm::vec2(j, i));
if (tile->ifStrucutreExists()) {
if (tile->ifStructureExists()) {
auto structure = tile->getStructure();
Append(structure);

Expand Down
2 changes: 2 additions & 0 deletions src/Scene/TutorialScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ void TutorialScene::Start() {
// combat test
m_EnemyObjectManager->spawn(m_Map, UnitType::INFANTRY, HouseType::ENEMY,
{6, 6});
m_EnemyObjectManager->spawn(m_Map, UnitType::BARRACKS, HouseType::ENEMY,
{10, 10});

stageStart();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Structure/Barracks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void Barracks::Start() {
m_HighLight.SetHLScale(this->GetTransform().scale);
whenSelected();
// State
getStructureOrder()->setStructOrder(StructureOrderType::CONSTRUCTED);
getStructureOrder()->setStructureOrder(StructureOrderType::CONSTRUCTED);
// health
Structure::getHealth()->setLivingStatus(
std::make_shared<LivingStatus>(LivingStatus::ALIVE));
Expand All @@ -47,7 +47,7 @@ void Barracks::Start(glm::vec2 location) {
SetVisible(true);
m_SpriteSheetAnimation->initSpriteSheetAnimation(m_StructureSpriteSheet,
false, INTERVAL, false);
getStructureOrder()->setStructOrder(StructureOrderType::BUILT);
getStructureOrder()->setStructureOrder(StructureOrderType::BUILT);
Structure::getHealth()->setLivingStatus(
std::make_shared<LivingStatus>(LivingStatus::ALIVE));
}
Loading

0 comments on commit 1ecace3

Please sign in to comment.