Skip to content

Commit

Permalink
struct and avatar comps
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed May 17, 2024
1 parent f795f9b commit cd6b3d0
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 103 deletions.
26 changes: 13 additions & 13 deletions include/Avatar/Avatar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
#include "Selectable.hpp"
#include "Util/Image.hpp"

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

public:
Avatar(){};
Expand Down Expand Up @@ -59,29 +55,33 @@ class Avatar : public AttackAndDamage,
"../assets/sprites/mech_single.png");
}

GameObjectID getID() { return m_ID; }

virtual void Update() override;

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

void DrawAvatar();

public:
std::shared_ptr<AttackAndDamage> getAttackAndDamager() {
GameObjectID getID() { return m_ID; }

std::shared_ptr<AttackAndDamage> getAttackAndDamage() {
return m_AttackAndDamage;
}
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) override {
m_Health = health;
}

protected:
std::shared_ptr<SpriteSheet> m_AvatarSpriteSheet =
std::make_shared<SpriteSheet>();
std::shared_ptr<Util::SpriteSheetAnimation> m_SpriteSheetAnimation =
std::make_shared<Util::SpriteSheetAnimation>();

// order
std::shared_ptr<AvatarOrder> m_Order = std::make_shared<AvatarOrder>();
// moving
std::shared_ptr<Moving> m_Moving = std::make_shared<Moving>();
// health
Expand Down
53 changes: 29 additions & 24 deletions include/Mechanics/BuiltStructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ class BuiltStructure {
virtual ~BuiltStructure() {}

void buildNewStructure(std::shared_ptr<MapClass> m_Map,
std::shared_ptr<Structure> newstruct,bool force=false) {
std::shared_ptr<Structure> newstruct,
bool force = false) {
std::vector<glm::vec2> coords = newstruct->GetAbsoluteOccupiedArea();
if(!force){
if (!force) {
if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB)) {
if (ifCanBuildStructureAtTile(m_Map, newstruct) == true) {
m_BuiltStructure.push_back(newstruct);
m_Map->builtStructureByCellPosition(newstruct, coords);
newstruct->setStructOrder(
StructureOrder::StructureOrderType::BUILT);
newstruct->getStructureOrder()->setStructOrder(
StructureOrderType::BUILT);
}
}
}else{
} else {
m_BuiltStructure.push_back(newstruct);
m_Map->builtStructureByCellPosition(newstruct, coords);
newstruct->setStructOrder(
StructureOrder::StructureOrderType::BUILT);
newstruct->getStructureOrder()->setStructOrder(
StructureOrderType::BUILT);
}
}

Expand Down Expand Up @@ -62,30 +63,34 @@ class BuiltStructure {
m_BuiltStructure.push_back(structure);
}

