Skip to content

Commit

Permalink
composition avatar and combat
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed May 17, 2024
1 parent a97d841 commit f4d2a4c
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 116 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.
20 changes: 12 additions & 8 deletions include/Avatar/Avatar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,28 @@
#include "Selectable.hpp"
#include "Util/Image.hpp"

class Avatar : public Moving,
public AttackAndDamage,
class Avatar : public AttackAndDamage,
public AvatarOrder,
public Util::GameObject,
public Selectable,
public AvatarOrder,
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 Down Expand Up @@ -70,16 +68,22 @@ class Avatar : public Moving,
m_Health = health;
}

void DrawAvatar();

public:
std::shared_ptr<AttackAndDamage> getAttackAndDamager() {
return m_AttackAndDamage;
}
std::shared_ptr<Moving> getMoving() { return m_Moving; }

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

// 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
37 changes: 28 additions & 9 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_PrevCell = getCurrentCell();
if (!m_MovePath.empty()) {
setCurrentDir(m_MovePath.front());
m_MovePath.pop_front();
} else {
finishedmovingUpdate();
}
}
moveToNextCell();
}

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

void moveToNextCell();
void moveToCellCorner(AvatarStandingCorner corner);
Expand All @@ -59,13 +77,14 @@ class Moving {
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(10),
m_Type(WeaponType::NONE) {}
Weapon(float firerate, float firerange, float softattack, float hardattack,
WeaponType weapontype)
: m_FireRange(firerange),
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
6 changes: 4 additions & 2 deletions include/Mechanics/NemesisManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ class NemesisManager {
if (ifAvatarHasNemesis(hunter) == false) {
return false;
}
if (hunter->getDistance(m_Nemesis[hunter]->getCurrentCell()) <=
hunter->getWeapon()->getFireRange()) // check with in range
if (hunter->getDistance(
m_Nemesis[hunter]->getMoving()->getCurrentCell()) <=
hunter->getWeapon()->getFireRange() *
CELL_SIZE.x) // check with in range
{
return true;
} else {
Expand Down
7 changes: 0 additions & 7 deletions include/Mechanics/UnitManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ class UnitManager : public Player {
return;
}



avatar->Start(m_StructureManager->getStructureArray()
->getPlayerBarrackCell());
// avatar
Expand All @@ -109,11 +107,6 @@ class UnitManager : public Player {
void spawn(std::shared_ptr<MapClass> m_Map, UnitType unit, HouseType house,
glm::vec2 cellPos) {
// 缺檢查敵方擁有建築的位置,並重生在該處
if (house == HouseType::ENEMY) {
// m_Enemy->addUnitConstructCount(unit, 1);
} else {
// m_Player->setUnitConstructCount(unit, 1);
}
switch (unit) {
case UnitType::BARRACKS: {
auto structure = std::make_shared<Barracks>(house);
Expand Down
66 changes: 29 additions & 37 deletions src/Avatar/Avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ void Avatar::whenSelected() {
}

void Avatar::Update() {

if (getMoving()->ifMovePathEmpty()) {
m_AvatarOrder = AvatarOrderType::NO_ORDER;
}
switch (*m_Health->getLivingStatus()) {
DrawAvatar();
case (LivingStatus::DEAD):
SetVisible(false);
break;
Expand All @@ -18,12 +23,14 @@ void Avatar::Update() {
whenSelected();

if (m_AvatarOrder == AvatarOrderType::OPEN_FIRE) {

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

// takendamage
} else if (m_AvatarOrder == AvatarOrderType::SPAWNED) {
spawnedUpdate();
}
Expand All @@ -32,39 +39,15 @@ void Avatar::Update() {
}
}

void Avatar::noOrderUpdate() {
m_CurrentDir = MoveDirection::IDLE;
void Avatar::noorderUpdate() {
getMoving()->setCurrentDir(MoveDirection::IDLE);
SetVisible(true);
m_Transform.translation = getCurrentLocation();

Draw();
m_Transform.translation = getMoving()->getCurrentLocation();
}

void Avatar::spawnedUpdate() {
SetVisible(true);
m_Transform.translation = getCurrentLocation();

Draw();
}
void Avatar::moveUpdate() {

if (ifArrivedAtNextCell()) {
m_PrevCell = getCurrentCell();
if (!m_MovePath.empty()) {
m_CurrentDir = m_MovePath.front();
m_MovePath.pop_front();
} else {
finishedmovingUpdate();
m_CurrentDir = MoveDirection::IDLE;
m_AvatarOrder = AvatarOrderType::NO_ORDER;
}
}
moveToNextCell();

SetVisible(true);
m_Transform.translation = getCurrentLocation();

Draw();
m_Transform.translation = getMoving()->getCurrentLocation();
}

void Avatar::Start(glm::vec2 spawnlocationcell) { // destination = Barrack's
Expand All @@ -73,9 +56,10 @@ void Avatar::Start(glm::vec2 spawnlocationcell) { // destination = Barrack's
this->SetDrawable(customizeImage());
// setSpriteSheet();
SetVisible(true);
setMovementSpeed(4);
getMoving()->setMovementSpeed(4);
m_AvatarOrder = AvatarOrderType::SPAWNED;
m_CurrentLocation = MapUtil::CellCoordToGlobal(spawnlocationcell);
getMoving()->getCurrentLocation() =
MapUtil::CellCoordToGlobal(spawnlocationcell);
m_Transform.scale = {1, 1};
getHealth()->setLivingStatus(
std::make_shared<LivingStatus>(LivingStatus::ALIVE));
Expand Down Expand Up @@ -124,9 +108,17 @@ void Avatar::DEBUG_printCurrentMoveDirection(MoveDirection Dir) {
}
}

void Avatar::finishedmovingUpdate() {
Moving::moveToCellCorner(AvatarStandingCorner::CENTER);
m_Transform.translation = getCurrentLocation();

void Avatar::DrawAvatar() {
m_Transform.translation = getMoving()->getCurrentLocation();
if (m_AvatarOrder == AvatarOrderType::OPEN_FIRE) {
this->SetDrawable(std::make_shared<Util::Image>(
"../assets/sprites/mech_open_fire.png"));
} else if (m_AvatarOrder == AvatarOrderType::TAKEN_DAMAGE) {
this->SetDrawable(std::make_shared<Util::Image>(
"../assets/sprites/mech_taken_damage.png"));
} else {
this->SetDrawable(
std::make_shared<Util::Image>("../assets/sprites/mech_single.png"));
}
Draw();
}
11 changes: 5 additions & 6 deletions src/Avatar/Hunter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
#include "Avatar/Hunter.hpp"

void Hunter::customizeUpdate() {
glm::vec2 targetCell = m_target->getCurrentCell();
glm::vec2 targetCell = m_target->getMoving()->getCurrentCell();
if (getDistance(targetCell) > ATTACK_RANGE - 1 &&
lastTargetCell != m_target->getCurrentCell()) {
lastTargetCell != m_target->getMoving()->getCurrentCell()) {
// glm::vec2 nextCell =
// getNextCellByCurrent(getDirByRelativeCells(getCurrentCell(),targetCell),getCurrentCell());
setDestinationCell(m_target->getCurrentCell());
lastTargetCell = m_target->getCurrentCell();

lastTargetCell = m_target->getMoving()->getCurrentCell();
// setNewDestination(nextCell);
} else if (getDistance(targetCell) < ATTACK_RANGE - 1) {
setDestinationCell(getCurrentCell());
lastTargetCell = getCurrentCell();
lastTargetCell = getMoving()->getCurrentCell();
attack(m_target);
}
}
22 changes: 11 additions & 11 deletions src/Avatar/Runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@
void Runner::setBeingChase(std::shared_ptr<Avatar> hunter) {
b_beingChase = true;
m_hunter = hunter;
lastTargetCell = getCurrentCell();
lastTargetCell = m_Moving->getCurrentCell();
}
void Runner::customizeUpdate() {
if (b_beingChase &&
*m_hunter->getHealth()->getLivingStatus() == LivingStatus::ALIVE) {
glm::vec2 hunterCell = m_hunter->getCurrentCell();
glm::vec2 hunterCell = m_hunter->getMoving()->getCurrentCell();
if (getDistance(hunterCell) <= ATTACK_RANGE - 1 &&
lastTargetCell == getCurrentCell()) {
lastTargetCell == getMoving()->getCurrentCell()) {
edgeCount = 0;
MoveDirection Dir = oppositeDir(PathUtility::getDirByRelativeCells(
getCurrentCell(), hunterCell),
runMode::LIDL_RANDOM);
MoveDirection Dir =
oppositeDir(PathUtility::getDirByRelativeCells(
getMoving()->getCurrentCell(), hunterCell),
runMode::LIDL_RANDOM);
DEBUG_printCurrentMoveDirection(Dir);
glm::vec2 nextCell =
getNextCellByCurrentPlus3(Dir, getCurrentCell(), 3, 1);
glm::vec2 nextCell = getNextCellByCurrentPlus3(
Dir, getMoving()->getCurrentCell(), 3, 1);
while (nextCell.x < 0 || nextCell.y < 0) {
edgeCount += rand() % 2 + 1;
Dir = findNewDir(Dir, edgeCount);
DEBUG_printCurrentMoveDirection(Dir);
nextCell =
getNextCellByCurrentPlus3(Dir, getCurrentCell(), 1, 3);
nextCell = getNextCellByCurrentPlus3(
Dir, getMoving()->getCurrentCell(), 1, 3);
}
lastTargetCell = nextCell;
setDestinationCell(nextCell);
}
} else {
b_beingChase = false;
Expand Down
Loading

0 comments on commit f4d2a4c

Please sign in to comment.