Added world loading and other various improvements

This commit is contained in:
2016-05-25 22:29:18 +02:00
parent 33a61ca2ed
commit 908786897f
23 changed files with 506 additions and 280 deletions
+28
View File
@@ -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
-104
View File
@@ -1,104 +0,0 @@
#include "CrystalJohan.h"
#include <GL/freeglut.h>
#include <cmath>
#include <cstring>
#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));
}
-28
View File
@@ -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
+109
View File
@@ -0,0 +1,109 @@
#include "CrystalPoint.h"
#include <GL/freeglut.h>
#include <cmath>
#include <cstring>
#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));
}
+5 -3
View File
@@ -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;
@@ -19,10 +19,11 @@
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{98776A7C-CD7A-4C62-946A-2263C27E9690}</ProjectGuid>
<ProjectGuid>{F82158C7-7345-4CB0-9F90-3AB49A071904}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>CrystalJohan</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<ProjectName>CrystalPoint</ProjectName>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@@ -150,10 +151,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="CrystalJohan.cpp" />
<ClCompile Include="CrystalPoint.cpp" />
<ClCompile Include="Enemy.cpp" />
<ClCompile Include="Entity.cpp" />
<ClCompile Include="HeightMap.cpp" />
@@ -165,9 +163,10 @@
<ClCompile Include="Vector.cpp" />
<ClCompile Include="Vertex.cpp" />
<ClCompile Include="World.cpp" />
<ClCompile Include="WorldHandler.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CrystalJohan.h" />
<ClInclude Include="CrystalPoint.h" />
<ClInclude Include="Enemy.h" />
<ClInclude Include="Entity.h" />
<ClInclude Include="HeightMap.h" />
@@ -176,14 +175,16 @@
<ClInclude Include="Main.h" />
<ClInclude Include="Model.h" />
<ClInclude Include="Player.h" />
<ClInclude Include="Singleton.h" />
<ClInclude Include="stb_image.h" />
<ClInclude Include="vector.h" />
<ClInclude Include="Vertex.h" />
<ClInclude Include="World.h" />
<ClInclude Include="WorldHandler.h" />
</ItemGroup>
<ItemGroup>
<None Include="worlds\world1.json" />
<None Include="worlds\fire.json" />
<None Include="worlds\ice.json" />
<None Include="worlds\worlds.json" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
@@ -13,52 +13,58 @@
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Source Files\Object">
<UniqueIdentifier>{1e464995-b141-43fd-9e25-16d6ac47176b}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\World">
<UniqueIdentifier>{0de1a037-e536-40df-a0d0-0d929f2fe752}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\json">
<UniqueIdentifier>{9c655946-3f99-44ea-bc97-2817656954e0}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<Text Include="ReadMe.txt" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="CrystalJohan.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="World.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Entity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Enemy.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="LevelObject.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Model.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Vector.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Player.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="json.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="HeightMap.cpp">
<ClCompile Include="WorldHandler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Entity.cpp">
<Filter>Source Files\Object</Filter>
</ClCompile>
<ClCompile Include="Model.cpp">
<Filter>Source Files\Object</Filter>
</ClCompile>
<ClCompile Include="Vertex.cpp">
<Filter>Source Files\Object</Filter>
</ClCompile>
<ClCompile Include="Vector.cpp">
<Filter>Source Files\Object</Filter>
</ClCompile>
<ClCompile Include="World.cpp">
<Filter>Source Files\World</Filter>
</ClCompile>
<ClCompile Include="HeightMap.cpp">
<Filter>Source Files\World</Filter>
</ClCompile>
<ClCompile Include="LevelObject.cpp">
<Filter>Source Files\World</Filter>
</ClCompile>
<ClCompile Include="Enemy.cpp">
<Filter>Source Files\Object</Filter>
</ClCompile>
<ClCompile Include="CrystalPoint.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CrystalJohan.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="World.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -83,9 +89,6 @@
<ClInclude Include="Player.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Singleton.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Main.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -98,8 +101,22 @@
<ClInclude Include="Vertex.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="WorldHandler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CrystalPoint.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="worlds\world1.json" />
<None Include="worlds\worlds.json">
<Filter>Source Files\json</Filter>
</None>
<None Include="worlds\fire.json">
<Filter>Source Files\json</Filter>
</None>
<None Include="worlds\ice.json">
<Filter>Source Files\json</Filter>
</None>
</ItemGroup>
</Project>
+2 -3
View File
@@ -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;
+1 -1
View File
@@ -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;
+2
View File
@@ -14,6 +14,8 @@ Entity::Entity()
Entity::~Entity()
{
if(model)
Model::unload(model);
}
+8 -6
View File
@@ -1,18 +1,18 @@
#include <GL/freeglut.h>
#include "CrystalJohan.h"
#include "CrystalPoint.h"
#include <stdio.h>
#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);
}
+8 -4
View File
@@ -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;
}
}
+24
View File
@@ -3,11 +3,35 @@
#include "Player.h"
#include <GL/freeglut.h>
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);
+7 -4
View File
@@ -1,19 +1,22 @@
#pragma once
#include "Singleton.h"
#include "Vector.h"
class Model;
class Player : public Singleton<Player>
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;
+7 -7
View File
@@ -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:
-13
View File
@@ -1,13 +0,0 @@
#pragma once
template <class T>
class Singleton
{
public:
static T& getInstance()
{
static T* t = new T();
return *t;
}
};
+45 -52
View File
@@ -6,22 +6,32 @@
#include <fstream>
#include <iostream>
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<std::pair<int, std::string>>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<int, std::string>(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;
+2 -4
View File
@@ -10,15 +10,13 @@ class Entity;
class World
{
public:
World();
World(const std::string &fileName);
~World();
Player& player;
std::vector<Entity*> entities;
std::vector<Enemy*> enemies;
Player* player;
HeightMap* heightmap;
void draw();
+129
View File
@@ -0,0 +1,129 @@
#include "WorldHandler.h"
#include "World.h"
#include "json.h"
#include <fstream>
#include <iostream>
#include <string>
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);
}
}
+35
View File
@@ -0,0 +1,35 @@
#pragma once
#include <string>
#include <vector>
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<std::string> worldfiles;
};
+26
View File
@@ -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
}
]
}
+4 -11
View File
@@ -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
}
}
]
}
+6
View File
@@ -0,0 +1,6 @@
{
"worlds": [
"worlds/fire.json",
"worlds/ice.json"
]
}