Skip to content

Commit

Permalink
fix player
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed Apr 25, 2024
1 parent 81dd62e commit 2e7db79
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 75 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,21 @@ set(SRC_FILES
${SRC_DIR}/Unit/Hunter.cpp
${SRC_DIR}/Unit/PathUtility.cpp
${SRC_DIR}/Unit/PathFindingUnit.cpp
${SRC_DIR}/Mechanics/FindValidPathToDest.cpp

${SRC_DIR}/Scene/DefaultScene.cpp
${SRC_DIR}/Camera.cpp
${SRC_DIR}/SpriteSheet.cpp
${SRC_DIR}/UI/UI.cpp
${SRC_DIR}/Mechanics/GameObjectID.cpp

${SRC_DIR}/Map/YAMLReader.cpp
${SRC_DIR}/Map/TerrainConfig.cpp
${SRC_DIR}/Map/MapbinReader.cpp
${SRC_DIR}/Map/Map.cpp

${SRC_DIR}/Mechanics/FindValidPathToDest.cpp
${SRC_DIR}/Mechanics/GameObjectID.cpp
${SRC_DIR}/Mechanics/CursorSelection.cpp

)
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(INCLUDE_FILES
Expand Down Expand Up @@ -159,9 +163,11 @@ set(INCLUDE_FILES
${INCLUDE_DIR}/Scene/MapScene.hpp


${INCLUDE_DIR}/App.hpp
${INCLUDE_DIR}/Mechanics/GameObjectManager.hpp
${INCLUDE_DIR}/Mechanics/GameObjectID.hpp
${INCLUDE_DIR}/Mechanics/CursorSelection.hpp

${INCLUDE_DIR}/App.hpp
${INCLUDE_DIR}/Tile.hpp
${INCLUDE_DIR}/config.hpp
${INCLUDE_DIR}/DrawOverlays.hpp
Expand Down
41 changes: 1 addition & 40 deletions include/Mechanics/CursorSelection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,7 @@ class CursorSelection {
CursorSelection() {}
virtual ~CursorSelection() {}
void CursorSelect(std::shared_ptr<MapClass> m_Map, glm::vec2 *start,
glm::vec2 *end) {

if (Util::Input::IsKeyDown(Util::Keycode::MOUSE_LB)) {
*start = Util::Input::GetCursorPosition();
}
if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB)) {
// clear up last selected
for (auto i : lastSeletctedObjects) {
i->setSelected(false);
}
lastSeletctedObjects.clear();

// get cursor position
*end = Util::Input::GetCursorPosition();

// select objects
auto startcell = MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(*start));
auto endcell = MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(*end));

int max_x_cell = std::max(startcell.x, endcell.x);
int max_y_cell = std::max(startcell.y, endcell.y);
int min_x_cell = std::min(startcell.x, endcell.x);
int min_y_cell = std::min(startcell.y, endcell.y);

for (int i = min_y_cell; i <= max_y_cell; i++) {
for (int j = min_x_cell; j <= max_x_cell; j++) {
auto objects = m_Map->getTileByCellPosition(glm::vec2(j, i))
->getSelectableObjects();
for (auto i : objects) {
if (i->getSelected() == false) {
i->setSelected(true);
lastSeletctedObjects.push_back(i);
}
}
}
}
}
}
glm::vec2 *end);

protected:
std::vector<std::shared_ptr<Selectable>> lastSeletctedObjects;
Expand Down
26 changes: 9 additions & 17 deletions include/Mechanics/GameObjectManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "GameObjectID.hpp"
#include "Mechanics/Constructing.hpp"
#include "Mechanics/CursorSelection.hpp"
#include "Mechanics/Player.hpp"
#include "Mechanics/StructureArray.hpp"
#include "Player.hpp"
#include "Structure/AdvencePowerPlants.hpp"
#include "Structure/Barracks.hpp"
#include "Structure/OreRefinery.hpp"
Expand All @@ -22,15 +22,14 @@
#include <unordered_map>
#include <utility>

