|
|
@@ -3,13 +3,19 @@
|
|
|
#include "Entity.h"
|
|
|
#include "json.h"
|
|
|
#include "Model.h"
|
|
|
+#include "CrystalPoint.h"
|
|
|
#include <fstream>
|
|
|
#include <iostream>
|
|
|
|
|
|
World::World(const std::string &fileName)
|
|
|
{
|
|
|
+ //Store player instance
|
|
|
player = Player::getInstance();
|
|
|
|
|
|
+ //Create the interface
|
|
|
+ interface = new Interface();
|
|
|
+
|
|
|
+ //Open world json file
|
|
|
std::ifstream file(fileName);
|
|
|
if(!file.is_open())
|
|
|
std::cout<<"Error, can't open world file - " << fileName << "\n";
|
|
|
@@ -30,60 +36,81 @@ World::World(const std::string &fileName)
|
|
|
//Load object templates
|
|
|
for (auto objt : v["world"]["object-templates"])
|
|
|
{
|
|
|
- objecttemplates.push_back(std::pair<int, std::string>(objt["color"], objt["file"]));
|
|
|
+ //collision
|
|
|
+ bool cancollide = true;
|
|
|
+ if (!objt["collision"].isNull())
|
|
|
+ cancollide = objt["collision"].asBool();
|
|
|
+
|
|
|
+ objecttemplates.push_back(std::pair<int, std::pair<std::string, bool>>(objt["color"], std::pair<std::string, bool>(objt["file"], cancollide)));
|
|
|
}
|
|
|
|
|
|
+ //Generate heightmap for this world
|
|
|
heightmap = new HeightMap(v["world"]["heightmap"].asString(), this);
|
|
|
|
|
|
+ //Map different texture to heightmap if available
|
|
|
if(!v["world"]["texture"].isNull())
|
|
|
heightmap->SetTexture(v["world"]["texture"].asString());
|
|
|
|
|
|
+ //Set player starting position
|
|
|
player->position.x = v["player"]["startposition"][0].asFloat();
|
|
|
player->position.y = v["player"]["startposition"][1].asFloat();
|
|
|
player->position.z = v["player"]["startposition"][2].asFloat();
|
|
|
|
|
|
-
|
|
|
+ //Load and place objects into world
|
|
|
for (auto object : v["objects"])
|
|
|
{
|
|
|
+ //Collision
|
|
|
bool hasCollision = true;
|
|
|
if (!object["collide"].isNull())
|
|
|
hasCollision = object["collide"].asBool();
|
|
|
|
|
|
+ //Rotation
|
|
|
Vec3f rotation(0, 0, 0);
|
|
|
if(!object["rot"].isNull())
|
|
|
rotation = Vec3f(object["rot"][0].asFloat(), object["rot"][1].asFloat(), object["rot"][2].asFloat());
|
|
|
|
|
|
+ //Scale
|
|
|
float scale = 1;
|
|
|
if (!object["scale"].isNull())
|
|
|
scale = object["scale"].asFloat();
|
|
|
|
|
|
+ //Position
|
|
|
if (object["pos"].isNull())
|
|
|
std::cout << "Invalid world file: objects pos - " << fileName << "\n";
|
|
|
+
|
|
|
+ //File
|
|
|
+ if (object["file"].isNull())
|
|
|
+ std::cout << "Invalid world file: objects file - " << fileName << "\n";
|
|
|
|
|
|
+ //Create
|
|
|
Vec3f position(object["pos"][0].asFloat(), object["pos"][1].asFloat(), object["pos"][2].asFloat());
|
|
|
- entities.push_back(new LevelObject(object["file"], position, rotation, scale, hasCollision));
|
|
|
+ entities.push_back(new LevelObject(object["file"].asString(), position, rotation, scale, hasCollision));
|
|
|
}
|
|
|
|
|
|
- //Enemies
|
|
|
+ //Load and place enemies into world
|
|
|
for (auto e : v["enemies"])
|
|
|
{
|
|
|
- std::string fileName = "";
|
|
|
- if (!e["file"].isNull())
|
|
|
- fileName = e["file"].asString();
|
|
|
-
|
|
|
- Vec3f position(0, 0, 0);
|
|
|
- if (!e["pos"].isNull())
|
|
|
- position = Vec3f(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
|
|
|
-
|
|
|
+ //Rotation
|
|
|
Vec3f rotation(0, 0, 0);
|
|
|
if (!e["rot"].isNull())
|
|
|
rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
|
|
|
|
|
|
+ //Scale
|
|
|
float scale = 1.0f;
|
|
|
if (!e["scale"].isNull())
|
|
|
scale = e["scale"].asFloat();
|
|
|
|
|
|
- enemies.push_back(new Enemy(fileName, position, rotation, scale));
|
|
|
+ //Position
|
|
|
+ if (e["pos"].isNull())
|
|
|
+ std::cout << "Invalid world file: enemies pos - " << fileName << "\n";
|
|
|
+
|
|
|
+ //File
|
|
|
+ if (e["file"].isNull())
|
|
|
+ std::cout << "Invalid world file: enemies file - " << fileName << "\n";
|
|
|
+
|
|
|
+ //Create
|
|
|
+ Vec3f position(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
|
|
|
+ enemies.push_back(new Enemy(e["file"].asString(), position, rotation, scale));
|
|
|
|
|
|
}
|
|
|
}
|
|
|
@@ -94,7 +121,7 @@ World::~World()
|
|
|
delete heightmap;
|
|
|
}
|
|
|
|
|
|
-std::string World::getObjectFromValue(int val)
|
|
|
+std::pair<std::string, bool> World::getObjectFromValue(int val)
|
|
|
{
|
|
|
for (auto i : objecttemplates)
|
|
|
{
|
|
|
@@ -116,10 +143,9 @@ void World::draw()
|
|
|
|
|
|
float lightPosition[4] = { 0, 2, 1, 0 };
|
|
|
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
|
|
|
- float lightAmbient[4] = { 0.5, 0.5, 0.5, 1 };
|
|
|
+ float lightAmbient[4] = { 0.2, 0.2, 0.2, 1 };
|
|
|
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
|
|
|
|
|
|
- glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
heightmap->Draw();
|
|
|
|
|
|
for (auto &enemy : enemies)
|
|
|
@@ -127,6 +153,8 @@ void World::draw()
|
|
|
|
|
|
for (auto &entity : entities)
|
|
|
entity->draw();
|
|
|
+
|
|
|
+ interface->draw();
|
|
|
}
|
|
|
|
|
|
void World::update(float elapsedTime)
|