Bladeren bron

Merge pull request #8 from CrystalPointA4/worldLoading

Added world loading and other various improvements
Kenneth van Ewijk 9 jaren geleden
bovenliggende
commit
b4a2da4b88
33 gewijzigde bestanden met toevoegingen van 734 en 327 verwijderingen
  1. 28 0
      Crystal.sln
  2. 0 104
      CrystalJohan.cpp
  3. 0 28
      CrystalJohan.sln
  4. 111 0
      CrystalPoint.cpp
  5. 5 3
      CrystalPoint.h
  6. 10 8
      CrystalPoint.vcxproj
  7. 47 27
      CrystalPoint.vcxproj.filters
  8. 2 3
      Enemy.cpp
  9. 1 1
      Enemy.h
  10. 6 2
      Entity.cpp
  11. 68 13
      HeightMap.cpp
  12. 4 3
      HeightMap.h
  13. 8 6
      Main.cpp
  14. 14 12
      Model.cpp
  15. 24 0
      Player.cpp
  16. 7 4
      Player.h
  17. 7 7
      ReadMe.txt
  18. 0 13
      Singleton.h
  19. 14 0
      Vector.cpp
  20. 4 0
      Vector.h
  21. 20 0
      Vertex.cpp
  22. 5 0
      Vertex.h
  23. 83 56
      World.cpp
  24. 8 4
      World.h
  25. 137 0
      WorldHandler.cpp
  26. 36 0
      WorldHandler.h
  27. 36 0
      worlds/fire.json
  28. 0 0
      worlds/hell.pdn
  29. 26 0
      worlds/ice.json
  30. 16 0
      worlds/small.json
  31. BIN
      worlds/small.png
  32. 0 33
      worlds/world1.json
  33. 7 0
      worlds/worlds.json

+ 28 - 0
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

+ 0 - 104
CrystalJohan.cpp

@@ -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));
-}

+ 0 - 28
CrystalJohan.sln

@@ -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

+ 111 - 0
CrystalPoint.cpp

@@ -0,0 +1,111 @@
+
+#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 = 1;
+
+	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;
+
+	player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 0.7f;
+
+	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
CrystalJohan.h → CrystalPoint.h

@@ -1,6 +1,7 @@
 #pragma once
 #pragma once
 
 
-class World;
+class WorldHandler;
+class Player;
 #include "Vector.h"
 #include "Vector.h"
 
 
 class KeyboardState
 class KeyboardState
@@ -13,14 +14,15 @@ public:
 	KeyboardState();
 	KeyboardState();
 };
 };
 
 
