diff --git a/include/Unit/Avatar.hpp b/include/Unit/Avatar.hpp index bdeced2b..cbcb4976 100644 --- a/include/Unit/Avatar.hpp +++ b/include/Unit/Avatar.hpp @@ -7,71 +7,72 @@ #include "Unit/PathfindingUnit.hpp" #include "Unit/WayPointUnit.hpp" -class Avatar:public PathfindingUnit{ +class Avatar : public PathfindingUnit { private: WayPointUnit m_wayPointUnit; - bool b_selectedNewTarget= false; + bool b_SelectedByCursor = true; bool b_justStarted = true; + public: - Avatar():PathfindingUnit(){}; - ~Avatar()override{}; + Avatar() + : PathfindingUnit(){}; + ~Avatar() override{}; - virtual void Start(glm::vec2 target){ //target = Barrack's waypointLocation - //setCurrentCell() //CurrentCell = Structure's Location - this->SetDrawable(std::make_unique("../assets/sprites/capybara.png")); + virtual void Start(glm::vec2 destination) { // destination = Barrack's + // waypointLocation + // setCurrentCell() //CurrentCell = Structure's Location + this->SetDrawable( + std::make_unique("../assets/sprites/capybara.png")); SetVisible(true); m_grid.SetActivate(true); - setCurrentCell(target); - setNextCell(target); - m_wayPointUnit.setCurrentCell(target); - m_wayPointUnit.setNextCell(target); - setNewTarget(getCurrentCell()); + setCurrentCell(destination); + setNextCell(destination); + m_wayPointUnit.setCurrentCell(destination); + m_wayPointUnit.setNextCell(destination); + setNewDestination(getCurrentCell()); setMovementSpeed(4); } - virtual void aliveUpdate(){ - if(walkTowardNextCell()||b_justStarted){ - b_justStarted= false; - setCurrentCell(MapClass::GlobalCoordToCellCoord(getCurrentLocation())); + virtual void aliveUpdate() { + if (walkTowardNextCell() || b_justStarted) { + b_justStarted = false; + setCurrentCell( + MapClass::GlobalCoordToCellCoord(getCurrentLocation())); setCurrentDir(m_wayPointUnit.getFirstCellDir()); UpdateNextCell(); printf("(aliveUpdate) getting new dir\n"); } m_wayPointUnit.Update(); - m_Transform.translation=getCurrentLocation(); + m_Transform.translation = getCurrentLocation(); Draw(); - onSelect(true); + cursorSetNewDest(); printf("-----------------------------\n"); } - virtual void Update()override{ - switch(m_currentMode){ - case (UnitMode::DEAD):{ + virtual void Update() override { + switch (m_currentMode) { + case (UnitMode::DEAD): { SetVisible(false); } - case (UnitMode::ALIVE):{ + case (UnitMode::ALIVE): { aliveUpdate(); } } } - void setNewTarget(glm::vec2 target){ - setTargetCell(target.x,target.y); + void setNewDestination(glm::vec2 destination) { + setDestinationCell(destination.x, destination.y); m_wayPointUnit.resetQueue(); m_wayPointUnit.setCurrentCell(getNextCell()); m_wayPointUnit.setNextCell(getNextCell()); - m_wayPointUnit.findPath(getTargetCell()); - //setCurrentDir(m_wayPointUnit.getFirstCellDir()); + m_wayPointUnit.findPath(getDestinationCell()); + // setCurrentDir(m_wayPointUnit.getFirstCellDir()); } - 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; - } + void cursorSetNewDest() { + if (b_SelectedByCursor && + Util::Input::IsKeyPressed(Util::Keycode::MOUSE_RB)) { + this->setNewDestination( + MapClass::GlobalCoordToCellCoord(MapClass::ScreenToGlobalCoord( + Util::Input::GetCursorPosition()))); } } }; 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 cbd527f8..c45c888c 100644 --- a/include/Unit/PathfindingUnit.hpp +++ b/include/Unit/PathfindingUnit.hpp @@ -5,91 +5,96 @@ #ifndef PRACTICALTOOLSFORSIMPLEDESIGN_PATHFINDINGUNIT_HPP #define PRACTICALTOOLSFORSIMPLEDESIGN_PATHFINDINGUNIT_HPP -#include "Tile.hpp" -#include "Map.hpp" -#include "Line.hpp" #include "Grid.hpp" -#include "glm/glm.hpp" +#include "Line.hpp" +#include "Map.hpp" +#include "Tile.hpp" #include "Util/GameObject.hpp" #include "Util/Transform.hpp" +#include "glm/glm.hpp" #define SPEED 1 -class PathfindingUnit:public Util::GameObject{ +class PathfindingUnit : public Util::GameObject { protected: - enum class UnitMode{ - DEAD,ALIVE - }; - enum class MoveDirection{ - UP,UP_RIGHT,UP_LEFT,RIGHT,LEFT,DOWN_RIGHT,DOWN_LEFT,DOWN,IDLE + enum class UnitMode { DEAD, ALIVE }; + enum class MoveDirection { + UP, + UP_RIGHT, + UP_LEFT, + RIGHT, + LEFT, + DOWN_RIGHT, + DOWN_LEFT, + DOWN, + IDLE }; Util::Transform m_emptyTrans; Line m_line; Grid m_grid; std::vector m_lineVector; - float defaultZIndex=15; - UnitMode m_currentMode=UnitMode::ALIVE;//debug :DEAD + float defaultZIndex = 15; + UnitMode m_currentMode = UnitMode::ALIVE; // debug :DEAD private: - glm::vec2 m_targetCell; + glm::vec2 m_destinationCell; glm::vec2 m_nextCell; glm::vec2 m_currentCell; glm::vec2 m_currentLocation; - MoveDirection m_currentDir=MoveDirection::IDLE; + MoveDirection m_currentDir = MoveDirection::IDLE; - float m_Hp=100.F; - float m_MovementSpeed=1.F; - float m_Armor=5.f; + float m_Hp = 100.F; + float m_MovementSpeed = 1.F; + float m_Armor = 5.f; int moveDistance = 0; + public: - PathfindingUnit(){ - m_Transform.scale={0.1,0.1}; - m_ZIndex=defaultZIndex; + PathfindingUnit() { + m_Transform.scale = {0.1, 0.1}; + m_ZIndex = defaultZIndex; }; virtual ~PathfindingUnit(){}; - //Set Unit - void setHp(float hp){m_Hp=hp;} + // Set Unit + void setHp(float hp) { m_Hp = hp; } // - 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,temp.y}; + void setDestinationCell(int x, int y) { + this->m_destinationCell = {glm::vec2(x, y)}; } - glm::vec2 getCurrentCell(){return m_currentCell;} - - void setNextCell(glm::vec2 cell){this->m_nextCell=glm::vec2(cell);} - glm::vec2 getNextCell(){return m_nextCell;} - - glm::vec2 getCurrentLocation(){return m_currentLocation;} + void setDestinationCell(glm::vec2 cell) { this->m_destinationCell = cell; } + glm::vec2 getDestinationCell() { return m_destinationCell; } + + 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, temp.y}; + } + glm::vec2 getCurrentCell() { return m_currentCell; } - void setMovementSpeed(int speed){this->m_MovementSpeed=speed;} + void setNextCell(glm::vec2 cell) { this->m_nextCell = glm::vec2(cell); } + glm::vec2 getNextCell() { return m_nextCell; } - MoveDirection getCurrentDir(){return m_currentDir;} - void setCurrentDir(MoveDirection direction){m_currentDir=direction;} + glm::vec2 getCurrentLocation() { return m_currentLocation; } + 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 findNextCellDir(MoveDirection lastDir, int times); void UpdateNextCell(); - bool UpdateNextCell(int* times); + bool UpdateNextCell(int *times); bool walkTowardNextCell(); - virtual void Start(){} - virtual void Update(){ - m_Transform.translation=getCurrentLocation(); + virtual void Start() {} + virtual void Update() { + m_Transform.translation = getCurrentLocation(); Draw(); } - - }; #endif // PRACTICALTOOLSFORSIMPLEDESIGN_PATHFINDINGUNIT_HPP diff --git a/src/Scene/DefaultScene.cpp b/src/Scene/DefaultScene.cpp index 03012648..df46bde8 100644 --- a/src/Scene/DefaultScene.cpp +++ b/src/Scene/DefaultScene.cpp @@ -29,12 +29,11 @@ void DefaultScene::Start() { DrawOverlays::OverlayShapes::R_CROSS); // m_GameObjectManager.Start(); - m_dummy.Start({5,5}); - + m_dummy.Start({5, 5}); } void DefaultScene::Update() { - m_Avatar.Update(); + m_dummy.Update(); m_waypointUnit.Update();