Skip to content

Commit

Permalink
avoid obstacles
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed Mar 23, 2024
1 parent daf9562 commit 1b11f67
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 223 deletions.
3 changes: 3 additions & 0 deletions include/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class MapClass : public Core::Drawable {
}
return m_Map[position.x][position.y];
}
bool getWalkable(glm::vec2 position) {
return getTileByCellPosition(position)->getWalkable();
}

static void setTileByCellPosition(glm::vec2 position,
std::shared_ptr<TileClass> tile) {
Expand Down
9 changes: 2 additions & 7 deletions include/Scene/DefaultScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include "Structure/WayPoint.hpp"
#include "UI/UI.hpp"
#include "Unit/Avatar.hpp"
#include "Unit/FindValidPathToDest.hpp"
#include "Unit/PathfindingUnit.hpp"
#include "Unit/WayPointUnit.hpp"
#include "Util/Image.hpp"
#include "Util/Input.hpp"
#include "Util/Keycode.hpp"
Expand All @@ -48,15 +48,10 @@ class DefaultScene {
GameObjectManager m_GameObjectManager;
Util::Renderer m_Renderer;
UIClass m_UI;
MapClass m_Map;
std::map<int, TileClass> m_tileSets;
std::vector<int> m_OgMap;
std::shared_ptr<SpriteSheet> m_TileSetSpriteSheet =
std::make_shared<SpriteSheet>();
std::shared_ptr<MapClass> m_Map = std::make_shared<MapClass>();
GameObjectManager m_Manager;

Avatar m_dummy;
WayPointUnit m_waypointUnit;
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_DEFAULTSCENE_HPP
16 changes: 4 additions & 12 deletions include/Tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,22 @@ class TileClass {
: m_Name(name),
m_Buildable(buildable),
m_Walkable(walkable),
m_Clickable(clickable),
m_SpriteSheetIndex(spritesheetindex) {}
m_Clickable(clickable) {}
TileClass()
: m_Name(""),
m_Buildable(false),
m_Walkable(false),
m_Clickable(true),
m_SpriteSheetIndex(0) {}
m_Buildable(true),
m_Walkable(true),
m_Clickable(true) {}
~TileClass() {}

bool getWalkable() { return m_Walkable; };
bool getBuildable() { return m_Buildable; };
bool getClickable() { return m_Clickable; };
bool getSpriteSheetIndex() { return m_SpriteSheetIndex; };

void setWalkable(bool value) { m_Walkable = value; };
void setBuildable(bool value) { m_Buildable = value; };
void setClickable(bool value) { m_Clickable = value; };

void setSpriteSheetIndex(unsigned int spritesheetindex) {
m_SpriteSheetIndex = spritesheetindex;
};
/*
bool operator==(const TileClass &tile) const {
if (tile.m_Name == m_Name && m_Walkable == tile.m_Walkable &&
Expand All @@ -57,7 +51,6 @@ class TileClass {
TileClass &operator=(const TileClass &tile) {
this->m_Walkable = tile.m_Walkable;
this->m_Clickable = tile.m_Clickable;
this->m_SpriteSheetIndex = tile.m_SpriteSheetIndex;
this->m_Buildable = tile.m_Buildable;
this->m_Name = tile.m_Name;
return *this;
Expand All @@ -67,7 +60,6 @@ class TileClass {
bool m_Walkable;
bool m_Buildable;
bool m_Clickable;
unsigned int m_SpriteSheetIndex;
std::string m_Name;
};

Expand Down
17 changes: 9 additions & 8 deletions include/Unit/Avatar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,29 @@

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_DUMMY_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_DUMMY_HPP
#include "Unit/FindValidPathToDest.hpp"
#include "Unit/PathfindingUnit.hpp"
#include "Unit/WayPointUnit.hpp"

class Avatar : public PathfindingUnit {
private:
WayPointUnit m_wayPointUnit;
FindValidPathToDest m_wayPointUnit;
bool b_SelectedByCursor = true;
bool b_justStarted = true;

public:
Avatar()
: PathfindingUnit(){};
Avatar(){};
~Avatar() override{};

virtual void Start(glm::vec2 destination) { // destination = Barrack's
// waypointLocation
virtual void
Start(glm::vec2 destination,
std::shared_ptr<MapClass> map) { // destination = Barrack's
// waypointLocation
// setCurrentCell() //CurrentCell = Structure's Location
this->SetDrawable(
std::make_unique<Util::Image>("../assets/sprites/capybara.png"));
SetVisible(true);
m_grid.SetActivate(true);

m_wayPointUnit.Start(map);
setCurrentCell(destination);
setNextCell(destination);
m_wayPointUnit.setCurrentCell(destination);
Expand All @@ -40,7 +41,7 @@ class Avatar : public PathfindingUnit {
setCurrentCell(
MapClass::GlobalCoordToCellCoord(getCurrentLocation()));
setCurrentDir(m_wayPointUnit.getFirstCellDir());
UpdateNextCell();
setNextCell(UpdateCellByDir(getCurrentDir(), getNextCell()));
printf("(aliveUpdate) getting new dir\n");
}
m_wayPointUnit.Update();
Expand Down
215 changes: 215 additions & 0 deletions include/Unit/FindValidPathToDest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
//
// Created by nudle on 2024/3/15.
//

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_FINDVALIDPATHTODEST_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_FINDVALIDPATHTODEST_HPP
#include "Unit/PathfindingUnit.hpp"
#include <random>
class FindValidPathToDest : public PathfindingUnit {
private:
std::shared_ptr<MapClass> m_Map = std::make_shared<MapClass>();
std::deque<MoveDirection> m_dirQue;
bool b_InitNewLine = false;

public:
enum class Side { R, L };
FindValidPathToDest()
: PathfindingUnit() {
setMovementSpeed(48);
};
virtual ~FindValidPathToDest() override{};
void Start(std::shared_ptr<MapClass> map) { m_Map = map; }
MoveDirection getFirstCellDir() {
if (!m_dirQue.empty()) {
MoveDirection front = m_dirQue.front();
m_dirQue.pop_front();
return front;
}

return MoveDirection::IDLE;
}

void resetQueue() {
b_InitNewLine = true;
this->m_dirQue.clear();
this->m_lineVector.clear();
}
Side randomlyChooseSide() {
// Create a random number generator engine
std::random_device rd; // Obtain a random seed from the hardware
std::mt19937 gen(rd()); // Initialize the Mersenne Twister
// pseudo-random number generator

// Define a distribution (uniform distribution in this case)
std::uniform_int_distribution<int> distribution(
0, 1); // Uniform distribution between 1 and 6 (inclusive)

// Generate a random number
int random_number = distribution(gen); // Generate a random number using
// the generator and distribution

switch (random_number) {
case 0:
return Side::R;
case 1:
return Side ::L;
}
}

MoveDirection getDirIfObstacle(MoveDirection nextcell, Side side) {

// please be notice that the "Side's" R or L represent its facing
// direction
MoveDirection newdir;
switch (nextcell) {
case MoveDirection::RIGHT: {
if (side == Side::R) {
newdir = MoveDirection::DOWN_RIGHT;
} else if (side == Side::L) {
newdir = MoveDirection::UP_RIGHT;
}
} break;

case MoveDirection::LEFT: {
if (side == Side::R) {
newdir = MoveDirection::UP_LEFT;
} else if (side == Side::L) {
newdir = MoveDirection::DOWN_LEFT;
}
break;
}
case MoveDirection::UP: {
if (side == Side::R) {
newdir = MoveDirection::UP_RIGHT;
} else if (side == Side::L) {
newdir = MoveDirection::UP_LEFT;
}
break;
}
case MoveDirection::DOWN: {
if (side == Side::R) {
newdir = MoveDirection::DOWN_LEFT;
} else if (side == Side::L) {
newdir = MoveDirection::DOWN_RIGHT;
}
break;
}
case MoveDirection::UP_RIGHT: {
if (side == Side::R) {
newdir = MoveDirection::RIGHT;
} else if (side == Side::L) {
newdir = MoveDirection::UP;
}
break;
}
case MoveDirection::DOWN_LEFT: {
if (side == Side::R) {
newdir = MoveDirection::LEFT;
} else if (side == Side::L) {
newdir = MoveDirection::DOWN;
}
break;
}
case MoveDirection::DOWN_RIGHT: {
if (side == Side::R) {
newdir = MoveDirection::DOWN;
} else if (side == Side::L) {
newdir = MoveDirection::RIGHT;
}
break;
}
case MoveDirection::UP_LEFT: {
if (side == Side::R) {
newdir = MoveDirection::UP;
} else if (side == Side::L) {
newdir = MoveDirection::LEFT;
}
break;
}
case MoveDirection::IDLE: {
printf("Direction debug didn't move\n");
break;
}

return newdir;
}
}

void findPath(glm::vec2 destination) {
// what if desintation is not walkable
setDestinationCell(destination);

while (getCurrentCell() != getDestinationCell()) {

if (m_Map
->getTileByCellPosition(
glm::vec2(getNextCell().x, getNextCell().y))
->getWalkable()) {
// move current to next cell
setCurrentCell(getNextCell());
m_dirQue.push_back(getCurrentDir());
setCurrentDir(
getNextCellDir(getCurrentCell(), getDestinationCell()));

// set next
setNextCell(UpdateCellByDir(getCurrentDir(), getNextCell()));

m_lineVector.push_back(
Line(MapClass::CellCoordToGlobal(getCurrentCell()),
MapClass::CellCoordToGlobal(getNextCell())));
} else {
setCurrentDir(getDirIfObstacle(
getNextCellDir(getCurrentCell(), getDestinationCell()),
randomlyChooseSide()));
setNextCell(UpdateCellByDir(getCurrentDir(), getNextCell()));
// not walkable
// select one direction (left or right) using one of
// several heuristics advance in the said direction
// keeping your left/right hand touching the obstacle's
// wall when you can advance in a straight line towards
// the target again, do so

// getNextCellObstacle(randomlyChooseSide(),
// nextCellClear);
}
}
/*
while (getCurrentCell().x != getDestinationCell().x &&
getCurrentCell().y != getDestinationCell().y) {
// printf("(FindValidPathToDest)destination :
{%.0f,%.0f}\n", getDestinationCell().x, getDestinationCell().y);
// printf(
"(FindValidPathToDest)finding : %d\n "
"(FindValidPathToDest)Cell "
"now "
":{%.0f,%.0f}\n(FindValidPathToDest)Cell next : "
"{%.0f,%.0f}\n",
// count++, getCurrentCell().x,
getCurrentCell().y,
// getNextCell().x, getNextCell().y);
setCurrentCell(getNextCell());
m_dirQue.push_back(getCurrentDir());
findNextCellDir();
UpdateNextCell();
m_lineVector.push_back(
Line(MapClass::CellCoordToGlobal(getCurrentCell()),
MapClass::CellCoordToGlobal(getNextCell())));
}
*/
// draw lines
if (b_InitNewLine && getCurrentCell().x == getDestinationCell().x &&
getCurrentCell().y == getDestinationCell().y) {
b_InitNewLine = false;
if (!m_lineVector.empty()) {
m_grid.Start(m_lineVector);
}
}
m_grid.SetActivate(true);
}
virtual void Update() override {
m_grid.DrawUsingCamera(m_emptyTrans, defaultZIndex);
}
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_FINDVALIDPATHTODEST_HPP
14 changes: 6 additions & 8 deletions include/Unit/PathfindingUnit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ class PathfindingUnit : public Util::GameObject {

MoveDirection m_currentDir = MoveDirection::IDLE;

float m_Hp = 100.F;
float m_MovementSpeed = 1.F;
float m_Armor = 5.f;

int moveDistance = 0;

Expand All @@ -56,8 +54,7 @@ class PathfindingUnit : public Util::GameObject {
m_ZIndex = defaultZIndex;
};
virtual ~PathfindingUnit(){};
// Set Unit
void setHp(float hp) { m_Hp = hp; }

//

void setDestinationCell(int x, int y) {
Expand All @@ -84,10 +81,11 @@ class PathfindingUnit : public Util::GameObject {
MoveDirection getCurrentDir() { return m_currentDir; }
void setCurrentDir(MoveDirection direction) { m_currentDir = direction; }

void findNextCellDir();
void findNextCellDir(MoveDirection lastDir, int times);
void UpdateNextCell();
bool UpdateNextCell(int *times);
MoveDirection getNextCellDir(glm::vec2 currentcell,
glm::vec2 destinationcell);
// void findNextCellDir(MoveDirection lastDir, int times);
glm::vec2 UpdateCellByDir(MoveDirection currentdir, glm::vec2 nextcell);
// bool UpdateNextCell(int *times);

bool walkTowardNextCell();

Expand Down
Loading

0 comments on commit 1b11f67

Please sign in to comment.