Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge2 #65

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ set(SRC_FILES
${SRC_DIR}/Avatar/Avatar.cpp
${SRC_DIR}/Avatar/Hunter.cpp
${SRC_DIR}/Avatar/PathUtility.cpp
${SRC_DIR}/Avatar/PathFindingUnit.cpp
${SRC_DIR}/Avatar/Moving.cpp

${SRC_DIR}/Scene/DefaultScene.cpp
${SRC_DIR}/Camera.cpp
Expand All @@ -113,7 +113,7 @@ set(SRC_FILES
${SRC_DIR}/Map/MapbinReader.cpp
${SRC_DIR}/Map/Map.cpp

${SRC_DIR}/Mechanics/FindValidPathToDest.cpp
${SRC_DIR}/Mechanics/AvatarNavigator.cpp
${SRC_DIR}/Mechanics/GameObjectID.cpp
${SRC_DIR}/Mechanics/CursorSelection.cpp
${SRC_DIR}/Mechanics/AvatarManager.cpp
Expand Down Expand Up @@ -201,7 +201,7 @@ set(INCLUDE_FILES
${INCLUDE_DIR}/Structure/WarFactory.hpp
${INCLUDE_DIR}/Structure/ADVPowerPlants.hpp

${INCLUDE_DIR}/Avatar/PathfindingUnit.hpp
${INCLUDE_DIR}/Avatar/Moving.hpp
${INCLUDE_DIR}/Avatar/Avatar.hpp
${INCLUDE_DIR}/Avatar/WayPointUnit.hpp
${INCLUDE_DIR}/Avatar/Runner.hpp
Expand Down
8 changes: 7 additions & 1 deletion include/Avatar/Avatar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class Avatar : public Moving,
~Avatar() override{};

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

Expand Down Expand Up @@ -64,6 +66,10 @@ class Avatar : public Moving,
m_Health = health;
}

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

protected:
std::shared_ptr<SpriteSheet> m_AvatarSpriteSheet =
std::make_shared<SpriteSheet>();
Expand Down
13 changes: 4 additions & 9 deletions include/Avatar/Moving.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class Moving {
MoveDirection m_CurrentDir = MoveDirection::IDLE;

float m_MovementSpeed = 1.F;
int m_MoveDistance = 0;

bool b_NewDestinationIsSetted = false;
glm::vec2 m_PrevCell;

public:
enum class AvatarStandingCorner {
Expand Down Expand Up @@ -56,16 +54,13 @@ class Moving {

void setMovePath(std::deque<MoveDirection> movepath) {
m_MovePath = movepath;
setNewDestinationIsSetted(false);
m_CurrentDir = m_MovePath.front();
m_MovePath.pop_front();
m_PrevCell = getCurrentCell();
}

bool ifNewDestionationIsSetted() { return b_NewDestinationIsSetted; }

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

void setNewDestinationIsSetted(bool value) {
b_NewDestinationIsSetted = value;
}
glm::vec2 getDestinationCell() {
return MapUtil::CellCoordToGlobal(m_DestinationLocation);
}
Expand Down
3 changes: 2 additions & 1 deletion include/Map/Tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class TileClass {
std::vector<std::shared_ptr<Avatar>> getAvatars() { return m_Avatars; }
std::shared_ptr<Structure> getStructure() { return m_Structure; }
bool ifStrucutreExists() {
if (m_Structure == std::make_shared<Structure>()) {
if (*m_Structure->getHealth()->getLivingStatus() ==
LivingStatus::NOT_BORN_YET) {
return false;
} else {
return true;
Expand Down
2 changes: 1 addition & 1 deletion include/Mechanics/NemesisManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class NemesisManager {
if (ifNemesisWithinWeaponRange(hunter)) {
hunter->setAvatarOrder(AvatarOrderType::OPEN_FIRE);
prey->setAvatarOrder(AvatarOrderType::TAKEN_DAMAGE);
hunter->openFireToTarget(prey);
hunter->getAttackAndDamager()->openFireToTarget(prey);
}

if (*pair.second->getHealth()->getLivingStatus() ==
Expand Down
32 changes: 23 additions & 9 deletions src/Avatar/Avatar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,53 @@ void Avatar::Update() {

case (LivingStatus::ALIVE):
whenSelected();
aliveUpdate();

if (m_AvatarOrder == AvatarOrderType::OPEN_FIRE) {

} else if (m_AvatarOrder == AvatarOrderType::MOVE) {

moveUpdate();
} else if (m_AvatarOrder == AvatarOrderType::NO_ORDER) {
noOrderUpdate();
} else if (m_AvatarOrder == AvatarOrderType::TAKEN_DAMAGE) {
} else if (m_AvatarOrder == AvatarOrderType::SPAWNED) {
spawnedUpdate();
}

break;
}
}

void Avatar::aliveUpdate() {
moveToNextCell();
void Avatar::noOrderUpdate() {
m_CurrentDir = MoveDirection::IDLE;
SetVisible(true);
m_Transform.translation = getCurrentLocation();

Draw();
}

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

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

if (ifArrivedAtNextCell()) {
if (m_MovePath.size() >= 1) {
m_CurrentDir = m_MovePath.front();
m_MovePath.pop_front();
} else {
m_CurrentDir = MoveDirection::IDLE;
m_AvatarOrder = AvatarOrderType::NO_ORDER;
}
}
moveToNextCell();

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

Draw();

printf("Avatar cell={%d,%d}\n", getCurrentLocation().x,
getCurrentLocation().y);
printf("-----------avatar------------------\n");
}

void Avatar::Start(glm::vec2 spawnlocationcell) { // destination = Barrack's
Expand All @@ -58,7 +72,7 @@ void Avatar::Start(glm::vec2 spawnlocationcell) { // destination = Barrack's
// setSpriteSheet();
SetVisible(true);
setMovementSpeed(4);
m_AvatarOrder = AvatarOrderType::MOVE;
m_AvatarOrder = AvatarOrderType::SPAWNED;
m_CurrentLocation = MapUtil::CellCoordToGlobal(spawnlocationcell);
m_Transform.scale = {1, 1};
getHealth()->setLivingStatus(
Expand Down
12 changes: 8 additions & 4 deletions src/Avatar/PathFindingUnit.cpp → src/Avatar/Moving.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ void Moving::moveToNextCell() {

break;
}
case MoveDirection::IDLE: {
return;
}
}
}

bool Moving::ifArrivedAtNextCell() {
// change this to check
// previous cell
// getCurrentCell() != previousCell()
return false;
if (m_PrevCell != getCurrentCell()) {
return true;
} else {
return false;
}
}
void Moving::moveToCellCorner(AvatarStandingCorner corner) {
float quaterHeight = CELL_SIZE.y / 4;
Expand Down
41 changes: 23 additions & 18 deletions src/Mechanics/AvatarManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,37 @@ void AvatarManager::Update() {
}

void AvatarManager::giveOrderToAvatar(std::shared_ptr<Avatar> unit) {
if (Util::Input::IsKeyDown(Util::Keycode::MOUSE_RB)) {
auto dest = Util::Input::GetCursorPosition();
auto queue = m_Navigator->findPath(
unit->getCurrentCell(), MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(dest)));
// unit
unit->setMovePath(queue);

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

if (m_Map->getTileByCellPosition(unit->getDestinationCell())
->ifEnemyAtTile()) {
m_NemesisManager->addNemesis(
unit, m_Map->getTileByCellPosition(unit->getDestinationCell())
->getAvatars()[0]);
} else {
unit->setAvatarOrder(AvatarOrderType::MOVE);
if (m_Map->getTileByCellPosition(unit->getDestinationCell())
->ifEnemyAtTile()) {
m_NemesisManager->addNemesis(
unit, m_Map->getTileByCellPosition(unit->getDestinationCell())
->getAvatars()[0]);
} else {
unit->setAvatarOrder(AvatarOrderType::MOVE);
}
}
}

void AvatarManager::updateTileWhileAvatarMoving(std::shared_ptr<Avatar> unit) {
if (unit->ifArrivedAtNextCell()) {
m_Map->removeAvatarsByCellPosition(unit, unitArrayAndLocation[unit]);
m_Map->setAvatarByCellPosition(unit, unit->getCurrentCell());
unitArrayAndLocation[unit] = unit->getCurrentCell();
void AvatarManager::updateTileWhileAvatarMoving(
std::shared_ptr<Avatar> avatar) {
if (avatar->ifArrivedAtNextCell()) {
m_Map->removeAvatarsByCellPosition(avatar,
unitArrayAndLocation[avatar]);
m_Map->setAvatarByCellPosition(avatar, avatar->getCurrentCell());
unitArrayAndLocation[avatar] = avatar->getCurrentCell();
}
}

void AvatarManager::AppendAvatar(std::shared_ptr<Avatar> newAvatar) {
m_AvatarArray.push_back(newAvatar);
unitArrayAndLocation[newAvatar] = newAvatar->getCurrentCell();
m_Map->setAvatarByCellPosition(newAvatar, newAvatar->getCurrentCell());
}
Loading