-class CrystalJohan
+class CrystalPoint
 {
 {
 public:
 public:
 	void init();
 	void init();
 	void draw();
 	void draw();
 	void update();
 	void update();
 
 
-	World* world;
+	WorldHandler* worldhandler;
+	Player* player;
 
 
 	int width, height;
 	int width, height;
 	KeyboardState keyboardState;
 	KeyboardState keyboardState;

+ 10 - 8
CrystalJohan.vcxproj → CrystalPoint.vcxproj

@@ -19,10 +19,11 @@
     </ProjectConfiguration>
     </ProjectConfiguration>
   </ItemGroup>
   </ItemGroup>
   <PropertyGroup Label="Globals">
   <PropertyGroup Label="Globals">
-    <ProjectGuid>{98776A7C-CD7A-4C62-946A-2263C27E9690}</ProjectGuid>
+    <ProjectGuid>{F82158C7-7345-4CB0-9F90-3AB49A071904}</ProjectGuid>
     <Keyword>Win32Proj</Keyword>
     <Keyword>Win32Proj</Keyword>
     <RootNamespace>CrystalJohan</RootNamespace>
     <RootNamespace>CrystalJohan</RootNamespace>
     <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
     <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
+    <ProjectName>CrystalPoint</ProjectName>
   </PropertyGroup>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@@ -150,10 +151,7 @@
     </Link>
     </Link>
   </ItemDefinitionGroup>
   </ItemDefinitionGroup>
   <ItemGroup>
   <ItemGroup>
-    <Text Include="ReadMe.txt" />
-  </ItemGroup>
-  <ItemGroup>
-    <ClCompile Include="CrystalJohan.cpp" />
+    <ClCompile Include="CrystalPoint.cpp" />
     <ClCompile Include="Enemy.cpp" />
     <ClCompile Include="Enemy.cpp" />
     <ClCompile Include="Entity.cpp" />
     <ClCompile Include="Entity.cpp" />
     <ClCompile Include="HeightMap.cpp" />
     <ClCompile Include="HeightMap.cpp" />
@@ -165,9 +163,10 @@
     <ClCompile Include="Vector.cpp" />
     <ClCompile Include="Vector.cpp" />
     <ClCompile Include="Vertex.cpp" />
     <ClCompile Include="Vertex.cpp" />
     <ClCompile Include="World.cpp" />
     <ClCompile Include="World.cpp" />
+    <ClCompile Include="WorldHandler.cpp" />
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
-    <ClInclude Include="CrystalJohan.h" />
+    <ClInclude Include="CrystalPoint.h" />
     <ClInclude Include="Enemy.h" />
     <ClInclude Include="Enemy.h" />
     <ClInclude Include="Entity.h" />
     <ClInclude Include="Entity.h" />
     <ClInclude Include="HeightMap.h" />
     <ClInclude Include="HeightMap.h" />
@@ -176,14 +175,17 @@
     <ClInclude Include="Main.h" />
     <ClInclude Include="Main.h" />
     <ClInclude Include="Model.h" />
     <ClInclude Include="Model.h" />
     <ClInclude Include="Player.h" />
     <ClInclude Include="Player.h" />
-    <ClInclude Include="Singleton.h" />
     <ClInclude Include="stb_image.h" />
     <ClInclude Include="stb_image.h" />
     <ClInclude Include="vector.h" />
     <ClInclude Include="vector.h" />
     <ClInclude Include="Vertex.h" />
     <ClInclude Include="Vertex.h" />
     <ClInclude Include="World.h" />
     <ClInclude Include="World.h" />
+    <ClInclude Include="WorldHandler.h" />
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
-    <None Include="worlds\world1.json" />
+    <None Include="worlds\fire.json" />
+    <None Include="worlds\ice.json" />
+    <None Include="worlds\small.json" />
+    <None Include="worlds\worlds.json" />
   </ItemGroup>
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   <ImportGroup Label="ExtensionTargets">

+ 47 - 27
CrystalJohan.vcxproj.filters → CrystalPoint.vcxproj.filters

@@ -13,52 +13,58 @@
       <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
       <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>
       <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
     </Filter>
     </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>
   <ItemGroup>
   <ItemGroup>
-    <Text Include="ReadMe.txt" />
-  </ItemGroup>
-  <ItemGroup>
-    <ClCompile Include="CrystalJohan.cpp">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="Main.cpp">
     <ClCompile Include="Main.cpp">
       <Filter>Source Files</Filter>
       <Filter>Source Files</Filter>
     </ClCompile>
     </ClCompile>
-    <ClCompile Include="World.cpp">
+    <ClCompile Include="Player.cpp">
       <Filter>Source Files</Filter>
       <Filter>Source Files</Filter>
     </ClCompile>
     </ClCompile>
-    <ClCompile Include="Entity.cpp">
+    <ClCompile Include="json.cpp">
       <Filter>Source Files</Filter>
       <Filter>Source Files</Filter>
     </ClCompile>
     </ClCompile>
-    <ClCompile Include="Enemy.cpp">
+    <ClCompile Include="WorldHandler.cpp">
       <Filter>Source Files</Filter>
       <Filter>Source Files</Filter>
     </ClCompile>
     </ClCompile>
-    <ClCompile Include="LevelObject.cpp">
-      <Filter>Source Files</Filter>
+    <ClCompile Include="Entity.cpp">
+      <Filter>Source Files\Object</Filter>
     </ClCompile>
     </ClCompile>
     <ClCompile Include="Model.cpp">
     <ClCompile Include="Model.cpp">
-      <Filter>Source Files</Filter>
+      <Filter>Source Files\Object</Filter>
     </ClCompile>
     </ClCompile>
-    <ClCompile Include="Vector.cpp">
-      <Filter>Source Files</Filter>
+    <ClCompile Include="Vertex.cpp">
+      <Filter>Source Files\Object</Filter>
     </ClCompile>
     </ClCompile>
-    <ClCompile Include="Player.cpp">
-      <Filter>Source Files</Filter>
+    <ClCompile Include="Vector.cpp">
+      <Filter>Source Files\Object</Filter>
     </ClCompile>
     </ClCompile>
-    <ClCompile Include="json.cpp">
-      <Filter>Source Files</Filter>
+    <ClCompile Include="World.cpp">
+      <Filter>Source Files\World</Filter>
     </ClCompile>
     </ClCompile>
     <ClCompile Include="HeightMap.cpp">
     <ClCompile Include="HeightMap.cpp">
-      <Filter>Source Files</Filter>
+      <Filter>Source Files\World</Filter>
     </ClCompile>
     </ClCompile>
-    <ClCompile Include="Vertex.cpp">
+    <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>
       <Filter>Source Files</Filter>
     </ClCompile>
     </ClCompile>
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
-    <ClInclude Include="CrystalJohan.h">
-      <Filter>Header Files</Filter>
-    </ClInclude>
     <ClInclude Include="World.h">
     <ClInclude Include="World.h">
       <Filter>Header Files</Filter>
       <Filter>Header Files</Filter>
     </ClInclude>
     </ClInclude>
@@ -83,9 +89,6 @@
     <ClInclude Include="Player.h">
     <ClInclude Include="Player.h">
       <Filter>Header Files</Filter>
       <Filter>Header Files</Filter>
     </ClInclude>
     </ClInclude>
-    <ClInclude Include="Singleton.h">
-      <Filter>Header Files</Filter>
-    </ClInclude>
     <ClInclude Include="Main.h">
     <ClInclude Include="Main.h">
       <Filter>Header Files</Filter>
       <Filter>Header Files</Filter>
     </ClInclude>
     </ClInclude>
@@ -98,8 +101,25 @@
     <ClInclude Include="Vertex.h">
     <ClInclude Include="Vertex.h">
       <Filter>Header Files</Filter>
       <Filter>Header Files</Filter>
     </ClInclude>
     </ClInclude>
+    <ClInclude Include="WorldHandler.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="CrystalPoint.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
   </ItemGroup>
   </ItemGroup>
   <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>
+    <None Include="worlds\small.json">
+      <Filter>Source Files\json</Filter>
+    </None>
   </ItemGroup>
   </ItemGroup>
 </Project>
 </Project>

+ 2 - 3
Enemy.cpp

@@ -9,14 +9,13 @@
 Enemy::Enemy(const std::string &fileName,
 Enemy::Enemy(const std::string &fileName,
 	const Vec3f &position,
 	const Vec3f &position,
 	Vec3f &rotation,
 	Vec3f &rotation,
-	const float &scale,
-	const bool &hasCollision)
+	const float &scale)
 {
 {
 	model = Model::load(fileName);
 	model = Model::load(fileName);
 	this->position = position;
 	this->position = position;
 	this->rotation = rotation;
 	this->rotation = rotation;
 	this->scale = scale;
 	this->scale = scale;
-	this->canCollide = hasCollision;
+	this->canCollide = true;
 	target = position;
 	target = position;
 	speed = 1;
 	speed = 1;
 	radius = 10;
 	radius = 10;

+ 1 - 1
Enemy.h

@@ -7,7 +7,7 @@
 class Enemy : public Entity
 class Enemy : public Entity
 {
 {
 public:
 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();
 	~Enemy();
 
 
 	bool hasTarget;
 	bool hasTarget;

+ 6 - 2
Entity.cpp

@@ -14,6 +14,8 @@ Entity::Entity()
 
 
 Entity::~Entity()
 Entity::~Entity()
 {
 {
+	if(model)
+		Model::unload(model);
 }
 }
 
 
 
 
@@ -23,11 +25,13 @@ void Entity::draw()
 	{
 	{
 		glPushMatrix();
 		glPushMatrix();
 
 
+
 		glTranslatef(position.x, position.y, position.z);
 		glTranslatef(position.x, position.y, position.z);
 		glRotatef(rotation.x, 1, 0, 0);
 		glRotatef(rotation.x, 1, 0, 0);
 		glRotatef(rotation.y, 0, 1, 0);
 		glRotatef(rotation.y, 0, 1, 0);
 		glRotatef(rotation.z, 0, 0, 1);
 		glRotatef(rotation.z, 0, 0, 1);
 		glScalef(scale, scale, scale);
 		glScalef(scale, scale, scale);
+//collision gaat hierdoor kapot		glTranslatef(-model->center.x, 0, -model->center.z);
 
 
 		glEnable(GL_CULL_FACE);
 		glEnable(GL_CULL_FACE);
 		glCullFace(GL_BACK);
 		glCullFace(GL_BACK);
@@ -46,8 +50,8 @@ bool Entity::inObject(const Vec3f & point)
 	if (!model)
 	if (!model)
 		return false;
 		return false;
 	Vec3f center = position + model->center;
 	Vec3f center = position + model->center;
-	float distance = sqrt((point.x - center.x) * (point.x - center.x) + (point.z - center.z)*(point.z - center.z));
-	if (distance < model->radius*scale)
+	float distance = ((point.x - center.x) * (point.x - center.x) + (point.z - center.z)*(point.z - center.z));
+	if (distance < model->radius*scale*model->radius*scale)
 		return true;
 		return true;
 	return false;
 	return false;
 }
 }

+ 68 - 13
HeightMap.cpp

@@ -1,29 +1,56 @@
 #include "HeightMap.h"
 #include "HeightMap.h"
 #include "stb_image.h"
 #include "stb_image.h"
+#include "vector.h"
+
+#include "LevelObject.h"
 
 
 #include <GL/freeglut.h>
 #include <GL/freeglut.h>
 #include <iostream>
 #include <iostream>
 #include <string>
 #include <string>
+#include "World.h"
 
 
+#define RED 0
+#define GREEN 1
+#define BLUE 2
+#define ALPHA 3
 
 
-HeightMap::HeightMap(const std::string &file)
+HeightMap::HeightMap(const std::string &file, World* world)
 {
 {
 	int bpp;
 	int bpp;
 	unsigned char* imgData = stbi_load(file.c_str(), &width, &height, &bpp, 4);
 	unsigned char* imgData = stbi_load(file.c_str(), &width, &height, &bpp, 4);
 
 
-	for (int h = 0; h < height; h++)
+	auto heightAt = [&](int x, int y)
+	{
+		return (imgData[(x + y * width) * 4 ] / 256.0f) * 50.0f;
+	};
+
+	auto valueAt = [&](int x, int y, int offset = 0)
+	{
+		return imgData[(x + y * width) * 4 + offset];
+	};
+
+	for (int y = 0; y < height-1; y++)
 	{
 	{
-		for (int w = 0; w < width; w++)
+		for (int x = 0; x < width-1; x++)
 		{
 		{
 			int offsets[4][2] = { { 0, 0 },{ 1, 0 },{ 1, 1 },{ 0, 1 } };
 			int offsets[4][2] = { { 0, 0 },{ 1, 0 },{ 1, 1 },{ 0, 1 } };
-			for (int i = 0; i < 4; i++)
+			Vec3f ca(0, heightAt(x, y + 1) - heightAt(x, y), 1);
+			Vec3f ba(1, heightAt(x + 1, y) - heightAt(x, y), 0);
+
+			if (valueAt(x, y, GREEN) > 0)
 			{
 			{
-				float y = ((float)imgData[((h + offsets[i][0]) + (w + offsets[i][1]) * width) * 4]);
-				y = (y / 256.0f) * 100.0f;
+				world->addLevelObject(new LevelObject(world->getObjectFromValue(valueAt(x, y, GREEN)), Vec3f(x, heightAt(x, y), y), Vec3f(0, rand()%360, 0), 1, true));
+			}
+
+			Vec3f normal = ca.cross(ba);
+			normal.Normalize();
 
 
-				vertices.push_back(Vertex{ (float)(h + offsets[i][0])*scale, y*scale, (float)(w + offsets[i][1])*scale,
-									0, 1, 0,
-									(h + offsets[i][0]) / (float)height, (w + offsets[i][1]) / (float)width } );
+			for (int i = 0; i < 4; i++)
+			{
+				float h = heightAt(x + offsets[i][0], y + offsets[i][1]);
+				vertices.push_back(Vertex{ (float)(x + offsets[i][0]), h, (float)(y + offsets[i][1]),
+									normal.x, normal.y, normal.z,
+									(x + offsets[i][0]) / (float)height, (y + offsets[i][1]) / (float)width } );
 			}
 			}
 		}
 		}
 	}
 	}
@@ -48,32 +75,60 @@ HeightMap::HeightMap(const std::string &file)
 
 
 HeightMap::~HeightMap()
 HeightMap::~HeightMap()
 {
 {
+	glDeleteTextures(1, &imageIndex);
 }
 }
 
 
 void HeightMap::Draw()
 void HeightMap::Draw()
 {
 {
+	glEnable(GL_LIGHTING);
+	float color[] = { 0.7f, 0.7f, 0.7f, 1 };
+	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
+	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
+
 	glEnable(GL_TEXTURE_2D);
 	glEnable(GL_TEXTURE_2D);
 	glBindTexture(GL_TEXTURE_2D, imageIndex);
 	glBindTexture(GL_TEXTURE_2D, imageIndex);
 
 
 	glEnableClientState(GL_VERTEX_ARRAY);
 	glEnableClientState(GL_VERTEX_ARRAY);
 	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 	//glEnableClientState(GL_COLOR_ARRAY);
 	//glEnableClientState(GL_COLOR_ARRAY);
-	//glEnableClientState(GL_NORMAL_ARRAY);
+	glEnableClientState(GL_NORMAL_ARRAY);
 
 
 	glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 0);
 	glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 0);
 	glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 6);
 	glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 6);
-	//glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)cubeVertices.data()) + 3);
+	glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 3);
 	glDrawArrays(GL_QUADS, 0, vertices.size());
 	glDrawArrays(GL_QUADS, 0, vertices.size());
 
 
 	glDisableClientState(GL_VERTEX_ARRAY);
 	glDisableClientState(GL_VERTEX_ARRAY);
 	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 	//glDisableClientState(GL_COLOR_ARRAY);
 	//glDisableClientState(GL_COLOR_ARRAY);
-	//glDisableClientState(GL_NORMAL_ARRAY);
+	glDisableClientState(GL_NORMAL_ARRAY);
 }
 }
 
 
-void HeightMap::GetHeigth(float x, float z)
+float HeightMap::GetHeight(float x, float y)
 {
 {
+	int ix = x;
+	int iy = y;
+
+	int index = (ix + (width - 1) * iy) * 4;
+
+	if (index + 3 >= vertices.size())
+		index = vertices.size() - 4;
+
+	if (index < 0)
+		index = 0;
+
+	Vertex& a = vertices[index];
+	Vertex& b = vertices[index+1];
+	Vertex& c = vertices[index+3];
+
+	float lowervalue = ((b.z - c.z)*(a.x - c.x) + (c.x - b.x)*(a.z - c.z));
+	float labda1 = ((b.z - c.z)*(x - c.x) + (c.x - b.x)*(y - c.z)) / lowervalue;
+	float labda2 = ((c.y - a.y)*(x - c.x) + (a.x - c.x)*(y - c.y)) / lowervalue;
+	float labda3 = 1 - labda1 - labda2;
+
+	Vertex z = a * labda1 + b * labda2 + c * labda3;
 
 
+	return z.y;
 }
 }
 
 
 void HeightMap::SetTexture(const std::string &file)
 void HeightMap::SetTexture(const std::string &file)

+ 4 - 3
HeightMap.h

@@ -5,6 +5,8 @@
 #include <vector>
 #include <vector>
 #include <GL/freeglut.h>
 #include <GL/freeglut.h>
 
 
+class World;
+
 class HeightMap
 class HeightMap
 {
 {
 private:
 private:
@@ -12,13 +14,12 @@ private:
 	int width;
 	int width;
 
 
 	GLuint imageIndex;
 	GLuint imageIndex;
-	int scale = 1;
 public:
 public:
-	HeightMap(const std::string &file);
+	HeightMap(const std::string &file, World* world);
 	~HeightMap();
 	~HeightMap();
 
 
 	void Draw();
 	void Draw();
-	void GetHeigth(float x, float z);
+	float GetHeight(float x, float y);
 	void SetTexture(const std::string &file);
 	void SetTexture(const std::string &file);
 
 
 	std::vector<Vertex> vertices;
 	std::vector<Vertex> vertices;

+ 8 - 6
Main.cpp

@@ -1,18 +1,18 @@
 #include <GL/freeglut.h>
 #include <GL/freeglut.h>
 
 
-#include "CrystalJohan.h"
+#include "CrystalPoint.h"
 #include <stdio.h>
 #include <stdio.h>
 #include "Vector.h"
 #include "Vector.h"
 
 
 void configureOpenGL(void);
 void configureOpenGL(void);
 
 
-CrystalJohan* app;
+CrystalPoint* app;
 
 
 bool justMoved = false;
 bool justMoved = false;
 
 
 int main(int argc, char* argv[])
 int main(int argc, char* argv[])
 {
 {
-	app = new CrystalJohan();
+	app = new CrystalPoint();
 	glutInit(&argc, argv);
 	glutInit(&argc, argv);
 
 
 	configureOpenGL();
 	configureOpenGL();
@@ -26,6 +26,8 @@ int main(int argc, char* argv[])
 	//Keyboard
 	//Keyboard
 	glutKeyboardFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = true; });
 	glutKeyboardFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = true; });
 	glutKeyboardUpFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = false; });
 	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
 	//Mouse
 	glutPassiveMotionFunc([](int x, int y)
 	glutPassiveMotionFunc([](int x, int y)
@@ -55,7 +57,7 @@ void configureOpenGL()
 	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
 	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
 	glutInitWindowSize(800, 600);
 	glutInitWindowSize(800, 600);
 	glutCreateWindow("Crystal Point");
 	glutCreateWindow("Crystal Point");
-	glutFullScreen();
+	//glutFullScreen();
 
 
 	//Depth testing
 	//Depth testing
 	glEnable(GL_DEPTH_TEST);
 	glEnable(GL_DEPTH_TEST);
@@ -81,8 +83,8 @@ void configureOpenGL()
 	//glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
 	//glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
 	//glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
 	//glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
 
 
-	//glEnable(GL_LIGHTING);
-	//glEnable(GL_LIGHT0);
+	glEnable(GL_LIGHTING);
+	glEnable(GL_LIGHT0);
 
 
 	glutSetCursor(GLUT_CURSOR_CROSSHAIR);
 	glutSetCursor(GLUT_CURSOR_CROSSHAIR);
 }
 }

+ 14 - 12
Model.cpp

@@ -158,12 +158,11 @@ void Model::Optimise(ObjGroup *t)
 	}
 	}
 }
 }
 
 
