Skip to content

Commit

Permalink
Merge pull request #93 from ntut-Tu/main
Browse files Browse the repository at this point in the history
move to structure fix
  • Loading branch information
jonylu7 authored Jun 14, 2024
2 parents cd6f71c + e830281 commit d1397bf
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 25 deletions.
21 changes: 14 additions & 7 deletions include/AI/AIGroupCommander.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class AIGroupCommander {
break;
case 2:
glm::vec2 targetCell = m_Map->findEnemyInRange(AUTO_FIND_RANGE,unit->getCurrentLocationInCell(),HouseType::ENEMY);
if(targetCell.x == -1 && targetCell.y == -1){
if(targetCell.x == -1.f && targetCell.y == -1.f){
return;
}
//attack
Expand All @@ -130,7 +130,7 @@ class AIGroupCommander {
if (!m_AIAvatarManager->ifAvatarHasNemesis(currentGroup.front())){
// find new target
glm::vec2 targetCell = findTargetForOffensiveUnit(currentGroup.front());
if(targetCell.x==-1.f){
if(targetCell.x<0){
printf("(updateOffensiveTroopAttackTarget) No Target\n");
break;
}
Expand All @@ -154,12 +154,19 @@ class AIGroupCommander {
}

glm::vec2 findTargetForOffensiveUnit(std::shared_ptr<Avatar> unit){
glm::vec2 targetCell = {-1.f,-1.f};
glm::vec2 targetCell = {-10.f,-10.f};
//issue: empty group should delete before this
if(false&&static_cast<int>(m_offensiveGroup.size())>=static_cast<int>(m_PlayerUnitManager->getAvatarManager()->getAvatarArray().size())){
targetCell = m_Map->findEnemyInRange(200,unit->getCurrentLocationInCell(),HouseType::ENEMY);
//attack
m_AIAvatarManager->assignAttackOrderToAvatar(unit,targetCell,HouseType::ENEMY);
if(static_cast<int>(m_offensiveGroup.size())>=static_cast<int>(m_PlayerUnitManager->getAvatarManager()->getAvatarArray().size())&&!m_PlayerUnitManager->getStructureManager()->getStructureArray()->getBuiltStructureArray().empty()){
if(!m_PlayerUnitManager->getStructureManager()->getStructureArray()->getBuiltStructureArray().empty()){
targetCell=m_PlayerUnitManager->getStructureManager()->getStructureArray()->getBuiltStructureArray().front()->getTopRightCell();
}else{
return targetCell;
}
for(auto i:m_PlayerUnitManager->getStructureManager()->getStructureArray()->getBuiltStructureArray()){
if(unit->getDistance(i->getTopRightCell())<unit->getDistance(targetCell)){
targetCell = i->getTopRightCell();
}
}
}else{
if(!m_PlayerUnitManager->getAvatarManager()->getAvatarArray().empty()){
targetCell=m_PlayerUnitManager->getAvatarManager()->getAvatarArray().front()->getCurrentLocationInCell();
Expand Down
4 changes: 4 additions & 0 deletions include/Scene/MenuScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class MenuScene : public Scene {
->getUIStatus()
->getUIStatusType() == UIStatusType::UI_CONTINUE) {
m_CurrentMode = SceneMode::SKIRMISH;
m_MenuUI.getUIStatus()->setUIStatusType(
UIStatusType::UI_DEFAULT);
m_SandBoxScene->setStage(true);
}
}
if (m_TutorialScene->getMissionAccomplishedUI()
Expand All @@ -82,6 +85,7 @@ class MenuScene : public Scene {
break;
case (SceneMode::SKIRMISH):
// skirmish update()
m_SandBoxScene->Update();
break;
}
}
Expand Down
8 changes: 6 additions & 2 deletions include/Scene/SandBoxScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define DEBUG_KEY P

class SandBoxScene : public Scene {
enum class Stages { STAGE1, STAGE2, STAGE3, STAGE4, START, END };
enum class Stages { STAGE1, STAGE2, STAGE3, STAGE4, START, END ,FORMAL_START ,FORMAL_UPDATE ,PLAYER_WON ,AI_WON};

public:
SandBoxScene(){};
Expand All @@ -22,7 +22,11 @@ class SandBoxScene : public Scene {
void Update() override;
void stageStart();
void stageUpdate();

void setStage(bool stage){
if(stage){
m_stage = Stages::FORMAL_START;
}
}
private:
SpriteSheet m_SpriteSheet;
std::shared_ptr<CursorClass> m_Cursor = std::make_shared<CursorClass>();
Expand Down
7 changes: 4 additions & 3 deletions include/Structure/Structure.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "Util/TransformUtils.hpp"
#include "glm/glm.hpp"
#define DEFAULT_ZINDEX 15
#define CHEAT 0.01
#define CHEAT 1
#define INTERVAL 50

class Structure : public Util::GameObject, public Selectable, public Huntable {
Expand Down Expand Up @@ -111,10 +111,11 @@ class Structure : public Util::GameObject, public Selectable, public Huntable {
m_Order->setStructureOrder(StructureOrderType::TAKEN_DAMAGE);
};
glm::vec2 getCurrentLocationInCell() override {
return getAbsoluteOccupiedArea()[0];
return getAbsoluteOccupiedArea().front();
};
glm::vec2 getTopRightCell(){
return getAbsoluteOccupiedArea()[-1];
glm::vec2 returnValue = getAbsoluteOccupiedArea().back();
return returnValue;
};
protected:
float m_ElectricPower;
Expand Down
14 changes: 10 additions & 4 deletions src/AI/AIScripts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//
#include "AI/AIScripts.hpp"

#define MAX_TROOPS_SIZE 25
#define MAX_TROOPS_SIZE 50
void AIScript::Start(std::shared_ptr<UnitManager> GameObjectManager,
std::shared_ptr<UnitManager> EnemyObjectManager,
std::shared_ptr<MapClass> map, bool active) {
Expand Down Expand Up @@ -105,18 +105,24 @@ void AIScript::setCDTime(float time, SpawnMode spawnMode, bool cheat) {
}
}

if (cheat) {
m_AvatarCDTime *= CHEAT;
m_buildingCDTime *= CHEAT;
if (m_EnemyObjectManager->getCheatMode()) {
m_AvatarCDTime *= 0.01;
m_buildingCDTime *= 0.01;
}
}

void AIScript::setCost(float cost, SpawnMode spawnMode) {
if (spawnMode == SpawnMode::AVATAR) {
m_avatarCost = cost;
if(m_EnemyObjectManager->getCheatMode()){
m_avatarCost = 1;
}
}
if (spawnMode == SpawnMode::BUILDINGS) {
m_buildingCost = cost;
if(m_EnemyObjectManager->getCheatMode()){
m_buildingCost = 1;
}
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/Mechanics/AvatarManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,24 @@ void AvatarManager::assignAttackOrderToAvatar(std::shared_ptr<Avatar> avatar,
void AvatarManager::assignAttackOrderToAvatar(std::shared_ptr<Avatar> avatar,
glm::vec2 destcell,HouseType myHouse) {
m_NemesisManager->removeNemesis(avatar);
if (m_Map->getTileByCellPosition(destcell)->ifEnemyAtTile(myHouse)) {
bool condition = m_Map->getTileByCellPosition(destcell)->ifEnemyAtTile(myHouse);
if (condition) {
if (m_Map->getTileByCellPosition(destcell)->ifStructureExists()) {
m_NemesisManager->addNemesis(
avatar, m_Map->getTileByCellPosition(destcell)->getStructure());
auto queue =
m_Navigator->findPath(avatar->getMoving()->getCurrentCell(), {destcell.x+1,destcell.y+1});
avatar->getMoving()->setMovePath(queue);
avatar->getAvatarOrder()->setAvatarOrder(AvatarOrderType::CHASE);
} else {
m_NemesisManager->addNemesis(
avatar,
m_Map->getTileByCellPosition(destcell)->getAvatars()[0]);
auto queue =
m_Navigator->findPath(avatar->getMoving()->getCurrentCell(), destcell);
avatar->getMoving()->setMovePath(queue);
avatar->getAvatarOrder()->setAvatarOrder(AvatarOrderType::CHASE);
}
auto queue =
m_Navigator->findPath(avatar->getMoving()->getCurrentCell(), destcell);
avatar->getMoving()->setMovePath(queue);
avatar->getAvatarOrder()->setAvatarOrder(AvatarOrderType::CHASE);
}
}

Expand Down
30 changes: 28 additions & 2 deletions src/Scene/SandBoxScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void SandBoxScene::Start() {
void SandBoxScene::Update() {
m_GameObjectManager->Update();
m_EnemyObjectManager->Update();
if (m_stage != Stages::START) {
if (m_stage != Stages::START && m_stage != Stages::FORMAL_START) {
m_EnemyScripts->Update();
}
stageUpdate();
Expand Down Expand Up @@ -54,6 +54,14 @@ void SandBoxScene::Update() {
m_GameObjectManager->spawnToWayPoint(
m_UI->getUnitTypeReadyToBeSpawned(), HouseType::MY);
}
if(m_stage==Stages::FORMAL_UPDATE){
if(m_GameObjectManager->getAvatarManager()->getAvatarArray().empty()&&m_GameObjectManager->getStructureManager()->getStructureArray()->getBuiltStructureArray().empty()){
m_stage=Stages::AI_WON;
}
if(m_EnemyObjectManager->getAvatarManager()->getAvatarArray().empty()&&m_EnemyObjectManager->getStructureManager()->getStructureArray()->getBuiltStructureArray().empty()){
m_stage=Stages::PLAYER_WON;
}
}
}
void SandBoxScene::stageStart() {
switch (m_stage) {
Expand Down Expand Up @@ -107,6 +115,11 @@ void SandBoxScene::stageStart() {
m_stage = Stages::END;
break;
}
case Stages::FORMAL_UPDATE:{
m_GameObjectManager->spawn(UnitType::INFANTRY, HouseType::MY, {7, 9});
m_GameObjectManager->spawn(UnitType::INFANTRY, HouseType::ENEMY, {18, 18});
break;
}
}
}
void SandBoxScene::stageUpdate() {
Expand All @@ -127,16 +140,29 @@ void SandBoxScene::stageUpdate() {
m_stage = Stages::STAGE3;
m_EnemyScripts->Start(m_GameObjectManager, m_EnemyObjectManager,
m_Map);

stageStart();
}
if (Util::Input::IsKeyPressed(Util::Keycode::NUM_4)) {
m_stage = Stages::STAGE4;
m_EnemyScripts->Start(m_GameObjectManager, m_EnemyObjectManager,
m_Map, true);
m_EnemyObjectManager->setCheatMode(true);
stageStart();
}
}else if(m_stage == Stages::FORMAL_START){
m_stage = Stages::FORMAL_UPDATE;
m_EnemyScripts->Start(m_GameObjectManager, m_EnemyObjectManager,
m_Map, true);
stageStart();
}

if(m_stage == Stages::PLAYER_WON){

}else if(m_stage == Stages::AI_WON){

}
if (Util::Input::IsKeyPressed(Util::Keycode::DEBUG_KEY )) {
m_EnemyObjectManager->addTotalCurrency(500);
m_EnemyObjectManager->setCheatMode(true);
}
}
4 changes: 2 additions & 2 deletions src/Structure/WarFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void WarFactory::Start() {
"../assets/sprites/WarFactory_SpriteSheet.png", 72, 48, 15, 0);
m_SpriteSheetAnimation->initSpriteSheetAnimation(m_StructureSpriteSheet,
false, INTERVAL, false);
SetRelativeOccupiedArea({{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}});
SetRelativeOccupiedArea({{0, 0}, {0, 1}, {1, 0}, {1, 1}, {2, 0},{2, 1}});
m_wayPoint->SetDrawable(
std::make_unique<Util::Image>("../assets/sprites/flagB.png"));
m_HighLight.SetDrawable(
Expand All @@ -26,7 +26,7 @@ void WarFactory::Start(glm::vec2 location) {
// Set Texture----------------------------------------
m_StructureSpriteSheet->Start(
"../assets/sprites/WarFactory_SpriteSheet.png", 72, 48, 15, 0);
SetRelativeOccupiedArea({{0, 0}, {0, 1}, {1, 0}, {1, 1}, {2, 1}, {2, 0}});
SetRelativeOccupiedArea({{0, 0}, {0, 1}, {1, 0}, {1, 1}, {2, 0},{2, 1}});
m_wayPoint->SetDrawable(
std::make_unique<Util::Image>("../assets/sprites/flagB.png"));
m_HighLight.SetDrawable(
Expand Down

0 comments on commit d1397bf

Please sign in to comment.