class GameObjectManager : public Constructing, public CursorSelection {
class GameObjectManager : public Constructing,
public CursorSelection,
public Player {
public:
GameObjectManager() {}
~GameObjectManager() {}
void Start(std::shared_ptr<MapClass> map, std::shared_ptr<Player> player,
std::shared_ptr<CursorClass> cursor) {
void Start(std::shared_ptr<MapClass> map) {
m_Map = map;
m_Player = player;
m_Cursor = cursor;

for (auto pair : m_BuiltStructure) {
pair->Start();
Expand Down Expand Up @@ -68,15 +67,6 @@ class GameObjectManager : public Constructing, public CursorSelection {

void RemoveStructByID(const GameObjectID id) {}

int GetTotalPower() {
int totalPower = 0;
for (int i = 0; i < m_BuiltStructure.size(); i++) {
totalPower += m_BuiltStructure[i]->GetElectricPower();
}
return totalPower;
}

float GetTotalCurrency() { return m_Player->getTotalCurrency(); }
std::vector<std::shared_ptr<Structure>> getStructureArray() {
return m_BuiltStructure;
}
Expand All @@ -93,12 +83,14 @@ class GameObjectManager : public Constructing, public CursorSelection {
}
}

int getTotalPower() {
return Player::getTotalPower(this->m_BuiltStructure);
}

private:
std::vector<std::shared_ptr<Avatar>> m_UnitArray;
FindValidPathToDest m_wayPointUnit;
std::shared_ptr<MapClass> m_Map = std::make_shared<MapClass>();
std::shared_ptr<Player> m_Player;
std::shared_ptr<CursorClass> m_Cursor;
std::chrono::high_resolution_clock::time_point m_StartTime;
double m_lastElapsed = 0.F;
};
Expand Down
22 changes: 16 additions & 6 deletions include/Mechanics/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_PLAYER_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_PLAYER_HPP
#include "Structure/Structure.hpp"
class Player {
public:
Player() {}
Expand All @@ -12,16 +13,25 @@ class Player {
void setTotalCurrency(int value) { m_totalCurrency = value; };
void addCurrency(int value) { m_totalCurrency += value; };

void setTotalPower(int value){m_totalPower=value;}
void addPower(int value){m_totalPower+=value;}
void setTotalPower(int value) { m_totalPower = value; }
void addPower(int value) { m_totalPower += value; }

int getTotalPower() {return m_totalPower;}
int getTotalCurrency() { return m_totalCurrency; }
int getMaxTroopSize(){return m_maxTroopSize;}
int getMaxTroopSize() { return m_maxTroopSize; }

private:
protected:
int
getTotalPower(std::vector<std::shared_ptr<Structure>> m_BuiltStructure) {
int totalPower = 0;
for (int i = 0; i < m_BuiltStructure.size(); i++) {
totalPower += m_BuiltStructure[i]->GetElectricPower();
}
return totalPower;
}

protected:
int m_maxTroopSize = 200;
int m_totalPower = 0 ;
int m_totalPower = 0;
int m_totalCurrency = 0;
};
#endif // PRACTICALTOOLSFORSIMPLEDESIGN_PLAYER_HPP
46 changes: 46 additions & 0 deletions src/Mechanics/CursorSelection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Created by 盧威任 on 4/25/24.
//
#include "Mechanics/CursorSelection.hpp"

void CursorSelection::CursorSelect(std::shared_ptr<MapClass> m_Map,
glm::vec2 *start, glm::vec2 *end) {

if (Util::Input::IsKeyDown(Util::Keycode::MOUSE_LB)) {
*start = Util::Input::GetCursorPosition();
}
if (Util::Input::IsKeyPressed(Util::Keycode::MOUSE_LB)) {
// clear up last selected
for (auto i : lastSeletctedObjects) {
i->setSelected(false);
}
lastSeletctedObjects.clear();

// get cursor position
*end = Util::Input::GetCursorPosition();

// select objects
auto startcell = MapUtil::GlobalCoordToCellCoord(
MapUtil::ScreenToGlobalCoord(*start));
auto endcell =
MapUtil::GlobalCoordToCellCoord(MapUtil::ScreenToGlobalCoord(*end));

int max_x_cell = std::max(startcell.x, endcell.x);
int max_y_cell = std::max(startcell.y, endcell.y);
int min_x_cell = std::min(startcell.x, endcell.x);
int min_y_cell = std::min(startcell.y, endcell.y);

for (int i = min_y_cell; i <= max_y_cell; i++) {
for (int j = min_x_cell; j <= max_x_cell; j++) {
auto objects = m_Map->getTileByCellPosition(glm::vec2(j, i))
->getSelectableObjects();
for (auto i : objects) {
if (i->getSelected() == false) {
i->setSelected(true);
lastSeletctedObjects.push_back(i);
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/Scene/DefaultScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void DefaultScene::Start() {
// m_GameObjectManager.Start();

// m_dummy.Start({5, 5}, m_Map);
m_GameObjectManager->Start(m_Map, m_Player, m_Cursor);
m_GameObjectManager->Start(m_Map);
// m_Cursor.Start(m_Map);
m_UI.Start(m_Map, m_Player, m_GameObjectManager);
m_Player->setTotalCurrency(5000);
Expand Down
5 changes: 3 additions & 2 deletions src/UI/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ void UIClass::ShowPlayerStatus() {
.c_str());
// ImGui::Text(fmt::format("Zoom: {}",
// m_SceneCamera.getCameraZoom()).c_str());
ImGui::Text(fmt::format("$ {}", m_Player->getTotalCurrency()).c_str());
ImGui::Text(
fmt::format("Power {}", m_gameObjectManager->GetTotalPower()).c_str());
fmt::format("$ {}", m_gameObjectManager->getTotalCurrency()).c_str());
ImGui::Text(
fmt::format("Power {}", m_gameObjectManager->getTotalPower()).c_str());
ImGui::PushFont(sacker_med);
}

Expand Down
12 changes: 6 additions & 6 deletions src/UI/UIScriptProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ void UIScriptProcess::CountDown() {
// Structure Building
std::chrono::duration<double> buildElapsed =
m_currentCountDownTime - m_buildStartTime;
if (m_gameObjectManager->GetTotalCurrency() <= 0 &&
if (m_gameObjectManager->getTotalCurrency() <= 0 &&
b_isBuildingInCoolDown && buildElapsed.count() < buildCoolDownTime) {
if (m_gameObjectManager->GetTotalPower() <= 0) {
if (m_gameObjectManager->getTotalPower() <= 0) {
SetBuildCountDown(buildCoolDownTime / 2 - buildElapsed.count());
} else {
SetBuildCountDown(buildCoolDownTime - buildElapsed.count());
Expand Down Expand Up @@ -90,9 +90,9 @@ void UIScriptProcess::CountDown() {
// Unit Spawning
std::chrono::duration<double> spawnElapsed =
m_currentCountDownTime - m_SpawnStartTime;
if (m_gameObjectManager->GetTotalCurrency() <= 0 &&
if (m_gameObjectManager->getTotalCurrency() <= 0 &&
b_isSpawningInCooldown && spawnElapsed.count() < spawnCoolDownTime) {
if (m_gameObjectManager->GetTotalPower() <= 0) {
if (m_gameObjectManager->getTotalPower() <= 0) {
SetSpawnCountDown(spawnCoolDownTime / 2 - spawnElapsed.count());
} else {
SetSpawnCountDown(spawnCoolDownTime - spawnElapsed.count());
Expand Down Expand Up @@ -235,14 +235,14 @@ std::shared_ptr<Avatar> UIScriptProcess::spawnAvatar() {
}

float UIScriptProcess::GetBuildCountDownTime() {
if (m_gameObjectManager->GetTotalPower() <= 0) {
if (m_gameObjectManager->getTotalPower() <= 0) {
return m_offPowerBuildCoolDownTime;
} else {
return m_buildCoolDownTime;
}
}
float UIScriptProcess::GetSpawnCountDownTime() {
if (m_gameObjectManager->GetTotalPower() <= 0) {
if (m_gameObjectManager->getTotalPower() <= 0) {
return m_offPowerSpawnCoolDownTime;
} else {
return m_spawnCoolDownTime;
Expand Down

0 comments on commit 2e7db79

Please sign in to comment.