-Model::~Model(void)
-{
-}
-
 void Model::draw()
 void Model::draw()
 {
 {
+	glEnableClientState(GL_VERTEX_ARRAY);
+	glEnableClientState(GL_NORMAL_ARRAY);
+	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 	for (auto &g : groups)
 	for (auto &g : groups)
 	{
 	{
 		if (materials[g->materialIndex]->hasTexture)
 		if (materials[g->materialIndex]->hasTexture)
@@ -194,19 +193,14 @@ void Model::draw()
 			}
 			}
 		}
 		}
 
 
-		glEnableClientState(GL_VERTEX_ARRAY);
-		glEnableClientState(GL_NORMAL_ARRAY);
-		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-
 		glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 0);
 		glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 0);
 		glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 3);
 		glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 3);
 		glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 6);
 		glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), ((float*)g->VertexArray.data()) + 6);
 		glDrawArrays(GL_TRIANGLES, 0, g->VertexArray.size());
 		glDrawArrays(GL_TRIANGLES, 0, g->VertexArray.size());
-
-		glDisableClientState(GL_VERTEX_ARRAY);
-		glDisableClientState(GL_NORMAL_ARRAY);
-		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 	}
 	}
+	glDisableClientState(GL_VERTEX_ARRAY);
+	glDisableClientState(GL_NORMAL_ARRAY);
+	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 }
 }
 
 
 void Model::loadMaterialFile(std::string fileName, std::string dirName)
 void Model::loadMaterialFile(std::string fileName, std::string dirName)
