From 351b93e01dad031158cfa920f3ebfc1e911bc135 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Mon, 25 Jul 2022 09:37:40 +0300 Subject: [PATCH] generating map cells --- CMakeLists.txt | 2 +- map.cpp | 29 ++++++++++++++++++++++++++++- map.h | 12 +++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 268e22d..8668298 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.9) project(battleship) set(CMAKE_BUILD_TYPE Debug) -set(BUILD_TESTS OFF) +set(BUILD_TESTS ON) cmake_policy(SET CMP0015 NEW) set(states activeGameState.cpp inGameAppState.cpp guiAppState.cpp) diff --git a/map.cpp b/map.cpp index af6dbcc..7a70987 100755 --- a/map.cpp +++ b/map.cpp @@ -69,7 +69,7 @@ namespace battleship{ } void Map::loadWaterbodies(LuaManager *luaManager){ - //TODO : replace the magic value when it's possible to extract fields from nested tables + //TODO : replace the magic value int numWaterBodies = 1; string table = "waterBodies"; Root *root = Root::getSingleton(); @@ -108,10 +108,37 @@ namespace battleship{ loadTerrain(luaManager); loadWaterbodies(luaManager); + generateCells(); + Camera *cam = Root::getSingleton()->getCamera(); cam->setPosition(Vector3(1, 1, 1) * 10); cam->lookAt(Vector3(-1, -1, -1).norm(), Vector3(-1, 1, -1).norm()); } + void Map::generateCells(){ + //TODO replace magic values by retrieving terrain dimensions + Vector3 terrainSize = Vector3(20, 6, 20); + Vector3 initPos = terrainSize * (-0.5); + int numCellsByDim[]{terrainSize.x / cellSize, terrainSize.y / cellSize, terrainSize.z / cellSize}; + cells = new GraphNode**[numCellsByDim[0]]; + + for(int i = 0; i < numCellsByDim[0]; i++){ + cells[i] = new GraphNode*[numCellsByDim[1]]; + + for(int j = 0; j < numCellsByDim[1]; j++){ + cells[i][j] = new GraphNode[numCellsByDim[2]]; + + for(int k = 0; k < numCellsByDim[2]; k++){ + GraphNode::Type type = GraphNode::SEA; + + GraphNode node; + node.type = type; + node.pos = initPos + Vector3(cellSize * i, cellSize * j, cellSize * k); + cells[i][j][k] = node; + } + } + } + } + void Map::unload() {} } diff --git a/map.h b/map.h index a24a7ff..930c3d7 100755 --- a/map.h +++ b/map.h @@ -11,6 +11,13 @@ namespace gameBase{ } namespace battleship{ + struct GraphNode{ + enum Type{LAND, SEA, AIR}; + + Type type; + vb01::Vector3 pos; + }; + class Map { public: Map(std::string); @@ -21,11 +28,14 @@ namespace battleship{ inline vb01::Node* getWaterNode(){return waterNode;} private: vb01::Node *waterNode = nullptr; - std::string mapName; + std::string mapName; + float cellSize = 1; + GraphNode*** cells = nullptr; void loadSkybox(gameBase::LuaManager*); void loadTerrain(gameBase::LuaManager*); void loadWaterbodies(gameBase::LuaManager*); + void generateCells(); }; }