void updateAvatarSpawnLocation(std::vector<std::shared_ptr<Structure>> structure){
for(auto i:structure){
if(std::dynamic_pointer_cast<Barracks>(i)){
if(i->getHouseType()==HouseType::ENEMY){
m_EnemyBarrackCell=i->GetObjectLocation();
m_EnemyWayPointCell=std::dynamic_pointer_cast<Barracks>(i)->GetWayPointLocation();
} else{
m_PlayerBarrackCell=i->GetObjectLocation();
m_PlayerWayPointCell=std::dynamic_pointer_cast<Barracks>(i)->GetWayPointLocation();
void updateAvatarSpawnLocation(
std::vector<std::shared_ptr<Structure>> structure) {
for (auto i : structure) {
if (std::dynamic_pointer_cast<Barracks>(i)) {
if (i->getHouseType() == HouseType::ENEMY) {
m_EnemyBarrackCell = i->GetObjectLocation();
m_EnemyWayPointCell = std::dynamic_pointer_cast<Barracks>(i)
->GetWayPointLocation();
} else {
m_PlayerBarrackCell = i->GetObjectLocation();
m_PlayerWayPointCell =
std::dynamic_pointer_cast<Barracks>(i)
->GetWayPointLocation();
}
}
}
}

glm::vec2 getEnemyBarrackCell(){return m_EnemyBarrackCell;}
glm::vec2 getEnemyWayPointCell(){return m_EnemyWayPointCell;}
glm::vec2 getPlayerBarrackCell(){return m_PlayerBarrackCell;}
glm::vec2 getPlayerWayPointCell(){return m_PlayerWayPointCell;}
glm::vec2 getEnemyBarrackCell() { return m_EnemyBarrackCell; }
glm::vec2 getEnemyWayPointCell() { return m_EnemyWayPointCell; }
glm::vec2 getPlayerBarrackCell() { return m_PlayerBarrackCell; }
glm::vec2 getPlayerWayPointCell() { return m_PlayerWayPointCell; }

protected:
std::vector<std::shared_ptr<Structure>> m_BuiltStructure;
glm::vec2 m_EnemyBarrackCell={-1,-1};
glm::vec2 m_EnemyWayPointCell={-1,-1};
glm::vec2 m_PlayerBarrackCell={-1,-1};
glm::vec2 m_PlayerWayPointCell={-1,-1};
glm::vec2 m_EnemyBarrackCell = {-1, -1};
glm::vec2 m_EnemyWayPointCell = {-1, -1};
glm::vec2 m_PlayerBarrackCell = {-1, -1};
glm::vec2 m_PlayerWayPointCell = {-1, -1};
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_BUILTSTRUCTURE_HPP
22 changes: 14 additions & 8 deletions include/Mechanics/NemesisManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class NemesisManager {
}
if (hunter->getDistance(
m_Nemesis[hunter]->getMoving()->getCurrentCell()) <=
hunter->getWeapon()->getFireRange() *
hunter->getAttackAndDamage()->getWeapon()->getFireRange() *
CELL_SIZE.x) // check with in range
{
return true;
Expand All @@ -50,24 +50,30 @@ class NemesisManager {
auto hunter = pair.first;
auto prey = pair.second;
if (ifNemesisWithinWeaponRange(hunter)) {
hunter->setAvatarOrder(AvatarOrderType::OPEN_FIRE);
prey->setAvatarOrder(AvatarOrderType::TAKEN_DAMAGE);
hunter->getAttackAndDamager()->openFireToTarget(prey);
hunter->getAvatarOrder()->setAvatarOrder(
AvatarOrderType::OPEN_FIRE);
prey->getAvatarOrder()->setAvatarOrder(
AvatarOrderType::TAKEN_DAMAGE);
hunter->getAttackAndDamage()->openFireToTarget(prey);
}

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

if (*pair.first->getHealth()->getLivingStatus() ==
LivingStatus::DEAD) {
removeNemesis(hunter);
hunter->setAvatarOrder(AvatarOrderType::NO_ORDER);
prey->setAvatarOrder(AvatarOrderType::NO_ORDER);
hunter->getAvatarOrder()->setAvatarOrder(
AvatarOrderType::NO_ORDER);
prey->getAvatarOrder()->setAvatarOrder(
AvatarOrderType::NO_ORDER);
break;
}
}
Expand Down
9 changes: 5 additions & 4 deletions include/Mechanics/StructureManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ class StructureManager {
newstruct->Start();
m_StructSelectingConstructionSite = newstruct;

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

void SelectingBuildSite() {
// bulit or not
if (m_StructSelectingConstructionSite->getStructOrder() ==
StructureOrder::StructureOrderType::SELECTING_SITE) {
if (m_StructSelectingConstructionSite->getStructureOrder()
->getStructureOrderType() ==
StructureOrderType::SELECTING_SITE) {
m_StructureArray.buildNewStructure(
m_Map, m_StructSelectingConstructionSite);
}
Expand Down
26 changes: 14 additions & 12 deletions include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#include "Display/SpriteSheet.hpp"
#include "Display/SpriteSheetAnimation.hpp"
#include "HighLight.h"
#include "Map/MapUtility.hpp"
#include "Mechanics/GameObjectID.hpp"
#include "Selectable.hpp"
#include "Structure/StructureOrder.hpp"
#include "Util/Image.hpp"

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

#include "Util/GameObject.hpp"
#include "Util/Input.hpp"
Expand All @@ -26,7 +26,6 @@

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

public:
Expand Down Expand Up @@ -79,34 +78,34 @@ class Structure : public Util::GameObject,
glm::vec2 GetObjectLocation() { return this->m_ObjectLocation; }
virtual void SetAttachVisible(bool visible);
glm::vec2 GetDrawLocation() { return m_DrawLocation; };
void SetID(GameObjectID id) { m_ID = id; };

virtual void attachmentUpdate();

float getElectricPower() { return this->m_ElectricPower; }
float getBuildingTime() { return this->m_BuildingTime; }
float getBuildingCost() { return this->m_BuildingCost; }
HouseType getHouseType() { return this->m_ID.getHouseType(); }

glm::vec2 GlobalCoordToCellCoord(glm::vec2 globalCoord) {
return glm::vec2(int(globalCoord[0] / CELL_SIZE.x),
int(globalCoord[1] / CELL_SIZE.y));
}
HouseType getHouseType() { return this->m_ID.getHouseType(); }

glm::vec2 GetObjectCell() {
return GlobalCoordToCellCoord(m_ObjectLocation);
glm::vec2 getLocationCell() {
return MapUtil::GlobalCoordToCellCoord(m_ObjectLocation);
}
std::vector<glm::vec2> GetAbsoluteOccupiedArea();
void SetRelativeOccupiedArea(std::vector<glm::vec2> Area) {
m_RelativeOccupiedArea = Area;
}

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;
}
std::shared_ptr<StructureOrder> getStructureOrder() { return m_Order; }

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

protected:
float m_ElectricPower;
Expand All @@ -127,6 +126,9 @@ class Structure : public Util::GameObject,
glm::vec2 m_ObjectLocation = {100, 100};
std::vector<glm::vec2> m_RelativeOccupiedArea = {{0, 0}};

// structure order
std::shared_ptr<StructureOrder> m_Order =
std::make_shared<StructureOrder>();
// health
std::shared_ptr<Health> m_Health = std::make_shared<Health>();
// attack and damage
Expand Down
19 changes: 9 additions & 10 deletions include/Structure/StructureOrder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTUREORDER_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTUREORDER_HPP

enum class StructureOrderType {
NOT_CONSTRUCTED_YET,
CONSTRUCTED,
SELECTING_SITE,
BUILT,
NO_ORDER
};
class StructureOrder {

public:
StructureOrder() {}
virtual ~StructureOrder() {}
enum class StructureOrderType {
NOT_CONSTRUCTED_YET,
CONSTRUCTED,
SELECTING_SITE,
BUILT,
NO_ORDER
};
StructureOrderType getStructOrder() { return m_StructOrder; }

StructureOrderType getStructureOrderType() { return m_StructOrder; }

void setStructOrder(StructureOrderType structorder) {
m_StructOrder = structorder;
Expand Down
3 changes: 2 additions & 1 deletion include/Structure/WayPointStructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class WayPointStructure : public Structure, public IWayPointStructure {
break;
}
case LivingStatus::ALIVE: {
if (m_StructOrder == StructureOrderType::SELECTING_SITE) {
if (getStructureOrder()->getStructureOrderType() ==
StructureOrderType::SELECTING_SITE) {
this->updateMoveable();
} else {
this->updateFixed();
Expand Down
25 changes: 15 additions & 10 deletions src/Avatar/Avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void Avatar::whenSelected() {
void Avatar::Update() {
DrawAvatar();
if (getMoving()->ifMovePathEmpty()) {
m_AvatarOrder = AvatarOrderType::NO_ORDER;
getAvatarOrder()->setAvatarOrder(AvatarOrderType::NO_ORDER);
}
switch (*m_Health->getLivingStatus()) {

Expand All @@ -22,16 +22,20 @@ void Avatar::Update() {
case (LivingStatus::ALIVE):
whenSelected();

if (m_AvatarOrder == AvatarOrderType::OPEN_FIRE) {
if (getAvatarOrder()->getAvatarOrder() == AvatarOrderType::OPEN_FIRE) {
// open fire
} else if (m_AvatarOrder == AvatarOrderType::MOVE) {
} else if (getAvatarOrder()->getAvatarOrder() ==
AvatarOrderType::MOVE) {
m_Moving->moveUpdate();
} else if (m_AvatarOrder == AvatarOrderType::NO_ORDER) {
} else if (getAvatarOrder()->getAvatarOrder() ==
AvatarOrderType::NO_ORDER) {
noorderUpdate();
} else if (m_AvatarOrder == AvatarOrderType::TAKEN_DAMAGE) {
} else if (getAvatarOrder()->getAvatarOrder() ==
AvatarOrderType::TAKEN_DAMAGE) {

// takendamage
} else if (m_AvatarOrder == AvatarOrderType::SPAWNED) {
} else if (getAvatarOrder()->getAvatarOrder() ==
AvatarOrderType::SPAWNED) {
spawnedUpdate();
}

Expand All @@ -57,7 +61,7 @@ void Avatar::Start(glm::vec2 spawnlocationcell) { // destination = Barrack's
// setSpriteSheet();
SetVisible(true);
getMoving()->setMovementSpeed(4);
m_AvatarOrder = AvatarOrderType::SPAWNED;
getAvatarOrder()->setAvatarOrder(AvatarOrderType::SPAWNED);
getMoving()->setCurrentLocation(
MapUtil::CellCoordToGlobal(spawnlocationcell));
m_Transform.scale = {1, 1};
Expand Down Expand Up @@ -115,11 +119,12 @@ void Avatar::DrawAvatar() {
this->SetDrawable(
std::make_shared<Util::Image>("../assets/sprites/mech_dead.png"));
} else {
if (m_AvatarOrder == AvatarOrderType::OPEN_FIRE) {

if (getAvatarOrder()->getAvatarOrder() == AvatarOrderType::OPEN_FIRE) {
this->SetDrawable(std::make_shared<Util::Image>(
"../assets/sprites/mech_open_fire.png"));
} else if (m_AvatarOrder == AvatarOrderType::TAKEN_DAMAGE) {
} else if (getAvatarOrder()->getAvatarOrder() ==
AvatarOrderType::TAKEN_DAMAGE) {
this->SetDrawable(std::make_shared<Util::Image>(
"../assets/sprites/mech_taken_damage.png"));
} else {
Expand Down
Loading

0 comments on commit cd6b3d0

Please sign in to comment.