@@ -381,3 +375,11 @@ void Model::unload(Model* model)
 		}
 		}
 	}
 	}
 }
 }
+
+Model::~Model(void)
+{
+	for (auto m : cache)
+	{
+		delete m.second.first;
+	}
+}

+ 24 - 0
Player.cpp

@@ -3,11 +3,35 @@
 #include "Player.h"
 #include "Player.h"
 #include <GL/freeglut.h>
 #include <GL/freeglut.h>
 
 
+Player* Player::instance = NULL;
+
 Player::Player()
 Player::Player()
 {
 {
 	speed = 10;
 	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()
 void Player::setCamera()
 {
 {
 	glRotatef(rotation.x, 1, 0, 0);
 	glRotatef(rotation.x, 1, 0, 0);

+ 7 - 4
Player.h

@@ -1,19 +1,22 @@
 #pragma once
 #pragma once
-
-#include "Singleton.h"
-
 #include "Vector.h"
 #include "Vector.h"
 
 
 class Model;
 class Model;
 
 
-class Player : public Singleton<Player>
+class Player
 {
 {
+private:
+	static Player* instance;
 public:
 public:
 	Player();
 	Player();
+	~Player();
 
 
 	void setCamera();
 	void setCamera();
 	void setPosition(float angle, float fac, bool height);
 	void setPosition(float angle, float fac, bool height);
 
 
+	static Player* getInstance(void);
+	static void init(void);
+
 	Vec3f position;
 	Vec3f position;
 	Vec2f rotation;
 	Vec2f rotation;
 
 

+ 7 - 7
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
 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.
     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
     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
     information about the platforms, configurations, and project features selected with the
     Application Wizard.
     Application Wizard.
 
 
-CrystalJohan.vcxproj.filters
+CrystalPoint.vcxproj.filters
     This is the filters file for VC++ projects generated using an Application Wizard. 
     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 
     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
     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
     similar extensions under a specific node (for e.g. ".cpp" files are associated with the
     "Source Files" filter).
     "Source Files" filter).
 
 
-CrystalJohan.cpp
+CrystalPoint.cpp
     This is the main application source file.
     This is the main application source file.
 
 
 /////////////////////////////////////////////////////////////////////////////
 /////////////////////////////////////////////////////////////////////////////
@@ -29,7 +29,7 @@ Other standard files:
 
 
 StdAfx.h, StdAfx.cpp
 StdAfx.h, StdAfx.cpp
     These files are used to build a precompiled header (PCH) file
     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:
 Other notes:

+ 0 - 13
Singleton.h

@@ -1,13 +0,0 @@
-#pragma once
-
-
-template <class T>
-class Singleton
-{
-public:
-	static T& getInstance()
-	{
-		static T* t = new T();
-		return *t;
-	}
-};

+ 14 - 0
Vector.cpp

@@ -80,6 +80,20 @@ bool Vec3f::operator!=(const Vec3f & other)
 	return x != other.x & y != other.y & z != other.z;
 	return x != other.x & y != other.y & z != other.z;
 }
 }
 
 
+Vec3f Vec3f::operator*(const float & other)
+{
+	return Vec3f(x*other, y*other, z*other);
+}
+
+Vec3f Vec3f::cross(const Vec3f & other)
+{
+	return Vec3f(
+		y*other.z - other.y*z,
+		z*other.x - other.z*x,
+		x*other.y - other.x*y
+		);
+}
+
 
 
 
 
 Vec2f::Vec2f(float x, float y)
 Vec2f::Vec2f(float x, float y)

+ 4 - 0
Vector.h

@@ -23,6 +23,10 @@ public:
 	Vec3f operator / (float value);
 	Vec3f operator / (float value);
 	bool operator ==(const Vec3f &other);
 	bool operator ==(const Vec3f &other);
 	bool operator !=(const Vec3f &other);
 	bool operator !=(const Vec3f &other);
+	Vec3f operator *(const float &other);
+
+	Vec3f cross(const Vec3f &other);
+
 };
 };
 
 
 class Vec2f
 class Vec2f

+ 20 - 0
Vertex.cpp

@@ -17,3 +17,23 @@ Vertex::Vertex(float x, float y, float z, float nx, float ny, float nz, float tx
 Vertex::~Vertex()
 Vertex::~Vertex()
 {
 {
 }
 }
+
+Vertex Vertex::operator/(float &other)
+{
+	return Vertex(x / other, y / other, z / other, normalX, normalY, normalZ, texX, texY);
+}
+
+Vertex Vertex::operator*(Vertex & other)
+{
+	return Vertex(x*other.x, y*other.y, z*other.z, normalX, normalY, normalZ, texX, texY);
+}
+
+Vertex Vertex::operator*(float & other)
+{
+	return Vertex(x*other, y*other, z*other, normalX, normalY, normalZ, texX, texY);
+}
+
+Vertex Vertex::operator+(Vertex & other)
+{
+	return Vertex(x+other.x, y+other.y, z+other.z, normalX, normalY, normalZ, texX, texY);
+}

+ 5 - 0
Vertex.h

@@ -15,5 +15,10 @@ public:
 
 
 	float texX;
 	float texX;
 	float texY;
 	float texY;
+
+	Vertex operator/(float &other);
+	Vertex operator*(Vertex &other);
+	Vertex operator*(float &other);
+	Vertex operator+(Vertex &other);
 };
 };
 
 

