diff --git a/Crystal.sln b/Crystal.sln new file mode 100644 index 0000000..bbc5482 --- /dev/null +++ b/Crystal.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.24720.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CrystalPoint", "CrystalPoint.vcxproj", "{F82158C7-7345-4CB0-9F90-3AB49A071904}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F82158C7-7345-4CB0-9F90-3AB49A071904}.Debug|x64.ActiveCfg = Debug|x64 + {F82158C7-7345-4CB0-9F90-3AB49A071904}.Debug|x64.Build.0 = Debug|x64 + {F82158C7-7345-4CB0-9F90-3AB49A071904}.Debug|x86.ActiveCfg = Debug|Win32 + {F82158C7-7345-4CB0-9F90-3AB49A071904}.Debug|x86.Build.0 = Debug|Win32 + {F82158C7-7345-4CB0-9F90-3AB49A071904}.Release|x64.ActiveCfg = Release|x64 + {F82158C7-7345-4CB0-9F90-3AB49A071904}.Release|x64.Build.0 = Release|x64 + {F82158C7-7345-4CB0-9F90-3AB49A071904}.Release|x86.ActiveCfg = Release|Win32 + {F82158C7-7345-4CB0-9F90-3AB49A071904}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/CrystalJohan.cpp b/CrystalJohan.cpp deleted file mode 100644 index dd6672a..0000000 --- a/CrystalJohan.cpp +++ /dev/null @@ -1,104 +0,0 @@ - -#include "CrystalJohan.h" -#include -#include -#include -#include "World.h" - -void CrystalJohan::init() -{ - world = new World(); - lastFrameTime = 0; - - glClearColor(0.7, 0.7, 1.0, 1.0); - glEnable(GL_DEPTH_TEST); - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - - mousePosition = Vec2f(width / 2, height / 2); -} - - -void CrystalJohan::draw() -{ - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - //Draw world - glEnable(GL_LIGHTING); - glEnable(GL_DEPTH_TEST); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(70, width / (float)height, 0.1f, 15000); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - world->draw(); - - //Draw Cursor - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0,width, height,0,-10,10); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - /*glDisable(GL_LIGHTING); - glDisable(GL_DEPTH_TEST); - glColor4f(1, cos(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), sin(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), 1); - glBegin(GL_TRIANGLES); - glVertex2f(mousePosition.x, mousePosition.y); - glVertex2f(mousePosition.x+15, mousePosition.y+15); - glVertex2f(mousePosition.x+5, mousePosition.y+20); - - glEnd();*/ - - glutSwapBuffers(); -} - - -void CrystalJohan::update() -{ - float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f; - float deltaTime = frameTime - lastFrameTime; - lastFrameTime = frameTime; - - // if(keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT]) - if (keyboardState.keys[27]) - exit(0); - - - world->player.rotation.y += mouseOffset.x / 10.0f; - world->player.rotation.x += mouseOffset.y / 10.0f; - if (world->player.rotation.x > 90) - world->player.rotation.x = 90; - if (world->player.rotation.x < -90) - world->player.rotation.x = -90; - - float speed = 20; - - Vec3f oldPosition = world->player.position; - if (keyboardState.keys['a']) world->player.setPosition(0, deltaTime*speed, false); - if (keyboardState.keys['d']) world->player.setPosition(180, deltaTime*speed, false); - if (keyboardState.keys['w']) world->player.setPosition(90, deltaTime*speed, false); - if (keyboardState.keys['s']) world->player.setPosition(270, deltaTime*speed, false); - if (keyboardState.keys['q']) world->player.setPosition(1, deltaTime*speed, true); - if (keyboardState.keys['e']) world->player.setPosition(-1, deltaTime*speed, true); - if (!world->isPlayerPositionValid()) - world->player.position = oldPosition; - - world->update(deltaTime); - - mousePosition = mousePosition + mouseOffset; - - mouseOffset = Vec2f(0, 0); - prevKeyboardState = keyboardState; - glutPostRedisplay(); -} - - - -KeyboardState::KeyboardState() -{ - memset(keys, 0, sizeof(keys)); - memset(special, 0, sizeof(special)); -} diff --git a/CrystalJohan.sln b/CrystalJohan.sln deleted file mode 100644 index c3d3c5b..0000000 --- a/CrystalJohan.sln +++ /dev/null @@ -1,28 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.24720.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CrystalJohan", "CrystalJohan.vcxproj", "{98776A7C-CD7A-4C62-946A-2263C27E9690}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {98776A7C-CD7A-4C62-946A-2263C27E9690}.Debug|x64.ActiveCfg = Debug|x64 - {98776A7C-CD7A-4C62-946A-2263C27E9690}.Debug|x64.Build.0 = Debug|x64 - {98776A7C-CD7A-4C62-946A-2263C27E9690}.Debug|x86.ActiveCfg = Debug|Win32 - {98776A7C-CD7A-4C62-946A-2263C27E9690}.Debug|x86.Build.0 = Debug|Win32 - {98776A7C-CD7A-4C62-946A-2263C27E9690}.Release|x64.ActiveCfg = Release|x64 - {98776A7C-CD7A-4C62-946A-2263C27E9690}.Release|x64.Build.0 = Release|x64 - {98776A7C-CD7A-4C62-946A-2263C27E9690}.Release|x86.ActiveCfg = Release|Win32 - {98776A7C-CD7A-4C62-946A-2263C27E9690}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/CrystalPoint.cpp b/CrystalPoint.cpp new file mode 100644 index 0000000..b5ac860 --- /dev/null +++ b/CrystalPoint.cpp @@ -0,0 +1,109 @@ + +#include "CrystalPoint.h" +#include +#include +#include +#include "WorldHandler.h" +#include "Player.h" + +void CrystalPoint::init() +{ + player = Player::getInstance(); + worldhandler = WorldHandler::getInstance(); + + lastFrameTime = 0; + + glClearColor(0.7, 0.7, 1.0, 1.0); + + mousePosition = Vec2f(width / 2, height / 2); +} + + +void CrystalPoint::draw() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + //Draw world + glEnable(GL_LIGHTING); + glEnable(GL_DEPTH_TEST); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(70, width / (float)height, 0.1f, 15000); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + worldhandler->draw(); + + //Draw Cursor + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0,width, height,0,-10,10); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glDisable(GL_LIGHTING); + glDisable(GL_DEPTH_TEST); + glColor4f(1, cos(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), sin(glutGet(GLUT_ELAPSED_TIME) / 1000.0f), 1); + + glBegin(GL_TRIANGLES); + glVertex2f(mousePosition.x, mousePosition.y); + glVertex2f(mousePosition.x+15, mousePosition.y+15); + glVertex2f(mousePosition.x+5, mousePosition.y+20); + glEnd(); + + glutSwapBuffers(); +} + + +void CrystalPoint::update() +{ + float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f; + float deltaTime = frameTime - lastFrameTime; + lastFrameTime = frameTime; + + if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT]) + worldhandler->PreviousWorld(); + if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT]) + worldhandler->NextWorld(); + if (keyboardState.keys[27]) + exit(0); + + Player* player = Player::getInstance(); + + player->rotation.y += mouseOffset.x / 10.0f; + player->rotation.x += mouseOffset.y / 10.0f; + if (player->rotation.x > 90) + player->rotation.x = 90; + if (player->rotation.x < -90) + player->rotation.x = -90; + + float speed = 20; + + Vec3f oldPosition = player->position; + if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false); + if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false); + if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false); + if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false); + if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true); + if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true); + + if (!worldhandler->isPlayerPositionValid()) + player->position = oldPosition; + + worldhandler->update(deltaTime); + + mousePosition = mousePosition + mouseOffset; + + mouseOffset = Vec2f(0, 0); + prevKeyboardState = keyboardState; + glutPostRedisplay(); +} + + + +KeyboardState::KeyboardState() +{ + memset(keys, 0, sizeof(keys)); + memset(special, 0, sizeof(special)); +} diff --git a/CrystalJohan.h b/CrystalPoint.h similarity index 78% rename from CrystalJohan.h rename to CrystalPoint.h index 51a42ad..fef6a36 100644 --- a/CrystalJohan.h +++ b/CrystalPoint.h @@ -1,6 +1,7 @@ #pragma once -class World; +class WorldHandler; +class Player; #include "Vector.h" class KeyboardState @@ -13,14 +14,15 @@ public: KeyboardState(); }; -class CrystalJohan +class CrystalPoint { public: void init(); void draw(); void update(); - World* world; + WorldHandler* worldhandler; + Player* player; int width, height; KeyboardState keyboardState; diff --git a/CrystalJohan.vcxproj b/CrystalPoint.vcxproj similarity index 95% rename from CrystalJohan.vcxproj rename to CrystalPoint.vcxproj index eb8f65c..f8cc70d 100644 --- a/CrystalJohan.vcxproj +++ b/CrystalPoint.vcxproj @@ -19,10 +19,11 @@ - {98776A7C-CD7A-4C62-946A-2263C27E9690} + {F82158C7-7345-4CB0-9F90-3AB49A071904} Win32Proj CrystalJohan 8.1 + CrystalPoint @@ -150,10 +151,7 @@ - - - - + @@ -165,9 +163,10 @@ + - + @@ -176,14 +175,16 @@ - + - + + + diff --git a/CrystalJohan.vcxproj.filters b/CrystalPoint.vcxproj.filters similarity index 70% rename from CrystalJohan.vcxproj.filters rename to CrystalPoint.vcxproj.filters index 4b5393a..e75e5d2 100644 --- a/CrystalJohan.vcxproj.filters +++ b/CrystalPoint.vcxproj.filters @@ -13,52 +13,58 @@ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + {1e464995-b141-43fd-9e25-16d6ac47176b} + + + {0de1a037-e536-40df-a0d0-0d929f2fe752} + + + {9c655946-3f99-44ea-bc97-2817656954e0} + - - - - - Source Files - Source Files - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - Source Files Source Files - + Source Files + + Source Files\Object + + + Source Files\Object + + Source Files\Object + + + Source Files\Object + + + Source Files\World + + + Source Files\World + + + Source Files\World + + + Source Files\Object + + Source Files - - Header Files - Header Files @@ -83,9 +89,6 @@ Header Files - - Header Files - Header Files @@ -98,8 +101,22 @@ Header Files + + Header Files + + + Header Files + - + + Source Files\json + + + Source Files\json + + + Source Files\json + \ No newline at end of file diff --git a/Enemy.cpp b/Enemy.cpp index c62fb46..a56fbfc 100644 --- a/Enemy.cpp +++ b/Enemy.cpp @@ -9,14 +9,13 @@ Enemy::Enemy(const std::string &fileName, const Vec3f &position, Vec3f &rotation, - const float &scale, - const bool &hasCollision) + const float &scale) { model = Model::load(fileName); this->position = position; this->rotation = rotation; this->scale = scale; - this->canCollide = hasCollision; + this->canCollide = true; target = position; speed = 1; radius = 10; diff --git a/Enemy.h b/Enemy.h index 9c14fb9..eae8ec6 100644 --- a/Enemy.h +++ b/Enemy.h @@ -7,7 +7,7 @@ class Enemy : public Entity { public: - Enemy(const std::string &fileName,const Vec3f &position,Vec3f &rotation,const float &scale,const bool &hasCollision); + Enemy(const std::string &fileName,const Vec3f &position,Vec3f &rotation,const float &scale); ~Enemy(); bool hasTarget; diff --git a/Entity.cpp b/Entity.cpp index c1e0282..b8c0eee 100644 --- a/Entity.cpp +++ b/Entity.cpp @@ -14,6 +14,8 @@ Entity::Entity() Entity::~Entity() { + if(model) + Model::unload(model); } diff --git a/Main.cpp b/Main.cpp index fdcb565..b385658 100644 --- a/Main.cpp +++ b/Main.cpp @@ -1,18 +1,18 @@ #include -#include "CrystalJohan.h" +#include "CrystalPoint.h" #include #include "Vector.h" void configureOpenGL(void); -CrystalJohan* app; +CrystalPoint* app; bool justMoved = false; int main(int argc, char* argv[]) { - app = new CrystalJohan(); + app = new CrystalPoint(); glutInit(&argc, argv); configureOpenGL(); @@ -26,6 +26,8 @@ int main(int argc, char* argv[]) //Keyboard glutKeyboardFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = true; }); glutKeyboardUpFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = false; }); + glutSpecialFunc([](int c, int, int) { app->keyboardState.special[c] = true; }); + glutSpecialUpFunc([](int c, int, int) { app->keyboardState.special[c] = false; }); //Mouse glutPassiveMotionFunc([](int x, int y) @@ -55,7 +57,7 @@ void configureOpenGL() glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize(800, 600); glutCreateWindow("Crystal Point"); - glutFullScreen(); + //glutFullScreen(); //Depth testing glEnable(GL_DEPTH_TEST); @@ -81,8 +83,8 @@ void configureOpenGL() //glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); //glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); - //glEnable(GL_LIGHTING); - //glEnable(GL_LIGHT0); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); glutSetCursor(GLUT_CURSOR_CROSSHAIR); } \ No newline at end of file diff --git a/Model.cpp b/Model.cpp index 9a86b2e..9027ec4 100644 --- a/Model.cpp +++ b/Model.cpp @@ -158,10 +158,6 @@ void Model::Optimise(ObjGroup *t) } } -Model::~Model(void) -{ -} - void Model::draw() { for (auto &g : groups) @@ -381,3 +377,11 @@ void Model::unload(Model* model) } } } + +Model::~Model(void) +{ + for (auto m : cache) + { + delete m.second.first; + } +} \ No newline at end of file diff --git a/Player.cpp b/Player.cpp index 2c77480..dcb172e 100644 --- a/Player.cpp +++ b/Player.cpp @@ -3,11 +3,35 @@ #include "Player.h" #include +Player* Player::instance = NULL; + Player::Player() { speed = 10; } +Player* Player::getInstance() +{ + if (instance == nullptr) + instance = new Player(); + + return instance; +} + +void Player::init() +{ + instance = new Player(); +} + +Player::~Player() +{ + if (leftWeapon) + delete leftWeapon; + + if (rightWeapon) + delete rightWeapon; +} + void Player::setCamera() { glRotatef(rotation.x, 1, 0, 0); diff --git a/Player.h b/Player.h index abb694e..d9a2a8f 100644 --- a/Player.h +++ b/Player.h @@ -1,19 +1,22 @@ #pragma once - -#include "Singleton.h" - #include "Vector.h" class Model; -class Player : public Singleton +class Player { +private: + static Player* instance; public: Player(); + ~Player(); void setCamera(); void setPosition(float angle, float fac, bool height); + static Player* getInstance(void); + static void init(void); + Vec3f position; Vec2f rotation; diff --git a/ReadMe.txt b/ReadMe.txt index 09d0ceb..2e988a3 100644 --- a/ReadMe.txt +++ b/ReadMe.txt @@ -1,27 +1,27 @@ ======================================================================== - CONSOLE APPLICATION : CrystalJohan Project Overview + CONSOLE APPLICATION : CrystalPoint Project Overview ======================================================================== -AppWizard has created this CrystalJohan application for you. +AppWizard has created this CrystalPoint application for you. This file contains a summary of what you will find in each of the files that -make up your CrystalJohan application. +make up your CrystalPoint application. -CrystalJohan.vcxproj +CrystalPoint.vcxproj This is the main project file for VC++ projects generated using an Application Wizard. It contains information about the version of Visual C++ that generated the file, and information about the platforms, configurations, and project features selected with the Application Wizard. -CrystalJohan.vcxproj.filters +CrystalPoint.vcxproj.filters This is the filters file for VC++ projects generated using an Application Wizard. It contains information about the association between the files in your project and the filters. This association is used in the IDE to show grouping of files with similar extensions under a specific node (for e.g. ".cpp" files are associated with the "Source Files" filter). -CrystalJohan.cpp +CrystalPoint.cpp This is the main application source file. ///////////////////////////////////////////////////////////////////////////// @@ -29,7 +29,7 @@ Other standard files: StdAfx.h, StdAfx.cpp These files are used to build a precompiled header (PCH) file - named CrystalJohan.pch and a precompiled types file named StdAfx.obj. + named CrystalPoint.pch and a precompiled types file named StdAfx.obj. ///////////////////////////////////////////////////////////////////////////// Other notes: diff --git a/Singleton.h b/Singleton.h deleted file mode 100644 index fd9236c..0000000 --- a/Singleton.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - - -template -class Singleton -{ -public: - static T& getInstance() - { - static T* t = new T(); - return *t; - } -}; \ No newline at end of file diff --git a/World.cpp b/World.cpp index 98e8a7d..8612df6 100644 --- a/World.cpp +++ b/World.cpp @@ -6,22 +6,32 @@ #include #include -World::World() : player(Player::getInstance()) +World::World(const std::string &fileName) { + player = Player::getInstance(); - std::ifstream file("worlds/world1.json"); + std::ifstream file(fileName); 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); file.close(); - heightmap = new HeightMap(v["world"]["heightmap"].asString()); - heightmap->SetTexture(v["world"]["texture"].asString()); + 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"; - player.position.x = v["player"]["startposition"][0]; - player.position.y = v["player"]["startposition"][1]; - player.position.z = v["player"]["startposition"][2]; + heightmap = new HeightMap(v["world"]["heightmap"].asString()); + + if(!v["world"]["texture"].isNull()) + heightmap->SetTexture(v["world"]["texture"].asString()); + + player->position.x = v["player"]["startposition"][0].asFloat(); + player->position.y = v["player"]["startposition"][1].asFloat(); + player->position.z = v["player"]["startposition"][2].asFloat(); for (auto object : v["objects"]) @@ -32,57 +42,40 @@ World::World() : player(Player::getInstance()) Vec3f rotation(0, 0, 0); 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()); float scale = 1; if (!object["scale"].isNull()) scale = object["scale"].asFloat(); - Vec3f position(object["pos"][0], object["pos"][1], object["pos"][2]); + if (object["pos"].isNull()) + std::cout << "Invalid world file: objects pos - " << fileName << "\n"; + + 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)); } - //look up table for the enemies - std::vector>enemy_models; - for (auto enemy_model : v["enemy_models"]) + //Enemies + for (auto e : v["enemies"]) { - int id = -1; - if (!enemy_model["id"].isNull()) - id = enemy_model["id"].asInt(); - std::string fileName = ""; - if (!enemy_model["file"].isNull()) - fileName = enemy_model["file"].asString(); + if (!e["file"].isNull()) + fileName = e["file"].asString(); - enemy_models.push_back(std::pair(id,fileName)); - } + Vec3f position(0, 0, 0); + if (!e["pos"].isNull()) + position = Vec3f(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat()); - 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 (!e["rot"].isNull()) + rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat()); - 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 (!e["scale"].isNull()) + scale = e["scale"].asFloat(); - float scale = 1.0f; - if (!enemy["scale"].isNull()) - scale = enemy["scale"].asFloat(); - - enemies.push_back(new Enemy(enemy_model.second,position,rotation,scale,true)); - } + enemies.push_back(new Enemy(fileName, position, rotation, scale)); - } - } } @@ -93,7 +86,7 @@ World::~World() void World::draw() { - player.setCamera(); + player->setCamera(); float lightPosition[4] = { 0, 2, 1, 0 }; glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); @@ -108,9 +101,6 @@ void World::draw() for (auto &entity : entities) entity->draw(); - - - } void World::update(float elapsedTime) @@ -120,11 +110,13 @@ void World::update(float elapsedTime) for (auto &enemy : enemies) { - if (enemy->position.Distance(player.position) <= enemy->radius) + + //Al deze code zou in enemy moeten staan + if (enemy->position.Distance(player->position) <= enemy->radius) { enemy->hasTarget = true; - enemy->target.x = player.position.x; - enemy->target.z = player.position.z; + enemy->target.x = player->position.x; + enemy->target.z = player->position.z; } else enemy->hasTarget = false; @@ -142,6 +134,7 @@ void World::update(float elapsedTime) } } } + //tot hier } } @@ -149,7 +142,7 @@ bool World::isPlayerPositionValid() { for (auto e : entities) { - if (e->canCollide && e->inObject(player.position)) + if (e->canCollide && e->inObject(player->position)) return false; } return true; diff --git a/World.h b/World.h index 91ad3e6..a558aa1 100644 --- a/World.h +++ b/World.h @@ -10,15 +10,13 @@ class Entity; class World { public: - World(); + World(const std::string &fileName); ~World(); - - Player& player; std::vector entities; - std::vector enemies; + Player* player; HeightMap* heightmap; void draw(); diff --git a/WorldHandler.cpp b/WorldHandler.cpp new file mode 100644 index 0000000..917972c --- /dev/null +++ b/WorldHandler.cpp @@ -0,0 +1,129 @@ +#include "WorldHandler.h" +#include "World.h" + +#include "json.h" +#include +#include +#include + +WorldHandler* WorldHandler::instance = nullptr; + +void WorldHandler::ChangeWorld(int i) +{ + if (i < 0) + i = worldfiles.size() - 1; + + else if (i >= worldfiles.size()) + i = 0; + + if (i != worldIndex) + { + loadingWorld = true; + + if(worldIndex != -1) + delete world; + + world = new World(worldfiles[i]); + worldIndex = i; + loadingWorld = false; + } +} + +WorldHandler::WorldHandler() +{ + loadingWorld = true; + worldIndex = -1; + + //Find worlds.json + std::ifstream file("worlds/worlds.json"); + if (!file.is_open()) + std::cout << "Error, can't open worlds overview file\n"; + + json::Value v = json::readJson(file); + file.close(); + + //Load file names into vector + if (v["worlds"].isNull() || !v["worlds"].isArray()) + std::cout << "Error, no content in worlds overview file\n"; + + for (auto line : v["worlds"]) + { + std::cout << "Found world: " << line << "\n"; + worldfiles.push_back(line); + } + + if (worldfiles.size() > 0) + { + ChangeWorld(0); + } +} + +WorldHandler::~WorldHandler() +{ + worldIndex = -1; + delete world; +} + +WorldHandler* WorldHandler::getInstance() +{ + if (instance == nullptr) + instance = new WorldHandler(); + + return instance; +} + +void WorldHandler::init() +{ + instance = new WorldHandler(); +} + +void WorldHandler::draw(void) +{ + if(!loadingWorld) + world->draw(); + else + { + //Draw Loading screen + } +} + +void WorldHandler::update(float deltaTime) +{ + if(!loadingWorld) + world->update(deltaTime); +} + +bool WorldHandler::isPlayerPositionValid(void) +{ + if(!loadingWorld) + return world->isPlayerPositionValid(); +} + + +void WorldHandler::Navigate(const std::string &fileName) +{ + if (!loadingWorld) + { + for (int i = 0; i < worldfiles.size(); i++) + { + if (worldfiles[i] == fileName) + ChangeWorld(i); + } + } +} + +void WorldHandler::NextWorld() +{ + if (!loadingWorld) + { + ChangeWorld(worldIndex + 1); + } +} + +void WorldHandler::PreviousWorld() +{ + if (!loadingWorld) + { + ChangeWorld(worldIndex - 1); + } +} \ No newline at end of file diff --git a/WorldHandler.h b/WorldHandler.h new file mode 100644 index 0000000..7688cda --- /dev/null +++ b/WorldHandler.h @@ -0,0 +1,35 @@ +#pragma once +#include +#include + +class World; + +class WorldHandler +{ +private: + WorldHandler(); + static WorldHandler* instance; + + bool loadingWorld; + World* world; + int worldIndex; + void ChangeWorld(int i); +public: + + ~WorldHandler(); + + static WorldHandler* getInstance(void); + static void init(); + + void draw(void); + void update(float deltaTime); + + bool isPlayerPositionValid(void); + + void Navigate(const std::string &fileName); + void NextWorld(); + void PreviousWorld(); + + std::vector worldfiles; +}; + diff --git a/worlds/fire.json b/worlds/fire.json new file mode 100644 index 0000000..c8a8dcd --- /dev/null +++ b/worlds/fire.json @@ -0,0 +1,26 @@ +{ + "world": { + "heightmap": "worlds/hell.png", + "texture": "worlds/helltexture.png" + }, + "player": { + "startposition": [ 100, 1.7, 100 ] + }, + "objects": [ + { + "file": "models/boom/Boom.obj", + "pos": [ 4, 0, -4 ] + }, + { + "file": "models/boom/Boom.obj", + "pos": [ -4, 0, -4 ] + } + ], + "enemies": [ + { + "file": "models/squid/Blooper.obj", + "pos": [ 1, 2, -10 ], + "scale": 0.01 + } + ] +} \ No newline at end of file diff --git a/worlds/world1.json b/worlds/ice.json similarity index 67% rename from worlds/world1.json rename to worlds/ice.json index 7eff300..f0f8673 100644 --- a/worlds/world1.json +++ b/worlds/ice.json @@ -13,21 +13,14 @@ }, { "file": "models/Teleporter/Teleporter.obj", - "pos": [ 0, 0, -4 ], - "rot": [ 0, 0, 0 ] + "pos": [ 0, 0, -4 ] } ], - "enemy_models": [ + "enemies": [ { - "id": 0, - "file": "models/squid/Blooper.obj" - } - ], - "enemy_data": [ - { - "id": 0, + "file": "models/squid/Blooper.obj", "pos": [ 1, 2, -10 ], "scale": 0.01 - } + } ] } \ No newline at end of file diff --git a/worlds/worlds.json b/worlds/worlds.json new file mode 100644 index 0000000..5516fea --- /dev/null +++ b/worlds/worlds.json @@ -0,0 +1,6 @@ +{ + "worlds": [ + "worlds/fire.json", + "worlds/ice.json" + ] +} \ No newline at end of file