diff --git a/Assets/Scripts/Gui/loadingScreen.lua b/Assets/Scripts/Gui/loadingScreen.lua new file mode 100644 index 0000000..cac1de5 --- /dev/null +++ b/Assets/Scripts/Gui/loadingScreen.lua @@ -0,0 +1,29 @@ +initPos = {x = 850, y = 100, z = 0} +Size = {x = 200, y = 20} + +gui = { + { + guiType = GuiType.TEXT, + text = 'Loading...', + name = 'loading', + pos = initPos, + scale = {x = 0.5, y = 0.5}, + font = 'batang.ttf', + fontFirstChar = 0, + fontLastChar = 256, + color = {x = 1, y = 1, z = 1, w = 1} + }, + { + guiType = GuiType.GUI_RECTANGLE, + pos = {x = initPos.x, y = initPos.y + Size.y, z = initPos.z}, + size = Size, + color = {x = 0, y = 0, z = 0, w = 1} + }, + { + guiType = GuiType.GUI_RECTANGLE, + pos = {x = initPos.x + 1, y = initPos.y + Size.y + 1, z = initPos.z + .1}, + size = {x = 30, y = Size.y - 2}, + name = '_loadingBar', + color = {x = 0, y = 0, z = 1, w = 1} + }, +} diff --git a/CMakeLists.txt b/CMakeLists.txt index cb5a78a..50058ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ set(BUTTONS statsButton.cpp activeStateButton.cpp minimapButton.cpp ${TRADING_BU set(LISTBOXES skyboxTextureListbox.cpp landTextureListbox.cpp gameObjectListbox.cpp mapListbox.cpp) set(GUI tooltip.cpp concreteGuiManager.cpp ${BUTTONS} ${LISTBOXES}) -set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp mapEditorAppState.cpp) +set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp mapEditorAppState.cpp loadingAppState.cpp) set(UTIL util.cpp binds.h) set(CONTROLLERS gameObjectFrameController.cpp cameraController.cpp) set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp addResourceCommand.cpp addTechnologyCommand.cpp toggleDebugCommand.cpp) diff --git a/concreteGuiManager.cpp b/concreteGuiManager.cpp index 6527717..d994c6a 100644 --- a/concreteGuiManager.cpp +++ b/concreteGuiManager.cpp @@ -509,6 +509,7 @@ namespace battleship{ return guiRectangle; } + //TODO distinguish between floats and vector-like tables for scale Text* ConcreteGuiManager::parseText(int guiId){ sol::state_view SOL_LUA_STATE = generateView(); sol::table guiTable = SOL_LUA_STATE["gui"][guiId + 1]; diff --git a/defConfigs.h b/defConfigs.h index b7f719e..616bcd7 100755 --- a/defConfigs.h +++ b/defConfigs.h @@ -24,9 +24,9 @@ namespace battleship{ const int maxNumGroups = 10, NUM_MAX_ZOOMS = 75; const vb01::u32 IMPASS_NODE_VAL = 65535; - const static int numAppStates = 4; - const static int numStaticBinds[numAppStates]{6, 0, 5, 19}; - const static int numConfBinds[numAppStates]{0, 1, 27, 0}; + const static int numAppStates = 5; + const static int numStaticBinds[numAppStates]{6, 0, 5, 19, 0}; + const static int numConfBinds[numAppStates]{0, 1, 27, 0, 0}; const static int maxStaticBinds = 19; const static int maxConfBinds = 23; const static int numScripts = 5; @@ -79,7 +79,8 @@ namespace battleship{ Bind::LOOK_LEFT, Bind::LOOK_RIGHT, Bind::LOOK_AROUND, - } + }, + {} }; const static Bind confBinds[numAppStates][maxConfBinds]{ {}, @@ -111,6 +112,7 @@ namespace battleship{ Bind::SELECT_STRUCTURE, Bind::DESELECT_STRUCTURE }, + {}, {} }; @@ -137,7 +139,8 @@ namespace battleship{ Mapping::MOUSE_AXIS_LEFT, Mapping::MOUSE_AXIS_RIGHT, 0 - } + }, + {} }; const static int confTriggers[numAppStates][maxConfBinds]{ {}, @@ -168,6 +171,7 @@ namespace battleship{ GLFW_KEY_B, GLFW_KEY_ESCAPE }, + {}, {} }; @@ -175,12 +179,14 @@ namespace battleship{ {0, 1, 1, 1, 1, 1}, {}, {0, 0, 0, 0, 1}, - {0, 0, 0, 0, 1} + {0, 0, 0, 0, 1}, + {} }; const static bool isConfKey[numAppStates][maxConfBinds]{ {}, {1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {}, {} }; @@ -188,12 +194,14 @@ namespace battleship{ {1, 1, 1, 1, 1, 1}, {}, {0, 0, 0, 0, 1}, - {0, 0, 0, 0, 1} + {0, 0, 0, 0, 1}, + {} }; const static bool isConfAction[numAppStates][maxConfBinds]{ {}, {1}, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {}, {} }; diff --git a/inGameAppState.cpp b/inGameAppState.cpp index f8a8a4d..e3c38f5 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -59,6 +59,8 @@ namespace battleship{ void InGameAppState::onAttached() { AbstractAppState::onAttached(); + Map::getSingleton()->loadPlayerGameObjects(); + Camera *cam = Root::getSingleton()->getCamera(); cam->setPosition(Map::getSingleton()->getSpawnPoint(playerId) + Vector3(1, 1, 1) * configData::CAMERA_DISTANCE); cam->lookAt(Vector3(0, -1, -1).norm(), Vector3(0, 1, -1).norm()); diff --git a/loadMapButton.cpp b/loadMapButton.cpp index 0a0ef9c..9d16784 100644 --- a/loadMapButton.cpp +++ b/loadMapButton.cpp @@ -2,6 +2,7 @@ #include "concreteGuiManager.h" #include "gameManager.h" #include "mapEditorAppState.h" +#include "loadingAppState.h" #include @@ -16,8 +17,7 @@ namespace battleship{ void LoadMapButton::OkButton::onClick(){ StateManager *sm = GameManager::getSingleton()->getStateManager(); string name = wstringToString(listbox->getContents()[listbox->getSelectedOption()]); - sm->attachAppState(new MapEditorAppState(name, Vector2::VEC_ZERO, false)); - ConcreteGuiManager::getSingleton()->readLuaScreenScript("mapEditor.lua"); + handleLoadingGui(new LoadingAppState(new MapEditorAppState(name, Vector2::VEC_ZERO, false), "mapEditor.lua")); } LoadMapButton::LoadMapButton(Vector3 pos, Vector2 size) : Button(pos, size, "Load map", GameManager::getSingleton()->getPath() + "Fonts/batang.ttf"){} diff --git a/loadingAppState.cpp b/loadingAppState.cpp new file mode 100644 index 0000000..84fc754 --- /dev/null +++ b/loadingAppState.cpp @@ -0,0 +1,51 @@ +#include "loadingAppState.h" +#include "concreteGuiManager.h" +#include "gameManager.h" +#include "defConfigs.h" + +#include +#include +#include +#include + +#include + +namespace battleship{ + using namespace configData; + using namespace gameBase; + using namespace vb01; + using namespace std; + + LoadingAppState::LoadingAppState(AbstractAppState *newSt, string newScr) : AbstractAppState( + AppStateType::LOADING_STATE, + configData::calcSumBinds(AppStateType::LOADING_STATE, true), + configData::calcSumBinds(AppStateType::LOADING_STATE, false), + GameManager::getSingleton()->getPath() + scripts[(int)ScriptFiles::OPTIONS] + ), + newState(newSt), + newScreen(newScr) + {} + + void LoadingAppState::onDettached(){ + GameManager::getSingleton()->getStateManager()->attachAppState(newState); + ConcreteGuiManager::getSingleton()->readLuaScreenScript(newScreen); + + AbstractAppState::onDettached(); + } + + void LoadingAppState::update(){ + Node *loadingBar = ConcreteGuiManager::getSingleton()->getGuiRectangle("_loadingBar"); + Quad *quad = (Quad*)loadingBar->getMesh(0); + quad->setSize(Vector3((1 - (float)loadableAssets.size() / initNumAssets) * 200.f, 20, 0)); + quad->updateVerts(quad->getMeshBase()); + + if(getTime() - lastUpdateTime > 5){ + AssetManager::getSingleton()->load(loadableAssets[0]); + loadableAssets.erase(loadableAssets.begin()); + + if(loadableAssets.empty()) GameManager::getSingleton()->getStateManager()->dettachAppState(this); + + lastUpdateTime = getTime(); + } + } +} diff --git a/loadingAppState.h b/loadingAppState.h new file mode 100644 index 0000000..8f0c10a --- /dev/null +++ b/loadingAppState.h @@ -0,0 +1,28 @@ +#ifndef LOADING_APP_STATE_H +#define LOADING_APP_STATE_H + +#include +#include + +namespace battleship{ + class LoadingAppState : public gameBase::AbstractAppState{ + public: + LoadingAppState(gameBase::AbstractAppState*, std::string); + ~LoadingAppState(){} + void onAttached(){} + void onDettached(); + void update(); + inline void setLoadableAssets(std::vector ls){ + this->loadableAssets = ls; + initNumAssets = ls.size(); + } + private: + int initNumAssets; + std::vector loadableAssets; + gameBase::AbstractAppState *newState = nullptr; + std::string newScreen = ""; + vb01::s64 lastUpdateTime = 0; + }; +} + +#endif diff --git a/map.cpp b/map.cpp index 6b9a3c4..8353a64 100755 --- a/map.cpp +++ b/map.cpp @@ -12,6 +12,7 @@ #include #include "map.h" +#include "util.h" #include "game.h" #include "player.h" #include "gameObject.h" @@ -394,16 +395,8 @@ namespace battleship{ //TODO move minimap loading elsewhere void Map::loadPlayerGameObjects(){ - AssetManager *assetManager = AssetManager::getSingleton(); - string path = GameManager::getSingleton()->getPath(); - assetManager->load(path + DEFAULT_TEXTURE); - - sol::state_view SOL_LUA_VIEW = generateView(); - string vfxPrefix = SOL_LUA_VIEW["vfxPrefix"], gameObjPrefix = SOL_LUA_VIEW["gameObjPrefix"]; - assetManager->load(path + vfxPrefix, true); - assetManager->load(path + gameObjPrefix, true); - int numPlayers = Game::getSingleton()->getNumPlayers(); + sol::state_view SOL_LUA_VIEW = generateView(); string playerInd = "players"; for(int i = 0; i < numPlayers; i++){ diff --git a/newMapButton.cpp b/newMapButton.cpp index ecc621a..f85fee5 100644 --- a/newMapButton.cpp +++ b/newMapButton.cpp @@ -1,6 +1,7 @@ #include "newMapButton.h" #include "gameManager.h" #include "mapEditorAppState.h" +#include "loadingAppState.h" #include "concreteGuiManager.h" #include @@ -25,10 +26,7 @@ namespace battleship{ atof(wstringToString(sizeX->getText()).c_str()), atof(wstringToString(sizeY->getText()).c_str()) ); - StateManager *stateManager = GameManager::getSingleton()->getStateManager(); - stateManager->attachAppState(new MapEditorAppState(wstringToString(name->getText()), size, true)); - - ConcreteGuiManager::getSingleton()->readLuaScreenScript("mapEditor.lua"); + handleLoadingGui(new LoadingAppState(new MapEditorAppState(wstringToString(name->getText()), size, true), "mapEditor.lua")); } NewMapButton::NewMapButton(Vector3 pos, Vector2 size) : Button(pos, size, "New map", GameManager::getSingleton()->getPath() + "Fonts/batang.ttf"){} diff --git a/playButton.cpp b/playButton.cpp index d22026c..7811ea1 100644 --- a/playButton.cpp +++ b/playButton.cpp @@ -1,6 +1,7 @@ #include "playButton.h" #include "gameManager.h" #include "inGameAppState.h" +#include "loadingAppState.h" #include "concreteGuiManager.h" #include "game.h" @@ -70,11 +71,6 @@ namespace battleship{ game->addPlayer(new Player(difficulty, faction, team, color, cpuPlayer, i, name)); } - map->loadPlayerGameObjects(); - - guiManager->readLuaScreenScript("inGame.lua"); - - StateManager *stateManager = GameManager::getSingleton()->getStateManager(); - stateManager->attachAppState(new InGameAppState()); + handleLoadingGui(new LoadingAppState(new InGameAppState(), "inGame.lua")); } } diff --git a/util.cpp b/util.cpp index 0dd5080..9d94310 100755 --- a/util.cpp +++ b/util.cpp @@ -1,12 +1,16 @@ #include +#include +#include #include +#include #include #include #include #include #include +#include #include #include @@ -16,11 +20,15 @@ #include "util.h" #include "defConfigs.h" #include "gameManager.h" +#include "concreteGuiManager.h" #include "guiAppState.h" #include "inGameAppState.h" +#include "loadingAppState.h" #include "mapEditorButton.h" using namespace std; +using namespace std::this_thread; +using namespace std::chrono; using namespace glm; using namespace vb01; using namespace vb01Gui; @@ -28,6 +36,26 @@ using namespace vb01Gui; namespace battleship{ using namespace configData; + void handleLoadingGui(LoadingAppState *loadState){ + ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); + guiManager->readLuaScreenScript("loadingScreen.lua"); + + sol::state_view SOL_LUA_VIEW = generateView(); + string vfxPrefix = SOL_LUA_VIEW["vfxPrefix"], gameObjPrefix = SOL_LUA_VIEW["gameObjPrefix"]; + + AssetManager *assetManager = AssetManager::getSingleton(); + string path = GameManager::getSingleton()->getPath(); + vector assets = vector{path + DEFAULT_TEXTURE}; + + assetManager->readDir(path + vfxPrefix, assets, true); + assetManager->readDir(path + gameObjPrefix, assets, true); + + loadState->setLoadableAssets(assets); + + StateManager *stateManager = GameManager::getSingleton()->getStateManager(); + stateManager->attachAppState(loadState); + } + void makeTitlescreenButtons(GuiAppState *state) { /* class SpButton : public Button { diff --git a/util.h b/util.h index 57ffef9..09ddf25 100755 --- a/util.h +++ b/util.h @@ -20,10 +20,12 @@ namespace battleship{ typedef int s32; typedef long long s64; - enum AppStateType{GUI_STATE, IN_GAME_STATE, ACTIVE_STATE, MAP_EDITOR}; + enum AppStateType{GUI_STATE, IN_GAME_STATE, ACTIVE_STATE, MAP_EDITOR, LOADING_STATE}; class GuiAppState; + class LoadingAppState; + void handleLoadingGui(LoadingAppState*); void makeTitlescreenButtons(GuiAppState*); std::vector readDir(std::string, bool); vb01::Vector2 spaceToScreen(vb01::Vector3);