+ 83 - 56
World.cpp

@@ -1,27 +1,46 @@
 #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 <fstream>
 #include <fstream>
 #include <iostream>
 #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())
 	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"])
+	{
+		objecttemplates.push_back(std::pair<int, std::string>(objt["color"], objt["file"]));
+	}
+
+	heightmap = new HeightMap(v["world"]["heightmap"].asString(), this);
+
+	if(!v["world"]["texture"].isNull())
+		heightmap->SetTexture(v["world"]["texture"].asString());
 
 
-	player.position.x = v["player"]["startposition"][0];
-	player.position.y = v["player"]["startposition"][1];
-	player.position.z = v["player"]["startposition"][2];
+	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"])
 	for (auto object : v["objects"])
@@ -32,68 +51,68 @@ World::World() : player(Player::getInstance())
 
 
 		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());
 
 
 		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]);
+		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));
 		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 = "";
 		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 (!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));
-			}
+		Vec3f rotation(0, 0, 0);
+		if (!e["rot"].isNull())
+			rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
+
+		float scale = 1.0f;
+		if (!e["scale"].isNull())
+			scale = e["scale"].asFloat();
+
+		enemies.push_back(new Enemy(fileName, position, rotation, scale));
 
 
-		}
-		
 	}
 	}
 }
 }
 
 
 
 
 World::~World()
 World::~World()
 {
 {
+	delete heightmap;
+}
+
+std::string 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);
@@ -108,9 +127,6 @@ void World::draw()
 
 
 	for (auto &entity : entities)
 	for (auto &entity : entities)
 		entity->draw();
 		entity->draw();
