Skip to content

Commit

Permalink
spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
jonylu7 committed May 30, 2024
1 parent 13a25bc commit 2e6f9ab
Show file tree
Hide file tree
Showing 18 changed files with 236 additions and 178 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ set(SRC_FILES
${SRC_DIR}/Mechanics/GameObjectID.cpp
${SRC_DIR}/Mechanics/CursorSelection.cpp
${SRC_DIR}/Mechanics/AvatarManager.cpp
${SRC_DIR}/Mechanics/UnitManager.cpp

)
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion include/Display/Sprite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_SPRITE_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_SPRITE_HPP
#include "Core/Drawable.hpp"
#include "Util/CustomizableImage.hpp"
#include "Display/CustomizableImage.hpp"
#include "Util/Image.hpp"
#include "pch.hpp" // IWYU pragma: export

Expand Down
5 changes: 3 additions & 2 deletions include/Display/SpriteSheet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#ifndef PRACTICALTOOLSFORSIMPLEDESIGN_SPRITESHEET_HPP
#define PRACTICALTOOLSFORSIMPLEDESIGN_SPRITESHEET_HPP
#include "Display/CustomizableImage.hpp"
#include "Sprite.hpp"
#include "Util/CustomizableImage.hpp"
class SpriteSheet {
public:
SpriteSheet(){};
Expand Down Expand Up @@ -46,7 +46,8 @@ class SpriteSheet {
glm::vec2(UV[4], UV[3])};
}

int getSize(){return m_SpriteSheet.size();}
int getSize() { return m_SpriteSheet.size(); }

private:
std::vector<std::unique_ptr<Sprite>> m_SpriteSheet;
std::vector<float> m_TextCoord;
Expand Down
12 changes: 5 additions & 7 deletions include/Mechanics/BuiltStructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,11 @@ class BuiltStructure {
glm::vec2 getPlayerBarrackSpawnCell() {
for (auto i : m_BuiltStructure) {
if (std::dynamic_pointer_cast<Barracks>(i)) {
glm::vec2 ogSpawnPoint = i->GetObjectLocation();
while (!m_Map->getTileByCellPosition(ogSpawnPoint)
->getWalkable()) {
ogSpawnPoint.x -= 1;
for (auto i : i->getNearbyArea()) {
if (m_Map->ifWalkable(i)) {
return i;
}
}
return ogSpawnPoint;
}
}
}
Expand All @@ -97,10 +96,9 @@ class BuiltStructure {
if (std::dynamic_pointer_cast<Barracks>(i)) {
return MapUtil::GlobalCoordToCellCoord(
std::dynamic_pointer_cast<Barracks>(i)
->GetWayPointLocation());
->getWaypointLocation());
}
}
return m_PlayerWayPointCell;
}

