Skip to content

Commit

Permalink
s
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed Jun 18, 2024
1 parent 86d8bf2 commit 9fee08e
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 18 deletions.
6 changes: 4 additions & 2 deletions include/Avatar/Moving.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class Moving {
UPPER_LEFT,
UPPER_RIGHT,
LOWER_LEFT,
LOWER_RIGHT
LOWER_RIGHT,
FILLED
};

Moving(){};
Expand Down Expand Up @@ -72,7 +73,7 @@ class Moving {
void moveToNextCell();
void moveToCellCorner(AvatarStandingCorner corner);
bool ifArrivedAtNextCell();

int getMovePathSize() { return m_MovePath.size(); }
void setMovePath(std::deque<MoveDirection> movepath) {
if (movepath.empty()) {
m_MovePath = {MoveDirection::IDLE};
Expand All @@ -82,6 +83,7 @@ class Moving {
m_CurrentDir = m_MovePath.front();
}
m_PrevCell = getCurrentCell();
setStandingCorner(0);
}

bool ifMovePathEmpty() {
Expand Down
10 changes: 7 additions & 3 deletions include/Map/Tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ class TileClass {
m_Structure = structure;
}
void pushAvatars(std::shared_ptr<Avatar> avatar) {
avatar->getMoving()->setStandingCorner(m_Avatars.size());
m_Avatars.push_back(avatar);
if (m_Avatars.size() > 4) {
avatar->getMoving()->setStandingCorner(5);
} else {
avatar->getMoving()->setStandingCorner(m_Avatars.size());
m_Avatars.push_back(avatar);
}

// if (m_Avatars.size() >= 5) {
// setWalkable(false);
//}
Expand All @@ -75,7 +80,6 @@ class TileClass {
m_Avatars.erase(it);
i--;


// if (m_Avatars.size() < 4 && m_TerrainBuildable) {
// setWalkable(true);
// }
Expand Down
15 changes: 8 additions & 7 deletions include/Mechanics/AvatarManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,24 @@ class AvatarManager {
void assignAttackOrderToAvatar(std::shared_ptr<Avatar> unit,
glm::vec2 destcell);
void assignAttackOrderToAvatar(std::shared_ptr<Avatar> avatar,
glm::vec2 destcell,HouseType myHouse);
int getAvatarSize(){
return static_cast<int>(m_AvatarArray.size());
}
bool ifAvatarHasNemesis(std::shared_ptr<Avatar> unit){
glm::vec2 destcell, HouseType myHouse);
int getAvatarSize() { return static_cast<int>(m_AvatarArray.size()); }
bool ifAvatarHasNemesis(std::shared_ptr<Avatar> unit) {
return m_NemesisManager->ifAvatarHasNemesis(unit);
}
bool ifAvatarHasHunter(std::shared_ptr<Avatar> unit){
bool ifAvatarHasHunter(std::shared_ptr<Avatar> unit) {
return m_NemesisManager->ifAvatarHasNemesis(unit);
}
glm::vec2 getAvatarNemesisCell(std::shared_ptr<Avatar> unit){
glm::vec2 getAvatarNemesisCell(std::shared_ptr<Avatar> unit) {
return m_NemesisManager->getNemesisCell(unit);
}

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

void findNearestStandableCellWhenFilled(std::shared_ptr<Avatar> avatar);

private:
std::vector<std::shared_ptr<Avatar>> m_AvatarArray;
std::unordered_map<std::shared_ptr<Avatar>, glm::vec2> unitArrayAndLocation;
Expand Down
6 changes: 4 additions & 2 deletions src/Avatar/Moving.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ bool Moving::ifArrivedAtNextCell() {
void Moving::moveToCellCorner(AvatarStandingCorner corner) {
float quaterHeight = CELL_SIZE.y / 4;
float quaterWidth = CELL_SIZE.x / 4;
m_CurrentLocation = MapUtil::CellCoordToGlobal(
MapUtil::GlobalCoordToCellCoord(m_CurrentLocation));
switch (corner) {
case (AvatarStandingCorner::CENTER):
m_CurrentLocation = MapUtil::CellCoordToGlobal(
MapUtil::GlobalCoordToCellCoord(m_CurrentLocation));
break;
case (AvatarStandingCorner::FILLED):
break;
case (AvatarStandingCorner::UPPER_LEFT):
m_CurrentLocation = {m_CurrentLocation.x - quaterWidth,
Expand Down
2 changes: 1 addition & 1 deletion src/Map/TerrainConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ std::unordered_map<std::string, std::shared_ptr<TileClass>>
{"Ore", std::make_shared<TileClass>(UnitType::OVERLAY_ORE, 0, 1, 1)},
{"River", std::make_shared<TileClass>(UnitType::TILE_RIVER, 0, 0, 0)},
{"Road", std::make_shared<TileClass>(UnitType::TILE_ROAD, 1, 1, 1)},
{"Rock", std::make_shared<TileClass>(UnitType::TILE_ROCK, 0, 1, 1)},
{"Rock", std::make_shared<TileClass>(UnitType::TILE_ROCK, 0, 0, 1)},
{"Rough", std::make_shared<TileClass>(UnitType::TILE_ROUGH, 0, 1, 1)},
{"Tree", std::make_shared<TileClass>(UnitType::TILE_TREE, 0, 0, 1)},
{"Water", std::make_shared<TileClass>(UnitType::TILE_WATER, 0, 0, 0)}};
21 changes: 21 additions & 0 deletions src/Mechanics/AvatarManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,27 @@ void AvatarManager::updateTileWhileAvatarMoving(
m_Map->setAvatarByCellPosition(avatar,
avatar->getMoving()->getCurrentCell());
unitArrayAndLocation[avatar] = avatar->getMoving()->getCurrentCell();
if (avatar->getMoving()->getMovePathSize() == 1) {
findNearestStandableCellWhenFilled(avatar);
}
}
}

void AvatarManager::findNearestStandableCellWhenFilled(
std::shared_ptr<Avatar> avatar) {
if (avatar->getMoving()->getStandingCorner() == 5) {
auto cell = avatar->getMoving()->getCurrentCell();
bool c = false;
while (m_Map->getTileByCellPosition(cell)->getAvatars().size() >= 5 &&
cell.x >= 0 && cell.y >= 0) {
if (c) {
cell.x -= 1;
} else {
cell.y -= 1;
}
c = !(c);
}
assignMoveOrderToAvatar(avatar, cell);
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/Mechanics/AvatarNavigator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ std::deque<MoveDirection> AvatarNavigator::findPath(glm::vec2 currentcell,
return dirQueComp[0];
}


for (int s = 0; s < 2; s++) {
Side whichSideToTouchObstacle = static_cast<Side>(s);
int count = 0;
Expand Down Expand Up @@ -56,7 +55,6 @@ std::deque<MoveDirection> AvatarNavigator::findPath(glm::vec2 currentcell,
dirQueComp[s].push_back(i);
currentcell =
PathUtility::getNextCellByCurrent(i, currentcell);

}
}
}
Expand Down Expand Up @@ -320,7 +318,7 @@ bool AvatarNavigator::findStraightPath(glm::vec2 currentcell,
return ifwalked;
}
}
if (path->size() > 1) {
if (path->size() > 1 || currentcell == destinationcell) {
return ifwalked;
} else {
return false;
Expand Down

0 comments on commit 9fee08e

Please sign in to comment.