-
-	
-
 }
 }
 
 
 void World::update(float elapsedTime)
 void World::update(float elapsedTime)
@@ -120,11 +136,13 @@ void World::update(float elapsedTime)
 
 
 	for (auto &enemy : enemies)
 	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->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
 		else
 			enemy->hasTarget = false;
 			enemy->hasTarget = false;
@@ -137,19 +155,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;

+ 8 - 4
World.h

@@ -4,25 +4,29 @@
 #include "HeightMap.h"
 #include "HeightMap.h"
 #include "Player.h"
 #include "Player.h"
 #include "Enemy.h"
 #include "Enemy.h"
+#include "LevelObject.h"
 
 
 class Entity;
 class Entity;
 
 
 class World
 class World
 {
 {
+private:
+	std::vector<std::pair<int, std::string>> objecttemplates;
 public:
 public:
-	World();
+	World(const std::string &fileName);
 	~World();
 	~World();
 
 
-
-	Player& player;
 	std::vector<Entity*> entities;
 	std::vector<Entity*> entities;
-
 	std::vector<Enemy*> enemies;
 	std::vector<Enemy*> enemies;
 
 
+	Player* player;
 	HeightMap* heightmap;
 	HeightMap* heightmap;
 
 
 	void draw();
 	void draw();
 	void update(float elapsedTime);
 	void update(float elapsedTime);
 	bool isPlayerPositionValid();
 	bool isPlayerPositionValid();
+	float getHeight(float x, float y);
+	void addLevelObject(LevelObject* obj);
+	std::string getObjectFromValue(int i);
 };
 };
 
 

