From 179f4c28acb1f4686fbf6537f478abaa38ff63e1 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 30 Oct 2021 08:09:30 +0300 Subject: [PATCH] factored out a state manager using GameManager via a singleton --- abstractBitmapText.cpp | 3 +- abstractBitmapText.h | 2 +- abstractImage.cpp | 5 +- abstractImage.h | 3 +- activeGameState.cpp | 125 ++++++++++++++++++++++--------------- activeGameState.h | 7 +-- aircraftCarrier.cpp | 6 +- aircraftCarrier.h | 2 +- button.cpp | 9 +-- button.h | 3 +- checkbox.cpp | 9 ++- checkbox.h | 5 +- consoleCommand.cpp | 17 +++-- consoleCommand.h | 8 +-- cruiser.cpp | 4 +- cruiser.h | 2 +- demoJet.cpp | 11 +++- demoJet.h | 2 +- depthCharge.cpp | 12 +++- depthCharge.h | 2 +- destroyer.cpp | 7 ++- destroyer.h | 2 +- eventListener.cpp | 34 ++++++---- exitButton.cpp | 4 +- exitButton.h | 2 +- gameManager.cpp | 61 +++++++----------- gameManager.h | 16 +++-- guiAppState.cpp | 25 +++++--- guiAppState.h | 3 +- guidedMissile.cpp | 4 +- guidedMissile.h | 2 +- inGameAppState.cpp | 121 ++++++++++++++++++++++-------------- inGameAppState.h | 26 ++++---- jet.cpp | 2 +- jet.h | 2 +- listbox.cpp | 36 ++++++----- listbox.h | 7 +-- main.cpp | 26 ++++---- map.cpp | 10 +-- map.h | 8 +-- missile.cpp | 6 +- missile.h | 2 +- missileJet.cpp | 17 +++-- missileJet.h | 2 +- optionsButton.cpp | 138 ++++++++++++++++++++++++----------------- optionsButton.h | 20 +++--- player.cpp | 3 +- player.h | 4 +- projectile.cpp | 24 ++++--- projectile.h | 3 +- shell.cpp | 2 +- shell.h | 2 +- slider.cpp | 20 +++--- slider.h | 7 +-- stateManager.cpp | 45 ++++++++++++++ stateManager.h | 27 ++++++++ submarine.cpp | 4 +- submarine.h | 2 +- textbox.cpp | 15 ++--- textbox.h | 6 +- tooltip.cpp | 5 +- tooltip.h | 2 +- torpedo.cpp | 4 +- torpedo.h | 2 +- unit.cpp | 37 ++++++----- unit.h | 3 +- util.cpp | 68 +++++++++++--------- util.h | 2 +- vessel.cpp | 16 ++--- vessel.h | 5 +- 70 files changed, 654 insertions(+), 474 deletions(-) create mode 100644 stateManager.cpp create mode 100644 stateManager.h diff --git a/abstractBitmapText.cpp b/abstractBitmapText.cpp index b974f5a..4e8c2b9 100644 --- a/abstractBitmapText.cpp +++ b/abstractBitmapText.cpp @@ -8,8 +8,7 @@ using namespace game::core; namespace game{ namespace gui{ - AbstractBitmapText::AbstractBitmapText(GameManager *gM, stringw text, vector2d pos, IGUIFont *font) { - gameManager = gM; + AbstractBitmapText::AbstractBitmapText(stringw text, vector2d pos, IGUIFont *font) { this->text = text; this->pos = pos; this->font = font; diff --git a/abstractBitmapText.h b/abstractBitmapText.h index 86ff9fd..582435e 100644 --- a/abstractBitmapText.h +++ b/abstractBitmapText.h @@ -7,7 +7,7 @@ namespace game{ namespace gui{ class AbstractBitmapText { public: - AbstractBitmapText(core::GameManager*, irr::core::stringw, irr::core::vector2d, irr::gui::IGUIFont*); + AbstractBitmapText(irr::core::stringw, irr::core::vector2d, irr::gui::IGUIFont*); ~AbstractBitmapText(); void update(); inline irr::core::vector2d getPos(){return pos;} diff --git a/abstractImage.cpp b/abstractImage.cpp index e044e33..75ede9c 100644 --- a/abstractImage.cpp +++ b/abstractImage.cpp @@ -7,8 +7,7 @@ using namespace game::core; namespace game { namespace gui{ - AbstractImage::AbstractImage(GameManager *gM, ITexture *image, vector2d pos, vector2d size) { - gameManager = gM; + AbstractImage::AbstractImage(ITexture *image, vector2d pos, vector2d size) { this->image = image; this->pos = pos; this->size = size; @@ -18,7 +17,7 @@ namespace game } void AbstractImage::update() { - irr::video::IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + irr::video::IVideoDriver *driver = GameManager::getSingleton()->getDevice()->getVideoDriver(); driver->draw2DImage(image, pos); } } diff --git a/abstractImage.h b/abstractImage.h index 3c2eb53..d8d21b4 100644 --- a/abstractImage.h +++ b/abstractImage.h @@ -9,7 +9,7 @@ namespace game namespace gui{ class AbstractImage { public: - AbstractImage(core::GameManager*, irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d); + AbstractImage(irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d); ~AbstractImage(); void update(); inline irr::video::ITexture* getImage(){return image;} @@ -20,7 +20,6 @@ namespace game inline void setSize(irr::core::vector2d s){this->size=s;} inline irr::core::stringw getPath(){return path;} private: - core::GameManager *gameManager; irr::video::ITexture *image; irr::core::vector2d pos, size; irr::core::stringw path; diff --git a/activeGameState.cpp b/activeGameState.cpp index 1279859..d35f654 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -1,5 +1,6 @@ #include "activeGameState.h" #include "inGameAppState.h" +#include "stateManager.h" #include "util.h" #include "cruiser.h" #include "destroyer.h" @@ -21,7 +22,7 @@ namespace game{ namespace core{ const int size=50; - ActiveGameState::UnitButton::UnitButton(GameManager *gm,ActiveGameState *activeState,vector2di pos, vector2di size, irr::core::stringw name,int faction,int unitId) : gui::Button(gm,pos,size,name,true){ + ActiveGameState::UnitButton::UnitButton(ActiveGameState *activeState,vector2di pos, vector2di size, irr::core::stringw name,int faction,int unitId) : gui::Button(pos,size,name,true){ this->activeState=activeState; this->faction=faction; this->unitId=unitId; @@ -33,19 +34,19 @@ namespace game{ Unit *u; switch(unitData::unitType[unitId]){ case unitData::UNIT_TYPE::VESSEL: - u=new Vessel(gameManager,player,spawnPoint,unitId); + u=new Vessel(player,spawnPoint,unitId); break; case unitData::UNIT_TYPE::DESTROYER: - u=new Destroyer(gameManager,player,spawnPoint,unitId); + u=new Destroyer(player,spawnPoint,unitId); break; case unitData::UNIT_TYPE::CRUISER: - u=new Cruiser(gameManager,player,spawnPoint,unitId); + u=new Cruiser(player,spawnPoint,unitId); break; case unitData::UNIT_TYPE::AIRCRAFT_CARRIER: - u=new AircraftCarrier(gameManager,player,spawnPoint,unitId); + u=new AircraftCarrier(player,spawnPoint,unitId); break; case unitData::UNIT_TYPE::SUBMARINE: - u=new Submarine(gameManager,player,spawnPoint,unitId); + u=new Submarine(player,spawnPoint,unitId); break; } player->addUnit(u); @@ -53,23 +54,25 @@ namespace game{ void ActiveGameState::UnitButton::onMouseOver(){ if(!mouseOverDone){ - GuiAppState *guiState=((GuiAppState*)gameManager->getAppState(AppStateTypes::GUI_STATE)); - guiState->addTooltip(new gui::Tooltip(gameManager,pos-vector2di(100,0),name)); + GameManager *gm = GameManager::getSingleton(); + GuiAppState *guiState=((GuiAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::GUI_STATE)); + guiState->addTooltip(new gui::Tooltip(pos-vector2di(100,0),name)); } } void ActiveGameState::UnitButton::onMouseAway(){ if(!mouseAwayDone){ - GuiAppState *guiState=((GuiAppState*)gameManager->getAppState(AppStateTypes::GUI_STATE)); + GuiAppState *guiState=((GuiAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::GUI_STATE)); guiState->removeAllTooltips(); } } - ActiveGameState::UnitActionButton::UnitActionButton(GameManager *gm, unitData::UNIT_TYPE type, vector2di pos, vector2di size, stringw name, stringw path) : gui::Button(gm,pos,size,name,true){ + ActiveGameState::UnitActionButton::UnitActionButton(unitData::UNIT_TYPE type, vector2di pos, vector2di size, stringw name, stringw path) : gui::Button(pos,size,name,true){ this->type=type; - IVideoDriver *driver=gameManager->getDevice()->getVideoDriver(); - activeState=((ActiveGameState*)gm->getAppState(AppStateTypes::ACTIVE_STATE)); - setImageButton(new Image(gameManager,driver->getTexture(path),pos,vector2di(50,50))); + GameManager *gm = GameManager::getSingleton(); + IVideoDriver *driver = gm->getDevice()->getVideoDriver(); + activeState=((ActiveGameState*)gm->getStateManager()->getAppState(AppStateTypes::ACTIVE_STATE)); + setImageButton(new Image(driver->getTexture(path),pos,vector2di(50,50))); } void ActiveGameState::UnitActionButton::onClick(){ @@ -81,27 +84,28 @@ namespace game{ void ActiveGameState::UnitActionButton::onMouseOver(){ if(!mouseOverDone){ - GuiAppState *guiState=((GuiAppState*)gameManager->getAppState(AppStateTypes::GUI_STATE)); - guiState->addTooltip(new gui::Tooltip(gameManager,pos-vector2di(100,0),name)); + GuiAppState *guiState=((GuiAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::GUI_STATE)); + guiState->addTooltip(new gui::Tooltip(pos-vector2di(100,0),name)); } } void ActiveGameState::UnitActionButton::onMouseAway(){ if(!mouseAwayDone){ - GuiAppState *guiState=((GuiAppState*)gameManager->getAppState(AppStateTypes::GUI_STATE)); + GuiAppState *guiState=((GuiAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::GUI_STATE)); guiState->removeAllTooltips(); } } - ActiveGameState::ActiveGameState(GameManager *gM, GuiAppState *guiState, Map *map, vector players, int playerId) { - gameManager = gM; + ActiveGameState::ActiveGameState(GuiAppState *guiState, Map *map, vector players, int playerId) { type = AppStateTypes::ACTIVE_STATE; this->guiState = guiState; this->map = map; this->players = players; - cam = gM->getDevice()->getSceneManager()->addCameraSceneNodeFPS(0, 100, 0.f, -1, nullptr, 10, false, 0.6f); + + GameManager *gm = GameManager::getSingleton(); + cam = gm->getDevice()->getSceneManager()->addCameraSceneNodeFPS(0, 100, 0.f, -1, nullptr, 10, false, 0.6f); cam->setInputReceiverEnabled(false); - gameManager->getDevice()->getCursorControl()->setVisible(true); + gm->getDevice()->getCursorControl()->setVisible(true); this->playerId = playerId; mainPlayer = players[playerId]; cam->setPosition(vector3df(0, 5, 0)); @@ -114,18 +118,22 @@ namespace game{ void ActiveGameState::onAttachment() { AbstractAppState::onAttachment(); - IVideoDriver *driver=gameManager->getDevice()->getVideoDriver(); - int faction=mainPlayer->getFaction(),width=gameManager->getWidth(),heigh=gameManager->getHeight(),size=50; - stringw names[5]={"Battleship","Destroyer","Cruiser","Carrier","Submarine"}; + GameManager *gm = GameManager::getSingleton(); + IVideoDriver *driver = gm->getDevice()->getVideoDriver(); + int faction = mainPlayer->getFaction(), width = gm->getWidth(), height = gm->getHeight(),size=50; + stringw names[5] = {"Battleship","Destroyer","Cruiser","Carrier","Submarine"}; + for(int i=0;i<5;i++){ int id=2*i+(i==4&&faction==1?0:faction); vector2di pos=vector2di(width-size-1,1+i*size),sizeVec=vector2di(size,size); - UnitButton *button=new UnitButton(gameManager,this,pos,sizeVec,names[i],faction,id); + UnitButton *button=new UnitButton(this,pos,sizeVec,names[i],faction,id); + if(i==3) names[i]="aircraftCarrier"; else names[i].make_lower(); - button->setImageButton(new Image(gameManager,driver->getTexture(PATH+"Textures/Icons/"+names[i]+"0"+stringw(faction)+".png"),pos,sizeVec)); + + button->setImageButton(new Image(driver->getTexture(PATH+"Textures/Icons/"+names[i]+"0"+stringw(faction)+".png"),pos,sizeVec)); guiState->addButton(button); } } @@ -183,7 +191,7 @@ namespace game{ } void ActiveGameState::renderUnits(vector units) { - ISceneManager *smgr = gameManager->getDevice()->getSceneManager(); + ISceneManager *smgr = GameManager::getSingleton()->getDevice()->getSceneManager(); for (Unit *rendUn : units) { vector3df rendUnPos = rendUn->getPos(); rendUnPos.Y = 0; @@ -202,8 +210,10 @@ namespace game{ } void ActiveGameState::renderGUIBorders(){ - IVideoDriver *driver=gameManager->getDevice()->getVideoDriver(); - int width=gameManager->getWidth(),height=gameManager->getHeight(),size=50; + GameManager *gm = GameManager::getSingleton(); + IVideoDriver *driver = gm->getDevice()->getVideoDriver(); + int width = gm->getWidth(), height = gm->getHeight(), size = 50; + for(int i=0;idraw2DRectangleOutline(recti(vector2di(width-(2*size+3),size*i),dimension2di(size+2,size+2))); driver->draw2DRectangleOutline(recti(vector2di(width-(size+2),size*i),dimension2di(size+2,size+2))); @@ -213,7 +223,7 @@ namespace game{ void ActiveGameState::renderActionButtons(){ class MakeJetButton : public UnitActionButton{ public: - MakeJetButton(GameManager *gm,int faction,unitData::UNIT_TYPE type,vector2di pos, vector2di size):UnitActionButton(gm,type,pos,size,"Jet",PATH+"Textures/Icons/jet0"+stringw(faction)+".png"){ + MakeJetButton(int faction,unitData::UNIT_TYPE type,vector2di pos, vector2di size):UnitActionButton(type,pos,size,"Jet",PATH+"Textures/Icons/jet0"+stringw(faction)+".png"){ } ~MakeJetButton(){} void onClick(){ @@ -225,7 +235,7 @@ namespace game{ }; class LaunchButton : public UnitActionButton{ public: - LaunchButton(GameManager *gm,int faction,unitData::UNIT_TYPE type,vector2di pos,vector2di size) : UnitActionButton(gm,type,pos,size,"Launch",PATH+"Textures/Icons/guidedMissile0"+stringw(faction)+".png"){} + LaunchButton(int faction,unitData::UNIT_TYPE type,vector2di pos,vector2di size) : UnitActionButton(type,pos,size,"Launch",PATH+"Textures/Icons/guidedMissile0"+stringw(faction)+".png"){} ~LaunchButton(){} void onClick(){ UnitActionButton::onClick(); @@ -235,7 +245,7 @@ namespace game{ }; class MissileButton : public UnitActionButton{ public: - MissileButton(GameManager *gm,bool aam,vector2di pos,vector2di size,stringw name,stringw path) : UnitActionButton(gm,unitData::UNIT_TYPE::MISSILE_JET,pos,size,name,path){ + MissileButton(bool aam,vector2di pos,vector2di size,stringw name,stringw path) : UnitActionButton(unitData::UNIT_TYPE::MISSILE_JET,pos,size,name,path){ this->aam=aam; } ~MissileButton(){} @@ -247,8 +257,11 @@ namespace game{ private: bool aam; }; - int width=gameManager->getWidth(),height=gameManager->getHeight(),slotSize=size+2,faction=mainPlayer->getFaction(); - bool carriers=false,cruisers=false,missileJets=false; + + GameManager *gm = GameManager::getSingleton(); + int width = gm->getWidth(), height = gm->getHeight(), slotSize = size + 2, faction = mainPlayer->getFaction(); + bool carriers=false, cruisers=false, missileJets=false; + for(int i=0;igetType()==unitData::UNIT_TYPE::AIRCRAFT_CARRIER) carriers=true; @@ -258,7 +271,7 @@ namespace game{ missileJets=true; } if(carriers&&!actionButtons[0]){ - actionButtons[0]=new MakeJetButton(gameManager,faction,unitData::UNIT_TYPE::AIRCRAFT_CARRIER,vector2di(width-2*slotSize,0),vector2di(size,size)); + actionButtons[0]=new MakeJetButton(faction,unitData::UNIT_TYPE::AIRCRAFT_CARRIER,vector2di(width-2*slotSize,0),vector2di(size,size)); guiState->addButton(actionButtons[0]); } else if(!carriers&&actionButtons[0]){ @@ -266,7 +279,7 @@ namespace game{ actionButtons[0]=nullptr; } if(cruisers&&!actionButtons[1]){ - actionButtons[1]=new LaunchButton(gameManager,faction,unitData::UNIT_TYPE::CRUISER,vector2di(width-2*slotSize,slotSize),vector2di(size,size)); + actionButtons[1]=new LaunchButton(faction,unitData::UNIT_TYPE::CRUISER,vector2di(width-2*slotSize,slotSize),vector2di(size,size)); guiState->addButton(actionButtons[1]); } else if(!cruisers&&actionButtons[1]){ @@ -274,8 +287,8 @@ namespace game{ actionButtons[1]=nullptr; } if(missileJets&&!actionButtons[2]){ - actionButtons[2]=new MissileButton(gameManager,true,vector2di(width-2*slotSize,slotSize*3),vector2di(size,size),"AAM",PATH+"Textures/Icons/aam.png"); - actionButtons[3]=new MissileButton(gameManager,false,vector2di(width-2*slotSize,slotSize*4),vector2di(size,size),"AWM",PATH+"Textures/Icons/awm.png"); + actionButtons[2]=new MissileButton(true,vector2di(width-2*slotSize,slotSize*3),vector2di(size,size),"AAM",PATH+"Textures/Icons/aam.png"); + actionButtons[3]=new MissileButton(false,vector2di(width-2*slotSize,slotSize*4),vector2di(size,size),"AWM",PATH+"Textures/Icons/awm.png"); guiState->addButton(actionButtons[2]); guiState->addButton(actionButtons[3]); } @@ -297,8 +310,10 @@ namespace game{ } void ActiveGameState::updateSelectionBox() { - vector2d mousePos = gameManager->getDevice()->getCursorControl()->getPosition(); - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + GameManager *gm = GameManager::getSingleton(); + vector2d mousePos = gm->getDevice()->getCursorControl()->getPosition(); + IVideoDriver *driver = gm->getDevice()->getVideoDriver(); + if (mousePos.X >= clickPoint.X && mousePos.Y >= clickPoint.Y) { selectionBoxOrigin = vector2d(clickPoint.X, clickPoint.Y); selectionBoxEnd = vector2d(mousePos.X, mousePos.Y); @@ -316,10 +331,12 @@ namespace game{ } void ActiveGameState::updateVectors() { - vector2d cursorPos = gameManager->getDevice()->getCursorControl()->getPosition(); + GameManager *gm = GameManager::getSingleton(); + vector2d cursorPos = gm->getDevice()->getCursorControl()->getPosition(); camDir = (cam->getTarget() - cam->getPosition()).normalize(); float vecAngle = getAngleBetween(vector3df(0, 0, 1), vector3df(camDir.X, 0, camDir.Z)); quaternion rotQuat; + if (quaternion(0, 0, 0, 0).fromAngleAxis(vecAngle, vector3df(0, 1, 0)) * vector3df(0, 0, 1) != vector3df(camDir.X, 0, camDir.Z).normalize()) { rotQuat.fromAngleAxis(-vecAngle + PI / 2, vector3df(0, 1, 0)); camLeft = rotQuat * vector3df(0, 0, -1); @@ -329,19 +346,19 @@ namespace game{ } vector3df forwVec = quaternion(0, 0, 0, 0).fromAngleAxis(PI / 2, vector3df(0, 1, 0)) * camLeft; camUp = quaternion(0, 0, 0, 0).fromAngleAxis(PI / 2, camLeft.normalize()) * camDir; - if (cursorPos.X == 0 && cursorPos.Y < gameManager->getHeight() && cursorPos.Y > 0) { + if (cursorPos.X == 0 && cursorPos.Y < gm->getHeight() && cursorPos.Y > 0) { cam->setPosition(cam->getPosition() + camLeft * camPanSpeed); cam->setTarget(cam->getPosition() + camDir); }//<- - if (cursorPos.X >= gameManager->getWidth() - 1 && cursorPos.Y < gameManager->getHeight() && cursorPos.Y > 0) { + if (cursorPos.X >= gm->getWidth() - 1 && cursorPos.Y < gm->getHeight() && cursorPos.Y > 0) { cam->setPosition(cam->getPosition() - camLeft * camPanSpeed); cam->setTarget(cam->getPosition() + camDir); }//-> - if (cursorPos.X > 0 && cursorPos.X < gameManager->getWidth() && cursorPos.Y == 0) { + if (cursorPos.X > 0 && cursorPos.X < gm->getWidth() && cursorPos.Y == 0) { cam->setPosition(cam->getPosition() + (forwVec.normalize()) * camPanSpeed); cam->setTarget(cam->getPosition() + camDir); }//^ - if (cursorPos.X > 0 && cursorPos.X < gameManager->getWidth() && cursorPos.Y >= gameManager->getHeight() - 1) { + if (cursorPos.X > 0 && cursorPos.X < gm->getWidth() && cursorPos.Y >= gm->getHeight() - 1) { cam->setPosition(cam->getPosition() - (forwVec.normalize()) * camPanSpeed); cam->setTarget(cam->getPosition() + camDir); } @@ -368,13 +385,14 @@ namespace game{ vector3df topLeft=cam->getViewFrustum()->getNearLeftUp(); vector3df topRight=cam->getViewFrustum()->getNearRightUp(); vector3df bottomLeft=cam->getViewFrustum()->getNearLeftDown(); - vector2d mousePos=gameManager->getDevice()->getCursorControl()->getPosition(); + GameManager *gm = GameManager::getSingleton(); + vector2d mousePos = gm->getDevice()->getCursorControl()->getPosition(); vector3df p=topLeft; - p+=(topRight-topLeft)*((float)mousePos.X/gameManager->getWidth()); - p+=(bottomLeft-topLeft)*((float)mousePos.Y/gameManager->getHeight()); + p+=(topRight-topLeft)*((float)mousePos.X / gm->getWidth()); + p+=(bottomLeft-topLeft)*((float)mousePos.Y / gm->getHeight()); vector3df orderDir=(p-camPos).normalize(); - ISceneManager *smgr = gameManager->getDevice()->getSceneManager(); + ISceneManager *smgr = gm->getDevice()->getSceneManager(); ISceneCollisionManager *collMan = smgr->getSceneCollisionManager(); line3d ray; ray.start = camPos; @@ -382,6 +400,7 @@ namespace game{ triangle3df t; vector3df collPoint; ISceneNode *collNode = collMan->getSceneNodeAndCollisionPointFromRay(ray, collPoint, t, 0, 0); + if (collNode) { for (Player *p : players) for (int i = 0; i < p->getNumberOfUnits(); i++) @@ -392,6 +411,7 @@ namespace game{ else { if(orderDir.Y>0) orderDir.Y*=-1; + float angle = getAngleBetween(vector3df(0, -1, 0), orderDir); float hyp = camPos.Y / cos(angle); vector3df *v=new vector3df(orderDir * hyp + camPos); @@ -400,21 +420,24 @@ namespace game{ } if (!selectingPatrolPoints) { Order::TYPE t = Order::TYPE::MOVE; + if (selectingGuidedMissileTarget) t = Order::TYPE::LAUNCH; else if (controlPressed) t = Order::TYPE::ATTACK; + issueOrder(t, orderPos, false); } } void ActiveGameState::onAction(Bind bind, bool isPressed) { - InGameAppState *inGameState = (InGameAppState*) gameManager->getAppState(AppStateTypes::IN_GAME_STATE); + GameManager *gm = GameManager::getSingleton(); + InGameAppState *inGameState = (InGameAppState*) gm->getStateManager()->getAppState(AppStateTypes::IN_GAME_STATE); switch(bind){ case DRAG_BOX: isSelectionBox = isPressed; if (isPressed) { - clickPoint = gameManager->getDevice()->getCursorControl()->getPosition(); + clickPoint = gm->getDevice()->getCursorControl()->getPosition(); if (!selectedUnits.empty()) addPos(); } @@ -454,7 +477,7 @@ namespace game{ break; case LOOK_AROUND: cam->setInputReceiverEnabled(isPressed); - gameManager->getDevice()->getCursorControl()->setVisible(!isPressed); + gm->getDevice()->getCursorControl()->setVisible(!isPressed); lookingAround=isPressed; break; case HALT: diff --git a/activeGameState.h b/activeGameState.h index 6086f3f..9d1f941 100755 --- a/activeGameState.h +++ b/activeGameState.h @@ -12,7 +12,7 @@ namespace game{ namespace core{ class ActiveGameState : public AbstractAppState { public: - ActiveGameState(core::GameManager*, GuiAppState*, content::Map*, std::vector players, int); + ActiveGameState(GuiAppState*, content::Map*, std::vector players, int); ~ActiveGameState(); void onAttachment(); void onDetachment(); @@ -26,7 +26,7 @@ namespace game{ private: class UnitButton : public gui::Button{ public: - UnitButton(GameManager*,ActiveGameState*,vector2di, vector2di, irr::core::stringw,int,int); + UnitButton(ActiveGameState*,vector2di, vector2di, irr::core::stringw,int,int); ~UnitButton(); virtual void onClick(); virtual void onMouseOver(); @@ -37,7 +37,7 @@ namespace game{ }; class UnitActionButton : public gui::Button{ public: - UnitActionButton(GameManager*, content::unitData::UNIT_TYPE, irr::core::vector2di, irr::core::vector2di, irr::core::stringw, irr::core::stringw); + UnitActionButton(content::unitData::UNIT_TYPE, irr::core::vector2di, irr::core::vector2di, irr::core::stringw, irr::core::stringw); ~UnitActionButton(); virtual void onClick(); virtual void onMouseOver(); @@ -56,7 +56,6 @@ namespace game{ void addPos(); void issueOrder(content::Order::TYPE, std::vector, bool); void lookAround(bool); - GameManager *gameManager; GuiAppState *guiState; content::Map *map; std::vector players; diff --git a/aircraftCarrier.cpp b/aircraftCarrier.cpp index 6dd45cf..405de93 100755 --- a/aircraftCarrier.cpp +++ b/aircraftCarrier.cpp @@ -7,7 +7,7 @@ using namespace game::core; namespace game{ namespace content{ - AircraftCarrier::AircraftCarrier(GameManager *gM, Player *player,vector3df pos, int unitId) : Vessel(gM, player,pos, unitId) { + AircraftCarrier::AircraftCarrier(Player *player,vector3df pos, int unitId) : Vessel(player,pos, unitId) { maxNumJets=unitData::numJets[unitId]; jets=new Jet*[maxNumJets]; for(int i=0;iid == 6) { id = 9; - j = (MissileJet*)new MissileJet(gameManager, player,getJetPos(slot), id); + j = (MissileJet*)new MissileJet(player,getJetPos(slot), id); } else { id = 10; - j = (DemoJet*)new DemoJet(gameManager, player,getJetPos(slot), id); + j = (DemoJet*)new DemoJet(player,getJetPos(slot), id); } j->setJetId(slot); jets[slot]=j; diff --git a/aircraftCarrier.h b/aircraftCarrier.h index fcf79f6..b7e42b9 100755 --- a/aircraftCarrier.h +++ b/aircraftCarrier.h @@ -10,7 +10,7 @@ namespace game{ namespace content{ class AircraftCarrier : public Vessel { public: - AircraftCarrier(core::GameManager*, Player*, irr::core::vector3df, int); + AircraftCarrier(Player*, irr::core::vector3df, int); ~AircraftCarrier(); void makeJet(); inline void setJet(int i, Jet *j){this->jets[i]=j;} diff --git a/button.cpp b/button.cpp index b3159ea..31ecc28 100755 --- a/button.cpp +++ b/button.cpp @@ -11,8 +11,7 @@ namespace game{ namespace gui{ Button::Button() {} - Button::Button(GameManager *gM, vector2d pos, vector2d size, stringw name, bool separate) { - gameManager = gM; + Button::Button(vector2d pos, vector2d size, stringw name, bool separate) { color = new SColor(255, 200, 200, 200); this->name = name; this->pos = pos; @@ -26,11 +25,13 @@ namespace game{ } void Button::update() { - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + GameManager *gm = GameManager::getSingleton(); + IVideoDriver *driver = gm->getDevice()->getVideoDriver(); + if (!imageButton) { driver->draw2DRectangle(*color, rect(pos.X, pos.Y, pos.X + size.X, pos.Y + size.Y), nullptr); if (separate) { - IGUIFont *font = gameManager->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fontcourier.bmp"); + IGUIFont *font = gm->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fontcourier.bmp"); font->draw(name, rect(pos.X + size.X / 4 + textXOffset, pos.Y + size.Y / 4 + textYOffset, size.X, size.Y), SColor(255, 250, 250, 250)); } } else diff --git a/button.h b/button.h index 6445f8e..bad37e4 100755 --- a/button.h +++ b/button.h @@ -14,7 +14,7 @@ namespace game{ class Button { public: Button(); - Button(core::GameManager*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + Button(irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); ~Button(); virtual void onClick(){} virtual void onMouseOver(){} @@ -45,7 +45,6 @@ namespace game{ protected: irr::core::stringw name; irr::core::vector2d pos, size; - core::GameManager *gameManager = nullptr; irr::video::SColor *color = nullptr; bool mouseOverDone=false,mouseAwayDone=false; }; diff --git a/checkbox.cpp b/checkbox.cpp index fff11e8..acb1293 100755 --- a/checkbox.cpp +++ b/checkbox.cpp @@ -7,7 +7,7 @@ using namespace game::core; namespace game{ namespace gui{ - Checkbox::CheckboxButton::CheckboxButton(GameManager *gM, Checkbox *ch, vector2d pos, vector2d size, stringw name, bool separate) :Button(gM, pos, size, name, separate) { + Checkbox::CheckboxButton::CheckboxButton(Checkbox *ch, vector2d pos, vector2d size, stringw name, bool separate) :Button(pos, size, name, separate) { checkbox = ch; } @@ -15,10 +15,9 @@ namespace game{ checkbox->check(); } - Checkbox::Checkbox(GameManager *gM,vector2d pos){ - gameManager = gM; + Checkbox::Checkbox(vector2d pos){ this->pos = pos; - checkboxButton = new CheckboxButton(gM, this, pos, vector2d(length,length), "CheckboxButton", false); + checkboxButton = new CheckboxButton(this, pos, vector2d(length,length), "CheckboxButton", false); } Checkbox::~Checkbox() { @@ -26,7 +25,7 @@ namespace game{ } void Checkbox::update() { - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + IVideoDriver *driver = GameManager::getSingleton()->getDevice()->getVideoDriver(); driver->draw2DRectangle(SColor(255, 100, 100, 100), rect(pos.X, pos.Y, pos.X + length, pos.Y + length), nullptr); if (checked) { driver->draw2DLine(pos, vector2d(pos.X + length, pos.Y + length),SColor(255,255,255,255)); diff --git a/checkbox.h b/checkbox.h index 7e630e6..c1c3e74 100755 --- a/checkbox.h +++ b/checkbox.h @@ -8,7 +8,7 @@ namespace game{ namespace gui { class Checkbox { public: - Checkbox(core::GameManager*, irr::core::vector2d); + Checkbox(irr::core::vector2d); ~Checkbox(); void update(); virtual void check(){checked=!checked;} @@ -16,14 +16,13 @@ namespace game{ private: class CheckboxButton : public Button { public: - CheckboxButton(core::GameManager*, Checkbox*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + CheckboxButton(Checkbox*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); private: Checkbox *checkbox = nullptr; }; const int length=15; bool checked = false; - core::GameManager *gameManager; irr::core::vector2d pos; CheckboxButton *checkboxButton; public: diff --git a/consoleCommand.cpp b/consoleCommand.cpp index a19886e..207c124 100755 --- a/consoleCommand.cpp +++ b/consoleCommand.cpp @@ -18,8 +18,7 @@ using namespace std; namespace game{ namespace gui{ - ConsoleCommand::ConsoleCommand(GameManager *gM, Listbox *l, vector players, stringw name, vector args) { - gameManager = gM; + ConsoleCommand::ConsoleCommand(Listbox *l, vector players, stringw name, vector args) { listbox = l; this->players = players; this->name = name; @@ -56,27 +55,27 @@ namespace game{ Unit *u = nullptr; switch (unitType[unitId]) { case UNIT_TYPE::VESSEL: - u = new Vessel(gameManager, players[playerId],pos, unitId); + u = new Vessel(players[playerId],pos, unitId); break; case UNIT_TYPE::DESTROYER: - u = new Destroyer(gameManager, players[playerId], pos, unitId); + u = new Destroyer(players[playerId], pos, unitId); break; case UNIT_TYPE::CRUISER: - u = new Cruiser(gameManager, players[playerId],pos, unitId); + u = new Cruiser(players[playerId],pos, unitId); break; case UNIT_TYPE::SUBMARINE: - u = new Submarine(gameManager, players[playerId], pos, unitId); + u = new Submarine(players[playerId], pos, unitId); break; case UNIT_TYPE::AIRCRAFT_CARRIER: { - u = new AircraftCarrier(gameManager, players[playerId],pos, unitId); + u = new AircraftCarrier(players[playerId],pos, unitId); break; } case UNIT_TYPE::MISSILE_JET: - u = new MissileJet(gameManager, players[playerId],pos, unitId, false); + u = new MissileJet(players[playerId],pos, unitId, false); break; case UNIT_TYPE::DEMO_JET: - u = new DemoJet(gameManager, players[playerId],pos, unitId, false); + u = new DemoJet(players[playerId],pos, unitId, false); break; default: break; diff --git a/consoleCommand.h b/consoleCommand.h index 43bf6aa..56ea195 100755 --- a/consoleCommand.h +++ b/consoleCommand.h @@ -2,23 +2,23 @@ #ifndef CONSOLE_COMMAND_H #define CONSOLE_COMMAND_H +#include +#include + #include "gameManager.h" #include "unit.h" #include "listbox.h" #include "player.h" -#include -#include namespace game { namespace gui{ class ConsoleCommand{ public: - ConsoleCommand(core::GameManager*,Listbox*,std::vector,irr::core::stringw,std::vector); + ConsoleCommand(Listbox*,std::vector,irr::core::stringw,std::vector); ~ConsoleCommand(); void execute(); private: - core::GameManager *gameManager; Listbox *listbox; void printConsoleMessage(irr::core::stringw); std::vector players; diff --git a/cruiser.cpp b/cruiser.cpp index af84f57..7db8813 100755 --- a/cruiser.cpp +++ b/cruiser.cpp @@ -10,7 +10,7 @@ using namespace game::util; namespace game{ namespace content{ - Cruiser::Cruiser(GameManager *gM, Player *player, vector3df pos, int id) : Vessel(gM, player, pos, id) { + Cruiser::Cruiser(Player *player, vector3df pos, int id) : Vessel(player, pos, id) { guidedMissiles = unitData::maxGuidedMissiles[id]; } @@ -19,7 +19,7 @@ namespace game{ float angle = getAngleBetween(*order.targetPos[0], dirVec); quaternion rotQuat = rotQuat.fromAngleAxis(dirVec.X < 0 ? angle : -angle, vector3df(0, 1, 0)); vector3df basePos = leftVec * projectileData::pos[getId()][1][guidedMissiles - 1].X + upVec * projectileData::pos[getId()][1][guidedMissiles - 1].Y - dirVec * projectileData::pos[getId()][1][guidedMissiles - 1].Z; - addProjectile(new GuidedMissile(gameManager, this, pos + basePos, *order.targetPos[0], rotQuat * vector3df(0, 1, 0), rotQuat * vector3df(1, 0, 0), rotQuat * vector3df(0, 0, -1), getId(), 1, 0)); + addProjectile(new GuidedMissile(this, pos + basePos, *order.targetPos[0], rotQuat * vector3df(0, 1, 0), rotQuat * vector3df(1, 0, 0), rotQuat * vector3df(0, 0, -1), getId(), 1, 0)); removeOrder(0); // guidedMissiles--; } diff --git a/cruiser.h b/cruiser.h index db45c6d..2a432ca 100755 --- a/cruiser.h +++ b/cruiser.h @@ -10,7 +10,7 @@ namespace game namespace content { class Cruiser : public Vessel { public: - Cruiser(core::GameManager*, Player*,vector3df, int); + Cruiser(Player*,vector3df, int); void launch(Order); private: bool canFire(); diff --git a/demoJet.cpp b/demoJet.cpp index 37fbfe6..62868bd 100755 --- a/demoJet.cpp +++ b/demoJet.cpp @@ -1,5 +1,6 @@ #include "demoJet.h" #include "inGameAppState.h" +#include "stateManager.h" #include "util.h" using namespace game::core; @@ -7,7 +8,7 @@ using namespace game::util; namespace game{ namespace content{ - DemoJet::DemoJet(GameManager *gM, Player *player,vector3df pos, int id, bool onBoard) :Jet(gM, player,pos, id,onBoard) {} + DemoJet::DemoJet(Player *player,vector3df pos, int id, bool onBoard) : Jet(player,pos, id,onBoard) {} void DemoJet::attack(Order order){ if(!onBoard){ @@ -27,14 +28,18 @@ namespace game{ void DemoJet::update(){ Jet::update(); if(!onBoard&&!orders.empty()&&orders[0].type==Order::TYPE::ATTACK){ - ISceneManager *smgr = gameManager->getDevice()->getSceneManager(); + GameManager *gm = GameManager::getSingleton(); + ISceneManager *smgr = gm->getDevice()->getSceneManager(); ISceneNode *collNode = castRay(smgr,pos,pos+dirVec*length); + if(collNode&&collNode!=node){ - InGameAppState *inGameState=((InGameAppState*)gameManager->getAppState(AppStateTypes::IN_GAME_STATE)); + InGameAppState *inGameState=((InGameAppState*)gm->getStateManager()->getAppState(AppStateTypes::IN_GAME_STATE)); + for(Player *p : inGameState->getPlayers()) for(Unit *u : p->getUnits()) if(u->getNode()==collNode) u->takeDamage(damage); + blowUp(); } else if(pos.Y<0) diff --git a/demoJet.h b/demoJet.h index 5200cf9..b9dfb5f 100755 --- a/demoJet.h +++ b/demoJet.h @@ -9,7 +9,7 @@ namespace game{ namespace content { class DemoJet : public Jet { public: - DemoJet(core::GameManager*, Player*,vector3df, int, bool = 1); + DemoJet(Player*,vector3df, int, bool = 1); void attack(Order); void update(); private: diff --git a/depthCharge.cpp b/depthCharge.cpp index d13be6a..39e1e50 100755 --- a/depthCharge.cpp +++ b/depthCharge.cpp @@ -1,4 +1,5 @@ #include "depthCharge.h" +#include "stateManager.h" #include "inGameAppState.h" #include "util.h" @@ -9,8 +10,8 @@ using namespace irr::scene; namespace game{ namespace content{ - DepthCharge::DepthCharge(GameManager *gM, Unit *unit, vector3df pos,vector3df dir, vector3df left, vector3df up, int id,int weaponTypeId,int weaponId) : - Projectile(gM, unit, nullptr, pos, dir, left, up, id,weaponTypeId,weaponId) { + DepthCharge::DepthCharge(Unit *unit, vector3df pos,vector3df dir, vector3df left, vector3df up, int id,int weaponTypeId,int weaponId) : + Projectile(unit, nullptr, pos, dir, left, up, id,weaponTypeId,weaponId) { speed=0; initTime=getTime(); } @@ -23,24 +24,29 @@ namespace game{ checkForCollision(); } void DepthCharge::checkForCollision() { - InGameAppState *inGameState=((InGameAppState*)gameManager->getAppState(AppStateTypes::IN_GAME_STATE)); + InGameAppState *inGameState = ((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::IN_GAME_STATE)); ISceneNode *collNode=nullptr; bool detonated=false; + if(pos.Y<-10) detonated=true; + if(!detonated) for(Player *p : inGameState->getPlayers()) for(Unit *u : p->getUnits()){ if(u==unit) continue; + vector3df p0=u->getCorner(0); vector3df p1=u->getCorner(1); vector3df p3=u->getCorner(3); vector3df p4=u->getCorner(4); + if(isWithinCuboid(p0,p1,p3,p4,pos)){ detonated=true; collNode=u->getNode(); } } + if(detonated) explode(collNode); } diff --git a/depthCharge.h b/depthCharge.h index 0cf2c04..91cdd58 100755 --- a/depthCharge.h +++ b/depthCharge.h @@ -8,7 +8,7 @@ namespace game{ namespace content{ class DepthCharge : public Projectile { public: - DepthCharge(core::GameManager*, Unit*, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, int, int, int); + DepthCharge(Unit*, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, int, int, int); void update(); private: s64 initTime; diff --git a/destroyer.cpp b/destroyer.cpp index c30e49e..8b1ade4 100755 --- a/destroyer.cpp +++ b/destroyer.cpp @@ -5,6 +5,7 @@ #include "defConfigs.h" #include "depthCharge.h" #include "inGameAppState.h" +#include "stateManager.h" #include "projectileData.h" #include "util.h" @@ -15,7 +16,7 @@ using namespace game::content::unitData; namespace game{ namespace content{ - Destroyer::Destroyer(GameManager *gM, Player *player,vector3df pos,int id) :Vessel(gM, player,pos, id) { + Destroyer::Destroyer(Player *player,vector3df pos,int id) :Vessel(player,pos, id) { this->maxDepthCharges=unitData::maxDepthCharges[id]; depthCharges=maxDepthCharges; this->rateOfDrops=unitData::rateOfDrops[id]; @@ -29,7 +30,7 @@ namespace game{ void Destroyer::attack(Order order){ vector3df t=*order.targetPos[0]; bool sub=false; - InGameAppState *inGameState=((InGameAppState*)gameManager->getAppState(AppStateTypes::IN_GAME_STATE)); + InGameAppState *inGameState=((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::IN_GAME_STATE)); for(Player *p : inGameState->getPlayers()) for(Unit *u : p->getUnits()) if(u->getType()==UNIT_TYPE::SUBMARINE&&order.targetPos[0]==u->getPosPtr()) @@ -44,7 +45,7 @@ namespace game{ float angle=rand()%360; float radius=1; vector3df offsetVec=vector3df(cos(angle),0,sin(angle))*radius; - addProjectile(new DepthCharge(gameManager,this,pos+projectileData::pos[id][1][1]+offsetVec,dirVec,leftVec,upVec,id,1,0)); + addProjectile(new DepthCharge(this,pos+projectileData::pos[id][1][1]+offsetVec,dirVec,leftVec,upVec,id,1,0)); lastDropTime=getTime(); if(depthCharges==maxDepthCharges) reloadStartTime=getTime(); diff --git a/destroyer.h b/destroyer.h index cf28ff0..1130528 100755 --- a/destroyer.h +++ b/destroyer.h @@ -12,7 +12,7 @@ namespace game namespace content { class Destroyer : public Vessel { public: - Destroyer(core::GameManager*, Player*,vector3df, int); + Destroyer(Player*,vector3df, int); void dropDepthCharge(); void attack(Order); void update(); diff --git a/eventListener.cpp b/eventListener.cpp index 4c24b55..20c2a87 100644 --- a/eventListener.cpp +++ b/eventListener.cpp @@ -1,4 +1,5 @@ #include "eventListener.h" +#include "stateManager.h" using namespace irr; @@ -12,25 +13,30 @@ namespace game{ bool EventListener::OnEvent(const SEvent& event) { int offsetTrigger = -1; + StateManager *stateManager = GameManager::getSingleton()->getStateManager(); + if (event.EventType == EET_KEY_INPUT_EVENT) { - for (int i = 0; i < gameManager->getAppStateNumber(); i++) { - for (Key *k : gameManager->getAppState(i)->getKeys()) { + for (int i = 0; i < stateManager->getAppStateNumber(); i++) { + for (Key *k : stateManager->getAppState(i)->getKeys()) { if (k->key && k->trigger == event.KeyInput.Key) { if (!k->analog) { if (!event.KeyInput.PressedDown) k->beingUsed=false; + if (!k->beingUsed) { if (event.KeyInput.PressedDown) k->beingUsed=true; + k->pressed=event.KeyInput.PressedDown; - gameManager->getAppState(i)->onAction(k->bind, k->pressed); + stateManager->getAppState(i)->onAction(k->bind, k->pressed); } } else { k->pressed=event.KeyInput.PressedDown; - gameManager->getAppState(i)->onAnalog(k->bind, 0.); + stateManager->getAppState(i)->onAnalog(k->bind, 0.); } } - gameManager->getAppState(i)->onRawKeyPress(event.KeyInput); + + stateManager->getAppState(i)->onRawKeyPress(event.KeyInput); } } } @@ -68,18 +74,20 @@ namespace game{ offsetTrigger = 4; break; } - for (int i = 0; i < gameManager->getAppStateNumber(); i++) { - for (Key *k : gameManager->getAppState(i)->getKeys()) { + + for (int i = 0; i < stateManager->getAppStateNumber(); i++) { + for (Key *k : stateManager->getAppState(i)->getKeys()) { if (offsetTrigger == k->trigger) { if (!k->analog) { k->pressed=isPressed; - gameManager->getAppState(i)->onAction(k->bind, k->pressed); + stateManager->getAppState(i)->onAction(k->bind, k->pressed); } else { k->beingUsed=isPressed; } } + if(offsetTrigger<=2) - gameManager->getAppState(i)->onRawMousePress(event.MouseInput); + stateManager->getAppState(i)->onRawMousePress(event.MouseInput); } } } @@ -87,10 +95,12 @@ namespace game{ } void EventListener::update() { - for (int i = 0; i < gameManager->getAppStateNumber(); i++) { - for (Key *k : gameManager->getAppState(i)->getKeys()) { + StateManager *stateManager = GameManager::getSingleton()->getStateManager(); + + for (int i = 0; i < stateManager->getAppStateNumber(); i++) { + for (Key *k : stateManager->getAppState(i)->getKeys()) { if (k->beingUsed && k->analog) { - gameManager->getAppState(i)->onAnalog(k->bind, 0.); + stateManager->getAppState(i)->onAnalog(k->bind, 0.); } } } diff --git a/exitButton.cpp b/exitButton.cpp index 92944c1..41b3d50 100755 --- a/exitButton.cpp +++ b/exitButton.cpp @@ -6,11 +6,11 @@ using namespace game::core; namespace game { namespace gui{ - ExitButton::ExitButton(GameManager *gM, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + ExitButton::ExitButton(vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { } void ExitButton::onClick() { - gameManager->getDevice()->closeDevice(); + GameManager::getSingleton()->getDevice()->closeDevice(); } } } diff --git a/exitButton.h b/exitButton.h index 1c14524..674ca7c 100755 --- a/exitButton.h +++ b/exitButton.h @@ -9,7 +9,7 @@ namespace game namespace gui { class ExitButton : public Button { public: - ExitButton(core::GameManager*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + ExitButton(irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); }; } diff --git a/gameManager.cpp b/gameManager.cpp index fccedde..be00d08 100755 --- a/gameManager.cpp +++ b/gameManager.cpp @@ -1,7 +1,9 @@ #include +#include #include "gameManager.h" #include "guiAppState.h" +#include "stateManager.h" #include "eventListener.h" #include "abstractBitmapText.h" #include "abstractImage.h" @@ -12,8 +14,24 @@ using namespace irr; namespace game{ namespace core{ - GameManager::GameManager(IrrlichtDevice *dev) { - device = dev; + GameManager *gameManager = nullptr; + + GameManager* GameManager::getSingleton(){ + if(!gameManager) + gameManager = new GameManager(); + + return gameManager; + } + + GameManager::GameManager() { + IrrlichtDevice *device = createDevice(video::EDT_OPENGL, irr::core::dimension2d(800,600), 32, false, true, true, nullptr); + device->setResizable(true); + irr::video::IVideoDriver *driver = device->getVideoDriver(); + irr::scene::ISceneManager *smgr = device->getSceneManager(); + irr::gui::IGUIEnvironment *guiEnv = device->getGUIEnvironment(); + + device->setWindowCaption(L"(((A)))"); + stateManager = new StateManager(); listener = new EventListener(this); device->setEventReceiver(listener); width = device->getVideoDriver()->getScreenSize().Width; @@ -22,31 +40,6 @@ namespace game{ GameManager::~GameManager() {} - void GameManager::attachState(AbstractAppState *a) { - bool alreadyAttached = false; - for (int i = 0; i < appStates.size()&&!alreadyAttached; i++) - if (appStates[i] == a) - alreadyAttached = true; - if (!alreadyAttached) { - a->onAttachment(); - appStates.push_back(a); - } - } - - void GameManager::dettachState(AbstractAppState *a) { - bool alreadyDetached = false; - for (int i = 0; i < appStates.size()&&!alreadyDetached; i++) - if (appStates[i] == a) - alreadyDetached = true; - if (!alreadyDetached) { - a->onDetachment(); - for (int i = 0; i < appStates.size(); i++) - if (a == appStates[i]) { - appStates.erase(appStates.begin() + i); - } - } - } - void GameManager::detachBitmapText(AbstractBitmapText *bitmapText) { for (int i = 0; i < bitmapTexts.size(); i++) if (bitmapText == bitmapTexts[i]) { @@ -79,21 +72,13 @@ namespace game{ void GameManager::update() { listener->update(); - for (AbstractAppState *a : appStates) - if (a->isAttached() && a) - a->update(); + stateManager->update(); + for (BitmapText *b : bitmapTexts) b->update(); + for (Image *i : images) i->update(); } - - AbstractAppState* GameManager::getAppState(AppStateTypes t) { - AbstractAppState *state = nullptr; - for (AbstractAppState *a : appStates) - if (a->getType() == t) - state = a; - return state; - } } } diff --git a/gameManager.h b/gameManager.h index bc91bbe..e43995f 100755 --- a/gameManager.h +++ b/gameManager.h @@ -21,14 +21,11 @@ namespace game{ namespace core{ class EventListener; + class StateManager; class GameManager { public: - GameManager(irr::IrrlichtDevice*); - ~GameManager(); - void attachState(AbstractAppState*); - void dettachState(AbstractAppState*); - void dettachState(AppStateTypes); + static GameManager* getSingleton(); inline void attachBitmapText(gui::AbstractBitmapText *a){bitmapTexts.push_back(a);} void detachBitmapText(gui::AbstractBitmapText*); void detachAllBitmapTexts(); @@ -39,16 +36,17 @@ namespace game{ inline irr::IrrlichtDevice* getDevice(){return device;} inline irr::scene::ISceneManager* getSceneManager(){return device->getSceneManager();} inline void setDevice(irr::IrrlichtDevice *d){this->device=d;} - inline AbstractAppState* getAppState(int id){return appStates[id];} - AbstractAppState* getAppState(AppStateTypes); - inline void setAppState(int i, AbstractAppState *a){appStates[i]=a;} - inline int getAppStateNumber(){return appStates.size();} inline int getWidth(){return width;} inline int getHeight(){return height;} inline EventListener* getListener(){return listener;} inline bool isServerSide(){return serverSide;} + inline StateManager* getStateManager(){return stateManager;} private: + GameManager(); + ~GameManager(); + irr::IrrlichtDevice *device = nullptr; + StateManager *stateManager = nullptr; std::vector appStates; int width, height; std::vector bitmapTexts; diff --git a/guiAppState.cpp b/guiAppState.cpp index 1f76951..f8f3830 100755 --- a/guiAppState.cpp +++ b/guiAppState.cpp @@ -11,50 +11,59 @@ namespace game{ namespace core{ irr::u16 mousePos[2]{}; - GuiAppState::GuiAppState(GameManager* gM) { - gameManager = gM; + GuiAppState::GuiAppState() { type = AppStateTypes::GUI_STATE; } GuiAppState::~GuiAppState() {} void GuiAppState::update() { - mousePos[0] = gameManager->getDevice()->getCursorControl()->getPosition().X; - mousePos[1] = gameManager->getDevice()->getCursorControl()->getPosition().Y; + GameManager *gm = GameManager::getSingleton(); + mousePos[0] = gm->getDevice()->getCursorControl()->getPosition().X; + mousePos[1] = gm->getDevice()->getCursorControl()->getPosition().Y; + for (Button *b : buttons) { if (b->isSeparate()) b->update(); + bool withinX=mousePos[0] > b->getPos().X && mousePos[0] < b->getPos().X + b->getSize().X; bool withinY=mousePos[1] > b->getPos().Y && mousePos[1] < b->getPos().Y + b->getSize().Y; + if(withinX&&withinY) b->onMouseOver(); else b->onMouseAway(); + b->setMouseOverDone(withinX&&withinY); b->setMouseAwayDone(!(withinX&&withinY)); } + for (Listbox *l : listboxes) l->update(); + for (Checkbox *c : checkboxes) c->update(); + for (Slider *s : sliders) s->update(); + for (Textbox *t : textboxes) t->update(); + for (Tooltip *t : tooltips) t->update(); } void GuiAppState::onAttachment() { AbstractAppState::onAttachment(); - gameManager->getDevice()->getCursorControl()->setVisible(true); + GameManager::getSingleton()->getDevice()->getCursorControl()->setVisible(true); attachKeyboardKeys(); } void GuiAppState::onDetachment() { AbstractAppState::onDetachment(); AbstractAppState::detachAllKeys(); - gameManager->getDevice()->getCursorControl()->setVisible(false); + GameManager::getSingleton()->getDevice()->getCursorControl()->setVisible(false); } void GuiAppState::attachKeyboardKeys() { @@ -258,7 +267,7 @@ namespace game{ for (int i = 0; i < buttons.size(); i++) { if (b == buttons[i]) { if (b->isImageButton()) - gameManager->detachImage(b->getImage()); + GameManager::getSingleton()->detachImage(b->getImage()); delete b; buttons.erase(buttons.begin() + i); } @@ -269,7 +278,7 @@ namespace game{ for (int i = 0; i < buttons.size(); i++) { if (name == buttons[i]->getName() && buttons[i]->isSeparate()) { if (buttons[i]->isImageButton()) - gameManager->detachImage(buttons[i]->getImage()); + GameManager::getSingleton()->detachImage(buttons[i]->getImage()); delete buttons[i]; buttons.erase(buttons.begin() + i); } diff --git a/guiAppState.h b/guiAppState.h index d172fa7..e33a99d 100755 --- a/guiAppState.h +++ b/guiAppState.h @@ -15,7 +15,7 @@ namespace game{ namespace core{ class GuiAppState : public AbstractAppState { public: - GuiAppState(GameManager*); + GuiAppState(); ~GuiAppState(); void onAttachment(); void onDetachment(); @@ -53,7 +53,6 @@ namespace game{ void checkKeyboard(gui::Textbox*, Bind, bool); void updateControlsListbox(int); - GameManager *gameManager; std::vector buttons; std::vector listboxes; std::vector checkboxes; diff --git a/guidedMissile.cpp b/guidedMissile.cpp index 173a9dd..0f05257 100755 --- a/guidedMissile.cpp +++ b/guidedMissile.cpp @@ -10,8 +10,8 @@ using namespace irr::core; namespace game{ namespace content{ - GuidedMissile::GuidedMissile(GameManager *gM, Unit *unit, vector3df pos, vector3df target, vector3df dirVec, vector3df leftVec, vector3df upVec, int id, int weaponTypeId, int weaponId) : - Projectile(gM, unit, nullptr, pos, dirVec, leftVec, upVec, id, weaponTypeId, weaponId) { + GuidedMissile::GuidedMissile(Unit *unit, vector3df pos, vector3df target, vector3df dirVec, vector3df leftVec, vector3df upVec, int id, int weaponTypeId, int weaponId) : + Projectile(unit, nullptr, pos, dirVec, leftVec, upVec, id, weaponTypeId, weaponId) { speed = .05; for (int i = 0; i < 180; i++) arcLength += speed * cos(turnAngle * i); diff --git a/guidedMissile.h b/guidedMissile.h index 7513e3d..d235bd9 100755 --- a/guidedMissile.h +++ b/guidedMissile.h @@ -8,7 +8,7 @@ namespace game{ namespace content{ class GuidedMissile : public Projectile { public: - GuidedMissile(core::GameManager*, Unit*, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, int, int, int); + GuidedMissile(Unit*, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, int, int, int); ~GuidedMissile(){} void update(); private: diff --git a/inGameAppState.cpp b/inGameAppState.cpp index 8aac464..c19bb0d 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -1,9 +1,10 @@ #include +#include "stateManager.h" #include "inGameAppState.h" #include "consoleCommand.h" -#include "vessel.h" #include "aircraftCarrier.h" +#include "vessel.h" using namespace game::gui; using namespace game::util; @@ -13,7 +14,7 @@ using namespace std; namespace game{ namespace core{ - InGameAppState::ResumeButton::ResumeButton(GuiAppState *guiState, InGameAppState *inGameState, GameManager *gM, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + InGameAppState::ResumeButton::ResumeButton(GuiAppState *guiState, InGameAppState *inGameState, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { this->guiState = guiState; this->inGameState = inGameState; } @@ -26,7 +27,7 @@ namespace game{ return guiState; } - InGameAppState::ConsoleButton::ConsoleButton(GuiAppState *guiState, InGameAppState *inGameState, GameManager *gM, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + InGameAppState::ConsoleButton::ConsoleButton(GuiAppState *guiState, InGameAppState *inGameState, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { this->guiState = guiState; this->inGameState = inGameState; } @@ -36,7 +37,7 @@ namespace game{ class ConsoleCommandEntryButton : public Button { public: - ConsoleCommandEntryButton(GameManager *gM, InGameAppState *inGameState, Textbox *t, Listbox *l, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + ConsoleCommandEntryButton(InGameAppState *inGameState, Textbox *t, Listbox *l, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { this->inGameState = inGameState; textbox = t; listbox = l; @@ -66,7 +67,7 @@ namespace game{ } } else name = textbox->getEntry(); - ConsoleCommand c(gameManager, listbox, inGameState->getPlayerList(), name, args); + ConsoleCommand c(listbox, inGameState->getPlayerList(), name, args); } private: InGameAppState *inGameState; @@ -78,16 +79,16 @@ namespace game{ for (int i = 0; i < emptyEntries; i++) list.push_back(""); vector2d pos(300, 100); - Listbox *consoleListbox = new Listbox(gameManager, vector2d(pos.X, pos.Y), vector2d(420, 20), list, 20); + Listbox *consoleListbox = new Listbox(vector2d(pos.X, pos.Y), vector2d(420, 20), list, 20); consoleListbox->openUp(); guiState->addListbox(consoleListbox); - Textbox *consoleTextbox = new Textbox(gameManager, vector2d(pos.X, pos.Y + 20 * (emptyEntries + 1)), vector2d(300, 20)); + Textbox *consoleTextbox = new Textbox(vector2d(pos.X, pos.Y + 20 * (emptyEntries + 1)), vector2d(300, 20)); guiState->addTextbox(consoleTextbox); - ConsoleCommandEntryButton *entryButton = new ConsoleCommandEntryButton(gameManager, inGameState, consoleTextbox, consoleListbox, vector2d(pos.X + 320, pos.Y + 20 * (emptyEntries + 1)), vector2d(100, 20), "Enter", true); + ConsoleCommandEntryButton *entryButton = new ConsoleCommandEntryButton(inGameState, consoleTextbox, consoleListbox, vector2d(pos.X + 320, pos.Y + 20 * (emptyEntries + 1)), vector2d(100, 20), "Enter", true); guiState->addButton(entryButton); } - InGameAppState::InGameOptionsButton::ReturnButton::ReturnButton(GuiAppState *guiState, InGameAppState *inGameState, GameManager *gM, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + InGameAppState::InGameOptionsButton::ReturnButton::ReturnButton(GuiAppState *guiState, InGameAppState *inGameState, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { this->guiState = guiState; this->inGameState = inGameState; } @@ -103,11 +104,11 @@ namespace game{ guiState->removeButton("Audio"); guiState->removeButton("Multiplayer"); vector2d pos(100, 100); - ResumeButton *resumeButton = new ResumeButton(guiState, inGameState, gameManager, vector2d(pos.X, pos.Y), vector2d(150, 50), "Resume", true); - ConsoleButton *consoleButton = new ConsoleButton(guiState, inGameState, gameManager, vector2d(pos.X, pos.Y + 60), vector2d(150, 50), "Console", true); - InGameOptionsButton *optionsButton = new InGameOptionsButton(guiState, inGameState, gameManager, vector2d(pos.X, pos.Y + 120), vector2d(150, 50), "Options", true); - MainMenuButton *mainMenuButton = new MainMenuButton(guiState, inGameState, gameManager, vector2d(pos.X, pos.Y + 180), vector2d(150, 50), "Main menu", true); - ExitButton *exitButton = new ExitButton(gameManager, vector2d(pos.X, pos.Y + 240), vector2d(150, 50), "Exit", true); + ResumeButton *resumeButton = new ResumeButton(guiState, inGameState, vector2d(pos.X, pos.Y), vector2d(150, 50), "Resume", true); + ConsoleButton *consoleButton = new ConsoleButton(guiState, inGameState, vector2d(pos.X, pos.Y + 60), vector2d(150, 50), "Console", true); + InGameOptionsButton *optionsButton = new InGameOptionsButton(guiState, inGameState, vector2d(pos.X, pos.Y + 120), vector2d(150, 50), "Options", true); + MainMenuButton *mainMenuButton = new MainMenuButton(guiState, inGameState, vector2d(pos.X, pos.Y + 180), vector2d(150, 50), "Main menu", true); + ExitButton *exitButton = new ExitButton(vector2d(pos.X, pos.Y + 240), vector2d(150, 50), "Exit", true); inGameState->setResumeButton(resumeButton); inGameState->setConsoleButton(consoleButton); inGameState->setOptionsButton(optionsButton); @@ -121,13 +122,13 @@ namespace game{ guiState->removeButton("Back"); } - InGameAppState::InGameOptionsButton::InGameOptionsButton(GuiAppState *guiState, InGameAppState *inGameState, GameManager *gM, vector2d pos, vector2d size, stringw name, bool separate) : OptionsButton(gM, pos, size, name, separate) { + InGameAppState::InGameOptionsButton::InGameOptionsButton(GuiAppState *guiState, InGameAppState *inGameState, vector2d pos, vector2d size, stringw name, bool separate) : OptionsButton(pos, size, name, separate) { this->guiState = guiState; this->inGameState = inGameState; } void InGameAppState::InGameOptionsButton::onClick() { - returnButton = new ReturnButton(guiState, inGameState, gameManager, vector2d(50, gameManager->getHeight() - 150), vector2d(150, 50), "Back", true); + returnButton = new ReturnButton(guiState, inGameState, vector2d(50, GameManager::getSingleton()->getHeight() - 150), vector2d(150, 50), "Back", true); guiState->addButton(returnButton); guiState->removeButton("Resume"); guiState->removeButton("Console"); @@ -136,7 +137,7 @@ namespace game{ OptionsButton::onClick(); } - InGameAppState::MainMenuButton::MainMenuButton(GuiAppState *guiState, InGameAppState *inGameState, GameManager *gM, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + InGameAppState::MainMenuButton::MainMenuButton(GuiAppState *guiState, InGameAppState *inGameState, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { this->guiState = guiState; this->inGameState = inGameState; } @@ -145,8 +146,8 @@ namespace game{ } - InGameAppState::UnitCreationButton::UnitCreationButton(GameManager *gM, ITexture *icon, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { - setImageButton(new Image(gameManager, icon, pos, size)); + InGameAppState::UnitCreationButton::UnitCreationButton(ITexture *icon, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { + setImageButton(new Image(icon, pos, size)); } void InGameAppState::UnitCreationButton::onClick() { @@ -156,43 +157,42 @@ namespace game{ Button::update(); } - InGameAppState::BattleshipCreationButton::BattleshipCreationButton(GameManager *gM, ITexture *icon, vector2d pos, vector2d size, stringw name, bool separate) - : InGameAppState::UnitCreationButton(gM, icon, pos, size, name, separate) { + InGameAppState::BattleshipCreationButton::BattleshipCreationButton(ITexture *icon, vector2d pos, vector2d size, stringw name, bool separate) + : InGameAppState::UnitCreationButton(icon, pos, size, name, separate) { } void InGameAppState::BattleshipCreationButton::onClick() { } - InGameAppState::DestroyerCreationButton::DestroyerCreationButton(GameManager *gM, ITexture *icon, vector2d pos, vector2d size, stringw name, bool separate) - : InGameAppState::UnitCreationButton(gM, icon, pos, size, name, separate) { + InGameAppState::DestroyerCreationButton::DestroyerCreationButton(ITexture *icon, vector2d pos, vector2d size, stringw name, bool separate) + : InGameAppState::UnitCreationButton(icon, pos, size, name, separate) { } void InGameAppState::DestroyerCreationButton::onClick() { } - InGameAppState::CruiserCreationButton::CruiserCreationButton(GameManager *gM, ITexture *icon, vector2d pos, vector2d size, stringw name, bool separate) - : InGameAppState::UnitCreationButton(gM, icon, pos, size, name, separate) { + InGameAppState::CruiserCreationButton::CruiserCreationButton(ITexture *icon, vector2d pos, vector2d size, stringw name, bool separate) + : InGameAppState::UnitCreationButton(icon, pos, size, name, separate) { } void InGameAppState::CruiserCreationButton::onClick() { } - InGameAppState::CarrierCreationButton::CarrierCreationButton(GameManager *gM, ITexture *icon, vector2d pos, vector2d size, stringw name, bool separate) - : InGameAppState::UnitCreationButton(gM, icon, pos, size, name, separate) { + InGameAppState::CarrierCreationButton::CarrierCreationButton(ITexture *icon, vector2d pos, vector2d size, stringw name, bool separate) + : InGameAppState::UnitCreationButton(icon, pos, size, name, separate) { } void InGameAppState::CarrierCreationButton::onClick() { } - InGameAppState::SubmarineCreationButton::SubmarineCreationButton(GameManager *gM, ITexture *icon, vector2d pos, vector2d size, stringw name, bool separate) - : InGameAppState::UnitCreationButton(gM, icon, pos, size, name, separate) { + InGameAppState::SubmarineCreationButton::SubmarineCreationButton(ITexture *icon, vector2d pos, vector2d size, stringw name, bool separate) + : InGameAppState::UnitCreationButton(icon, pos, size, name, separate) { } void InGameAppState::SubmarineCreationButton::onClick() { } - InGameAppState::InGameAppState(GameManager *gM, vector difficultyLevels, vector factions) { - gameManager = gM; + InGameAppState::InGameAppState(vector difficultyLevels, vector factions) { type = AppStateTypes::IN_GAME_STATE; this->playerId = 0; this->difficultyLevels = difficultyLevels; @@ -204,8 +204,9 @@ namespace game{ void InGameAppState::onAttachment() { AbstractAppState::onAttachment(); - map = new Map(gameManager); + map = new Map(); map->load(); + for (int i = 0; i < factions.size(); i++) { int faction; int difficulty; @@ -219,15 +220,17 @@ namespace game{ else difficulty = 2; } + faction = factions[i][0] - 48; - Player *p = new Player(gameManager, difficulty, faction); + Player *p = new Player(difficulty, faction); players.push_back(p); p->setId(players.size() - 1); } mainPlayer = players[playerId]; - guiState = ((GuiAppState*)gameManager->getAppState(AppStateTypes::GUI_STATE)); - activeState = new ActiveGameState(gameManager, guiState, map, players, playerId); - gameManager->attachState(activeState); + StateManager *stateManager = GameManager::getSingleton()->getStateManager(); + guiState = ((GuiAppState*)stateManager->getAppState(AppStateTypes::GUI_STATE)); + activeState = new ActiveGameState(guiState, map, players, playerId); + stateManager->attachState(activeState); } void InGameAppState::onDetachment() { @@ -237,28 +240,37 @@ namespace game{ void InGameAppState::update() { if (map) map->update(); + if (isMainMenuActive) { - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); - driver->draw2DRectangle(SColor(100, 0, 0, 0), rect(0, 0, gameManager->getWidth(), gameManager->getHeight())); + GameManager *gm = GameManager::getSingleton(); + IVideoDriver *driver = gm->getDevice()->getVideoDriver(); + driver->draw2DRectangle(SColor(100, 0, 0, 0), rect(0, 0, gm->getWidth(), gm->getHeight())); } + for (Player *p : players) if (p) { p->update(); + for (int i=0;igetNumberOfUnits();i++){ Unit *u=p->getUnit(i); + if(u->isWorking()) u->update(); else{ int selectedId=-1; std::vector &units=p->getUnits(),&selectedUnits=activeState->getSelectedUnits(); units.erase(units.begin()+i); + if(u->getType()==unitData::UNIT_TYPE::MISSILE_JET||u->getType()==unitData::UNIT_TYPE::DEMO_JET){ AircraftCarrier *carrier=nullptr; + for(int i2=0;i2getUnits().size()&&!carrier;i2++){ AircraftCarrier *a=((Jet*)u)->getAircraftCarrier(); + if(a) carrier=a; } + if(carrier){ Jet** jets=carrier->getJets(); jets[((Jet*)u)->getJetId()]=nullptr; @@ -266,8 +278,10 @@ namespace game{ } else if(u->getType()==unitData::UNIT_TYPE::AIRCRAFT_CARRIER){ AircraftCarrier *carrier=(AircraftCarrier*)u; + for(int i2=0;i2getMaxNumJets();i2++){ Jet *j=carrier->getJet(i2); + if(j&&j->isOnBoard()) j->blowUp(); } @@ -276,23 +290,30 @@ namespace game{ for(int i2=0;i2<10;i2++){ int id=-1; std::vector &group=activeState->getUnitGroup(i2); + for(int i3=0;i3isExploded()){ p->update(); } @@ -301,17 +322,21 @@ namespace game{ projectiles.erase(projectiles.begin()+i); } } + for(int i=0;ifx[i].time){ IParticleSystemSceneNode *node=fx[i].node; + if(node){ node->removeAllAffectors(); node->setVisible(false); } + if(fx[i].sfx){ delete fx[i].sfx->getBuffer(); delete fx[i].sfx; } + fx.erase(fx.begin()+i); } } @@ -319,9 +344,11 @@ namespace game{ void InGameAppState::attachGui() { int idOffset = 0; + if (mainPlayer->getFaction() == 1) idOffset += 7; - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + + IVideoDriver *driver = GameManager::getSingleton()->getDevice()->getVideoDriver(); /* bcb = new BattleshipCreationButton(gameManager, driver->getTexture(iconPath[idOffset]), vector2d(gameManager->getWidth() - 100, gameManager->getHeight() - 540), vector2d(100, 100), "battleships", true); dcb = new DestroyerCreationButton(gameManager, driver->getTexture(iconPath[idOffset + 1]), vector2d(gameManager->getWidth() - 100, gameManager->getHeight() - 430), vector2d(100, 100), "destroyers", true); @@ -345,16 +372,18 @@ namespace game{ } void InGameAppState::toggleMainMenu() { + GameManager *gm = GameManager::getSingleton(); + if (!isMainMenuActive) { isMainMenuActive = true; - gameManager->dettachState(activeState); + gm->getStateManager()->dettachState(activeState); vector2d pos(100, 100); //detachGui(); - resumeButton = new ResumeButton(guiState, this, gameManager, vector2d(pos.X, pos.Y), vector2d(150, 50), "Resume", true); - consoleButton = new ConsoleButton(guiState, this, gameManager, vector2d(pos.X, pos.Y + 60), vector2d(150, 50), "Console", true); - optionsButton = new InGameOptionsButton(guiState, this, gameManager, vector2d(pos.X, pos.Y + 120), vector2d(150, 50), "Options", true); - mainMenuButton = new MainMenuButton(guiState, this, gameManager, vector2d(pos.X, pos.Y + 180), vector2d(150, 50), "Main menu", true); - exitButton = new ExitButton(gameManager, vector2d(pos.X, pos.Y + 240), vector2d(150, 50), "Exit", true); + resumeButton = new ResumeButton(guiState, this, vector2d(pos.X, pos.Y), vector2d(150, 50), "Resume", true); + consoleButton = new ConsoleButton(guiState, this, vector2d(pos.X, pos.Y + 60), vector2d(150, 50), "Console", true); + optionsButton = new InGameOptionsButton(guiState, this, vector2d(pos.X, pos.Y + 120), vector2d(150, 50), "Options", true); + mainMenuButton = new MainMenuButton(guiState, this, vector2d(pos.X, pos.Y + 180), vector2d(150, 50), "Main menu", true); + exitButton = new ExitButton(vector2d(pos.X, pos.Y + 240), vector2d(150, 50), "Exit", true); guiState->addButton(resumeButton); guiState->addButton(consoleButton); guiState->addButton(optionsButton); @@ -364,7 +393,7 @@ namespace game{ } else { isMainMenuActive = false; - gameManager->attachState(activeState); + gm->getStateManager()->attachState(activeState); //attachGui(); guiState->removeAllCheckboxes(); guiState->removeAllListboxes(); diff --git a/inGameAppState.h b/inGameAppState.h index 9827f0f..6f3930d 100755 --- a/inGameAppState.h +++ b/inGameAppState.h @@ -21,7 +21,7 @@ namespace game{ class InGameAppState : public AbstractAppState { public: - InGameAppState(GameManager*, std::vector, std::vector); + InGameAppState(std::vector, std::vector); ~InGameAppState(); void onAttachment(); void onDetachment(); @@ -36,7 +36,7 @@ namespace game{ private: class ResumeButton : public gui::Button { public: - ResumeButton(GuiAppState*, InGameAppState*, GameManager*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + ResumeButton(GuiAppState*, InGameAppState*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); GuiAppState *getGuiState(); private: @@ -46,7 +46,7 @@ namespace game{ class ConsoleButton : public gui::Button { public: - ConsoleButton(GuiAppState*, InGameAppState*, GameManager*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + ConsoleButton(GuiAppState*, InGameAppState*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); private: GuiAppState *guiState; @@ -55,7 +55,7 @@ namespace game{ class MainMenuButton : public gui::Button { public: - MainMenuButton(GuiAppState*, InGameAppState*, GameManager*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + MainMenuButton(GuiAppState*, InGameAppState*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); private: GuiAppState *guiState; @@ -64,12 +64,12 @@ namespace game{ class InGameOptionsButton : public gui::OptionsButton { public: - InGameOptionsButton(GuiAppState*, InGameAppState*, GameManager*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + InGameOptionsButton(GuiAppState*, InGameAppState*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); private: class ReturnButton : public gui::Button { public: - ReturnButton(GuiAppState*, InGameAppState*, GameManager*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + ReturnButton(GuiAppState*, InGameAppState*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); private: GuiAppState *guiState; @@ -82,7 +82,7 @@ namespace game{ class UnitCreationButton : public gui::Button { public: - UnitCreationButton(GameManager*, irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + UnitCreationButton(irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); void update(); private: @@ -91,33 +91,34 @@ namespace game{ class BattleshipCreationButton : public UnitCreationButton { public: - BattleshipCreationButton(GameManager*, irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + BattleshipCreationButton(irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); }; class DestroyerCreationButton : public UnitCreationButton { public: - DestroyerCreationButton(GameManager*, irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + DestroyerCreationButton(irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); }; class CruiserCreationButton : public UnitCreationButton { public: - CruiserCreationButton(GameManager*, irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + CruiserCreationButton(irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); }; class CarrierCreationButton : public UnitCreationButton { public: - CarrierCreationButton(GameManager*, irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + CarrierCreationButton(irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); }; class SubmarineCreationButton : public UnitCreationButton { public: - SubmarineCreationButton(GameManager*, irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + SubmarineCreationButton(irr::video::ITexture*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); }; + ResumeButton *resumeButton; ConsoleButton *consoleButton; InGameOptionsButton *optionsButton; @@ -138,7 +139,6 @@ namespace game{ std::vector projectiles; std::vector fx; content::Player *mainPlayer; - GameManager *gameManager; content::Map *map; int playerId; ActiveGameState* activeState; diff --git a/jet.cpp b/jet.cpp index 6f208cb..60925ab 100755 --- a/jet.cpp +++ b/jet.cpp @@ -8,7 +8,7 @@ using namespace game::content::unitData; namespace game{ namespace content{ - Jet::Jet(GameManager *gM, Player *player, vector3df pos, int unitId, bool onBoard) : Unit(gM, player, pos, unitId) { + Jet::Jet(Player *player, vector3df pos, int unitId, bool onBoard) : Unit(player, pos, unitId) { offsetPos = pos; this->onBoard = onBoard; this->pitchSpeed = unitData::pitchSpeed[id]; diff --git a/jet.h b/jet.h index 2c412cd..399c466 100755 --- a/jet.h +++ b/jet.h @@ -13,7 +13,7 @@ namespace game{ class Jet : public Unit { public: - Jet(core::GameManager*, Player*,vector3df, int, bool); + Jet(Player*,vector3df, int, bool); // virtual void attack(Order); virtual void update(); inline void setJetId(int i){this->jetId=i;} diff --git a/listbox.cpp b/listbox.cpp index 4450f68..fea548a 100755 --- a/listbox.cpp +++ b/listbox.cpp @@ -10,7 +10,7 @@ using namespace std; namespace game{ namespace gui{ - Listbox::ListboxButton::ListboxButton(GameManager *gM, Listbox *l, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + Listbox::ListboxButton::ListboxButton(Listbox *l, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { listbox = l; } @@ -21,58 +21,62 @@ namespace game{ listbox->close(); } - Listbox::ScrollingButton::ScrollingButton(GameManager *gM, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + Listbox::ScrollingButton::ScrollingButton(vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { } void Listbox::ScrollingButton::onClick() { } - Listbox::Listbox(GameManager *gM, vector2d pos, vector2d size, vector lines, int maxDisplay, bool controlsListbox) { - gameManager = gM; + Listbox::Listbox(vector2d pos, vector2d size, vector lines, int maxDisplay, bool controlsListbox) { this->pos = pos; this->size = size; this->lines = lines; this->maxDisplay = maxDisplay; this->controlsListbox=controlsListbox; - listboxButton = new ListboxButton(gM, this, pos, size, "ListboxButton", false); - scrollingButton = new ScrollingButton(gM, vector2d(pos.X + size.X - 20, pos.Y + 20), vector2d(20, 20.0 * (maxDisplay - 2) / (lines.size() - maxDisplay)), "scrollingButton", false); + listboxButton = new ListboxButton(this, pos, size, "ListboxButton", false); + scrollingButton = new ScrollingButton(vector2d(pos.X + size.X - 20, pos.Y + 20), vector2d(20, 20.0 * (maxDisplay - 2) / (lines.size() - maxDisplay)), "scrollingButton", false); } Listbox::~Listbox() {} void Listbox::update() { - IGUIFont *font = gameManager->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fontcourier.bmp"); + GameManager *gm = GameManager::getSingleton(); + IGUIFont *font = gm->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fontcourier.bmp"); + if (open) { - gameManager->getDevice()->getVideoDriver()->draw2DRectangle(*listboxButton->getColor(), rect(pos.X, pos.Y, pos.X + size.X, pos.Y + size.Y * maxDisplay), nullptr); - int mousePosX = gameManager->getDevice()->getCursorControl()->getPosition().X, mousePosY = gameManager->getDevice()->getCursorControl()->getPosition().Y; + gm->getDevice()->getVideoDriver()->draw2DRectangle(*listboxButton->getColor(), rect(pos.X, pos.Y, pos.X + size.X, pos.Y + size.Y * maxDisplay), nullptr); + int mousePosX = gm->getDevice()->getCursorControl()->getPosition().X, mousePosY = gm->getDevice()->getCursorControl()->getPosition().Y; + for (int i = 0; i < maxDisplay; i++) { if (mousePosY > pos.Y + size.Y * i && mousePosY < pos.Y + size.Y * (i + 1)){ selectedOption=scrollOffset+i; - gameManager->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 200, 125, 0), rect(pos.X, pos.Y + size.Y * i, pos.X + size.X - 20, pos.Y + size.Y * (i + 1)), nullptr); + gm->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 200, 125, 0), rect(pos.X, pos.Y + size.Y * i, pos.X + size.X - 20, pos.Y + size.Y * (i + 1)), nullptr); } else if (mousePosY < pos.Y){ selectedOption=scrollOffset; - gameManager->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 200, 125, 0), rect(pos.X, pos.Y, pos.X + size.X - 20, pos.Y + size.Y), nullptr); + gm->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 200, 125, 0), rect(pos.X, pos.Y, pos.X + size.X - 20, pos.Y + size.Y), nullptr); } else if (mousePosY > pos.Y + size.Y * maxDisplay){ selectedOption=scrollOffset+maxDisplay-1; - gameManager->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 200, 125, 0), rect(pos.X, pos.Y + size.Y * (maxDisplay - 1), pos.X + size.X - 20, pos.Y + size.Y * (maxDisplay + 0)), nullptr); + gm->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 200, 125, 0), rect(pos.X, pos.Y + size.Y * (maxDisplay - 1), pos.X + size.X - 20, pos.Y + size.Y * (maxDisplay + 0)), nullptr); } } - for (int i = 0; i < maxDisplay; i++) { + + for (int i = 0; i < maxDisplay; i++) font->draw(lines[i + scrollOffset], rect(pos.X, pos.Y + size.Y*i, pos.X + size.X, pos.Y + size.Y * (i + 1)), SColor(255, 255, 255, 255)); - } + if (scrollOffset > 0) { vector2d scrollingButtonPos = scrollingButton->getPos(); scrollingButtonPos.Y = pos.Y + 20 + scrollingButton->getSize().Y * (scrollOffset - 1); scrollingButton->setPos(scrollingButtonPos); } - gameManager->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 150, 150, 150), rect( + + gm->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 150, 150, 150), rect( scrollingButton->getPos().X, scrollingButton->getPos().Y, scrollingButton->getPos().X + scrollingButton->getSize().X, scrollingButton->getPos().Y + scrollingButton->getSize().Y)); } else { - gameManager->getDevice()->getVideoDriver()->draw2DRectangle(*listboxButton->getColor(), rect(pos.X, pos.Y, pos.X + size.X, pos.Y + size.Y), nullptr); + gm->getDevice()->getVideoDriver()->draw2DRectangle(*listboxButton->getColor(), rect(pos.X, pos.Y, pos.X + size.X, pos.Y + size.Y), nullptr); font->draw(lines[selectedOption], rect(pos.X, pos.Y, pos.X + size.X, pos.Y + size.Y), SColor(255, 255, 255, 255)); } } diff --git a/listbox.h b/listbox.h index 1044946..cbe9368 100755 --- a/listbox.h +++ b/listbox.h @@ -8,7 +8,7 @@ namespace game{ namespace gui { class Listbox { public: - Listbox(core::GameManager*, irr::core::vector2d, irr::core::vector2d, std::vector, int,bool=false); + Listbox(irr::core::vector2d, irr::core::vector2d, std::vector, int,bool=false); ~Listbox(); void update(); void openUp(); @@ -24,14 +24,14 @@ namespace game{ private: class ListboxButton : public Button { public: - ListboxButton(core::GameManager*, Listbox*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + ListboxButton(Listbox*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); private: Listbox *listbox = nullptr; }; class ScrollingButton : public Button { public: - ScrollingButton(core::GameManager*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + ScrollingButton(irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); private: }; @@ -41,7 +41,6 @@ namespace game{ irr::core::vector2d pos, size; ListboxButton *listboxButton = nullptr; Button *scrollingButton = nullptr; - core::GameManager *gameManager; public: inline ListboxButton* getListboxButton(){return listboxButton;} inline bool isControlsListbox(){return controlsListbox;} diff --git a/main.cpp b/main.cpp index 35be9f0..478259f 100755 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ -#include #include "util.h" #include "gameManager.h" +#include "stateManager.h" #include "guiAppState.h" using namespace irr; @@ -13,25 +13,21 @@ using namespace game::util; using namespace irr::video; int main() { - IrrlichtDevice *device = createDevice(video::EDT_OPENGL, core::dimension2d(800,600), 32, false, true, true, nullptr); - device->setResizable(true); - IVideoDriver *driver = device->getVideoDriver(); - ISceneManager *smgr = device->getSceneManager(); - IGUIEnvironment *guiEnv = device->getGUIEnvironment(); - device->setWindowCaption(L"(((A)))"); - GameManager *gameManager = new GameManager(device); - GuiAppState *state = new GuiAppState(gameManager); - gameManager->attachState(state); - makeTitlescreenButtons(gameManager, state); - //smgr->getMesh(PATH+"Models/Battel"); + GameManager *gameManager = GameManager::getSingleton(); + GuiAppState *state = new GuiAppState(); + gameManager->getStateManager()->attachState(state); + makeTitlescreenButtons(state); + IrrlichtDevice *device = gameManager->getDevice(); + IVideoDriver *driver = device->getVideoDriver(); + while (device->run()) { driver->beginScene(true, true, 0); - smgr->drawAll(); - guiEnv->drawAll(); + device->getSceneManager()->drawAll(); + device->getGUIEnvironment()->drawAll(); gameManager->update(); driver->endScene(); } + device->drop(); - delete gameManager; return 0; } diff --git a/map.cpp b/map.cpp index d3230f1..31e26e0 100755 --- a/map.cpp +++ b/map.cpp @@ -1,6 +1,8 @@ #include "map.h" +#include "gameManager.h" #include "defConfigs.h" +using namespace irr; using namespace irr::core; using namespace irr::video; using namespace irr::scene; @@ -9,8 +11,7 @@ using namespace game::core; namespace game{ namespace content{ - Map::Map(GameManager *gM) { - gameManager = gM; + Map::Map() { size = vector2d(50, 50); } @@ -21,9 +22,10 @@ namespace game{ } void Map::load() { - ISceneManager *smgr = gameManager->getDevice()->getSceneManager(); + GameManager *gm = GameManager::getSingleton(); + ISceneManager *smgr = gm->getDevice()->getSceneManager(); smgr->setAmbientLight(SColor(255, 100, 100, 100)); - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + IVideoDriver *driver = gm->getDevice()->getVideoDriver(); // sunLight=smgr->addLightSceneNode(0,vector3df(0,20,0),SColor(255,255,255,255)); // sunLight->setLightType(E_LIGHT_TYPE::ELT_COUNT); ISceneNode *skybox = smgr->addSkyBoxSceneNode(driver->getTexture(PATH + "Textures/up.jpg") diff --git a/map.h b/map.h index 5036e33..2f1650d 100755 --- a/map.h +++ b/map.h @@ -3,21 +3,19 @@ #define MAP_H #include -#include "gameManager.h" namespace game{ namespace content{ class Map { public: - Map(core::GameManager*); + Map(); ~Map(); void update(); void load(); void unload(); - inline irr::core::vector2d getSize(){return size;} + inline irr::core::vector2d getSize(){return size;} private: - core::GameManager *gameManager; - irr::core::vector2d size; + irr::core::vector2d size; irr::scene::ILightSceneNode *sunLight; irr::scene::IAnimatedMesh *waterMesh; irr::scene::ISceneNode *waterNode; diff --git a/missile.cpp b/missile.cpp index 3bece50..6b49499 100755 --- a/missile.cpp +++ b/missile.cpp @@ -7,8 +7,8 @@ using namespace irr::scene; namespace game{ namespace content{ - Missile::Missile(GameManager *gM, Unit *unit, ISceneNode *node, vector3df *target,vector3df pos,vector3df dir, vector3df left, vector3df up,int id,int weaponTypeId,int weaponId) : - Projectile(gM, unit, node, pos, dir, left, up,id,weaponTypeId,weaponId) { + Missile::Missile(Unit *unit, ISceneNode *node, vector3df *target,vector3df pos,vector3df dir, vector3df left, vector3df up,int id,int weaponTypeId,int weaponId) : + Projectile(unit, node, pos, dir, left, up,id,weaponTypeId,weaponId) { this->target=target; this->type=(MissileType)weaponId; this->initTime=getTime(); @@ -20,10 +20,12 @@ namespace game{ node->setPosition(pos); float angle=getAngleBetween(dirVec,*target-pos); vector3df axis=dirVec.crossProduct(*target-pos).normalize(); + if(rotationSpeed/180*PIselfDistructTime) Projectile::explode(nullptr); } diff --git a/missile.h b/missile.h index 0a5c29b..b6b38ad 100755 --- a/missile.h +++ b/missile.h @@ -10,7 +10,7 @@ namespace game{ class Missile : public Projectile { public: - Missile(core::GameManager*, Unit*, irr::scene::ISceneNode*, irr::core::vector3df*,irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, int, int, int); + Missile(Unit*, irr::scene::ISceneNode*, irr::core::vector3df*,irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, int, int, int); ~Missile(){} void update(); private: diff --git a/missileJet.cpp b/missileJet.cpp index 51a1d27..84b996c 100755 --- a/missileJet.cpp +++ b/missileJet.cpp @@ -1,6 +1,7 @@ #include "missileJet.h" #include "projectileData.h" #include "inGameAppState.h" +#include "stateManager.h" using namespace game::core; using namespace game::util; @@ -8,7 +9,7 @@ using namespace irr::scene; namespace game{ namespace content{ - MissileJet::MissileJet(GameManager *gM, Player *player, vector3df pos, int id, bool onBoard) : Jet(gM, player, pos, id, onBoard) {} + MissileJet::MissileJet(Player *player, vector3df pos, int id, bool onBoard) : Jet(player, pos, id, onBoard) {} void MissileJet::attack(Order order) { if(!onBoard&&canFire()&&missilesInstalled){ @@ -16,7 +17,7 @@ namespace game{ float distance=pos.getDistanceFrom(target); if(distance <= range){ vector3df *targetPtr=nullptr; - InGameAppState *inGameState=((InGameAppState*)gameManager->getAppState(AppStateTypes::IN_GAME_STATE)); + InGameAppState *inGameState=((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::IN_GAME_STATE)); for(Player *p : inGameState->getPlayers()) for(Unit *u : p->getUnits()){ bool jet=u->getType()==UNIT_TYPE::MISSILE_JET||u->getType()==UNIT_TYPE::DEMO_JET; @@ -39,9 +40,9 @@ namespace game{ } void MissileJet::fireMissile(vector3df *t) { - ISceneManager *smgr=gameManager->getDevice()->getSceneManager(); + ISceneManager *smgr=GameManager::getSingleton()->getDevice()->getSceneManager(); vector3df p = pos + projectileData::pos[id][0][missiles - 1].X * leftVec + projectileData::pos[id][0][missiles - 1].Y * upVec - projectileData::pos[id][0][missiles - 1].Z*dirVec; - addProjectile(new Missile(gameManager, this, missileNodes[missiles-1], t, p, dirVec, leftVec, upVec, id, 0, 0)); + addProjectile(new Missile(this, missileNodes[missiles-1], t, p, dirVec, leftVec, upVec, id, 0, 0)); missileNodes[missiles-1]->setParent(smgr->getRootSceneNode()); missileNodes[missiles-1]=nullptr; missiles--; @@ -51,14 +52,18 @@ namespace game{ void MissileJet::installMissiles(bool aam){ if(!missilesInstalled){ missiles=2; - ISceneManager *smgr=gameManager->getDevice()->getSceneManager(); - IVideoDriver *driver=gameManager->getDevice()->getVideoDriver(); + GameManager *gm = GameManager::getSingleton(); + ISceneManager *smgr = gm->getDevice()->getSceneManager(); + IVideoDriver *driver = gm->getDevice()->getVideoDriver(); + for(int i=0;igetMesh(projectileData::meshPath[id][i]); missileNodes[i]=smgr->addAnimatedMeshSceneNode(missileMesh); ITexture *diffuseTexture = driver->getTexture(projectileData::diffuseMapTextPath[id][i]); + if (!diffuseTexture) diffuseTexture = driver->getTexture(DEFAULT_TEXTURE); + missileNodes[i]->setMaterialTexture(0, diffuseTexture); missileNodes[i]->setMaterialFlag(EMF_LIGHTING, false); missileNodes[i]->setPosition(projectileData::pos[id][aam][i]); diff --git a/missileJet.h b/missileJet.h index 49b7f6a..8dcf25b 100755 --- a/missileJet.h +++ b/missileJet.h @@ -10,7 +10,7 @@ namespace game{ namespace content { class MissileJet : public Jet { public: - MissileJet(core::GameManager*, Player*,vector3df, int, bool = 1); + MissileJet(Player*,vector3df, int, bool = 1); void installMissiles(bool); private: void attack(Order); diff --git a/optionsButton.cpp b/optionsButton.cpp index 96fa47a..66eeb7b 100755 --- a/optionsButton.cpp +++ b/optionsButton.cpp @@ -1,4 +1,5 @@ #include "optionsButton.h" +#include "stateManager.h" #include "util.h" using namespace game::core; @@ -10,56 +11,64 @@ using namespace irr::gui; namespace game{ namespace gui{ - OptionsButton::OkButton::OkButton(GameManager *gm) :Button(gm, vector2d(50, gm->getHeight() - 150), vector2d(140, 50), "Ok", true) { - this->state = ((GuiAppState*)gm->getAppState(AppStateTypes::GUI_STATE)); + OptionsButton::OkButton::OkButton() :Button(vector2d(50, GameManager::getSingleton()->getHeight() - 150), vector2d(140, 50), "Ok", true) { + this->state = ((GuiAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::GUI_STATE)); } void OptionsButton::OkButton::onClick() { } - OptionsButton::DefaultsButton::DefaultsButton(GameManager *gm) :Button(gm, vector2d(200, gm->getHeight() - 150), vector2d(140, 50), "Restore defaults", true) { - this->state = ((GuiAppState*)gm->getAppState(AppStateTypes::GUI_STATE)); + OptionsButton::DefaultsButton::DefaultsButton() :Button(vector2d(200, GameManager::getSingleton()->getHeight() - 150), vector2d(140, 50), "Restore defaults", true) { + this->state = ((GuiAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::GUI_STATE)); } void OptionsButton::DefaultsButton::onClick() { } - OptionsButton::BackButton::BackButton(GameManager *gm) :Button(gm, vector2d(350, gm->getHeight() - 150), vector2d(140, 50), "Back", true) { - this->state = ((GuiAppState*)gm->getAppState(AppStateTypes::GUI_STATE)); + OptionsButton::BackButton::BackButton() :Button(vector2d(350, GameManager::getSingleton()->getHeight() - 150), vector2d(140, 50), "Back", true) { + this->state = ((GuiAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::GUI_STATE)); } void OptionsButton::BackButton::onClick() { - gameManager->detachAllBitmapTexts(); - gameManager->detachAllImages(); + GameManager *gm = GameManager::getSingleton(); + gm->detachAllBitmapTexts(); + gm->detachAllImages(); state->removeAllListboxes(); state->removeAllCheckboxes(); state->removeAllSliders(); state->removeAllTextboxes(); state->removeButton("Ok"); state->removeButton("Restore defaults"); - OptionsButton *optionsButton = new OptionsButton(gameManager, vector2d(), vector2d(), "Options", true); + OptionsButton *optionsButton = new OptionsButton(vector2d(), vector2d(), "Options", true); optionsButton->onClick(); state->removeButton("Back"); } - OptionsButton::TabButton::TabButton(GameManager *gm, vector2d pos, vector2d size, stringw name) :Button(gm, pos, size, name, true) { - this->state = ((GuiAppState*)gm->getAppState(AppStateTypes::GUI_STATE)); + OptionsButton::TabButton::TabButton(vector2d pos, vector2d size, stringw name) :Button(pos, size, name, true) { + this->state = ((GuiAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::GUI_STATE)); } void OptionsButton::TabButton::onClick() { state->removeButton("Back"); - OkButton *okButton = new OkButton(gameManager); - DefaultsButton *defaultsButton = new DefaultsButton(gameManager); - BackButton *returnButton = new BackButton(gameManager); + OkButton *okButton = new OkButton(); + DefaultsButton *defaultsButton = new DefaultsButton(); + BackButton *returnButton = new BackButton(); defaultsButton->moveText(-25, 0); state->addButton(okButton); state->addButton(defaultsButton); state->addButton(returnButton); } - OptionsButton::ControlsTab::ControlsTab(GameManager *gm) :TabButton(gm, vector2d(gm->getWidth() / 4, gm->getHeight() / 10), vector2d(100, 50), "Controls") {} + OptionsButton::ControlsTab::ControlsTab() : TabButton( + vector2d(GameManager::getSingleton()->getWidth() / 4, + GameManager::getSingleton()->getHeight() / 10), + vector2d(100, 50), + "Controls" + ) {} + void OptionsButton::ControlsTab::onClick() { TabButton::onClick(); std::vector lines=readFile(std::string((PATH+path("../options.cfg")).c_str())); - Listbox *listbox = new Listbox(gameManager, vector2d(gameManager->getWidth() / 4, gameManager->getHeight() / 10), vector2d(360, 20), lines, lines.size()<5?lines.size():5,true); + GameManager *gm = GameManager::getSingleton(); + Listbox *listbox = new Listbox(vector2d(gm->getWidth() / 4, gm->getHeight() / 10), vector2d(360, 20), lines, lines.size()<5?lines.size():5,true); listbox->openUp(); state->addListbox(listbox); state->removeButton("Mouse"); @@ -69,16 +78,22 @@ namespace game{ state->removeButton("Controls"); } - OptionsButton::MouseTab::MouseTab(GameManager *gm) :TabButton(gm, vector2d(gm->getWidth() / 4, gm->getHeight() / 10 + 60), vector2d(100, 50), "Mouse") {} + OptionsButton::MouseTab::MouseTab() : TabButton( + vector2d(GameManager::getSingleton()->getWidth() / 4, GameManager::getSingleton()->getHeight() / 10 + 60), + vector2d(100, 50), + "Mouse" + ) {} + void OptionsButton::MouseTab::onClick() { TabButton::onClick(); - vector2d pos(gameManager->getWidth() / 3, gameManager->getHeight() / 4); - IGUIFont *font = gameManager->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fonthaettenschweiler.bmp"); - Slider *mouseSensitivitySlider = new Slider(gameManager, vector2d(pos.X, pos.Y), vector2d(300, 10), .1, 3.); - Textbox *mouseSensitivityTextbox = new Textbox(gameManager, vector2d(pos.X + 320, pos.Y - 10), vector2d(100, 20)); - Checkbox *reverseMouseCheckbox = new Checkbox(gameManager, vector2d(pos.X, pos.Y + 50)); - gameManager->attachBitmapText(new BitmapText(gameManager, "MouseSensitivity", vector2d(gameManager->getWidth() / 3 - 90, gameManager->getHeight() / 4 - 5), font)); - gameManager->attachBitmapText(new BitmapText(gameManager, "ReverseMouse", vector2d(gameManager->getWidth() / 3 + 20, gameManager->getHeight() / 4 + 50), font)); + GameManager *gm = GameManager::getSingleton(); + vector2d pos(gm->getWidth() / 3, gm->getHeight() / 4); + IGUIFont *font = gm->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fonthaettenschweiler.bmp"); + Slider *mouseSensitivitySlider = new Slider(vector2d(pos.X, pos.Y), vector2d(300, 10), .1, 3.); + Textbox *mouseSensitivityTextbox = new Textbox(vector2d(pos.X + 320, pos.Y - 10), vector2d(100, 20)); + Checkbox *reverseMouseCheckbox = new Checkbox(vector2d(pos.X, pos.Y + 50)); + gm->attachBitmapText(new BitmapText("MouseSensitivity", vector2d(gm->getWidth() / 3 - 90, gm->getHeight() / 4 - 5), font)); + gm->attachBitmapText(new BitmapText("ReverseMouse", vector2d(gm->getWidth() / 3 + 20, gm->getHeight() / 4 + 50), font)); state->addTextbox(mouseSensitivityTextbox); state->addSlider(mouseSensitivitySlider); state->addCheckbox(reverseMouseCheckbox); @@ -89,7 +104,7 @@ namespace game{ state->removeButton("Mouse"); } - OptionsButton::VideoTab::VideoTab(GameManager *gm) :TabButton(gm, vector2d(gm->getWidth() / 4, gm->getHeight() / 10 + 120), vector2d(100, 50), "Video") {} + OptionsButton::VideoTab::VideoTab() :TabButton(vector2d(GameManager::getSingleton()->getWidth() / 4, GameManager::getSingleton()->getHeight() / 10 + 120), vector2d(100, 50), "Video") {} void OptionsButton::VideoTab::onClick() { TabButton::onClick(); std::vector lines; @@ -98,25 +113,30 @@ namespace game{ s += i; lines.push_back(s); } - IGUIFont *font = gameManager->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fonthaettenschweiler.bmp"); - vector2d pos(gameManager->getWidth() / 3, gameManager->getHeight() / 8); - Listbox *resolutionsListbox = new Listbox(gameManager, vector2d(pos.X, pos.Y), vector2d(100, 20), lines, 5); - Checkbox *fullscreenCheckbox = new Checkbox(gameManager, vector2d(pos.X, pos.Y + 30)); - Checkbox *vsyncCheckbox = new Checkbox(gameManager, vector2d(pos.X, pos.Y + 50)); - Checkbox *normalMapCheckbox = new Checkbox(gameManager, vector2d(pos.X, pos.Y + 70)); - Checkbox *parallaxMapCheckbox = new Checkbox(gameManager, vector2d(pos.X, pos.Y + 90)); - Checkbox *specularMapCheckbox = new Checkbox(gameManager, vector2d(pos.X, pos.Y + 110)); - gameManager->attachBitmapText(new BitmapText(gameManager, "Fullscreen", vector2d(pos.X + 20, pos.Y + 30), font)); - gameManager->attachBitmapText(new BitmapText(gameManager, "VSync", vector2d(pos.X + 20, pos.Y + 50), font)); - gameManager->attachBitmapText(new BitmapText(gameManager, "Normal map", vector2d(pos.X + 20, pos.Y + 70), font)); - gameManager->attachBitmapText(new BitmapText(gameManager, "Parallax map", vector2d(pos.X + 20, pos.Y + 90), font)); - gameManager->attachBitmapText(new BitmapText(gameManager, "Specular map", vector2d(pos.X + 20, pos.Y + 110), font)); + GameManager *gm = GameManager::getSingleton(); + IGUIFont *font = gm->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fonthaettenschweiler.bmp"); + vector2d pos(gm->getWidth() / 3, gm->getHeight() / 8); + Listbox *resolutionsListbox = new Listbox(vector2d(pos.X, pos.Y), vector2d(100, 20), lines, 5); + + Checkbox *fullscreenCheckbox = new Checkbox(vector2d(pos.X, pos.Y + 30)); + Checkbox *vsyncCheckbox = new Checkbox(vector2d(pos.X, pos.Y + 50)); + Checkbox *normalMapCheckbox = new Checkbox(vector2d(pos.X, pos.Y + 70)); + Checkbox *parallaxMapCheckbox = new Checkbox(vector2d(pos.X, pos.Y + 90)); + Checkbox *specularMapCheckbox = new Checkbox(vector2d(pos.X, pos.Y + 110)); + + gm->attachBitmapText(new BitmapText("Fullscreen", vector2d(pos.X + 20, pos.Y + 30), font)); + gm->attachBitmapText(new BitmapText("VSync", vector2d(pos.X + 20, pos.Y + 50), font)); + gm->attachBitmapText(new BitmapText("Normal map", vector2d(pos.X + 20, pos.Y + 70), font)); + gm->attachBitmapText(new BitmapText("Parallax map", vector2d(pos.X + 20, pos.Y + 90), font)); + gm->attachBitmapText(new BitmapText("Specular map", vector2d(pos.X + 20, pos.Y + 110), font)); + state->addListbox(resolutionsListbox); state->addCheckbox(fullscreenCheckbox); state->addCheckbox(vsyncCheckbox); state->addCheckbox(normalMapCheckbox); state->addCheckbox(parallaxMapCheckbox); state->addCheckbox(specularMapCheckbox); + state->removeButton("Controls"); state->removeButton("Mouse"); state->removeButton("Audio"); @@ -124,12 +144,13 @@ namespace game{ state->removeButton("Video"); } - OptionsButton::AudioTab::AudioTab(GameManager *gm) :TabButton(gm, vector2d(gm->getWidth() / 4, gm->getHeight() / 10 + 180), vector2d(100, 50), "Audio") {} + OptionsButton::AudioTab::AudioTab() :TabButton(vector2d(GameManager::getSingleton()->getWidth() / 4, GameManager::getSingleton()->getHeight() / 10 + 180), vector2d(100, 50), "Audio") {} void OptionsButton::AudioTab::onClick() { TabButton::onClick(); - vector2d pos(gameManager->getWidth() / 3, gameManager->getHeight() / 8); - Slider *volumeSlider = new Slider(gameManager, vector2d(pos.X, pos.Y), vector2d(300, 10), 0., 2.); - Textbox *volumeTextbox = new Textbox(gameManager, vector2d(pos.X + 320, pos.Y), vector2d(100, 20)); + GameManager *gm = GameManager::getSingleton(); + vector2d pos(gm->getWidth() / 3, gm->getHeight() / 8); + Slider *volumeSlider = new Slider(vector2d(pos.X, pos.Y), vector2d(300, 10), 0., 2.); + Textbox *volumeTextbox = new Textbox(vector2d(pos.X + 320, pos.Y), vector2d(100, 20)); state->addTextbox(volumeTextbox); state->addSlider(volumeSlider); state->removeButton("Controls"); @@ -139,17 +160,18 @@ namespace game{ state->removeButton("Audio"); } - OptionsButton::MultiplayerTab::MultiplayerTab(GameManager *gm) :TabButton(gm, vector2d(gm->getWidth() / 4, gm->getHeight() / 10 + 240), vector2d(100, 50), "Multiplayer") {} + OptionsButton::MultiplayerTab::MultiplayerTab() : TabButton(vector2d(GameManager::getSingleton()->getWidth() / 4, GameManager::getSingleton()->getHeight() / 10 + 240), vector2d(100, 50), "Multiplayer") {} void OptionsButton::MultiplayerTab::onClick() { TabButton::onClick(); - IGUIFont *font = gameManager->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fonthaettenschweiler.bmp"); - vector2d pos(gameManager->getWidth() / 3, gameManager->getHeight() / 6); - Textbox *tcpTextbox = new Textbox(gameManager, vector2d(pos.X, pos.Y), vector2d(100, 20)); - Textbox *udpTextbox = new Textbox(gameManager, vector2d(pos.X, pos.Y + 30), vector2d(100, 20)); - Textbox *playerNameTextbox = new Textbox(gameManager, vector2d(pos.X, pos.Y + 60), vector2d(100, 20)); - gameManager->attachBitmapText(new BitmapText(gameManager, "TCP port", vector2d(pos.X - 50, pos.Y), font)); - gameManager->attachBitmapText(new BitmapText(gameManager, "UDP port", vector2d(pos.X - 50, pos.Y + 30), font)); - gameManager->attachBitmapText(new BitmapText(gameManager, "Player name", vector2d(pos.X - 65, pos.Y + 60), font)); + GameManager *gm = GameManager::getSingleton(); + IGUIFont *font = gm->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fonthaettenschweiler.bmp"); + vector2d pos(gm->getWidth() / 3, gm->getHeight() / 6); + Textbox *tcpTextbox = new Textbox(vector2d(pos.X, pos.Y), vector2d(100, 20)); + Textbox *udpTextbox = new Textbox(vector2d(pos.X, pos.Y + 30), vector2d(100, 20)); + Textbox *playerNameTextbox = new Textbox(vector2d(pos.X, pos.Y + 60), vector2d(100, 20)); + gm->attachBitmapText(new BitmapText("TCP port", vector2d(pos.X - 50, pos.Y), font)); + gm->attachBitmapText(new BitmapText("UDP port", vector2d(pos.X - 50, pos.Y + 30), font)); + gm->attachBitmapText(new BitmapText("Player name", vector2d(pos.X - 65, pos.Y + 60), font)); state->addTextbox(tcpTextbox); state->addTextbox(udpTextbox); state->addTextbox(playerNameTextbox); @@ -160,15 +182,15 @@ namespace game{ state->removeButton("Multiplayer"); } - OptionsButton::OptionsButton(GameManager *gm, vector2d pos, vector2d size, stringw name, bool separate) :Button(gm, pos, size, name, separate) { - this->state = ((GuiAppState*)gm->getAppState(AppStateTypes::GUI_STATE)); + OptionsButton::OptionsButton(vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { + this->state = ((GuiAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::GUI_STATE)); } void OptionsButton::onClick() { - ControlsTab *controlsTab = new ControlsTab(this->gameManager); - MouseTab *mouseTab = new MouseTab(this->gameManager); - VideoTab *videoTab = new VideoTab(this->gameManager); - AudioTab *audioTab = new AudioTab(this->gameManager); - MultiplayerTab *mupltiplayerTab = new MultiplayerTab(this->gameManager); + ControlsTab *controlsTab = new ControlsTab(); + MouseTab *mouseTab = new MouseTab(); + VideoTab *videoTab = new VideoTab(); + AudioTab *audioTab = new AudioTab(); + MultiplayerTab *mupltiplayerTab = new MultiplayerTab(); mupltiplayerTab->moveText(-20, 0); state->addButton(controlsTab); state->addButton(mouseTab); diff --git a/optionsButton.h b/optionsButton.h index 2b2ca48..6caa9ad 100755 --- a/optionsButton.h +++ b/optionsButton.h @@ -10,59 +10,59 @@ namespace game{ namespace gui { class OptionsButton : public Button { public: - OptionsButton(core::GameManager*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + OptionsButton(irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); class OkButton : public Button { public: - OkButton(core::GameManager*); + OkButton(); void onClick(); private: core::GuiAppState *state; }; class DefaultsButton : public Button { public: - DefaultsButton(core::GameManager*); + DefaultsButton(); void onClick(); private: core::GuiAppState *state; }; class BackButton : public Button { public: - BackButton(core::GameManager*); + BackButton(); void onClick(); private: core::GuiAppState *state; }; class TabButton : public Button { public: - TabButton(core::GameManager*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw); + TabButton(irr::core::vector2d, irr::core::vector2d, irr::core::stringw); void onClick(); protected: core::GuiAppState *state; }; class ControlsTab : public TabButton { public: - ControlsTab(core::GameManager*); + ControlsTab(); void onClick(); }; class MouseTab : public TabButton { public: - MouseTab(core::GameManager*); + MouseTab(); void onClick(); private: }; class VideoTab : public TabButton { public: - VideoTab(core::GameManager*); + VideoTab(); void onClick(); }; class AudioTab : public TabButton { public: - AudioTab(core::GameManager*); + AudioTab(); void onClick(); }; class MultiplayerTab : public TabButton { public: - MultiplayerTab(core::GameManager*); + MultiplayerTab(); void onClick(); }; virtual void onClick(); diff --git a/player.cpp b/player.cpp index afb062e..e795a26 100755 --- a/player.cpp +++ b/player.cpp @@ -4,8 +4,7 @@ using namespace game::core; namespace game{ namespace content{ - Player::Player(GameManager *gM, int difficulty, int faction,vector3df spawnPoint) { - gameManager = gM; + Player::Player(int difficulty, int faction,vector3df spawnPoint) { this->difficulty = difficulty; this->faction = faction; this->spawnPoint=spawnPoint; diff --git a/player.h b/player.h index 2679e4b..792fb48 100755 --- a/player.h +++ b/player.h @@ -4,6 +4,7 @@ #include #include + #include "gameManager.h" #include "unit.h" @@ -11,7 +12,7 @@ namespace game{ namespace content{ class Player { public: - Player(core::GameManager*, int, int,irr::core::vector3df=irr::core::vector3df(0,0,0)); + Player(int, int,irr::core::vector3df=irr::core::vector3df(0,0,0)); ~Player(); void update(); bool isThisPlayersUnit(Unit*); @@ -28,7 +29,6 @@ namespace game{ int credits, faction, difficulty,side,id; std::vector units; irr::core::vector3df spawnPoint; - core::GameManager *gameManager; }; } } diff --git a/projectile.cpp b/projectile.cpp index 2296655..9511d58 100755 --- a/projectile.cpp +++ b/projectile.cpp @@ -1,6 +1,7 @@ #include #include "projectile.h" +#include "stateManager.h" #include "projectileData.h" #include "inGameAppState.h" #include "unit.h" @@ -17,8 +18,7 @@ using namespace sf; namespace game{ namespace content{ - Projectile::Projectile(GameManager *gM, Unit *unit, ISceneNode *node, vector3df pos, vector3df dir, vector3df left, vector3df up, int id, int weaponTypeId, int weaponId) { - gameManager = gM; + Projectile::Projectile(Unit *unit, ISceneNode *node, vector3df pos, vector3df dir, vector3df left, vector3df up, int id, int weaponTypeId, int weaponId) { this->unit=unit; this->id=id; this->weaponTypeId=weaponTypeId; @@ -26,9 +26,12 @@ namespace game{ leftVec = left; upVec = up; this->rayLength=projectileData::length[id][weaponTypeId][weaponId]; - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); - ISceneManager *smgr = gameManager->getDevice()->getSceneManager(); + + GameManager *gm = GameManager::getSingleton(); + IVideoDriver *driver = gm->getDevice()->getVideoDriver(); + ISceneManager *smgr = gm->getDevice()->getSceneManager(); this->node=node; + if(!node){ mesh = smgr->getMesh(projectileData::meshPath[id][weaponTypeId]); this->node = smgr->addAnimatedMeshSceneNode(mesh); @@ -60,31 +63,34 @@ namespace game{ } Projectile::~Projectile(){ - ISceneManager *smgr = gameManager->getDevice()->getSceneManager(); + ISceneManager *smgr = GameManager::getSingleton()->getDevice()->getSceneManager(); node->getParent()->removeChild(node); } void Projectile::update() { if(unit->isDebuggable()) debug(); + checkForCollision(); } void Projectile::checkForCollision() { - ISceneNode *collNode = castRay(gameManager->getSceneManager(),pos,pos+dirVec*rayLength); + ISceneNode *collNode = castRay(GameManager::getSingleton()->getSceneManager(),pos,pos+dirVec*rayLength); + if (pos.Y<-7 || (collNode&&collNode!=unit->getNode())) explode(collNode); } void Projectile::explode(ISceneNode *collNode) { exploded = true; - vector players = ((InGameAppState*) gameManager->getAppState(AppStateTypes::IN_GAME_STATE))->getPlayers(); + StateManager *stateManager = GameManager::getSingleton()->getStateManager(); + vector players = ((InGameAppState*) stateManager->getAppState(AppStateTypes::IN_GAME_STATE))->getPlayers(); for (Player *p : players) { for (Unit *u : p->getUnits()) if (collNode == u->getNode()) u->takeDamage(damage); } - InGameAppState *inGameState=((InGameAppState*)gameManager->getAppState(AppStateTypes::IN_GAME_STATE)); + InGameAppState *inGameState=((InGameAppState*)stateManager->getAppState(AppStateTypes::IN_GAME_STATE)); if(id==8) detonateTorpedo(inGameState,pos); else if(weaponTypeId==1&&(id==2||id==3)) @@ -94,7 +100,7 @@ namespace game{ } void Projectile::debug(){ - IVideoDriver *driver=gameManager->getDevice()->getVideoDriver(); + IVideoDriver *driver = GameManager::getSingleton()->getDevice()->getVideoDriver(); driver->draw3DLine(pos,pos+dirVec,SColor(255,0,0,255)); driver->draw3DLine(pos,pos+leftVec,SColor(255,255,0,0)); driver->draw3DLine(pos,pos+upVec,SColor(255,0,255,0)); diff --git a/projectile.h b/projectile.h index 860cf7e..ce24b27 100755 --- a/projectile.h +++ b/projectile.h @@ -14,7 +14,7 @@ namespace game{ class Projectile { public: - Projectile(core::GameManager*, Unit*, irr::scene::ISceneNode*, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, int, int, int); + Projectile(Unit*, irr::scene::ISceneNode*, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, int, int, int); virtual ~Projectile(); virtual void update(); virtual void debug(); @@ -24,7 +24,6 @@ namespace game{ protected: virtual void checkForCollision(); void explode(irr::scene::ISceneNode*); - core::GameManager *gameManager; irr::scene::IAnimatedMesh *mesh; irr::scene::ISceneNode *node=nullptr; bool exploded = false; diff --git a/shell.cpp b/shell.cpp index e4f7b88..4d80731 100755 --- a/shell.cpp +++ b/shell.cpp @@ -10,7 +10,7 @@ using namespace irr::core; namespace game{ namespace content{ - Shell::Shell(GameManager *gM, Unit *unit, vector3df pos, vector3df dir, vector3df left, vector3df up, int id, int weaponTypeId, int weaponId) : Projectile(gM, unit, nullptr, pos, dir, left, up, id, weaponTypeId, weaponId) { + Shell::Shell(Unit *unit, vector3df pos, vector3df dir, vector3df left, vector3df up, int id, int weaponTypeId, int weaponId) : Projectile(unit, nullptr, pos, dir, left, up, id, weaponTypeId, weaponId) { this->speed = projectileData::speed[id][weaponTypeId][weaponId]; node->setScale(vector3df(1, 1, 1) * projectileData::scale[id][weaponTypeId][weaponId]); initTime = getTime(); diff --git a/shell.h b/shell.h index 980e60f..eabf9e2 100755 --- a/shell.h +++ b/shell.h @@ -9,7 +9,7 @@ namespace game{ namespace content { class Shell : public Projectile { public: - Shell(core::GameManager*, Unit*, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, int, int, int); + Shell(Unit*, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, int, int, int); ~Shell(){} virtual void update(); protected: diff --git a/slider.cpp b/slider.cpp index 0473588..ea2256b 100755 --- a/slider.cpp +++ b/slider.cpp @@ -11,7 +11,7 @@ using namespace game::core; namespace game{ namespace gui{ - Slider::MovableSliderButton::MovableSliderButton(GameManager *gM, Slider *slider, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + Slider::MovableSliderButton::MovableSliderButton(Slider *slider, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { this->slider = slider; } @@ -20,7 +20,9 @@ namespace game{ } void Slider::MovableSliderButton::update() { - int posX = gameManager->getDevice()->getCursorControl()->getPosition().X, posY = gameManager->getDevice()->getCursorControl()->getPosition().Y; + GameManager *gm = GameManager::getSingleton(); + int posX = gm->getDevice()->getCursorControl()->getPosition().X, posY = gm->getDevice()->getCursorControl()->getPosition().Y; + if (clicked && posY > slider->getPos().Y && posY < slider->getPos().Y + slider->getSize().Y) { if (posX < slider->getPos().X) posX = slider->getPos().X; @@ -30,23 +32,22 @@ namespace game{ } } - Slider::StaticSliderButton::StaticSliderButton(GameManager *gM, Slider *slider, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + Slider::StaticSliderButton::StaticSliderButton(Slider *slider, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { this->slider = slider; } void Slider::StaticSliderButton::onClick() { - s16 buttonClickPoint = -slider->getPos().X + gameManager->getDevice()->getCursorControl()->getPosition().X; + s16 buttonClickPoint = -slider->getPos().X + GameManager::getSingleton()->getDevice()->getCursorControl()->getPosition().X; slider->setValue(((double) buttonClickPoint / slider->getSize().X) * slider->getMaxValue()); } - Slider::Slider(GameManager *gM, vector2d pos, vector2d size, double minValue, double maxValue) { - gameManager = gM; + Slider::Slider(vector2d pos, vector2d size, double minValue, double maxValue) { this->pos = pos; this->size = size; this->minValue = minValue; this->maxValue = maxValue; - staticSliderButton = new StaticSliderButton(gM, this, pos, size, "StaticSliderButton", false); - movableSliderButton = new MovableSliderButton(gM, this, vector2d(pos.X + staticSliderButton->getSize().X / 1, pos.Y - 15), vector2d(10, 40), "MovableSliderButton", false); + staticSliderButton = new StaticSliderButton(this, pos, size, "StaticSliderButton", false); + movableSliderButton = new MovableSliderButton(this, vector2d(pos.X + staticSliderButton->getSize().X / 1, pos.Y - 15), vector2d(10, 40), "MovableSliderButton", false); value = (maxValue - minValue) / 2; } @@ -54,7 +55,8 @@ namespace game{ } void Slider::update() { - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + IVideoDriver *driver = GameManager::getSingleton()->getDevice()->getVideoDriver(); + if (value < minValue) value = minValue; else if (value > maxValue) diff --git a/slider.h b/slider.h index 17542cf..175270d 100755 --- a/slider.h +++ b/slider.h @@ -11,7 +11,7 @@ namespace game{ namespace gui{ class Slider { public: - Slider(core::GameManager*, irr::core::vector2d, irr::core::vector2d, double, double); + Slider(irr::core::vector2d, irr::core::vector2d, double, double); ~Slider(); void update(); inline void addTextbox(Textbox *t){this->textbox=t;} @@ -27,7 +27,7 @@ namespace game{ private: class MovableSliderButton : public Button { public: - MovableSliderButton(core::GameManager*, Slider*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + MovableSliderButton(Slider*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); void update(); private: @@ -36,7 +36,7 @@ namespace game{ }; class StaticSliderButton : public Button { public: - StaticSliderButton(core::GameManager*, Slider*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + StaticSliderButton(Slider*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); private: Slider *slider; @@ -44,7 +44,6 @@ namespace game{ MovableSliderButton *movableSliderButton = nullptr; StaticSliderButton *staticSliderButton = nullptr; double minValue, value, maxValue, increment = .1; - core::GameManager *gameManager = nullptr; irr::core::vector2d pos, size; Textbox *textbox = nullptr; public: diff --git a/stateManager.cpp b/stateManager.cpp new file mode 100644 index 0000000..202ac0f --- /dev/null +++ b/stateManager.cpp @@ -0,0 +1,45 @@ +#include "stateManager.h" +#include "abstractAppState.h" + +namespace game{ + namespace core{ + void StateManager::attachState(AbstractAppState *a) { + bool alreadyAttached = false; + for (int i = 0; i < appStates.size()&&!alreadyAttached; i++) + if (appStates[i] == a) + alreadyAttached = true; + if (!alreadyAttached) { + a->onAttachment(); + appStates.push_back(a); + } + } + + void StateManager::dettachState(AbstractAppState *a) { + bool alreadyDetached = false; + for (int i = 0; i < appStates.size()&&!alreadyDetached; i++) + if (appStates[i] == a) + alreadyDetached = true; + if (!alreadyDetached) { + a->onDetachment(); + for (int i = 0; i < appStates.size(); i++) + if (a == appStates[i]) { + appStates.erase(appStates.begin() + i); + } + } + } + + void StateManager::update() { + for (AbstractAppState *a : appStates) + if (a->isAttached() && a) + a->update(); + } + + AbstractAppState* StateManager::getAppState(AppStateTypes t) { + AbstractAppState *state = nullptr; + for (AbstractAppState *a : appStates) + if (a->getType() == t) + state = a; + return state; + } + } +} diff --git a/stateManager.h b/stateManager.h new file mode 100644 index 0000000..e947bf1 --- /dev/null +++ b/stateManager.h @@ -0,0 +1,27 @@ +#ifndef STATE_MANAGER_H +#define STATE_MANAGER_H + +#include + +#include "abstractAppState.h" + +namespace game{ + namespace core{ + class StateManager{ + public: + StateManager(){} + void update(); + void attachState(AbstractAppState*); + void dettachState(AbstractAppState*); + void dettachState(AppStateTypes); + AbstractAppState* getAppState(AppStateTypes); + inline AbstractAppState* getAppState(int id){return appStates[id];} + inline void setAppState(int i, AbstractAppState *a){appStates[i]=a;} + inline int getAppStateNumber(){return appStates.size();} + private: + std::vector appStates; + }; + } +} + +#endif diff --git a/submarine.cpp b/submarine.cpp index f2729d0..3587c69 100755 --- a/submarine.cpp +++ b/submarine.cpp @@ -10,7 +10,7 @@ using namespace game::util; namespace game{ namespace content{ - Submarine::Submarine(GameManager *gM, Player *player,vector3df pos, int id) : Unit(gM, player,pos, id) {} + Submarine::Submarine(Player *player,vector3df pos, int id) : Unit(player,pos, id) {} void Submarine::attack(Order order) { float angle=getAngleBetween(dirVec,*(order.targetPos[0])-pos); @@ -18,7 +18,7 @@ namespace game{ Unit::move(order, range); if (canFire()) { vector3df p = pos + projectileData::pos[id][0][0].X * leftVec + projectileData::pos[id][0][0].Y * upVec - projectileData::pos[id][0][0].Z*dirVec; - addProjectile(new Torpedo(gameManager, this, p, dirVec, leftVec, upVec, getId(), 0, 0)); + addProjectile(new Torpedo(this, p, dirVec, leftVec, upVec, getId(), 0, 0)); lastShotTime=getTime(); } } diff --git a/submarine.h b/submarine.h index 8a468f9..4f58cbe 100755 --- a/submarine.h +++ b/submarine.h @@ -9,7 +9,7 @@ namespace game{ namespace content{ class Submarine : public Unit { public: - Submarine(core::GameManager*, Player*, irr::core::vector3df, int); + Submarine(Player*, irr::core::vector3df, int); inline bool isSubmerged() {return submerged;} void emerge(); void submerge(); diff --git a/textbox.cpp b/textbox.cpp index 30f9168..4b3f6b1 100755 --- a/textbox.cpp +++ b/textbox.cpp @@ -9,7 +9,7 @@ using namespace irr::video; namespace game{ namespace gui{ - Textbox::TextboxButton::TextboxButton(GameManager *gM, Textbox *t, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + Textbox::TextboxButton::TextboxButton(Textbox *t, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { textbox = t; } @@ -20,26 +20,27 @@ namespace game{ textbox->disable(); } - Textbox::Textbox(GameManager *gM, vector2d pos, vector2d size) { - gameManager = gM; + Textbox::Textbox(vector2d pos, vector2d size) { this->pos = pos; this->size = size; - textboxButton = new TextboxButton(gM, this, pos, size, "TextboxButton", false); - font = gameManager->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/bigfont.png"); + textboxButton = new TextboxButton(this, pos, size, "TextboxButton", false); + font = GameManager::getSingleton()->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/bigfont.png"); } Textbox::~Textbox() {} void Textbox::update() { - gameManager->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 200, 200, 200), rect(pos.X, pos.Y, pos.X + size.X, pos.Y + size.Y), nullptr); + GameManager *gm = GameManager::getSingleton(); + gm->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 200, 200, 200), rect(pos.X, pos.Y, pos.X + size.X, pos.Y + size.Y), nullptr); font->draw(entry, rect(pos.X, pos.Y - 8, pos.X + size.X, pos.Y - 8 + size.Y), SColor(255, 255, 255, 255)); + if (enabled) { if (canChangeCursor()) { canShowCursor = !canShowCursor; lastBlinkTime = getTime(); } if (canShowCursor) - gameManager->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 100, 100, 100), rect(pos.X + 20 * (entry.size() - cursorPosOffset), pos.Y, pos.X + 4 + 20 * (entry.size() - cursorPosOffset), pos.Y + size.Y), nullptr); + gm->getDevice()->getVideoDriver()->draw2DRectangle(SColor(255, 100, 100, 100), rect(pos.X + 20 * (entry.size() - cursorPosOffset), pos.Y, pos.X + 4 + 20 * (entry.size() - cursorPosOffset), pos.Y + size.Y), nullptr); } } diff --git a/textbox.h b/textbox.h index e5f688c..e6f167a 100755 --- a/textbox.h +++ b/textbox.h @@ -10,7 +10,7 @@ namespace game{ namespace gui { class Textbox { public: - Textbox(core::GameManager*, irr::core::vector2d, irr::core::vector2d); + Textbox(irr::core::vector2d, irr::core::vector2d); ~Textbox(); void update(); void enable(); @@ -27,12 +27,12 @@ namespace game{ private: class TextboxButton : public Button { public: - TextboxButton(core::GameManager*, Textbox*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); + TextboxButton(Textbox*, irr::core::vector2d, irr::core::vector2d, irr::core::stringw, bool); void onClick(); private: Textbox *textbox; }; - core::GameManager *gameManager; + irr::gui::IGUIFont *font; irr::core::vector2d pos, size; irr::core::stringw entry = ""; diff --git a/tooltip.cpp b/tooltip.cpp index 26e135d..be02989 100644 --- a/tooltip.cpp +++ b/tooltip.cpp @@ -6,10 +6,9 @@ namespace game{ using namespace irr::video; using namespace game::core; - Tooltip::Tooltip(GameManager *gm, vector2di pos, stringw entry){ - gameManager=gm; + Tooltip::Tooltip(vector2di pos, stringw entry){ this->pos=pos; - text=new BitmapText(gm,entry,pos,gm->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fontcourier.bmp")); + text=new BitmapText(entry,pos,GameManager::getSingleton()->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fontcourier.bmp")); gameManager->attachBitmapText(text); } diff --git a/tooltip.h b/tooltip.h index d54ca9a..1d95798 100644 --- a/tooltip.h +++ b/tooltip.h @@ -7,7 +7,7 @@ namespace game{ namespace gui{ class Tooltip{ public: - Tooltip(core::GameManager*,irr::core::vector2di, irr::core::stringw); + Tooltip(irr::core::vector2di, irr::core::stringw); ~Tooltip(); void update(); private: diff --git a/torpedo.cpp b/torpedo.cpp index 98c8ab7..86d1a4d 100755 --- a/torpedo.cpp +++ b/torpedo.cpp @@ -4,8 +4,8 @@ using namespace game::core; namespace game{ namespace content{ - Torpedo::Torpedo(GameManager *gM, Unit *unit, irr::core::vector3df pos, irr::core::vector3df dir, irr::core::vector3df left, irr::core::vector3df up, int id,int weaponTypeId,int weaponId) : - Projectile(gM, unit, nullptr, pos, dir, left, up, id,weaponTypeId,weaponId) {this->damage=50;} + Torpedo::Torpedo(Unit *unit, irr::core::vector3df pos, irr::core::vector3df dir, irr::core::vector3df left, irr::core::vector3df up, int id,int weaponTypeId,int weaponId) : + Projectile(unit, nullptr, pos, dir, left, up, id,weaponTypeId,weaponId) {this->damage=50;} void Torpedo::update() { Projectile::update(); diff --git a/torpedo.h b/torpedo.h index bb18f2b..d787860 100755 --- a/torpedo.h +++ b/torpedo.h @@ -8,7 +8,7 @@ namespace game{ namespace content{ class Torpedo : public Projectile { public: - Torpedo(core::GameManager*, Unit*, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, int, int, int); + Torpedo(Unit*, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, irr::core::vector3df, int, int, int); ~Torpedo(){} void update(); // void explode() diff --git a/unit.cpp b/unit.cpp index 20603f3..97dbce0 100755 --- a/unit.cpp +++ b/unit.cpp @@ -1,5 +1,6 @@ #include "unit.h" #include "inGameAppState.h" +#include "stateManager.h" #include "util.h" #include "unitData.h" @@ -10,10 +11,9 @@ using namespace sf; namespace game{ namespace content{ - Unit::Unit(GameManager *gM, Player *player, vector3df pos, int id) { + Unit::Unit(Player *player, vector3df pos, int id) { this->id = id; this->player = player; - gameManager = gM; type = unitData::unitType[id]; this->health = unitData::health[id]; this->maxTurnAngle = unitData::maxTurnAngle[id]; @@ -22,8 +22,10 @@ namespace game{ width = unitData::unitCornerPoints[id][0].X - unitData::unitCornerPoints[id][1].X; height = unitData::unitCornerPoints[id][4].Y - unitData::unitCornerPoints[id][0].Y; length = unitData::unitCornerPoints[id][3].Z - unitData::unitCornerPoints[id][0].Z; - ISceneManager *smgr = gameManager->getDevice()->getSceneManager(); - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + + GameManager *gm = GameManager::getSingleton(); + ISceneManager *smgr = gm->getDevice()->getSceneManager(); + IVideoDriver *driver = gm->getDevice()->getVideoDriver(); this->speed = unitData::speed[id]; mesh = smgr->getMesh(basePath[id] + meshPath[id]); node = smgr->addAnimatedMeshSceneNode(mesh); @@ -33,22 +35,26 @@ namespace game{ node->setTriangleSelector(tr); tr->drop(); ITexture *diffuseTexture = driver->getTexture(basePath[id] + "diffuseMap.png"); + if (diffuseTexture) node->setMaterialTexture(0, diffuseTexture); else node->setMaterialTexture(0, driver->getTexture(DEFAULT_TEXTURE)); + light = smgr->addLightSceneNode(node, vector3df(0, 2, 0), SColor(255, 255, 255, 255), lineOfSight); light->setVisible(false); node->setVisible(false); selectionSfxBuffer=new SoundBuffer(); path p=PATH+"Sounds/"+unitData::name[id]+"s/selection.ogg"; + if(selectionSfxBuffer->loadFromFile(p.c_str())) selectionSfx=new Sound(*selectionSfxBuffer); } Unit::~Unit() { - ISceneManager *smgr = gameManager->getDevice()->getSceneManager(); - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + GameManager *gm = GameManager::getSingleton(); + ISceneManager *smgr = gm->getDevice()->getSceneManager(); + IVideoDriver *driver = gm->getDevice()->getVideoDriver(); // driver->removeTexture(node->getMaterial(0).getTexture(0)); node->removeChild(light); node->getParent()->removeChild(node); @@ -71,7 +77,7 @@ namespace game{ r=255,g=255; break; } - gameManager->getDevice()->getVideoDriver()->draw3DLine(pos,*(orders[0].targetPos[0]),SColor(255,r,g,b)); + GameManager::getSingleton()->getDevice()->getVideoDriver()->draw3DLine(pos,*(orders[0].targetPos[0]),SColor(255,r,g,b)); } while (patrolPoints.size() > 0 && orders[0].type != Order::TYPE::PATROL) { patrolPoints.pop_back(); @@ -92,15 +98,18 @@ namespace game{ float unitH = camPos.getDistanceFrom(pos) * cos(getAngleBetween(pos - camPos, camDir)); float ratio = unitH / camH; float minRatio = ((cam->getViewFrustum()->getNearLeftUp() - camPos) / (farLeftUpPoint - camPos)).getLength(); + if (minRatio <= ratio && ratio <= 1.) { float width = farRightUpPoint.getDistanceFrom(farLeftUpPoint) * ratio; float height = farLeftUpPoint.getDistanceFrom(farLeftDownPoint) * ratio; vector3df edgePoint = (farLeftUpPoint - camPos) * ratio + camPos; float angle = getAngleBetween(pos - edgePoint, -camLeft); float x = pos.getDistanceFrom(edgePoint) * cos(angle), y = pos.getDistanceFrom(edgePoint) * cos(PI / 2 - angle); + if (x <= width && y <= height) { selectable = true; - screenPos = vector2d(gameManager->getWidth() * x / width, gameManager->getHeight() * y / height); + GameManager *gm = GameManager::getSingleton(); + screenPos = vector2d(gm->getWidth() * x / width, gm->getHeight() * y / height); } else selectable = false; } else @@ -120,7 +129,7 @@ namespace game{ } void Unit::displayUnitStats() { - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + IVideoDriver *driver = GameManager::getSingleton()->getDevice()->getVideoDriver(); SColor white = SColor(255, 255, 255, 255); driver->draw2DLine(screenPos + vector2d(-51, 0), screenPos + vector2d(50, 0), white); driver->draw2DLine(screenPos + vector2d(-51, 0), screenPos + vector2d(-51, -20), white); @@ -131,7 +140,7 @@ namespace game{ } void Unit::drawCuboid() { - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + IVideoDriver *driver = GameManager::getSingleton()->getDevice()->getVideoDriver(); driver->setTransform(ETS_WORLD, IdentityMatrix); for (int i = 0; i < 8; i++) { vector3df vec = unitCornerPoints[id][i]; @@ -150,7 +159,7 @@ namespace game{ } void Unit::debug() { - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + IVideoDriver *driver = GameManager::getSingleton()->getDevice()->getVideoDriver(); driver->setTransform(ETS_WORLD, IdentityMatrix); driver->setMaterial(createLineMaterial()); float length = unitAxisLength[id]; @@ -297,7 +306,7 @@ namespace game{ std::vector Unit::getProjectiles(){ std::vector projectiles; - InGameAppState *inGameState=((InGameAppState*)gameManager->getAppState(AppStateTypes::IN_GAME_STATE)); + InGameAppState *inGameState=((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::IN_GAME_STATE)); for(Projectile *p: inGameState->getProjectiles()) if(p->getUnit()==this) projectiles.push_back(p); @@ -307,7 +316,7 @@ namespace game{ void Unit::removeOrder(int id) { for (vector3df *v : orders[id].targetPos) { bool isUnitPos = false; - InGameAppState *state = (InGameAppState*) gameManager->getAppState(AppStateTypes::IN_GAME_STATE); + InGameAppState *state = (InGameAppState*) GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::IN_GAME_STATE); for (Player *p : state->getPlayers()) for (Unit *u : p->getUnits()) if (v == u->getPosPtr()) @@ -331,6 +340,6 @@ namespace game{ return mat; } - void Unit::addProjectile(Projectile *p) {((InGameAppState*)gameManager->getAppState(AppStateTypes::IN_GAME_STATE))->addProjectile(p);} + void Unit::addProjectile(Projectile *p) {((InGameAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::IN_GAME_STATE))->addProjectile(p);} } } diff --git a/unit.h b/unit.h index 01348bc..abf1708 100755 --- a/unit.h +++ b/unit.h @@ -32,7 +32,7 @@ namespace game{ class Unit { public: - Unit(core::GameManager*, Player*,vector3df, int); + Unit(Player*,vector3df, int); ~Unit(); virtual void update(); virtual void blowUp(); @@ -80,7 +80,6 @@ namespace game{ sf::SoundBuffer *selectionSfxBuffer; sf::Sound *selectionSfx=nullptr; protected: - core::GameManager *gameManager; Player *player; MoveDir moveDir = MoveDir::FORWARD; irr::video::SMaterial createLineMaterial(); diff --git a/util.cpp b/util.cpp index 16e020b..41897f2 100755 --- a/util.cpp +++ b/util.cpp @@ -2,6 +2,7 @@ #include "util.h" #include "gameManager.h" +#include "stateManager.h" #include "guiAppState.h" #include "inGameAppState.h" @@ -51,12 +52,12 @@ namespace game{ outFile.close(); } - void makeTitlescreenButtons(GameManager *gameManager, GuiAppState *state) { + void makeTitlescreenButtons(GuiAppState *state) { class SpButton : public Button { public: - SpButton(GuiAppState *state, GameManager *gM, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + SpButton(GuiAppState *state, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { this->state = state; } @@ -65,8 +66,8 @@ namespace game{ class PlayButton : public Button { public: - PlayButton(GameManager *gM, Listbox **difficulties, Listbox **factions, int lengths[2], vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { - this->state = ((GuiAppState*)gM->getAppState(AppStateTypes::GUI_STATE)); + PlayButton(Listbox **difficulties, Listbox **factions, int lengths[2], vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { + this->state = ((GuiAppState*)GameManager::getSingleton()->getStateManager()->getAppState(AppStateTypes::GUI_STATE)); this->lengths[0]=lengths[0]; this->lengths[1]=lengths[1]; difficultiesListboxes=difficulties; @@ -74,15 +75,19 @@ namespace game{ } void onClick() { - gameManager->detachAllBitmapTexts(); + GameManager *gm = GameManager::getSingleton(); + gm->detachAllBitmapTexts(); state->removeButton("Back"); // gameManager->dettachState(state); std::vector difficulties,factions; + for(int i=0;igetLine(difficultiesListboxes[i]->getSelectedOption())); + for(int i=0;igetSelectedOption())); - gameManager->attachState(new InGameAppState(gameManager, difficulties, factions)); + + gm->getStateManager()->attachState(new InGameAppState(difficulties, factions)); state->removeAllListboxes(); delete[] difficultiesListboxes; delete[] factionsListboxes; @@ -97,7 +102,7 @@ namespace game{ class ReturnButton : public Button { public: - ReturnButton(GuiAppState *state, GameManager *gM, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + ReturnButton(GuiAppState *state, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { this->state = state; } @@ -106,36 +111,38 @@ namespace game{ state->removeAllListboxes(); state->removeAllSliders(); state->removeAllTextboxes(); - gameManager->detachAllBitmapTexts(); - makeTitlescreenButtons(gameManager, state); + GameManager::getSingleton()->detachAllBitmapTexts(); + makeTitlescreenButtons(state); state->removeButton("Play"); state->removeButton("Back"); } private: GuiAppState *state; }; - IGUIFont *font = gameManager->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fonthaettenschweiler.bmp"); - vector2d pos(gameManager->getWidth() / 8, gameManager->getHeight() / 8); + + GameManager *gm = GameManager::getSingleton(); + IGUIFont *font = gm->getDevice()->getGUIEnvironment()->getFont(PATH + "Fonts/fonthaettenschweiler.bmp"); + vector2d pos(gm->getWidth() / 8, gm->getHeight() / 8); difficulties.push_back("Easy"); difficulties.push_back("Medium"); difficulties.push_back("Hard"); factions.push_back("0"); factions.push_back("1"); - gameManager->attachBitmapText(new BitmapText(gameManager, "Difficulty", vector2d(pos.X, pos.Y - 20), font)); - gameManager->attachBitmapText(new BitmapText(gameManager, "Faction", vector2d(pos.X + 110, pos.Y - 20), font)); - gameManager->attachBitmapText(new BitmapText(gameManager, "Player", vector2d(pos.X - 40, pos.Y), font)); - gameManager->attachBitmapText(new BitmapText(gameManager, "CPU", vector2d(pos.X - 20, pos.Y + 30), font)); - Listbox *cpuDifficulty = new Listbox(gameManager, vector2d(pos.X, pos.Y + 30), vector2d(100, 20), difficulties, 3); - Listbox *cpuFaction = new Listbox(gameManager, vector2d(pos.X + 110, pos.Y + 30), vector2d(100, 20), factions, 2); - Listbox *playerFaction = new Listbox(gameManager, vector2d(pos.X + 110, pos.Y), vector2d(100, 20), factions, 2); + gm->attachBitmapText(new BitmapText("Difficulty", vector2d(pos.X, pos.Y - 20), font)); + gm->attachBitmapText(new BitmapText("Faction", vector2d(pos.X + 110, pos.Y - 20), font)); + gm->attachBitmapText(new BitmapText("Player", vector2d(pos.X - 40, pos.Y), font)); + gm->attachBitmapText(new BitmapText("CPU", vector2d(pos.X - 20, pos.Y + 30), font)); + Listbox *cpuDifficulty = new Listbox(vector2d(pos.X, pos.Y + 30), vector2d(100, 20), difficulties, 3); + Listbox *cpuFaction = new Listbox(vector2d(pos.X + 110, pos.Y + 30), vector2d(100, 20), factions, 2); + Listbox *playerFaction = new Listbox(vector2d(pos.X + 110, pos.Y), vector2d(100, 20), factions, 2); Listbox **difficultyListboxes=new Listbox*[1]; Listbox **factionListboxes=new Listbox*[2]; difficultyListboxes[0]=cpuDifficulty; factionListboxes[0]=playerFaction; factionListboxes[1]=cpuFaction; int lengths[]{1,2}; - PlayButton *playButton = new PlayButton(gameManager, difficultyListboxes, factionListboxes, lengths, vector2d(50, gameManager->getHeight() - 150), vector2d(140, 50), "Play", true); - ReturnButton *returnButton = new ReturnButton(state, gameManager, vector2d(200, gameManager->getHeight() - 150), vector2d(140, 50), "Back", true); + PlayButton *playButton = new PlayButton(difficultyListboxes, factionListboxes, lengths, vector2d(50, gm->getHeight() - 150), vector2d(140, 50), "Play", true); + ReturnButton *returnButton = new ReturnButton(state, vector2d(200, gm->getHeight() - 150), vector2d(140, 50), "Back", true); state->addButton(playButton); state->addButton(returnButton); state->addListbox(cpuDifficulty); @@ -152,12 +159,12 @@ namespace game{ class MainMenuOptionsButton : public OptionsButton { public: - MainMenuOptionsButton(GuiAppState *state, GameManager *gM, vector2d pos, vector2d size, stringw name, bool separate) : OptionsButton(gM, pos, size, name, separate) { + MainMenuOptionsButton(GuiAppState *state, vector2d pos, vector2d size, stringw name, bool separate) : OptionsButton(pos, size, name, separate) { this->state = state; } void onClick() { - ReturnButton *returnButton = new ReturnButton(state, gameManager, vector2d(50, gameManager->getHeight() - 150), vector2d(150, 50), "Back", true); + ReturnButton *returnButton = new ReturnButton(state, vector2d(50, GameManager::getSingleton()->getHeight() - 150), vector2d(150, 50), "Back", true); state->addButton(returnButton); state->removeButton("Singleplayer"); state->removeButton("Exit"); @@ -169,7 +176,7 @@ namespace game{ class ReturnButton : public Button { public: - ReturnButton(GuiAppState *state, GameManager *gM, vector2d pos, vector2d size, stringw name, bool separate) : Button(gM, pos, size, name, separate) { + ReturnButton(GuiAppState *state, vector2d pos, vector2d size, stringw name, bool separate) : Button(pos, size, name, separate) { this->state = state; } @@ -183,10 +190,11 @@ namespace game{ state->removeButton("Video"); state->removeButton("Audio"); state->removeButton("Multiplayer"); - SpButton *spButton = new SpButton(state, gameManager, vector2d(gameManager->getWidth() / 16, gameManager->getHeight() / 12), vector2d(150, 40), "Singleplayer", true); - MainMenuOptionsButton *optionsButton = new MainMenuOptionsButton(state, gameManager, vector2d(gameManager->getWidth() / 16, gameManager->getHeight() / 12 * 2), vector2d(150, 40), "Options", true); + GameManager *gm = GameManager::getSingleton(); + SpButton *spButton = new SpButton(state, vector2d(gm->getWidth() / 16, gm->getHeight() / 12), vector2d(150, 40), "Singleplayer", true); + MainMenuOptionsButton *optionsButton = new MainMenuOptionsButton(state, vector2d(gm->getWidth() / 16, gm->getHeight() / 12 * 2), vector2d(150, 40), "Options", true); state->addButton(optionsButton); - ExitButton *exitButton = new ExitButton(gameManager, vector2d(gameManager->getWidth() / 16, gameManager->getHeight() / 12 * 3), vector2d(150, 40), "Exit", true); + ExitButton *exitButton = new ExitButton(vector2d(gm->getWidth() / 16, gm->getHeight() / 12 * 3), vector2d(150, 40), "Exit", true); state->addButton(spButton); state->addButton(exitButton); state->removeButton("Back"); @@ -195,9 +203,11 @@ namespace game{ GuiAppState *state; }; }; - SpButton *spButton = new SpButton(state, gameManager, vector2d(gameManager->getWidth() / 16, gameManager->getHeight() / 12), vector2d(150, 40), "Singleplayer", true); - MainMenuOptionsButton *optionsButton = new MainMenuOptionsButton(state, gameManager, vector2d(gameManager->getWidth() / 16, gameManager->getHeight() / 12 * 2), vector2d(150, 40), "Options", true); - ExitButton *exitButton = new ExitButton(gameManager, vector2d(gameManager->getWidth() / 16, gameManager->getHeight() / 12 * 3), vector2d(150, 40), "Exit", true); + + GameManager *gm = GameManager::getSingleton(); + SpButton *spButton = new SpButton(state, vector2d(gm->getWidth() / 16, gm->getHeight() / 12), vector2d(150, 40), "Singleplayer", true); + MainMenuOptionsButton *optionsButton = new MainMenuOptionsButton(state, vector2d(gm->getWidth() / 16, gm->getHeight() / 12 * 2), vector2d(150, 40), "Options", true); + ExitButton *exitButton = new ExitButton(vector2d(gm->getWidth() / 16, gm->getHeight() / 12 * 3), vector2d(150, 40), "Exit", true); state->addButton(optionsButton); state->addButton(spButton); state->addButton(exitButton); diff --git a/util.h b/util.h index 94681c8..07e7a4c 100755 --- a/util.h +++ b/util.h @@ -28,7 +28,7 @@ namespace game{ irr::scene::ISceneNode* castRay(irr::scene::ISceneManager*,irr::core::vector3df,irr::core::vector3df); std::vector readFile(std::string,int=0,int=-1); void writeFile(std::string,std::vector); - void makeTitlescreenButtons(core::GameManager*,core::GuiAppState*); + void makeTitlescreenButtons(core::GuiAppState*); double getAngleBetween(irr::core::vector3df, irr::core::vector3df); bool isWithinRect(irr::core::vector3df,irr::core::vector3df,irr::core::vector3df,irr::core::vector3df); bool isWithinCuboid(irr::core::vector3df,irr::core::vector3df,irr::core::vector3df,irr::core::vector3df,irr::core::vector3df); diff --git a/vessel.cpp b/vessel.cpp index 5b0524e..fb30aff 100755 --- a/vessel.cpp +++ b/vessel.cpp @@ -17,14 +17,14 @@ using namespace sf; namespace game{ namespace content{ - Vessel::Turret::Turret(GameManager *gM, Unit *vessel, int unitId, int turretId) { - gameManager = gM; + Vessel::Turret::Turret(Unit *vessel, int unitId, int turretId) { this->vessel = vessel; hullNode = vessel->getNode(); this->unitId = unitId; this->turretId = turretId; - ISceneManager *smgr = gameManager->getDevice()->getSceneManager(); - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + GameManager *gm = GameManager::getSingleton(); + ISceneManager *smgr = gm->getDevice()->getSceneManager(); + IVideoDriver *driver = gm->getDevice()->getVideoDriver(); turretMesh = smgr->getMesh(basePath[unitId] + turretNames[unitId][turretId] + ".x"); turretNode = smgr->addAnimatedMeshSceneNode(turretMesh); turretNode->setMaterialFlag(EMF_LIGHTING, false); @@ -129,7 +129,7 @@ namespace game{ void Vessel::Turret::debug(const SMaterial &mat) { float length = turretAxisLength[unitId][turretId]*0 + 1; - IVideoDriver *driver = gameManager->getDevice()->getVideoDriver(); + IVideoDriver *driver = GameManager::getSingleton()->getDevice()->getVideoDriver(); driver->setTransform(ETS_WORLD, IdentityMatrix); driver->setMaterial(mat); driver->draw3DLine(turretPosition, turretPosition + dirVec*length, SColor(255, 0, 0, 255)); @@ -167,16 +167,16 @@ namespace game{ startPos += rotQuat * (leftVec * turretMantletNodes[barId]->getPosition().X + upVec * turretMantletNodes[barId]->getPosition().Y + dirVec * turretMantletNodes[barId]->getPosition().Z); startPos += rotQuat * (leftVec * turretBarrelNodes[barId]->getPosition().X + upVec * turretBarrelNodes[barId]->getPosition().Y + dirVec * turretBarrelNodes[barId]->getPosition().Z); startPos += rotQuat * (leftVec * projectileData::pos[unitId][0][turretId].X + upVec * projectileData::pos[unitId][0][turretId].Y + dirVec * projectileData::pos[unitId][0][turretId].Z); - vessel->addProjectile(new Shell(gameManager, vessel, startPos, rotQuat*dirVec, rotQuat*leftVec, rotQuat*upVec, unitId, 0, turretId)); + vessel->addProjectile(new Shell(vessel, startPos, rotQuat*dirVec, rotQuat*leftVec, rotQuat*upVec, unitId, 0, turretId)); // fxNode->setVisible(true); if(sfx) sfx->play(); lastShotTime = getTime(); } - Vessel::Vessel(GameManager *gM, Player *player,vector3df pos, int id) : Unit(gM, player,pos, id) { + Vessel::Vessel(Player *player,vector3df pos, int id) : Unit(player,pos, id) { if (numberOfTurrets[id] > 0) for (int i = 0; i < numberOfTurrets[id]; i++) - turrets.push_back(new Turret(Unit::gameManager, this, id, i)); + turrets.push_back(new Turret(this, id, i)); } void Vessel::update() { diff --git a/vessel.h b/vessel.h index 5188c4d..a0029c9 100755 --- a/vessel.h +++ b/vessel.h @@ -9,7 +9,7 @@ namespace game{ namespace content { class Vessel : public Unit { public: - Vessel(core::GameManager*, Player*,vector3df, int); + Vessel(Player*,vector3df, int); void update(); protected: void attack(Order); @@ -17,7 +17,7 @@ namespace game{ void debug(); class Turret { public: - Turret(core::GameManager*, Unit*, int, int); + Turret(Unit*, int, int); void update(); void fire(); void rotate(double); @@ -36,7 +36,6 @@ namespace game{ irr::scene::IParticleSystemSceneNode *fxNode; irr::scene::IParticleEmitter *emitter; irr::scene::IParticleAffector *affector; - core::GameManager *gameManager; Unit *vessel; irr::scene::IAnimatedMesh *turretMesh; irr::scene::ISceneNode *turretNode, *hullNode;