implemented unit factory

improved map loading
This commit is contained in:
devZoGok
2023-01-13 19:52:00 +02:00
parent f91592712f
commit 6a96bafdb7
26 changed files with 197 additions and 53 deletions
+87 -18
View File
@@ -8,6 +8,7 @@
#include "map.h"
#include "player.h"
#include "unitFactory.h"
#include "pathfinder.h"
#include "gameManager.h"
#include "defConfigs.h"
@@ -33,16 +34,20 @@ namespace battleship{
p->update();
}
void Map::loadSkybox(LuaManager *luaManager){
int numPaths = 6;
string skyTable = "skybox", basePath = GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/";
string path[] = {
basePath + luaManager->getStringFromTable(mapTable, vector<Index>{Index(skyTable), Index("left")}),
basePath + luaManager->getStringFromTable(mapTable, vector<Index>{Index(skyTable), Index("right")}),
basePath + luaManager->getStringFromTable(mapTable, vector<Index>{Index(skyTable), Index("up")}),
basePath + luaManager->getStringFromTable(mapTable, vector<Index>{Index(skyTable), Index("down")}),
basePath + luaManager->getStringFromTable(mapTable, vector<Index>{Index(skyTable), Index("front")}),
basePath + luaManager->getStringFromTable(mapTable, vector<Index>{Index(skyTable), Index("back")})
void Map::loadSkybox(){
LuaManager *lm = LuaManager::getSingleton();
string skyboxName = lm->getStringFromTable(mapTable, vector<Index>{Index("skybox")});
string basePath = GameManager::getSingleton()->getPath() + "Textures/Skyboxes/" + skyboxName;
const int numPaths = 6;
string path[numPaths] = {
basePath + "/left.jpg",
basePath + "/right.jpg",
basePath + "/up.jpg",
basePath + "/down.jpg",
basePath + "/front.jpg",
basePath + "/back.jpg"
};
for(int i = 0; i < numPaths; i++)
@@ -51,7 +56,8 @@ namespace battleship{
Root::getSingleton()->createSkybox(path);
}
void Map::loadTerrainObject(LuaManager *luaManager, int id){
void Map::loadTerrainObject(int id){
LuaManager *luaManager = LuaManager::getSingleton();
string terrainTable = (id == -1 ? "terrain" : "waterBodies");
vector<Index> baseIndices = (id == -1 ? vector<Index>{Index(terrainTable)} : vector<Index>{Index(terrainTable), Index(id + 1)});
Vector3 pos = Vector3::VEC_ZERO, size = Vector3::VEC_ZERO;
@@ -137,14 +143,23 @@ namespace battleship{
Node *node = nullptr;
Quad *quad = nullptr;
TerrainObject::Type type;
string basePath = GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/";
string basePath = GameManager::getSingleton()->getPath();
string texPath = basePath;
//TODO remove this quick fix for landmass model loading
if(id == -1){
type = TerrainObject::LANDMASS;
string terrainFile = basePath + luaManager->getStringFromTable(mapTable, vector<Index>{terrInd, Index("model")});
string mapPathSuffix = "Models/Maps/" + mapName + "/";
texPath += mapPathSuffix;
string terrainFile = basePath + mapPathSuffix + luaManager->getStringFromTable(mapTable, vector<Index>{terrInd, Index("model")});
AssetManager::getSingleton()->load(terrainFile);
node = new Model(terrainFile);
node = (Model*)((new Model(terrainFile))->getChild(0));
Node *par = node->getParent();
par->dettachChild(node);
delete par;
}
else{
type = TerrainObject::RECT_WATERBODY;
@@ -152,6 +167,8 @@ namespace battleship{
quad = new Quad(Vector3(size.x, size.z, 1), true);
node->attachMesh(quad);
node->setOrientation(Quaternion(-1.57, Vector3::VEC_I));
texPath += "Textures/Water/";
}
nodeParent->attachChild(node);
@@ -163,7 +180,9 @@ namespace battleship{
ind = baseIndices;
ind.push_back(Index("albedoMap"));
string fr[]{basePath + luaManager->getStringFromTable(mapTable, ind)};
texPath += luaManager->getStringFromTable(mapTable, ind);
string fr[]{texPath};
AssetManager::getSingleton()->load(fr[0]);
Texture *t = new Texture(fr, 1, false);
@@ -177,6 +196,54 @@ namespace battleship{
terrainObjects.push_back(TerrainObject(pos, size, Vector3(cellSize.x, (id == -1 ? 0 : cellSize.y), cellSize.z), type, node, numCells, cells, weights));
}
void Map::loadSpawnPoints(){
LuaManager *lm = LuaManager::getSingleton();
int numSpawnPoints = lm->getIntFromTable(mapTable, vector<Index>{Index("numSpawnPoints")});
string spawnPointInd = "spawnPoints";
for(int i = 0; i < numSpawnPoints; i++){
float x = lm->getFloatFromTable(mapTable, vector<Index>{Index(spawnPointInd), Index(i + 1), Index("x")});
float y = lm->getFloatFromTable(mapTable, vector<Index>{Index(spawnPointInd), Index(i + 1), Index("y")});
float z = lm->getFloatFromTable(mapTable, vector<Index>{Index(spawnPointInd), Index(i + 1), Index("z")});
spawnPoints.push_back(Vector3(x, y, z));
}
}
//TODO simplify the dubplicatory field extraction statements
void Map::loadPlayers(){
AssetManager *assetManager = AssetManager::getSingleton();
assetManager->load(DEFAULT_TEXTURE);
LuaManager *lm = LuaManager::getSingleton();
assetManager->load(GameManager::getSingleton()->getPath() + lm->getString("modelPrefix"), true);
int numPlayers = lm->getIntFromTable(mapTable, vector<Index>{Index("numPlayers")});
string playerInd = "players", unitInd = "units", spawnPointInd = "spawnPointId", posInd = "pos", rotInd = "rot";
for(int i = 0; i < numPlayers; i++){
int spawnPointId = lm->getIntFromTable(mapTable, vector<Index>{Index(playerInd), Index(i + 1), Index(spawnPointId)});
int numUnits = lm->getIntFromTable(mapTable, vector<Index>{Index(playerInd), Index(i + 1), Index("numUnits")});
players.push_back(new Player(0, 0, 0, spawnPoints[i]));
for(int j = 0; j < numUnits; j++){
float posX = lm->getFloatFromTable(mapTable, vector<Index>{Index(playerInd), Index(i + 1), Index(unitInd), Index(j + 1), Index(posInd), Index("x")});
float posY = lm->getFloatFromTable(mapTable, vector<Index>{Index(playerInd), Index(i + 1), Index(unitInd), Index(j + 1), Index(posInd), Index("y")});
float posZ = lm->getFloatFromTable(mapTable, vector<Index>{Index(playerInd), Index(i + 1), Index(unitInd), Index(j + 1), Index(posInd), Index("z")});
Vector3 pos = Vector3(posX, posY, posZ);
float rotW = lm->getFloatFromTable(mapTable, vector<Index>{Index(playerInd), Index(i + 1), Index(unitInd), Index(j + 1), Index(rotInd), Index("w")});
float rotX = lm->getFloatFromTable(mapTable, vector<Index>{Index(playerInd), Index(i + 1), Index(unitInd), Index(j + 1), Index(rotInd), Index("x")});
float rotY = lm->getFloatFromTable(mapTable, vector<Index>{Index(playerInd), Index(i + 1), Index(unitInd), Index(j + 1), Index(rotInd), Index("y")});
float rotZ = lm->getFloatFromTable(mapTable, vector<Index>{Index(playerInd), Index(i + 1), Index(unitInd), Index(j + 1), Index(rotInd), Index("z")});
Quaternion rot = Quaternion(rotW, rotX, rotY, rotZ);
int id = lm->getIntFromTable(mapTable, vector<Index>{Index(playerInd), Index(i + 1), Index(unitInd), Index("id")});
players[i]->addUnit(UnitFactory::createUnit(players[i], id, pos, rot));
}
}
}
void Map::preprareScene(){
nodeParent = new Node();
Root *root = Root::getSingleton();
@@ -199,11 +266,13 @@ namespace battleship{
lm->buildScript(vector<string>{GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/" + mapName + ".lua"});
int numWaterbodies = lm->getIntFromTable(mapTable, vector<Index>{Index("numWaterBodies")});
loadSkybox(lm);
loadTerrainObject(lm, -1);
loadSpawnPoints();
loadPlayers();
loadSkybox();
loadTerrainObject(-1);
for(int i = 0; i < numWaterbodies; i++)
loadTerrainObject(lm, i);
loadTerrainObject(i);
}
}