+ 137 - 0
WorldHandler.cpp

@@ -0,0 +1,137 @@
+#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();
+}
+
+float WorldHandler::getHeight(float x, float y)
+{
+	if (!loadingWorld)
+		return world->getHeight(x, y);
+	else
+		return 0.0f;
+}
+
+
+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);
+	}
+}

+ 36 - 0
WorldHandler.h

@@ -0,0 +1,36 @@
+#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);
+	float getHeight(float x, float y);
+
+	void Navigate(const std::string &fileName);
+	void NextWorld();
+	void PreviousWorld();
+
+	std::vector<std::string> worldfiles;
+};
+

+ 36 - 0
worlds/fire.json

@@ -0,0 +1,36 @@
+{
+   "world": {
+    "heightmap": "worlds/hell.png",
+    "texture": "worlds/helltexture.png",
+	    "object-templates": [
+			  {
+				  "color": 25,
+				  "file": "models/boom/Boom.obj"
+			  },
+			  {
+				  "color": 23,
+				  "file": "models/boom/Boom.obj"
+			  }
+	    ]
+   },
+  "player": {
+    "startposition": [ 200, 40, 200 ]
+  },
+  "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
+    }
+  ]
+}

File diff suppressed because it is too large
+ 0 - 0
worlds/hell.pdn


