Skip to content

Commit

Permalink
Merge pull request #40 from jonylu7/buttonUpdate
Browse files Browse the repository at this point in the history
Unit update
  • Loading branch information
jonylu7 authored Mar 29, 2024
2 parents d8b110e + d191462 commit 2b8623f
Show file tree
Hide file tree
Showing 18 changed files with 273 additions and 9 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ set(SRC_FILES
${SRC_DIR}/Structure/WayPointStructure.cpp

${SRC_DIR}/Unit/PathfindingUnit.cpp

${SRC_DIR}/Unit/Runner.cpp
${SRC_DIR}/Unit/Hunter.cpp

${SRC_DIR}/Scene/DefaultScene.cpp
${SRC_DIR}/Camera.cpp
Expand Down Expand Up @@ -163,6 +164,8 @@ set(INCLUDE_FILES
${INCLUDE_DIR}/Unit/PathfindingUnit.hpp
${INCLUDE_DIR}/Unit/Avatar.hpp
${INCLUDE_DIR}/Unit/WayPointUnit.hpp
${INCLUDE_DIR}/Unit/Runner.hpp
${INCLUDE_DIR}/Unit/Hunter.hpp

${INCLUDE_DIR}/imgui/imconfig.h
${INCLUDE_DIR}/imgui/imgui.h
Expand Down
Binary file added assets/sprites/Hunter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/Runner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/sprites/temp_Bullet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions include/GameObjectManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class GameObjectManager {

void Append(std::shared_ptr<Structure> newstruct) {
newstruct->Start();
// newstruct->importMap(m_Map);
m_BuiltStructure.push_back(newstruct);
}
void Append(std::shared_ptr<Avatar> newUnit) {
Expand Down
4 changes: 4 additions & 0 deletions include/Scene/DefaultScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "UI/UI.hpp"
#include "Unit/Avatar.hpp"
#include "Unit/FindValidPathToDest.hpp"
#include "Unit/Hunter.hpp"
#include "Unit/Runner.hpp"
#include "Unit/PathfindingUnit.hpp"
#include "Util/Image.hpp"
#include "Util/Input.hpp"
Expand Down Expand Up @@ -52,6 +54,8 @@ class DefaultScene {
std::shared_ptr<GameObjectManager> m_Manager = std::make_shared<GameObjectManager>();

Avatar m_dummy;
std::shared_ptr<Hunter> m_hunter=std::make_shared<Hunter>();
std::shared_ptr<Runner> m_runner=std::make_shared<Runner>();
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_DEFAULTSCENE_HPP
2 changes: 2 additions & 0 deletions include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Structure : public Util::GameObject, public Selectable {
float GetBuildingHp();
GameObjectID GetID() { return m_ID; }

// void importMap(std::shared_ptr<MapClass> map){m_Map=map;}
private:
updateMode m_CurrentState = updateMode::Invisidable;
glm::vec2 ObjectLocation = {100, 100};
Expand All @@ -103,6 +104,7 @@ class Structure : public Util::GameObject, public Selectable {
GameObjectID m_ID;

protected:
// std::shared_ptr<MapClass> m_Map;
bool b_selectingNewWayPoint = false;
};

Expand Down
1 change: 1 addition & 0 deletions include/Unit/AttackAndDamageUnit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_ATTACKANDDAMAGEUNIT_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_ATTACKANDDAMAGEUNIT_HPP
#include "Weapon.hpp"

class AttackAndDamageUnit {
public:
AttackAndDamageUnit() {}
Expand Down
51 changes: 50 additions & 1 deletion include/Unit/Avatar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "Unit/PathfindingUnit.hpp"

class Avatar : public PathfindingUnit, public AttackAndDamageUnit {
protected:
std::shared_ptr<Util::Image> m_Image;
private:
FindValidPathToDest m_wayPointUnit;
bool b_SelectedByCursor = true;
Expand All @@ -24,7 +26,7 @@ class Avatar : public PathfindingUnit, public AttackAndDamageUnit {
// waypointLocation
// setCurrentCell() //CurrentCell = Structure's Location
this->SetDrawable(
std::make_unique<Util::Image>("../assets/sprites/capybara.png"));
customizeImage());
SetVisible(true);
m_grid.SetActivate(true);
m_wayPointUnit.Start(map);
Expand All @@ -51,13 +53,16 @@ class Avatar : public PathfindingUnit, public AttackAndDamageUnit {
cursorSetNewDest();
printf("-----------------------------\n");
}
virtual void customizeUpdate(){}
virtual std::shared_ptr<Util::Image> customizeImage(){ return std::make_unique<Util::Image>("../assets/sprites/capybara.png");}
virtual void Update() override {
switch (m_currentMode) {
case (UnitMode::DEAD): {
SetVisible(false);
}
case (UnitMode::ALIVE): {
aliveUpdate();
customizeUpdate();
}
}
}
Expand All @@ -77,5 +82,49 @@ class Avatar : public PathfindingUnit, public AttackAndDamageUnit {
Util::Input::GetCursorPosition())));
}
}
float getDistance(glm::vec2 cell){
return sqrt(pow(cell.x-getCurrentCell().x,2)+pow(cell.y-getCurrentCell().y,2));
}

void DEBUG_printCurrentMoveDirection(MoveDirection Dir){
switch (Dir) {
case MoveDirection::RIGHT: {
printf("(DEBUG)Avatar DIR:Right\n");
break;
}
case MoveDirection::LEFT: {
printf("(DEBUG)Avatar DIR:Left\n");
break;
}
case MoveDirection::UP: {
printf("(DEBUG)Avatar DIR:Up\n");
break;
}
case MoveDirection::DOWN: {
printf("(DEBUG)Avatar DIR:Down\n");
break;
}
case MoveDirection::UP_RIGHT: {
printf("(DEBUG)Avatar DIR:Up_Right\n");
break;
}
case MoveDirection::DOWN_LEFT: {
printf("(DEBUG)Avatar DIR:Down_Left\n");
break;
}
case MoveDirection::DOWN_RIGHT: {
printf("(DEBUG)Avatar DIR:Down_Right\n");
break;
}
case MoveDirection::UP_LEFT: {
printf("(DEBUG)Avatar DIR:Up_Left\n");
break;
}
case MoveDirection::IDLE: {
printf("(DEBUG)Avatar DIR:IDLE\n");
break;
}
}
}
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_DUMMY_HPP
23 changes: 23 additions & 0 deletions include/Unit/Hunter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Created by nudle on 2024/3/28.
//

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_HUNTER_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_HUNTER_HPP
#include "Avatar.hpp"
#include "Runner.hpp"

#define ATTACK_RANGE 7

class Hunter:public Avatar{
private:
std::shared_ptr<Runner> m_target;
glm::vec2 lastTargetCell;

public:
void setTarget(std::shared_ptr<Runner> m_target){this->m_target=m_target;}
std::shared_ptr<Util::Image> customizeImage()override{ return std::make_unique<Util::Image>("../assets/sprites/Hunter.png");}
void customizeUpdate() override;
void attack(std::shared_ptr<Avatar> attackTarget){};
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_HUNTER_HPP
2 changes: 1 addition & 1 deletion include/Unit/PathfindingUnit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class PathfindingUnit : public Util::GameObject {
virtual ~PathfindingUnit(){};

//

UnitMode getUnitMode(){return m_currentMode;}
void setDestinationCell(int x, int y) {
this->m_destinationCell = {glm::vec2(x, y)};
}
Expand Down
31 changes: 31 additions & 0 deletions include/Unit/Runner.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Created by nudle on 2024/3/28.
//

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_RUNNER_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_RUNNER_HPP
#include "Avatar.hpp"
#define ATTACK_RANGE 7

class Runner : public Avatar{
private:
bool b_beingChase= false;

std::shared_ptr<Avatar> m_hunter;
glm::vec2 lastTargetCell;
std::vector<MoveDirection> dictionary = {
MoveDirection::UP, MoveDirection::UP_RIGHT,
MoveDirection::RIGHT, MoveDirection::DOWN_RIGHT,
MoveDirection::DOWN, MoveDirection::DOWN_LEFT,
MoveDirection::LEFT, MoveDirection::UP_LEFT};
enum class runMode{REASONABLE,LIDL_RANDOM,FULL_RANDOM};
int edgeCount=0;
public:
void setBeingChase(std::shared_ptr<Avatar> hunter);
std::shared_ptr<Util::Image> customizeImage()override{ return std::make_unique<Util::Image>("../assets/sprites/Runner.png");}
void customizeUpdate()override;
MoveDirection findNewDir(MoveDirection Dir,int edgeCount);
MoveDirection oppositeDir(MoveDirection Dir,runMode mode);
glm::vec2 getNextCellByCurrentPlus3(MoveDirection currentdir,glm::vec2 currentcell,int n,int m) ;
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_RUNNER_HPP
1 change: 1 addition & 0 deletions include/Unit/Weapon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_WEAPON_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_WEAPON_HPP
#include <memory>
enum class WeaponType {
COLT_45,
M16,
Expand Down
10 changes: 10 additions & 0 deletions src/Scene/DefaultScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ void DefaultScene::Start() {


m_Player->setTotalCurrency(5000);

// m_hunter->setCurrentCell({20,10});
// m_runner->setCurrentCell({10,10});
m_hunter->Start({20,9},m_Map);
m_runner->Start({9,10},m_Map);
m_hunter->setTarget(m_runner);
m_runner->setBeingChase(m_hunter);
}

void DefaultScene::Update() {
//m_dummy.Update();

m_hunter->Update();
m_runner->Update();

m_Manager->Update();

Util::Transform trans;
Expand Down
7 changes: 4 additions & 3 deletions src/Structure/Structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ void Structure::updateMoveable() {
this->SetVisible(true);
this->Draw();
glm::vec2 cellPos = MapUtil::GlobalCoordToCellCoord(location);
if (Util::Input::IsKeyPressed(
Util::Keycode::
MOUSE_LB) /*&&MapClass::getTileByCellPosition(cellPos)->getBuildable()*/) {
// std::shared_ptr<TileClass> tileClass = m_Map->getTileByCellPosition(cellPos);
if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB) /*tileClass->getBuildable()*/) {
this->SetObjectLocation(location);
this->SetCurrentUpdateMode(updateMode::Fixed);
// tileClass->setBuildable(false);
// tileClass->setWalkable(false);
// 在這裡增加設置Tile屬性
/*
std::shared_ptr<TileClass>tile =
Expand Down
7 changes: 4 additions & 3 deletions src/Structure/WayPointStructure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ void WayPointStructure::updateMoveable() {
this->SetVisible(true);
this->Draw();
glm::vec2 cellPos = MapUtil::GlobalCoordToCellCoord(location);
if (Util::Input::IsKeyPressed(
Util::Keycode::
MOUSE_LB) /*&&MapClass::getTileByCellPosition(cellPos)->getBuildable()*/) {
// std::shared_ptr<TileClass> tileClass = m_Map->getTileByCellPosition(cellPos);
if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB) /*tileClass->getBuildable()*/) {
this->SetObjectLocation(location);
this->SetWayPointLocation({GetDrawLocation().x + CELL_SIZE.x,
GetDrawLocation().y + CELL_SIZE.y});
onSelected();
this->SetCurrentUpdateMode(updateMode::Fixed);
// tileClass->setBuildable(false);
// tileClass->setWalkable(false);
// 在這裡增加設置Tile屬性
/*
std::shared_ptr<TileClass>tile =
Expand Down
20 changes: 20 additions & 0 deletions src/Unit/Hunter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Created by nudle on 2024/3/29.
//

#include "Unit/Hunter.hpp"

void Hunter::customizeUpdate(){
glm::vec2 targetCell = m_target->getCurrentCell();
if(getDistance(targetCell)>ATTACK_RANGE-1 && lastTargetCell!=m_target->getCurrentCell()){
// glm::vec2 nextCell = getNextCellByCurrent(getDirByRelativeCells(getCurrentCell(),targetCell),getCurrentCell());
setNewDestination(m_target->getCurrentCell());
lastTargetCell=m_target->getCurrentCell();
// setNewDestination(nextCell);
}
else if(getDistance(targetCell)<ATTACK_RANGE-1 ){
setNewDestination(getCurrentCell());
lastTargetCell=getCurrentCell();
attack(m_target);
}
}
Loading

0 comments on commit 2b8623f

Please sign in to comment.