Skip to content

Commit

Permalink
remove when dead
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed May 18, 2024
1 parent cd6b3d0 commit 4ba8955
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 41 deletions.
9 changes: 7 additions & 2 deletions include/Map/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ class MapClass : public Core::Drawable {
getTileByCellPosition(position)->setBuildable(false);
getTileByCellPosition(position)->pushAvatars(avatar);
}
void removeAvatarsByCellPosition(std::shared_ptr<Avatar> avatar,
glm::vec2 position) {

void removeAvatarByCellPosition(std::shared_ptr<Avatar> avatar,
glm::vec2 position) {
getTileByCellPosition(position)->removeAvatar(avatar);
}

void removeStrcutureByCellPosition(glm::vec2 position) {
getTileByCellPosition(position)->removeStructure();
}

protected:
void InitGrid();

Expand Down
41 changes: 28 additions & 13 deletions include/Mechanics/BuiltStructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
#include "Structure/Barracks.hpp"
#include "Structure/Structure.hpp"
#include "pch.hpp"

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

void buildNewStructure(std::shared_ptr<MapClass> m_Map,
std::shared_ptr<Structure> newstruct,
void Start(std::shared_ptr<MapClass> map) {
m_Map = map;
StartBuiltStructure();
}
void buildNewStructure(std::shared_ptr<Structure> newstruct,
bool force = false) {
std::vector<glm::vec2> coords = newstruct->GetAbsoluteOccupiedArea();
std::vector<glm::vec2> coords = newstruct->getAbsoluteOccupiedArea();
if (!force) {
if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB)) {
if (ifCanBuildStructureAtTile(m_Map, newstruct) == true) {
// check whehter or not can built at tile
if (ifCanBuildStructureAtTile(newstruct) == true) {
m_BuiltStructure.push_back(newstruct);
m_Map->builtStructureByCellPosition(newstruct, coords);
newstruct->getStructureOrder()->setStructOrder(
Expand All @@ -33,9 +37,8 @@ class BuiltStructure {
}
}

bool ifCanBuildStructureAtTile(std::shared_ptr<MapClass> m_Map,
std::shared_ptr<Structure> newstruct) {
std::vector<glm::vec2> coords = newstruct->GetAbsoluteOccupiedArea();
bool ifCanBuildStructureAtTile(std::shared_ptr<Structure> newstruct) {
std::vector<glm::vec2> coords = newstruct->getAbsoluteOccupiedArea();
for (auto i : coords) {
if (m_Map->getTileByCellPosition(i)->getBuildable() == false) {
return false;
Expand All @@ -49,14 +52,25 @@ class BuiltStructure {
}

void StartBuiltStructure() {
for (auto pair : m_BuiltStructure) {
pair->Start();
for (auto structure : m_BuiltStructure) {
structure->Start();
}
}

void UpdateBuiltStructure() {
for (auto pair : m_BuiltStructure) {
pair->Update();
// check whether the structure is dead or update
for (int i = 0; i < m_BuiltStructure.size(); i++) {
if (m_BuiltStructure[i]->getHealth()->ifDead()) {
// remove from map
for (auto a : m_BuiltStructure[i]->getAbsoluteOccupiedArea()) {
m_Map->removeStrcutureByCellPosition(a);
}
// remove from builtstructure array
m_BuiltStructure.erase(m_BuiltStructure.begin() + i);
i--;
} else {
m_BuiltStructure[i]->Update();
}
}
}
void add(std::shared_ptr<Structure> structure) {
Expand Down Expand Up @@ -86,7 +100,8 @@ class BuiltStructure {
glm::vec2 getPlayerBarrackCell() { return m_PlayerBarrackCell; }
glm::vec2 getPlayerWayPointCell() { return m_PlayerWayPointCell; }

protected:
private:
std::shared_ptr<MapClass> m_Map = std::make_shared<MapClass>();
std::vector<std::shared_ptr<Structure>> m_BuiltStructure;
glm::vec2 m_EnemyBarrackCell = {-1, -1};
glm::vec2 m_EnemyWayPointCell = {-1, -1};
Expand Down
4 changes: 2 additions & 2 deletions include/Mechanics/StructureManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class StructureManager {
virtual ~StructureManager() {}

void Start(std::shared_ptr<MapClass> map) {
m_StructureArray.StartBuiltStructure();
m_StructureArray.Start(map);
m_Map = map;
}
void Update() {
Expand All @@ -37,7 +37,7 @@ class StructureManager {
->getStructureOrderType() ==
StructureOrderType::SELECTING_SITE) {
m_StructureArray.buildNewStructure(
m_Map, m_StructSelectingConstructionSite);
m_StructSelectingConstructionSite);
}
}

Expand Down
10 changes: 5 additions & 5 deletions include/Mechanics/UnitManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,35 +112,35 @@ class UnitManager : public Player {
auto structure = std::make_shared<Barracks>(house);
structure->Start(cellPos);
m_StructureManager->getStructureArray()->buildNewStructure(
m_Map, structure, true);
structure, true);
break;
}
case UnitType::ORE_REF: {
auto structure = std::make_shared<OreRefinery>(house);
structure->Start(cellPos);
m_StructureManager->getStructureArray()->buildNewStructure(
m_Map, structure, true);
structure, true);
break;
}
case UnitType::POWER_PLANT: {
auto structure = std::make_shared<PowerPlants>(house);
structure->Start(cellPos);
m_StructureManager->getStructureArray()->buildNewStructure(
m_Map, structure, true);
structure, true);
break;
}
case UnitType::WAR_FACT: {
auto structure = std::make_shared<WarFactory>(house);
structure->Start(cellPos);
m_StructureManager->getStructureArray()->buildNewStructure(
m_Map, structure, true);
structure, true);
break;
}
case UnitType::ADV_POWER_PLANT: {
auto structure = std::make_shared<ADVPowerPlants>(house);
structure->Start(cellPos);
m_StructureManager->getStructureArray()->buildNewStructure(
m_Map, structure, true);
structure, true);
break;
}
case UnitType::INFANTRY: {
Expand Down
2 changes: 1 addition & 1 deletion include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Structure : public Util::GameObject,
glm::vec2 getLocationCell() {
return MapUtil::GlobalCoordToCellCoord(m_ObjectLocation);
}
std::vector<glm::vec2> GetAbsoluteOccupiedArea();
std::vector<glm::vec2> getAbsoluteOccupiedArea();
void SetRelativeOccupiedArea(std::vector<glm::vec2> Area) {
m_RelativeOccupiedArea = Area;
}
Expand Down
16 changes: 10 additions & 6 deletions include/Unit/Health.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,25 @@ class Health {

void setHP(int hp) {
m_HP = hp;
if (m_HP <= 0) {
m_LivingStatus = std::make_shared<LivingStatus>(LivingStatus::DEAD);
}
ifDead();
}

void addHP(int value) {
m_HP += value;
if (m_HP <= 0) {
m_LivingStatus = std::make_shared<LivingStatus>(LivingStatus::DEAD);
}
ifDead();
}

int getHP() { return m_HP; }
void setArmorRate(float armorrate) { m_ArmorRate = armorrate; }
float getArmorRate() { return m_ArmorRate; }
bool ifDead() {
if (m_HP <= 0) {
m_LivingStatus = std::make_shared<LivingStatus>(LivingStatus::DEAD);
return true;
} else {
return false;
}
}

private:
std::shared_ptr<LivingStatus> m_LivingStatus =
Expand Down
4 changes: 2 additions & 2 deletions include/pch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

#include <GL/glew.h>

//suppose to be "GL/glut", yet GLUT/glut on mac
//#include <GLUT/glut.h>
// suppose to be "GL/glut", yet GLUT/glut on mac
// #include <GLUT/glut.h>

#include <glm/glm.hpp>
#include <glm/gtx/matrix_transform_2d.hpp>
Expand Down
33 changes: 24 additions & 9 deletions src/Mechanics/AvatarManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,29 @@

void AvatarManager::Update() {
m_NemesisManager->Update();
for (auto unit : m_AvatarArray) {
unit->Update();
if (unit->getAvatarOrder()->getAvatarOrder() == AvatarOrderType::MOVE) {
updateTileWhileAvatarMoving(unit);
}
if (unit->getSelected()) {
giveOrderToMyAvatar(unit);
for (int i = 0; i < m_AvatarArray.size(); i++) {
// if dead remove, else update it
if (m_AvatarArray[i]->getHealth()->ifDead()) {
// remove from map
m_Map->removeAvatarByCellPosition(
m_AvatarArray[i],
m_AvatarArray[i]->getMoving()->getCurrentCell());
// remove from array
m_AvatarArray.erase(m_AvatarArray.begin() + i);
i--;
} else {
m_AvatarArray[i]->Update();

// update tile while avatar is moving
if (m_AvatarArray[i]->getAvatarOrder()->getAvatarOrder() ==
AvatarOrderType::MOVE) {
updateTileWhileAvatarMoving(m_AvatarArray[i]);
}

// give order to avatar
if (m_AvatarArray[i]->getSelected()) {
giveOrderToMyAvatar(m_AvatarArray[i]);
}
}
}
}
Expand Down Expand Up @@ -55,8 +71,7 @@ void AvatarManager::forceMove(std::shared_ptr<Avatar> unit, glm::vec2 cell) {
void AvatarManager::updateTileWhileAvatarMoving(
std::shared_ptr<Avatar> avatar) {
if (avatar->getMoving()->ifArrivedAtNextCell()) {
m_Map->removeAvatarsByCellPosition(avatar,
unitArrayAndLocation[avatar]);
m_Map->removeAvatarByCellPosition(avatar, unitArrayAndLocation[avatar]);
m_Map->setAvatarByCellPosition(avatar,
avatar->getMoving()->getCurrentCell());
unitArrayAndLocation[avatar] = avatar->getMoving()->getCurrentCell();
Expand Down
2 changes: 1 addition & 1 deletion src/Structure/Structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void Structure::attachmentUpdate() {
m_HighLight.SetObjectLocation(this->GetDrawLocation());
m_HighLight.Draw();
}
std::vector<glm::vec2> Structure::GetAbsoluteOccupiedArea() {
std::vector<glm::vec2> Structure::getAbsoluteOccupiedArea() {
std::vector<glm::vec2> Area;
for (auto i : m_RelativeOccupiedArea) {
Area.push_back({i.x + getLocationCell().x, i.y + getLocationCell().y});
Expand Down

0 comments on commit 4ba8955

Please sign in to comment.