+ 26 - 0
worlds/ice.json

@@ -0,0 +1,26 @@
+{
+   "world": {
+    "heightmap": "worlds/hmcs.png",
+    "texture": "worlds/hmcstexture.png",
+   },
+  "player": {
+    "startposition": [ 100, 20, 100 ]
+  },
+  "objects": [
+    {
+      "file": "models/boom/Boom.obj",
+      "pos": [ 4, 0, -4 ]
+    },
+    {
+      "file": "models/Teleporter/Teleporter.obj",
+      "pos": [ 0, 0, -4 ]
+    }
+  ],
+  "enemies": [
+    {
+      "file": "models/squid/Blooper.obj",
+      "pos": [ 1, 2, -10 ],
+      "scale": 0.01
+    }
+  ]
+}

+ 16 - 0
worlds/small.json

@@ -0,0 +1,16 @@
+{
+   "world": {
+    "heightmap": "worlds/small.png",
+	"object-templates": [
+		{
+			"color":100,
+			"file": "models/boom/Boom.obj"
+		}
+	]
+   },
+  "player": {
+    "startposition": [ 20, 5, 20 ]
+  },
+  "objects": [],
+  "enemies": []
+}

BIN
worlds/small.png


+ 0 - 33
worlds/world1.json

@@ -1,33 +0,0 @@
-{
-   "world": {
-      "heightmap": "worlds/hmcs.png",
-	  "texture": "worlds/hmcstexture.png"
-   },
-  "player": {
-    "startposition": [ -100, 1.7, -100 ]
-  },
-  "objects": [
-    {
-      "file": "models/boom/Boom.obj",
-      "pos": [ 4, 0, -4 ]
-    },
-    {
-      "file": "models/Teleporter/Teleporter.obj",
-      "pos": [ 0, 0, -4 ],
-      "rot": [ 0, 0, 0 ]
-    }
-  ],
-  "enemy_models": [
-    {
-      "id": 0,
-      "file": "models/squid/Blooper.obj"
-    }
-  ],
-  "enemy_data": [
-    {
-      "id": 0,
-      "pos": [ 1, 2, -10 ],
-      "scale": 0.01
-    }   
-  ]
-}

+ 7 - 0
worlds/worlds.json

@@ -0,0 +1,7 @@
+{
+	"worlds": [
+			"worlds/small.json",
+			"worlds/ice.json",
+			"worlds/fire.json"
+	]
+}

Some files were not shown because too many files changed in this diff