Skip to content

Commit

Permalink
change names and minor opt
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed Mar 22, 2024
1 parent 6dd21e0 commit daf9562
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 126 deletions.
73 changes: 37 additions & 36 deletions include/Unit/Avatar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Util::Image>("../assets/sprites/capybara.png"));
virtual void Start(glm::vec2 destination) { // 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);

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())));
}
}
};
Expand Down
40 changes: 0 additions & 40 deletions include/Unit/Dummy.hpp

This file was deleted.

99 changes: 52 additions & 47 deletions include/Unit/PathfindingUnit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Line> 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
5 changes: 2 additions & 3 deletions src/Scene/DefaultScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit daf9562

Please sign in to comment.