|
@@ -1,106 +1,151 @@
|
|
|
#include "World.h"
|
|
#include "World.h"
|
|
|
#include <GL/freeglut.h>
|
|
#include <GL/freeglut.h>
|
|
|
#include "Entity.h"
|
|
#include "Entity.h"
|
|
|
-#include "LevelObject.h"
|
|
|
|
|
#include "json.h"
|
|
#include "json.h"
|
|
|
|
|
+#include "Model.h"
|
|
|
|
|
+#include "CrystalPoint.h"
|
|
|
#include <fstream>
|
|
#include <fstream>
|
|
|
#include <iostream>
|
|
#include <iostream>
|
|
|
|
|
|
|
|
-World::World() : player(Player::getInstance())
|
|
|
|
|
|
|
+World::World(const std::string &fileName)
|
|
|
{
|
|
{
|
|
|
|
|
+ //Store player instance
|
|
|
|
|
+ player = Player::getInstance();
|
|
|
|
|
|
|
|
- std::ifstream file("worlds/world1.json");
|
|
|
|
|
|
|
+ //Create the interface
|
|
|
|
|
+ interface = new Interface();
|
|
|
|
|
+
|
|
|
|
|
+ //Open world json file
|
|
|
|
|
+ std::ifstream file(fileName);
|
|
|
if(!file.is_open())
|
|
if(!file.is_open())
|
|
|
- std::cout<<"Uhoh, can't open file\n";
|
|
|
|
|
|
|
+ std::cout<<"Error, can't open world file - " << fileName << "\n";
|
|
|
|
|
|
|
|
json::Value v = json::readJson(file);
|
|
json::Value v = json::readJson(file);
|
|
|
file.close();
|
|
file.close();
|
|
|
|
|
|
|
|
- heightmap = new HeightMap(v["world"]["heightmap"].asString());
|
|
|
|
|
- heightmap->SetTexture(v["world"]["texture"].asString());
|
|
|
|
|
|
|
+ //Check file
|
|
|
|
|
+ if(v["world"].isNull() || v["world"]["heightmap"].isNull())
|
|
|
|
|
+ std::cout << "Invalid world file: world - " << fileName << "\n";
|
|
|
|
|
+ if (v["player"].isNull() || v["player"]["startposition"].isNull())
|
|
|
|
|
+ std::cout << "Invalid world file: player - " << fileName << "\n";
|
|
|
|
|
+ if (v["objects"].isNull())
|
|
|
|
|
+ std::cout << "Invalid world file: objects - " << fileName << "\n";
|
|
|
|
|
+ if (v["world"]["object-templates"].isNull())
|
|
|
|
|
+ std::cout << "Invalid world file: object templates - " << fileName << "\n";
|
|
|
|
|
+
|
|
|
|
|
+ //Load object templates
|
|
|
|
|
+ for (auto objt : v["world"]["object-templates"])
|
|
|
|
|
+ {
|
|
|
|
|
+ //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);
|
|
|
|
|
|
|
|
- player.position.x = v["player"]["startposition"][0];
|
|
|
|
|
- player.position.y = v["player"]["startposition"][1];
|
|
|
|
|
- player.position.z = v["player"]["startposition"][2];
|
|
|
|
|
|
|
+ //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"])
|
|
for (auto object : v["objects"])
|
|
|
{
|
|
{
|
|
|
|
|
+ //Collision
|
|
|
bool hasCollision = true;
|
|
bool hasCollision = true;
|
|
|
if (!object["collide"].isNull())
|
|
if (!object["collide"].isNull())
|
|
|
hasCollision = object["collide"].asBool();
|
|
hasCollision = object["collide"].asBool();
|
|
|
|
|
|
|
|
|
|
+ //Rotation
|
|
|
Vec3f rotation(0, 0, 0);
|
|
Vec3f rotation(0, 0, 0);
|
|
|
if(!object["rot"].isNull())
|
|
if(!object["rot"].isNull())
|
|
|
- rotation = Vec3f(object["rot"][0], object["rot"][1], object["rot"][2]);
|
|
|
|
|
|
|
+ rotation = Vec3f(object["rot"][0].asFloat(), object["rot"][1].asFloat(), object["rot"][2].asFloat());
|
|
|
|
|
|
|
|
|
|
+ //Scale
|
|
|
float scale = 1;
|
|
float scale = 1;
|
|
|
if (!object["scale"].isNull())
|
|
if (!object["scale"].isNull())
|
|
|
scale = object["scale"].asFloat();
|
|
scale = object["scale"].asFloat();
|
|
|
|
|
|
|
|
- Vec3f position(object["pos"][0], object["pos"][1], object["pos"][2]);
|
|
|
|
|
- entities.push_back(new LevelObject(object["file"], position, rotation, scale, hasCollision));
|
|
|
|
|
|
|
+ //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"].asString(), position, rotation, scale, hasCollision));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //look up table for the enemies
|
|
|
|
|
- std::vector<std::pair<int, std::string>>enemy_models;
|
|
|
|
|
- for (auto enemy_model : v["enemy_models"])
|
|
|
|
|
|
|
+ //Load and place enemies into world
|
|
|
|
|
+ for (auto e : v["enemies"])
|
|
|
{
|
|
{
|
|
|
- int id = -1;
|
|
|
|
|
- if (!enemy_model["id"].isNull())
|
|
|
|
|
- id = enemy_model["id"].asInt();
|
|
|
|
|
|
|
+ //Rotation
|
|
|
|
|
+ Vec3f rotation(0, 0, 0);
|
|
|
|
|
+ if (!e["rot"].isNull())
|
|
|
|
|
+ rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
|
|
|
|
|
|
|
|
- std::string fileName = "";
|
|
|
|
|
- if (!enemy_model["file"].isNull())
|
|
|
|
|
- fileName = enemy_model["file"].asString();
|
|
|
|
|
|
|
+ //Scale
|
|
|
|
|
+ float scale = 1.0f;
|
|
|
|
|
+ if (!e["scale"].isNull())
|
|
|
|
|
+ scale = e["scale"].asFloat();
|
|
|
|
|
|
|
|
- enemy_models.push_back(std::pair<int, std::string>(id,fileName));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ //Position
|
|
|
|
|
+ if (e["pos"].isNull())
|
|
|
|
|
+ std::cout << "Invalid world file: enemies pos - " << fileName << "\n";
|
|
|
|
|
|
|
|
- for (auto enemy : v["enemy_data"])
|
|
|
|
|
- {
|
|
|
|
|
- int id = -1;
|
|
|
|
|
- if (!enemy["id"].isNull())
|
|
|
|
|
- id = enemy["id"];
|
|
|
|
|
- for (auto enemy_model : enemy_models)
|
|
|
|
|
- {
|
|
|
|
|
- if (id == enemy_model.first)
|
|
|
|
|
- {
|
|
|
|
|
- Vec3f position(0, 0, 0);
|
|
|
|
|
- if (!enemy["pos"].isNull())
|
|
|
|
|
- position = Vec3f(enemy["pos"][0], enemy["pos"][1], enemy["pos"][2]);
|
|
|
|
|
-
|
|
|
|
|
- Vec3f rotation(0, 0, 0);
|
|
|
|
|
- if (!enemy["rot"].isNull())
|
|
|
|
|
- rotation = Vec3f(enemy["rot"][0], enemy["rot"][1], enemy["rot"][2]);
|
|
|
|
|
-
|
|
|
|
|
- float scale = 1.0f;
|
|
|
|
|
- if (!enemy["scale"].isNull())
|
|
|
|
|
- scale = enemy["scale"].asFloat();
|
|
|
|
|
-
|
|
|
|
|
- enemies.push_back(new Enemy(enemy_model.second,position,rotation,scale,true));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ //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));
|
|
|
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
World::~World()
|
|
World::~World()
|
|
|
{
|
|
{
|
|
|
|
|
+ //delete heightmap;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+std::pair<std::string, bool> World::getObjectFromValue(int val)
|
|
|
|
|
+{
|
|
|
|
|
+ for (auto i : objecttemplates)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (i.first == val)
|
|
|
|
|
+ return i.second;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return objecttemplates[0].second;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+float World::getHeight(float x, float y)
|
|
|
|
|
+{
|
|
|
|
|
+ return heightmap->GetHeight(x, y);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void World::draw()
|
|
void World::draw()
|
|
|
{
|
|
{
|
|
|
- player.setCamera();
|
|
|
|
|
|
|
+ player->setCamera();
|
|
|
|
|
|
|
|
float lightPosition[4] = { 0, 2, 1, 0 };
|
|
float lightPosition[4] = { 0, 2, 1, 0 };
|
|
|
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
|
|
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);
|
|
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
|
|
|
|
|
|
|
|
- glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
|
|
heightmap->Draw();
|
|
heightmap->Draw();
|
|
|
|
|
|
|
|
for (auto &enemy : enemies)
|
|
for (auto &enemy : enemies)
|
|
@@ -109,8 +154,7 @@ void World::draw()
|
|
|
for (auto &entity : entities)
|
|
for (auto &entity : entities)
|
|
|
entity->draw();
|
|
entity->draw();
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+ interface->draw();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void World::update(float elapsedTime)
|
|
void World::update(float elapsedTime)
|
|
@@ -120,16 +164,11 @@ void World::update(float elapsedTime)
|
|
|
|
|
|
|
|
for (auto &enemy : enemies)
|
|
for (auto &enemy : enemies)
|
|
|
{
|
|
{
|
|
|
- if (enemy->position.Distance(player.position) <= enemy->radius)
|
|
|
|
|
- {
|
|
|
|
|
- enemy->hasTarget = true;
|
|
|
|
|
- enemy->target.x = player.position.x;
|
|
|
|
|
- enemy->target.z = player.position.z;
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- enemy->hasTarget = false;
|
|
|
|
|
-
|
|
|
|
|
- Vec3f oldpos = enemy->position;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ //Al deze code zou in enemy moeten staan
|
|
|
|
|
+ enemy->inEyeSight(player->position);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
enemy->update(elapsedTime);
|
|
enemy->update(elapsedTime);
|
|
|
if (enemy->hasTarget)
|
|
if (enemy->hasTarget)
|
|
|
{
|
|
{
|
|
@@ -137,19 +176,28 @@ void World::update(float elapsedTime)
|
|
|
{
|
|
{
|
|
|
if (e->canCollide && e->inObject(enemy->position))
|
|
if (e->canCollide && e->inObject(enemy->position))
|
|
|
{
|
|
{
|
|
|
- enemy->position = oldpos;
|
|
|
|
|
|
|
+ Vec3f difference = e->position - enemy->position; //zou misschien omgedraait moeten worden
|
|
|
|
|
+ difference.Normalize();
|
|
|
|
|
+ difference = difference * (e->model->radius + 0.01f);
|
|
|
|
|
+ enemy->position = e->position + difference;
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ //tot hier
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+void World::addLevelObject(LevelObject* obj)
|
|
|
|
|
+{
|
|
|
|
|
+ entities.push_back(obj);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
bool World::isPlayerPositionValid()
|
|
bool World::isPlayerPositionValid()
|
|
|
{
|
|
{
|
|
|
for (auto e : entities)
|
|
for (auto e : entities)
|
|
|
{
|
|
{
|
|
|
- if (e->canCollide && e->inObject(player.position))
|
|
|
|
|
|
|
+ if (e->canCollide && e->inObject(player->position))
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
return true;
|
|
return true;
|