Skip to content

Commit

Permalink
Merge pull request #70 from jonylu7/combat
Browse files Browse the repository at this point in the history
Combat
  • Loading branch information
jonylu7 authored May 18, 2024
2 parents 7a8ce33 + 165e2e8 commit 1a082b6
Show file tree
Hide file tree
Showing 32 changed files with 356 additions and 256 deletions.
Binary file modified assets/sprites/.DS_Store
Binary file not shown.
Binary file removed assets/sprites/mech.png
Binary file not shown.
Binary file added assets/sprites/mech_dead.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/mech_open_fire.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/mech_taken_damage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions include/Avatar/AttackAndDamage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class 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());
auto damage =
((1 - targethealth->getArmorRate()) * m_Weapon->getSoftAttack()) +
(targethealth->getArmorRate() * m_Weapon->getHardAttack());
targethealth->addHP(-1 * damage);
}

void openFireToTarget(std::shared_ptr<IHealthable> target) {
Expand Down
36 changes: 20 additions & 16 deletions include/Avatar/Avatar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,24 @@
#include "Selectable.hpp"
#include "Util/Image.hpp"

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

public:
Avatar(){};
Avatar(UnitType unit, HouseType house)
: m_ID(GameObjectID(UnitType::NONE, HouseType::MY)){};
: m_ID(GameObjectID(UnitType::NONE, house)){};
~Avatar() override{};

virtual void Start(glm::vec2 spawnlocationcell);
void noOrderUpdate();
void noorderUpdate();
void spawnedUpdate();
void finishedmovingUpdate();
void moveUpdate();
void deadUpdate();
void attackUpdate();

float getDistance(glm::vec2 cell) {
return sqrt(pow(cell.x - getCurrentCell().x, 2) +
pow(cell.y - getCurrentCell().y, 2));
return sqrt(pow(cell.x - m_Moving->getCurrentCell().x, 2) +
pow(cell.y - m_Moving->getCurrentCell().y, 2));
}

void setSpriteSheet() {
Expand All @@ -61,25 +55,35 @@ class Avatar : public Moving,
"../assets/sprites/mech_single.png");
}

virtual void Update() override;

void DrawAvatar();

public:
GameObjectID getID() { return m_ID; }

virtual void Update() override;
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;
}

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

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
std::shared_ptr<Health> m_Health = std::make_shared<Health>();
// attack and damage
Expand Down
6 changes: 3 additions & 3 deletions include/Avatar/Infantry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class Infantry : public Avatar {
public:
Infantry()
: Avatar() {
setMovementSpeed(4),
getMoving()->setMovementSpeed(4),
m_Health = std::make_shared<Health>(
std::make_shared<LivingStatus>(LivingStatus::NOT_BORN_YET), 100,
0.5);
}
Infantry(HouseType house)
: Avatar(UnitType::INFANTRY,house){
: Avatar(UnitType::INFANTRY, house) {
// setHp(50);
setMovementSpeed(4);
getMoving()->setMovementSpeed(4);
}

private:
Expand Down
38 changes: 28 additions & 10 deletions include/Avatar/Moving.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Moving {

std::vector<Line> m_lineVector;
glm::vec2 m_CurrentLocation;
glm::vec2 m_DestinationLocation;

MoveDirection m_CurrentDir = MoveDirection::IDLE;

Expand All @@ -37,7 +36,7 @@ class Moving {
};

Moving(){};
virtual ~Moving(){};
~Moving(){};

glm::vec2 getCurrentCell() {
return MapUtil::GlobalCoordToCellCoord(getCurrentLocation());
Expand All @@ -47,6 +46,25 @@ class Moving {
m_CurrentLocation = location;
}
MoveDirection getCurrentDir() { return m_CurrentDir; }
void setCurrentDir(MoveDirection dir) { m_CurrentDir = dir; }

void moveUpdate() {
if (ifArrivedAtNextCell()) {
m_MovePath.pop_front();
m_PrevCell = getCurrentCell();
if (!m_MovePath.empty()) {
setCurrentDir(m_MovePath.front());
} else {
finishedmovingUpdate();
}
}
moveToNextCell();
}

void finishedmovingUpdate() {
moveToCellCorner(AvatarStandingCorner::CENTER);
setCurrentDir(MoveDirection::IDLE);
}

void moveToNextCell();
void moveToCellCorner(AvatarStandingCorner corner);
Expand All @@ -55,17 +73,17 @@ class Moving {
void setMovePath(std::deque<MoveDirection> movepath) {
m_MovePath = movepath;
m_CurrentDir = m_MovePath.front();
m_MovePath.pop_front();
m_PrevCell = getCurrentCell();
}

void setMovementSpeed(float speed) { m_MovementSpeed = speed; }

glm::vec2 getDestinationCell() {
return MapUtil::CellCoordToGlobal(m_DestinationLocation);
}
void setDestinationCell(glm::vec2 destination) {
m_DestinationLocation = MapUtil::GlobalCoordToCellCoord(destination);
bool ifMovePathEmpty() {
if (m_MovePath.empty()) {
return true;
} else {
return false;
}
}

void setMovementSpeed(float speed) { m_MovementSpeed = speed; }
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_MOVING_HPP
10 changes: 8 additions & 2 deletions include/Avatar/Weapon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ enum class WeaponType {
Art_105mm,
Art_120mm,
Nuke,
Grenade
Grenade,
NONE
};

class Weapon {
public:
Weapon() {}
Weapon()
: m_FireRange(1),
m_SoftAttack(10),
m_HardAttack(10),
m_FireRateInMs(180),
m_Type(WeaponType::NONE) {}
Weapon(float firerate, float firerange, float softattack, float hardattack,
WeaponType weapontype)
: m_FireRange(firerange),
Expand Down
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
5 changes: 3 additions & 2 deletions include/Mechanics/AvatarManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class AvatarManager {
std::vector<std::shared_ptr<Avatar>> getAvatarArray() {
return m_AvatarArray;
}
void forceMove(std::shared_ptr<Avatar> unit,glm::vec2 cell);
void forceMove(std::shared_ptr<Avatar> unit, glm::vec2 cell);

protected:
void giveOrderToAvatar(std::shared_ptr<Avatar> unit);
void giveOrderToMyAvatar(std::shared_ptr<Avatar> unit);

void updateTileWhileAvatarMoving(std::shared_ptr<Avatar> unit);

Expand Down
92 changes: 56 additions & 36 deletions include/Mechanics/BuiltStructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,38 @@
#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,bool force=false) {
std::vector<glm::vec2> coords = newstruct->GetAbsoluteOccupiedArea();
if(!force){
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();
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->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);
}
}

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 @@ -48,44 +52,60 @@ 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) {
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:
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};
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
Loading

0 comments on commit 1a082b6

Please sign in to comment.