diff --git a/CMakeLists.txt b/CMakeLists.txt index ad99b5d1..b5eaac3f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,6 +84,7 @@ set(SRC_FILES ${SRC_DIR}/Unit/PathfindingUnit.cpp + ${SRC_DIR}/Scene/DefaultScene.cpp ${SRC_DIR}/Camera.cpp ${SRC_DIR}/SpriteSheet.cpp @@ -159,7 +160,7 @@ set(INCLUDE_FILES ${INCLUDE_DIR}/Structure/ADVPowerPlants.hpp ${INCLUDE_DIR}/Unit/PathfindingUnit.hpp - ${INCLUDE_DIR}/Unit/Dummy.hpp + ${INCLUDE_DIR}/Unit/Avatar.hpp ${INCLUDE_DIR}/Unit/WayPointUnit.hpp ${INCLUDE_DIR}/imgui/imconfig.h diff --git a/include/Grid.hpp b/include/Grid.hpp index 5541edb4..9133a600 100644 --- a/include/Grid.hpp +++ b/include/Grid.hpp @@ -10,6 +10,7 @@ #include "Core/VertexArray.hpp" #include "Line.hpp" #include "Util/TransformUtils.hpp" +#include class Grid : public Core::Drawable { @@ -18,6 +19,7 @@ class Grid : public Core::Drawable { ~Grid(){}; void InitVertexAndColor(); void Start(std::vector lineVector); + void queStart(std::deque lineVector); void Draw(const Util::Transform &transform, const float zIndex) override; void DrawUsingCamera(const Util::Transform &transform, const float zIndex) override; diff --git a/include/Scene/DefaultScene.hpp b/include/Scene/DefaultScene.hpp index e9465022..13e41864 100644 --- a/include/Scene/DefaultScene.hpp +++ b/include/Scene/DefaultScene.hpp @@ -22,7 +22,7 @@ #include "Structure/WarFactory.hpp" #include "Structure/WayPoint.hpp" #include "UI/UI.hpp" -#include "Unit/Dummy.hpp" +#include "Unit/Avatar.hpp" #include "Unit/PathfindingUnit.hpp" #include "Unit/WayPointUnit.hpp" #include "Util/Image.hpp" diff --git a/include/Unit/Avatar.hpp b/include/Unit/Avatar.hpp new file mode 100644 index 00000000..a154a19b --- /dev/null +++ b/include/Unit/Avatar.hpp @@ -0,0 +1,70 @@ +// +// Created by nudle on 2024/3/15. +// + +#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_DUMMY_HPP +#define PRACTICALTOOLSFORSIMPLEDESIGN_DUMMY_HPP +#include "Unit/PathfindingUnit.hpp" +#include "Unit/WayPointUnit.hpp" + +class Avatar:public PathfindingUnit{ +private: + WayPointUnit m_wayPointUnit; + long count=0; + bool b_selectedNewTarget; +public: + void Start(glm::vec2 target){ //target = Barrack's waypointLocation + //setCurrentCell() //CurrentCell = Structure's Location + this->SetDrawable(std::make_unique("../assets/sprites/capybara.png")); + setTargetCell(target.x,target.y); + SetVisible(true); + m_grid.SetActivate(true); + + m_wayPointUnit.setCurrentCell(getCurrentCell()); + m_wayPointUnit.setNextCell(getCurrentCell()); + m_wayPointUnit.SetVisible(true); + m_wayPointUnit.findPath(getTargetCell().x,getTargetCell().y); + + setMovementSpeed(4); + setNextCell(m_wayPointUnit.getFirstCell()); + setCurrentDir(m_wayPointUnit.getFirstCellDir()); + } + virtual void Update()override{ + m_wayPointUnit.findPath(getTargetCell().x,getTargetCell().y); + if(Walk()){ + glm::vec2 nextCell = m_wayPointUnit.getFirstCell(); + if(nextCell.x!=-1&&nextCell.y!=-1) { + setCurrentCell(getNextCell()); // reset object location + setNextCell(nextCell); + m_wayPointUnit.setCurrentCell(getCurrentCell()); + setCurrentDir(m_wayPointUnit.getFirstCellDir()); + } + }else{//m_grid.Start({Line(getCurrentLocation(),MapClass::CellCoordToGlobal(getNextCell()))}); + } + m_grid.DrawUsingCamera(m_emptyTrans,defaultZIndex); + m_wayPointUnit.Update(); + m_Transform.translation=getCurrentLocation(); + Draw(); + onSelect(true); + } + void setNewTarget(glm::vec2 target){ + setTargetCell(target.x,target.y); + m_wayPointUnit.resetQueue(); + m_wayPointUnit.setCurrentCell(getNextCell()); + m_wayPointUnit.setNextCell(getNextCell()); + m_wayPointUnit.findPath(getTargetCell().x,getTargetCell().y); + } + void onSelect(bool selected){ + if (b_selectedNewTarget) { + this->setNewTarget( + MapClass::GlobalCoordToCellCoord(MapClass::ScreenToGlobalCoord(Util::Input::GetCursorPosition()))); + b_selectedNewTarget = false; + } + if (selected) { + if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_RB)) { + b_selectedNewTarget = true; + } + } + } +}; +#endif // PRACTICALTOOLSFORSIMPLEDESIGN_DUMMY_HPP diff --git a/include/Unit/Dummy.hpp b/include/Unit/Dummy.hpp deleted file mode 100644 index 988b0f74..00000000 --- a/include/Unit/Dummy.hpp +++ /dev/null @@ -1,40 +0,0 @@ -// -// Created by nudle on 2024/3/15. -// - -#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_DUMMY_HPP -#define PRACTICALTOOLSFORSIMPLEDESIGN_DUMMY_HPP -#include "Unit/PathfindingUnit.hpp" -#include "Unit/WayPointUnit.hpp" - -class Dummy:public PathfindingUnit{ -private: - WayPointUnit m_wayPointUnit; - long count=0; -public: - virtual void Start()override{ - this->SetDrawable(std::make_unique("../assets/sprites/capybara.png")); - m_Transform.scale={0.2,0.2}; - setCurrentCell({5,5}); - //setCurrentLocation(MapClass::CellCoordToGlobal(getCurrentCell())); - SetVisible(true); - m_grid.SetActivate(true); - - //m_wayPointUnit.setCurrentLocation(getCurrentLocation()); - m_wayPointUnit.setCurrentCell(getCurrentCell()); - m_wayPointUnit.SetVisible(true); - - //setTargetCell({20,20}); - //m_wayPointUnit.setTargetCell(getTargetCell()); - //m_wayPointUnit.findPath(getTargetCell()); - } - virtual void Update()override{ - Walk(); - m_grid.Start({Line(getCurrentLocation(),MapClass::CellCoordToGlobal(m_wayPointUnit.getFirstCell()))}); - m_grid.Draw(m_emptyTrans,defaultZIndex); - m_wayPointUnit.Update(); - m_Transform.translation=getCurrentLocation(); - - } -}; -#endif // PRACTICALTOOLSFORSIMPLEDESIGN_DUMMY_HPP diff --git a/include/Unit/PathfindingUnit.hpp b/include/Unit/PathfindingUnit.hpp index 3cabf29b..e98cca4d 100644 --- a/include/Unit/PathfindingUnit.hpp +++ b/include/Unit/PathfindingUnit.hpp @@ -13,6 +13,8 @@ #include "Util/GameObject.hpp" #include "Util/Transform.hpp" +#define SPEED 240 + class PathfindingUnit:public Util::GameObject{ protected: enum class UnitMode{ @@ -24,7 +26,7 @@ class PathfindingUnit:public Util::GameObject{ Util::Transform m_emptyTrans; Line m_line; Grid m_grid; - std::vector m_lineVector; + std::deque m_lineVector; float defaultZIndex=15; private: glm::vec2 m_targetCell; @@ -42,20 +44,20 @@ class PathfindingUnit:public Util::GameObject{ int moveDistance = 0; public: PathfindingUnit(){ - m_Transform.scale={1,1}; + m_Transform.scale={0.1,0.1}; m_ZIndex=defaultZIndex; }; virtual ~PathfindingUnit(){}; - void setTargetCell(int x,int y){ - this->m_targetCell={glm::vec2(x,y)}; - } + void setTargetCell(int x,int y){this->m_targetCell={glm::vec2(x,y)};} + void setTargetCell(glm::vec2 cell){this->m_targetCell=cell;} glm::vec2 getTargetCell(){return m_targetCell;} void setCurrentCell(glm::vec2 cell){ this->m_currentCell=glm::vec2(cell); glm::vec2 temp =MapClass::CellCoordToGlobal(m_currentCell); - m_currentLocation={temp.x+CELL_SIZE.x/2,temp.y+CELL_SIZE.y/2}; + //m_currentLocation={temp.x+CELL_SIZE.x/2,temp.y+CELL_SIZE.y/2}; + m_currentLocation={temp.x,temp.y}; } glm::vec2 getCurrentCell(){return m_currentCell;} @@ -66,29 +68,31 @@ class PathfindingUnit:public Util::GameObject{ void setMovementSpeed(int speed){this->m_MovementSpeed=speed;} + 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); void walkTowardNextCell(); - void NextCell(){ - this->m_currentCell=m_nextCell; - UpdateNextCell(); - }; - virtual void Start(){} virtual void Update(){ Walk(); m_Transform.translation=getCurrentLocation(); Draw(); } - virtual void Walk(){ + virtual bool Walk(){ walkTowardNextCell(); if(moveDistance>=48){ - NextCell(); moveDistance = 0; + return true; } + return false; } }; diff --git a/include/Unit/WayPointUnit.hpp b/include/Unit/WayPointUnit.hpp index a5c98400..67a1dfc8 100644 --- a/include/Unit/WayPointUnit.hpp +++ b/include/Unit/WayPointUnit.hpp @@ -9,32 +9,60 @@ class WayPointUnit:public PathfindingUnit{ private: - std::vector m_pathQueue; + std::deque m_dirQue; + std::deque m_pathQueue; int count=0; + int m_gridCount=0; public: WayPointUnit():PathfindingUnit(){setMovementSpeed(48);}; virtual ~WayPointUnit()override{}; - glm::vec2 getFirstCell(){return m_pathQueue.front();} + glm::vec2 getFirstCell(){ + if(m_pathQueue.size()!=0){ + glm::vec2 front = m_pathQueue.front(); + m_pathQueue.pop_front(); + if(m_lineVector.size()!=0){ + m_lineVector.pop_front(); + } + return front; + } + return {-1,-1}; + } + MoveDirection getFirstCellDir(){ + if( m_dirQue.size()!=0){ + MoveDirection front = m_dirQue.front(); + m_dirQue.pop_front(); + return front; + } + return MoveDirection::IDLE; + } - void resetQueue(){this->m_pathQueue.clear();} + void resetQueue(){ + this->m_pathQueue.clear(); + this->m_dirQue.clear(); + this->m_lineVector.clear(); + } void findPath(int x,int y){ setTargetCell(x,y); - while(getCurrentCell()!=getTargetCell()&&count<=50){ + while(getCurrentCell().x!=getTargetCell().x&&getCurrentCell().y!=getTargetCell().y){ printf("target : {%.0f,%.0f}\n",getTargetCell().x,getTargetCell().y); printf("finding : %d\n Cell now :{%.0f,%.0f}\n,Cell next : {%.0f,%.0f}\n",count++,getCurrentCell().x,getCurrentCell().y,getNextCell().x,getNextCell().y); - findNextCellDir(); //find Direction only - NextCell(); //set current=next then use dir to find next - + setCurrentCell(getNextCell()); + m_dirQue.push_back(getCurrentDir()); m_pathQueue.push_back(getCurrentCell()); + findNextCellDir(); + UpdateNextCell(); m_lineVector.push_back(Line(MapClass::CellCoordToGlobal(getCurrentCell()),MapClass::CellCoordToGlobal(getNextCell()))); } m_grid.SetActivate(true); } virtual void Update()override{ - m_grid.Start( m_lineVector); + if(m_gridCount%30==0){ + m_grid.queStart( m_lineVector); + } + m_gridCount++; m_grid.DrawUsingCamera(m_emptyTrans,defaultZIndex); } }; diff --git a/src/Grid.cpp b/src/Grid.cpp index c9fdd9bb..1de80faa 100644 --- a/src/Grid.cpp +++ b/src/Grid.cpp @@ -13,6 +13,13 @@ void Grid::Start(std::vector lineVector) { InitVertexAndColor(); } +void Grid::queStart(std::deque lineQue) { + + // debug grid: + m_lineVector = std::vector (lineQue.begin(),lineQue.end()); + + InitVertexAndColor(); +} void Grid::Draw(const Util::Transform &transform, const float zindex) { // nothing diff --git a/src/Scene/DefaultScene.cpp b/src/Scene/DefaultScene.cpp index 7cc4721e..4ed7112b 100644 --- a/src/Scene/DefaultScene.cpp +++ b/src/Scene/DefaultScene.cpp @@ -28,15 +28,13 @@ void DefaultScene::Start() { m_testdraw.Start(std::vector({glm::vec2(0.F, 0.F)}), DrawOverlays::OverlayShapes::R_CROSS); // m_GameObjectManager.Start(); + m_dummy.setCurrentCell({5,5}); + m_dummy.Start({23,16}); - // m_dummy.Start(); - m_waypointUnit.setCurrentCell({glm::vec2(0, 0)}); - m_waypointUnit.setNextCell({glm::vec2(0, 0)}); - m_waypointUnit.findPath(15, 10); } void DefaultScene::Update() { - // m_dummy.Update(); + m_dummy.Update(); m_waypointUnit.Update(); diff --git a/src/Unit/PathfindingUnit.cpp b/src/Unit/PathfindingUnit.cpp index eab9e22d..13b2ff6f 100644 --- a/src/Unit/PathfindingUnit.cpp +++ b/src/Unit/PathfindingUnit.cpp @@ -38,8 +38,59 @@ void PathfindingUnit::UpdateNextCell(){ } case MoveDirection::IDLE:{ printf("Direction debug didn't move\n"); + break; } } + +} + + +bool PathfindingUnit::UpdateNextCell(int* times){ + switch (m_currentDir) { + case MoveDirection::RIGHT:{ + m_nextCell={m_nextCell.x+1,m_nextCell.y}; + break; + } + case MoveDirection::LEFT:{ + m_nextCell={m_nextCell.x-1,m_nextCell.y}; + break; + } + case MoveDirection::UP:{ + m_nextCell={m_nextCell.x,m_nextCell.y+1}; + break; + } + case MoveDirection::DOWN:{ + m_nextCell={m_nextCell.x,m_nextCell.y-1}; + break; + } + case MoveDirection::UP_RIGHT:{ + m_nextCell={m_nextCell.x+1,m_nextCell.y+1}; + break; + } + case MoveDirection::DOWN_LEFT:{ + m_nextCell={m_nextCell.x-1,m_nextCell.y-1}; + break; + } + case MoveDirection::DOWN_RIGHT:{ + m_nextCell={m_nextCell.x+1,m_nextCell.y-1}; + break; + } + case MoveDirection::UP_LEFT:{ + m_nextCell={m_nextCell.x-1,m_nextCell.y+1}; + break; + } + case MoveDirection::IDLE:{ + printf("Direction debug didn't move\n"); + break; + } + } + if(false){//not walkable + times+=1; + return true; //Dir.pop_back + }else{ + return false; + } + } void PathfindingUnit::findNextCellDir() { int targetCellX = m_targetCell.x; @@ -101,6 +152,22 @@ void PathfindingUnit::findNextCellDir() { printf("(find)Direction debug didn't move\n"); } } +void PathfindingUnit::findNextCellDir(MoveDirection lastDir,int times){ + std::vector dictionary = {MoveDirection::UP,MoveDirection::UP_RIGHT,MoveDirection::RIGHT,MoveDirection::DOWN_RIGHT,MoveDirection::DOWN,MoveDirection::DOWN_LEFT,MoveDirection::LEFT,MoveDirection::UP_LEFT}; + int index=0; + if(times!=0){ + while(dictionary[index]!=lastDir){ + index++; + } + if(index+times>dictionary.size()){ + times-=dictionary.size(); + } + m_currentDir =dictionary[index+times]; + return; + } + +} + void PathfindingUnit::walkTowardNextCell(){ switch (m_currentDir) { case MoveDirection::RIGHT:{