Skip to content

Commit

Permalink
unit update
Browse files Browse the repository at this point in the history
  • Loading branch information
ntut-Tu committed Mar 22, 2024
1 parent 64000fa commit b9dac97
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 68 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions include/Grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Core/VertexArray.hpp"
#include "Line.hpp"
#include "Util/TransformUtils.hpp"
#include <deque>

class Grid : public Core::Drawable {

Expand All @@ -18,6 +19,7 @@ class Grid : public Core::Drawable {
~Grid(){};
void InitVertexAndColor();
void Start(std::vector<Line> lineVector);
void queStart(std::deque<Line> lineVector);
void Draw(const Util::Transform &transform, const float zIndex) override;
void DrawUsingCamera(const Util::Transform &transform,
const float zIndex) override;
Expand Down
2 changes: 1 addition & 1 deletion include/Scene/DefaultScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
70 changes: 70 additions & 0 deletions include/Unit/Avatar.hpp
Original file line number Diff line number Diff line change
@@ -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<Util::Image>("../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
40 changes: 0 additions & 40 deletions include/Unit/Dummy.hpp

This file was deleted.

30 changes: 17 additions & 13 deletions include/Unit/PathfindingUnit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "Util/GameObject.hpp"
#include "Util/Transform.hpp"

#define SPEED 240

class PathfindingUnit:public Util::GameObject{
protected:
enum class UnitMode{
Expand All @@ -24,7 +26,7 @@ class PathfindingUnit:public Util::GameObject{
Util::Transform m_emptyTrans;
Line m_line;
Grid m_grid;
std::vector<Line> m_lineVector;
std::deque<Line> m_lineVector;
float defaultZIndex=15;
private:
glm::vec2 m_targetCell;
Expand All @@ -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;}

Expand All @@ -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;
}

};
Expand Down
44 changes: 36 additions & 8 deletions include/Unit/WayPointUnit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,60 @@

class WayPointUnit:public PathfindingUnit{
private:
std::vector<glm::vec2> m_pathQueue;
std::deque<MoveDirection> m_dirQue;
std::deque<glm::vec2> 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);
}
};
Expand Down
7 changes: 7 additions & 0 deletions src/Grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ void Grid::Start(std::vector<Line> lineVector) {

InitVertexAndColor();
}
void Grid::queStart(std::deque<Line> lineQue) {

// debug grid:
m_lineVector = std::vector<Line> (lineQue.begin(),lineQue.end());

InitVertexAndColor();
}

void Grid::Draw(const Util::Transform &transform, const float zindex) {
// nothing
Expand Down
8 changes: 3 additions & 5 deletions src/Scene/DefaultScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Loading

0 comments on commit b9dac97

Please sign in to comment.