bool ifBarrackBuilt() {
Expand Down
125 changes: 4 additions & 121 deletions include/Mechanics/UnitManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,7 @@ class UnitManager : public Player {
m_StartTime = std::chrono::high_resolution_clock::now();
}

void Update() {
m_StructureManager->Update();
m_AvatarManager->Update();
m_CursorSelection->Update();

// currency update
std::chrono::high_resolution_clock::time_point m_currentTime =
std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = m_currentTime - m_StartTime;
if (elapsed.count() - m_lastElapsed >= 1) { // update every second
m_lastElapsed = elapsed.count();
}

m_StructureManager->SelectingBuildSite();
}
void Update();

public:
int getTotalPower() {
Expand All @@ -63,113 +49,10 @@ class UnitManager : public Player {
return m_StructureManager;
}

int UpdateCurrency() {
for (auto i : m_StructureManager->getStructureArray()
->getBuiltStructureArray()) {
if (std::dynamic_pointer_cast<OreRefinery>(i)) {
addTotalCurrency(150);
}
}
}

void spawnToWayPoint(UnitType unit, HouseType house) {
int updateCurrency();

switch (unit) {
case UnitType::INFANTRY: {
auto avatar = std::make_shared<Infantry>(house);
if (m_StructureManager->getStructureArray()
->getPlayerBarrackSpawnCell()
.x == -1) {
return;
}

avatar->Start(m_StructureManager->getStructureArray()
->getPlayerBarrackSpawnCell());

m_AvatarManager->assignMoveOrderToAvatar(
avatar, {m_StructureManager->getStructureArray()
->getPlayerBarrackWayPointCell()});
// assign order
m_AvatarManager->assignMoveOrderToAvatar(
avatar, m_StructureManager->getStructureArray()
->getPlayerBarrackWayPointCell());
m_AvatarManager->AppendAvatar(avatar);
m_troopSize += 1;
}

default: {
printf("(GOM)error! try to spawn unknown type\n");
break;
}
}
}
void spawn(UnitType unit, HouseType house, glm::vec2 cellPos) {
// 缺檢查敵方擁有建築的位置,並重生在該處
switch (unit) {
case UnitType::BARRACKS: {
auto structure = std::make_shared<Barracks>(house);
auto globalPos = MapUtil::CellCoordToGlobal(cellPos);
structure->Start(globalPos);
structure->SetWayPointLocationByCellCoord(
{cellPos.x + 2, cellPos.y - 2});
m_StructureManager->getStructureArray()->buildNewStructure(
structure, true);
break;
}
case UnitType::ORE_REF: {
auto structure = std::make_shared<OreRefinery>(house);
auto globalPos = MapUtil::CellCoordToGlobal(cellPos);
structure->Start(globalPos);
m_StructureManager->getStructureArray()->buildNewStructure(
structure, true);
break;
}
case UnitType::POWER_PLANT: {
auto structure = std::make_shared<PowerPlants>(house);
auto globalPos = MapUtil::CellCoordToGlobal(cellPos);
structure->Start(globalPos);
m_StructureManager->getStructureArray()->buildNewStructure(
structure, true);
break;
}
case UnitType::WAR_FACT: {
auto structure = std::make_shared<WarFactory>(house);
auto globalPos = MapUtil::CellCoordToGlobal(cellPos);
structure->Start(globalPos);
structure->SetWayPointLocationByCellCoord(
{cellPos.x + 2, cellPos.y - 2});
m_StructureManager->getStructureArray()->buildNewStructure(
structure, true);
break;
}
case UnitType::ADV_POWER_PLANT: {
auto structure = std::make_shared<ADVPowerPlants>(house);
auto globalPos = MapUtil::CellCoordToGlobal(cellPos);
structure->Start(globalPos);
m_StructureManager->getStructureArray()->buildNewStructure(
structure, true);
break;
}
case UnitType::INFANTRY: {
auto avatar = std::make_shared<Infantry>(house);
avatar->Start(cellPos);
// avatar ->setNewDestination(cellPos);
m_AvatarManager->assignMoveOrderToAvatar(
avatar, {cellPos.x + 1, cellPos.y + 1});
m_AvatarManager->AppendAvatar(avatar);
m_troopSize += 1;
break;
}
case UnitType::NONE: {
printf("(GOM)error! try to build when type == NONE\n");
break;
}
default: {
printf("(GOM)error! try to spawn unknown type\n");
break;
}
}
}
void spawnToWayPoint(UnitType unit, HouseType house);
void spawn(UnitType unit, HouseType house, glm::vec2 cellPos);

void addUnitConstructCount(UnitType type, int value) {
unitCount[type] += value;
Expand Down
12 changes: 0 additions & 12 deletions include/Structure/IWayPoint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,7 @@

class IWayPointStructure {
private:
glm::vec2 waypointLocation = {0, 0};

public:
virtual glm::vec2 GetWayPointLocation() { return this->waypointLocation; };

void
SetWayPointLocationByCellCoord(glm::vec2 newLocation) { //=DrawLocation+Cell

newLocation = {newLocation.x * CELL_SIZE.x,
newLocation.y * CELL_SIZE.y};
this->waypointLocation = {newLocation.x + 0.5 * CELL_SIZE.x,
newLocation.y + 0.5 * CELL_SIZE.y};
};
};

#endif // PRACTICALTOOLSFORSIMPLEDESIGN_IWAYPOINT_HPP
1 change: 1 addition & 0 deletions include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class Structure : public Util::GameObject, public Selectable, public Huntable {
return MapUtil::GlobalCoordToCellCoord(m_ObjectLocation);
}
std::vector<glm::vec2> getAbsoluteOccupiedArea();
std::vector<glm::vec2> getNearbyArea();
void SetRelativeOccupiedArea(std::vector<glm::vec2> Area) {
m_RelativeOccupiedArea = Area;
}
Expand Down
31 changes: 29 additions & 2 deletions include/Structure/WayPointStructure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,48 @@
#include "Display/Line.hpp"
#include "HighLight.h"
#include "Map/MapUtility.hpp"
#include "Structure/IWayPoint.hpp"
#include "Structure/Structure.hpp"
#include "Util/Image.hpp"
#include "Util/Input.hpp"
#include "Util/Keycode.hpp"
#include "WayPoint.hpp"

class WayPointStructure : public Structure, public IWayPointStructure {
class WayPointStructure : public Structure {
protected:
std::shared_ptr<WayPoint> m_wayPoint = std::make_shared<WayPoint>();
HighLight m_HighLight;
Grid m_Grid;
Line m_Line;
std::vector<Line> m_lineVector;

glm::vec2 m_WaypointLocation = {-10, -10};
bool m_WaypointSetted = false;

public:
bool ifWaypointSetted() {
if (m_WaypointLocation == glm::vec2(-10, -10)) {
return false;
} else {
return true;
};
}
glm::vec2 getWaypointLocation() {
if (ifWaypointSetted()) {
return this->m_WaypointLocation;
} else {
return getNearbyArea()[0];
}
}

void
setWaypointLocationByCellCoord(glm::vec2 newLocation) { //=DrawLocation+Cell

newLocation = {newLocation.x * CELL_SIZE.x,
newLocation.y * CELL_SIZE.y};
this->m_WaypointLocation = {newLocation.x + 0.5 * CELL_SIZE.x,
newLocation.y + 0.5 * CELL_SIZE.y};
};

public:
WayPointStructure(
float electricPower = -10.F, float buildingTime = 10.F,
Expand Down
15 changes: 12 additions & 3 deletions include/UI/UI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ class UIClass {
std::unique_ptr<Structure> getSelectedBuilding();
bool getIfAnyBuildingReadyToBuild(); // 避免Scene收到空的getSelectedBuilding

std::shared_ptr<Avatar> getUnitFromUI();
bool getIfUnitReadyToSpawn() {
return m_UIScriptProcess->getIfReadytoSpawn();
bool ifUnitReadyToSpawn() {
if (m_UIScriptProcess->getIfReadytoSpawn()) {
m_UIScriptProcess->setIfReadytoSpawn(false);
return true;
} else {
return false;
};
}
void setIfUnitReadyToSpawn(bool b) {
m_UIScriptProcess->setIfReadytoSpawn(b);
Expand All @@ -46,6 +50,11 @@ class UIClass {
void
checkExistBuilding(std::vector<std::shared_ptr<Structure>> buildingList);

public:
UnitType getUnitTypeReadyToBeSpawned() {
return m_UIScriptProcess->GetCurrentInfType();
};

private:
std::shared_ptr<UIScriptProcess> m_UIScriptProcess =
std::make_shared<UIScriptProcess>();
Expand Down
2 changes: 1 addition & 1 deletion src/Display/CustomizableImage.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "Util/CustomizableImage.hpp"
#include "Display/CustomizableImage.hpp"

#include "pch.hpp"

Expand Down
Loading

0 comments on commit 2e6f9ab

Please sign in to comment.