Skip to content

Commit

Permalink
nemesis
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed May 3, 2024
1 parent 8faba1f commit 335710d
Show file tree
Hide file tree
Showing 15 changed files with 233 additions and 119 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ set(SRC_FILES
${SRC_DIR}/Mechanics/FindValidPathToDest.cpp
${SRC_DIR}/Mechanics/GameObjectID.cpp
${SRC_DIR}/Mechanics/CursorSelection.cpp
${SRC_DIR}/Mechanics/AvatarManager.cpp

)
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
Expand Down
4 changes: 3 additions & 1 deletion include/Avatar/AttackAndDamageUnit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AttackAndDamageUnit {
}
}

void attackUpdate(std::shared_ptr<AttackAndDamageUnit> target) {
void openFireToTarget(std::shared_ptr<AttackAndDamageUnit> target) {
// cd time
m_DeltaTime += m_Time.GetDeltaTime();
if (m_Weapon.getIntervalInS() <= m_DeltaTime) {
Expand All @@ -52,6 +52,8 @@ class AttackAndDamageUnit {

void setLivingStatus(LivingStatus status) { m_LivingStatus = status; };

Weapon getWeapon() { return m_Weapon; }

private:
float m_DeltaTime = 0;

Expand Down
2 changes: 1 addition & 1 deletion include/Avatar/AvatarOrder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_AVATARORDER_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_AVATARORDER_HPP
enum class AvatarOrderType { ATTACK, MOVE_ATTACK, MOVE, NO_ORDER };
enum class AvatarOrderType { OPEN_FIRE, MOVE, NO_ORDER, TAKEN_DAMAGE };
class AvatarOrder {
public:
AvatarOrder() {}
Expand Down
35 changes: 19 additions & 16 deletions include/Avatar/Weapon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,48 @@ class Weapon {
public:
Weapon() {}
Weapon(float firerate, float firerange, float softattack, float hardattack,
WeaponType weapon) {}
WeaponType weapontype)
: m_FireRange(firerange),
m_SoftAttack(softattack),
m_HardAttack(hardattack),
m_FireRateInMs(firerate),
m_Type(weapontype) {}
~Weapon() {}
// Getter and setter methods for m_fireRate
static float getFireRateInM() { return m_FireRateInMs; }
float getFireRateInM() { return m_FireRateInMs; }

static void setFireRateInM(float fireRateInM) {
m_FireRateInMs = fireRateInM;
}
void setFireRateInM(float fireRateInM) { m_FireRateInMs = fireRateInM; }

static float getIntervalInS() {
float getIntervalInS() {
auto r = m_FireRateInMs / 60;
return (1 / r);
}

// Getter and setter methods for m_fireRange
static float getFireRange() { return m_FireRange; }
float getFireRange() { return m_FireRange; }

static void setFireRange(float fireRange) { m_FireRange = fireRange; }
void setFireRange(float fireRange) { m_FireRange = fireRange; }

// Getter and setter methods for m_SoftAttack
static float getSoftAttack() { return m_SoftAttack; }
float getSoftAttack() { return m_SoftAttack; }

static void setSoftAttack(float softAttack) { m_SoftAttack = softAttack; }
void setSoftAttack(float softAttack) { m_SoftAttack = softAttack; }

// Getter and setter methods for m_HardAttack
static float getHardAttack() { return m_HardAttack; }
float getHardAttack() { return m_HardAttack; }

static void setHardAttack(float hardAttack) { m_HardAttack = hardAttack; }
void setHardAttack(float hardAttack) { m_HardAttack = hardAttack; }

// Getter and setter methods for m_Type
WeaponType getType() const { return m_Type; }

void setType(WeaponType type) { m_Type = type; }

private:
static float m_FireRateInMs;
static float m_FireRange;
static float m_SoftAttack;
static float m_HardAttack;
float m_FireRateInMs;
float m_FireRange;
float m_SoftAttack;
float m_HardAttack;
WeaponType m_Type;
};

Expand Down
32 changes: 11 additions & 21 deletions include/Mechanics/AvatarManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_AVATARMANAGER_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_AVATARMANAGER_HPP
#include "Avatar/Avatar.hpp"
#include "FindValidPathToDest.hpp"
#include "Map/Map.hpp"
#include "NemesisManager.hpp"
#include <unordered_map>
class AvatarManager {
public:
Expand All @@ -20,32 +23,16 @@ class AvatarManager {
unit->setSelected(true);

unit->Update();

if (unit->ifNewDestionationIsSetted()) {
setNewDestinationForAvatar(unit);
if (unit->getAvatarOrder() == AvatarOrderType::MOVE) {
updateTileWhileAvatarMoving(unit);
}
keepUpdatingAvatarsPosition(unit);
}
m_NemesisManager.Update();
}

void setNewDestinationForAvatar(std::shared_ptr<Avatar> unit) {
FindValidPathToDest m_wayPointAvatar(m_Map);

auto queue = m_wayPointAvatar.findPath(unit->getCurrentCell(),
unit->getDestinationCell());
// unit
unit->setMovePath(queue);
unit->setNewDestinationIsSetted(false);

if (m_Map->getTileByCellPosition(unit->getDestinationCell())
->ifEnemyAtTile()) {
unit->setAvatarOrder(AvatarOrderType::MOVE_ATTACK);
} else {
unit->setAvatarOrder(AvatarOrderType::MOVE);
}
}
void giveOrderToAvatar(std::shared_ptr<Avatar> unit);

void keepUpdatingAvatarsPosition(std::shared_ptr<Avatar> unit) {
void updateTileWhileAvatarMoving(std::shared_ptr<Avatar> unit) {
if (unit->ifArrivedAtNextCell()) {
m_Map->removeAvatarsByCellPosition(unit,
unitArrayAndLocation[unit]);
Expand All @@ -54,7 +41,10 @@ class AvatarManager {
}
}

// if given order has enemy

protected:
NemesisManager m_NemesisManager;
std::vector<std::shared_ptr<Avatar>> m_AvatarArray;
std::unordered_map<std::shared_ptr<Avatar>, glm::vec2> unitArrayAndLocation;

Expand Down
53 changes: 53 additions & 0 deletions include/Mechanics/BuiltStructure.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Created by 盧威任 on 4/24/24.
//

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_BUILTSTRUCTURE_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_BUILTSTRUCTURE_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) {
m_BuiltStructure.push_back(newstruct);
std::vector<glm::vec2> coords = newstruct->GetAbsoluteOccupiedArea();
if (ifCanBuildStructureAtTile(m_Map, newstruct) == true) {
m_Map->builtStructureByCellPosition(newstruct, coords);
}
}

bool ifCanBuildStructureAtTile(std::shared_ptr<MapClass> m_Map,
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;
}
}
return true;
}

std::vector<std::shared_ptr<Structure>> getBuiltStructureArray() {
return m_BuiltStructure;
}

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

void UpdateBuiltStructure() {
for (auto pair : m_BuiltStructure) {
pair->Update();
}
}

protected:
std::vector<std::shared_ptr<Structure>> m_BuiltStructure;
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_BUILTSTRUCTURE_HPP
13 changes: 11 additions & 2 deletions include/Mechanics/CursorSelection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,19 @@ class CursorSelection {
public:
CursorSelection() {}
virtual ~CursorSelection() {}
void CursorSelect(std::shared_ptr<MapClass> m_Map);
void cursorSelect(std::shared_ptr<MapClass> m_Map);
void Update();

private:
std::vector<std::shared_ptr<Selectable>> m_LastSeletctedObjects;
void clearAllSelectedObjects();
void Append(std::shared_ptr<Structure> structure);
void Append(std::shared_ptr<Avatar> avatar);

private:
std::shared_ptr<Structure> m_SelectedStructure =
std::make_shared<Structure>();
std::vector<std::shared_ptr<Avatar>> m_SelectedAvatars =
std::vector<std::shared_ptr<Avatar>>();
glm::vec2 m_CursorStart;
glm::vec2 m_CursorEnd;
};
Expand Down
67 changes: 67 additions & 0 deletions include/Mechanics/NemesisManager.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// Created by 盧威任 on 5/3/24.
//

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_NEMESISMANAGER_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_NEMESISMANAGER_HPP
#include "Map/MapUtility.hpp"
class NemesisManager {

public:
void addNemesis(std::shared_ptr<Avatar> avatar,
std::shared_ptr<Avatar> nemesis) {
m_Nemesis[avatar] = nemesis;
}

void removeNemesis(std::shared_ptr<Avatar> avatar) {
if (ifAvatarHasNemesis(avatar)) {
m_Nemesis.erase(avatar);
}
}

bool ifAvatarHasNemesis(std::shared_ptr<Avatar> avatar) {
if (m_Nemesis.count(avatar)) {
return true;
} else {
return false;
}
}
bool ifNemesisWithinWeaponRange(std::shared_ptr<Avatar> avatar) {
if (ifAvatarHasNemesis(avatar) == false) {
return false;
}
if (1 == 0) // with in range
{
return true;
} else {
return false;
}
}

void Update() {
for (auto pair : m_Nemesis) {
if (ifNemesisWithinWeaponRange(pair.first)) {
pair.first->setAvatarOrder(AvatarOrderType::OPEN_FIRE);
pair.second->setAvatarOrder(AvatarOrderType::TAKEN_DAMAGE);
pair.first->openFireToTarget(pair.second);
}

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

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

private:
std::unordered_map<std::shared_ptr<Avatar>, std::shared_ptr<Avatar>>
m_Nemesis;
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_NEMESISMANAGER_HPP
40 changes: 0 additions & 40 deletions include/Mechanics/StructureArray.hpp

This file was deleted.

22 changes: 9 additions & 13 deletions include/Mechanics/StructureManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTUREMANAGER_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTUREMANAGER_HPP
#include "Mechanics/StructureArray.hpp"
#include "Structure/Structure.hpp"
#include "Mechanics/BuiltStructure.hpp"
class StructureManager {
#include "Structure/Structure.hpp"
public:
StructureManager() {}
virtual ~StructureManager() {}
Expand All @@ -17,25 +17,21 @@ class StructureManager {
void AddStructSelectingBuiltSite(std::shared_ptr<Structure> newstruct) {
newstruct->Start();
m_StructSelectingConstructionSite = newstruct;
m_IsSelectingConstructionSite = true;
// m_IsSelectingConstructionSite = true;
}

void SelectdBuiltSite(std::shared_ptr<MapClass> m_Map) {
if (m_IsSelectingConstructionSite) {
if (m_StructSelectingConstructionSite->getBuilt()) {
m_StructureArray.Built(m_Map,
m_StructSelectingConstructionSite);
m_IsSelectingConstructionSite = false;
}
m_StructSelectingConstructionSite->Update();
if (m_StructSelectingConstructionSite->getBuilt()) {
m_StructureArray.buildNewStructure(
m_Map, m_StructSelectingConstructionSite);
}
m_StructSelectingConstructionSite->Update();
}
StructureArray getStructureArray() { return m_StructureArray; }
BuiltStructure getStructureArray() { return m_StructureArray; }

protected:
StructureArray m_StructureArray;
BuiltStructure m_StructureArray;
std::shared_ptr<Structure> m_StructSelectingConstructionSite =
std::make_shared<Structure>();
bool m_IsSelectingConstructionSite = false;
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_STRUCTUREMANAGER_HPP
Loading

0 comments on commit 335710d

Please sign in to comment.