From 19f16da9ed0d1e49a0efab258e72594acdd85eaa Mon Sep 17 00:00:00 2001 From: devZoGok Date: Thu, 29 Dec 2022 11:42:19 +0200 Subject: [PATCH] generated landmass plane --- CMakeLists.txt | 2 +- defConfigs.h | 30 +++++++++----- map.cpp | 32 ++++++++------- map.h | 3 +- mapEditorAppState.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++ mapEditorAppState.h | 38 +++++++++++++++++ util.cpp | 21 ++++++++-- util.h | 2 +- 8 files changed, 192 insertions(+), 32 deletions(-) create mode 100644 mapEditorAppState.cpp create mode 100644 mapEditorAppState.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8811fa0..f0c283d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ set(CMAKE_BUILD_TYPE Debug) set(BUILD_TESTS ON) cmake_policy(SET CMP0015 NEW) -set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp) +set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp mapEditorAppState.cpp) set(UTIL util.cpp binds.h) set(GUI tooltip.cpp optionsButton.cpp exitButton.cpp consoleCommand.cpp) set(CORE gameManager.cpp defConfigs.cpp) diff --git a/defConfigs.h b/defConfigs.h index dcc5a67..88b0913 100755 --- a/defConfigs.h +++ b/defConfigs.h @@ -19,9 +19,9 @@ namespace battleship{ const double camPanSpeed = .1; const int maxNumGroups = 10; - const static int numAppStates = 3; - const static int numStaticBinds[numAppStates]{6, 0, 5}; - const static int numConfBinds[numAppStates]{0, 1, 22}; + const static int numAppStates = 4; + const static int numStaticBinds[numAppStates]{6, 0, 5, 0}; + const static int numConfBinds[numAppStates]{0, 1, 22, 0}; const static int maxStaticBinds = 6; const static int maxConfBinds = 22; const static int numScripts = 5; @@ -51,7 +51,8 @@ namespace battleship{ Bind::LOOK_LEFT, Bind::LOOK_RIGHT, Bind::LOOK_AROUND, - } + }, + {} }; const static Bind confBinds[numAppStates][maxConfBinds]{ {}, @@ -81,7 +82,8 @@ namespace battleship{ Bind::GROUP_9, Bind::SELECT_STRUCTURE, Bind::DESELECT_STRUCTURE - } + }, + {} }; const static int staticTriggers[numAppStates][maxStaticBinds]{ @@ -100,7 +102,8 @@ namespace battleship{ Mapping::MOUSE_AXIS_LEFT, Mapping::MOUSE_AXIS_RIGHT, 0 - } + }, + {} }; const static int confTriggers[numAppStates][maxConfBinds]{ {}, @@ -130,29 +133,34 @@ namespace battleship{ GLFW_KEY_9, GLFW_KEY_B, GLFW_KEY_ESCAPE - } + }, + {} }; const static bool isStaticKey[numAppStates][maxStaticBinds]{ {0, 1, 1, 1, 1, 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} + {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {} }; const static bool isStaticAction[numAppStates][maxStaticBinds]{ {1, 1, 1, 1, 1, 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} + {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + {} }; int calcSumStaticBinds(int, bool); diff --git a/map.cpp b/map.cpp index 6588c6d..70f601f 100755 --- a/map.cpp +++ b/map.cpp @@ -181,22 +181,24 @@ namespace battleship{ cam->lookAt(Vector3(-1, -1, -1).norm(), Vector3(-1, 1, -1).norm()); } - void Map::load(string mapName) { - this->mapName = mapName; - cellSize = Vector3(7, 6, 7); - Pathfinder::getSingleton()->setImpassibleNodeVal(u16(0 - 1)); + void Map::load(string mapName, bool empty) { + this->mapName = mapName; + cellSize = Vector3(7, 6, 7); + Pathfinder::getSingleton()->setImpassibleNodeVal(u16(0 - 1)); + + preprareScene(); - preprareScene(); - - LuaManager *lm = LuaManager::getSingleton(); - lm->buildScript(vector{GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/" + mapName + ".lua"}); - int numWaterbodies = lm->getIntFromTable(mapTable, vector{Index("numWaterBodies")}); - - loadSkybox(lm); - loadTerrainObject(lm, -1); - - for(int i = 0; i < numWaterbodies; i++) - loadTerrainObject(lm, i); + if(!empty){ + LuaManager *lm = LuaManager::getSingleton(); + lm->buildScript(vector{GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/" + mapName + ".lua"}); + int numWaterbodies = lm->getIntFromTable(mapTable, vector{Index("numWaterBodies")}); + + loadSkybox(lm); + loadTerrainObject(lm, -1); + + for(int i = 0; i < numWaterbodies; i++) + loadTerrainObject(lm, i); + } } void Map::unload() {} diff --git a/map.h b/map.h index be6bf6a..92be68a 100755 --- a/map.h +++ b/map.h @@ -51,10 +51,11 @@ namespace battleship{ static Map* getSingleton(); ~Map(){} void update(){} - void load(std::string); + void load(std::string, bool = false); void unload(); int getCellId(vb01::Vector3, int); bool isPointWithinTerrainObject(vb01::Vector3, int); + inline void addTerrainObject(TerrainObject obj){terrainObjects.push_back(obj);} inline TerrainObject getTerrainObject(int i){return terrainObjects[i];} inline int getNumTerrainObjects(){return terrainObjects.size();} inline vb01::Node* getNodeParent(){return nodeParent;} diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp new file mode 100644 index 0000000..611702a --- /dev/null +++ b/mapEditorAppState.cpp @@ -0,0 +1,96 @@ +#include "mapEditorAppState.h" +#include "gameManager.h" +#include "defConfigs.h" +#include "map.h" +#include "mesh.h" +#include "util.h" + +#include + +namespace battleship{ + using namespace configData; + using namespace vb01; + using namespace gameBase; + using namespace std; + + MapEditorAppState::MapEditor::MapEditor(string name, Vector2 size){ + map = Map::getSingleton(); + map->load(name, true); + + generatePlane(size); + } + + void MapEditorAppState::MapEditor::generatePlane(Vector2 size){ + int numSideVerts = 100, numIndices = 6 * (numSideVerts - 1) * (numSideVerts - 1); + MeshData::Vertex *vertices = new MeshData::Vertex[numSideVerts * numSideVerts]; + u32 *indices = new u32[numIndices]; + + for(int i = 0; i < numSideVerts; i++){ + for(int j = 0; j < numSideVerts; j++){ + MeshData::Vertex vert; + + float x = size.x * (2 * j - numSideVerts) / (2 * numSideVerts); + float z = size.y * (numSideVerts - 2 * i) / (2 * numSideVerts); + + vert.pos = Vector3(x, 0, z); + vert.uv = Vector2((float)(j / numSideVerts), (float)(i / numSideVerts)); + vert.tan = Vector3::VEC_I; + vert.biTan = Vector3::VEC_K; + vert.norm = vert.tan.cross(vert.biTan); + + vertices[i * numSideVerts + j] = vert; + } + } + + for(int i = 0; i < numSideVerts - 1; i++){ + for(int j = 0; j < numSideVerts - 1; j++){ + int id = (i * (numSideVerts - 1) + j) * 6; + indices[id] = i * numSideVerts + j; + indices[id + 1] = i * numSideVerts + j + 1; + indices[id + 2] = (i + 1) * numSideVerts + j + 1; + + indices[id + 3] = (i + 1) * numSideVerts + j + 1; + indices[id + 4] = (i + 1) * numSideVerts + j; + indices[id + 5] = i * numSideVerts + j; + } + } + + Root *root = Root::getSingleton(); + Material *mat = new Material(root->getLibPath() + "texture"); + mat->addBoolUniform("texturingEnabled", false); + mat->addBoolUniform("lightingEnabled", false); + mat->addVec4Uniform("diffuseColor", Vector4(1, 0, 0, 1)); + + MeshData meshData(vertices, indices, numIndices / 6); + Mesh *mesh = new Mesh(meshData); + mesh->construct(); + mesh->setMaterial(mat); + + Node *node = new Node(); + node->attachMesh(mesh); + root->getRootNode()->attachChild(node); + + map->addTerrainObject(TerrainObject(Vector3(0, 0, 0), Vector3(size.x, 0, size.y), Vector3(14, 6, 14), TerrainObject::LANDMASS, node, 0, new Cell, new vb01::u32*)); + } + + MapEditorAppState::MapEditorAppState(string name, Vector2 size) : AbstractAppState( + AppStateType::MAP_EDITOR, + configData::calcSumBinds(AppStateType::MAP_EDITOR, true), + configData::calcSumBinds(AppStateType::MAP_EDITOR, false), + GameManager::getSingleton()->getPath() + "Scripts/options.lua"){ + mapName = name; + mapSize = size; + } + + void MapEditorAppState::update(){} + + void MapEditorAppState::onAttached(){ + mapEditor = new MapEditor(mapName, mapSize); + } + + void MapEditorAppState::onDettached(){} + + void MapEditorAppState::onAction(int bind, bool isPressed){} + + void MapEditorAppState::onAnalog(int bind, float str){} +} diff --git a/mapEditorAppState.h b/mapEditorAppState.h new file mode 100644 index 0000000..ca86ecb --- /dev/null +++ b/mapEditorAppState.h @@ -0,0 +1,38 @@ +#ifndef MAP_EDITOR_APP_STATE_H +#define MAP_EDITOR_APP_STATE_H + +#include + +#include + +#include + +namespace battleship{ + class MapEditor; + class Map; + + class MapEditorAppState : public gameBase::AbstractAppState{ + public: + MapEditorAppState(std::string, vb01::Vector2); + void update(); + void onAttached(); + void onDettached(); + void onAction(int, bool); + void onAnalog(int, float); + private: + class MapEditor{ + public: + MapEditor(std::string, vb01::Vector2); + private: + void generatePlane(vb01::Vector2); + + Map *map; + }; + + MapEditor *mapEditor = nullptr; + std::string mapName; + vb01::Vector2 mapSize; + }; +} + +#endif diff --git a/util.cpp b/util.cpp index a431b1d..65b84c8 100755 --- a/util.cpp +++ b/util.cpp @@ -18,6 +18,7 @@ #include "gameManager.h" #include "guiAppState.h" #include "inGameAppState.h" +#include "mapEditorAppState.h" using namespace std; using namespace glm; @@ -222,14 +223,28 @@ namespace battleship{ void onClick(){ class OkButton : public Button{ public: - OkButton(Vector2 pos, Vector2 size, Textbox *sx, Textbox *sy) : Button(pos, size, "Ok", GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", -1, true){ + OkButton(Vector2 pos, Vector2 size, Textbox *name, Textbox *sx, Textbox *sy) : Button(pos, size, "Ok", GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", -1, true){ + this->name = name; this->sizeX = sx; this->sizeY = sy; } void onClick(){ + GameManager *gm = GameManager::getSingleton(); + StateManager *stateManager = gm->getStateManager(); + + stateManager->attachAppState(new MapEditorAppState( + wstringToString(name->getText()), + Vector2( + atof(wstringToString(sizeX->getText()).c_str()), + atof(wstringToString(sizeY->getText()).c_str())) + ) + ); + + GuiAppState *state = (GuiAppState*)stateManager->getAppStateByType((int)AppStateType::GUI_STATE); + state->removeAllButtons(); } private: - Textbox *sizeX, *sizeY; + Textbox *name, *sizeX, *sizeY; }; GameManager *gm = GameManager::getSingleton(); @@ -247,7 +262,7 @@ namespace battleship{ state->addTextbox(sizeX); state->addTextbox(sizeY); - state->addButton(new OkButton(Vector2(200, gm->getHeight() - 150), Vector2(140, 50), sizeX, sizeY)); + state->addButton(new OkButton(Vector2(200, gm->getHeight() - 150), Vector2(140, 50), name, sizeX, sizeY)); state->removeButton(this); } diff --git a/util.h b/util.h index 4ec0181..5a1054d 100755 --- a/util.h +++ b/util.h @@ -20,7 +20,7 @@ namespace battleship{ typedef int s32; typedef long long s64; - enum AppStateType{GUI_STATE, IN_GAME_STATE, ACTIVE_STATE}; + enum AppStateType{GUI_STATE, IN_GAME_STATE, ACTIVE_STATE, MAP_EDITOR}; class GuiAppState;