Merge pull request #9 from CrystalPointA4/feature/interface
Feature/interface
This commit is contained in:
+6
-16
@@ -6,10 +6,14 @@
|
|||||||
#include "WorldHandler.h"
|
#include "WorldHandler.h"
|
||||||
#include "Player.h"
|
#include "Player.h"
|
||||||
|
|
||||||
|
int CrystalPoint::width = 0;
|
||||||
|
int CrystalPoint::height = 0;
|
||||||
|
|
||||||
void CrystalPoint::init()
|
void CrystalPoint::init()
|
||||||
{
|
{
|
||||||
player = Player::getInstance();
|
player = Player::getInstance();
|
||||||
worldhandler = WorldHandler::getInstance();
|
worldhandler = WorldHandler::getInstance();
|
||||||
|
//cursor = Cursor::getInstance();
|
||||||
|
|
||||||
lastFrameTime = 0;
|
lastFrameTime = 0;
|
||||||
|
|
||||||
@@ -34,23 +38,8 @@ void CrystalPoint::draw()
|
|||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
|
||||||
worldhandler->draw();
|
worldhandler->draw();
|
||||||
|
|
||||||
//Draw Cursor
|
|
||||||
glMatrixMode(GL_PROJECTION);
|
|
||||||
glLoadIdentity();
|
|
||||||
glOrtho(0,width, height,0,-10,10);
|
|
||||||
glMatrixMode(GL_MODELVIEW);
|
|
||||||
glLoadIdentity();
|
|
||||||
|
|
||||||
glDisable(GL_LIGHTING);
|
//cursor->draw();
|
||||||
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();
|
glutSwapBuffers();
|
||||||
}
|
}
|
||||||
@@ -96,6 +85,7 @@ void CrystalPoint::update()
|
|||||||
worldhandler->update(deltaTime);
|
worldhandler->update(deltaTime);
|
||||||
|
|
||||||
mousePosition = mousePosition + mouseOffset;
|
mousePosition = mousePosition + mouseOffset;
|
||||||
|
//cursor->update(mousePosition);
|
||||||
|
|
||||||
mouseOffset = Vec2f(0, 0);
|
mouseOffset = Vec2f(0, 0);
|
||||||
prevKeyboardState = keyboardState;
|
prevKeyboardState = keyboardState;
|
||||||
|
|||||||
+1
-2
@@ -24,12 +24,11 @@ public:
|
|||||||
WorldHandler* worldhandler;
|
WorldHandler* worldhandler;
|
||||||
Player* player;
|
Player* player;
|
||||||
|
|
||||||
int width, height;
|
static int width, height;
|
||||||
KeyboardState keyboardState;
|
KeyboardState keyboardState;
|
||||||
KeyboardState prevKeyboardState;
|
KeyboardState prevKeyboardState;
|
||||||
|
|
||||||
Vec2f mouseOffset;
|
Vec2f mouseOffset;
|
||||||
|
|
||||||
Vec2f mousePosition;
|
Vec2f mousePosition;
|
||||||
|
|
||||||
float lastFrameTime;
|
float lastFrameTime;
|
||||||
|
|||||||
@@ -152,9 +152,11 @@
|
|||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="CrystalPoint.cpp" />
|
<ClCompile Include="CrystalPoint.cpp" />
|
||||||
|
<ClCompile Include="Cursor.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" />
|
||||||
|
<ClCompile Include="Interface.cpp" />
|
||||||
<ClCompile Include="json.cpp" />
|
<ClCompile Include="json.cpp" />
|
||||||
<ClCompile Include="LevelObject.cpp" />
|
<ClCompile Include="LevelObject.cpp" />
|
||||||
<ClCompile Include="Main.cpp" />
|
<ClCompile Include="Main.cpp" />
|
||||||
@@ -167,9 +169,11 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="CrystalPoint.h" />
|
<ClInclude Include="CrystalPoint.h" />
|
||||||
|
<ClInclude Include="Cursor.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" />
|
||||||
|
<ClInclude Include="Interface.h" />
|
||||||
<ClInclude Include="json.h" />
|
<ClInclude Include="json.h" />
|
||||||
<ClInclude Include="LevelObject.h" />
|
<ClInclude Include="LevelObject.h" />
|
||||||
<ClInclude Include="Main.h" />
|
<ClInclude Include="Main.h" />
|
||||||
|
|||||||
@@ -63,6 +63,12 @@
|
|||||||
<ClCompile Include="CrystalPoint.cpp">
|
<ClCompile Include="CrystalPoint.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Cursor.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
|
<ClCompile Include="Interface.cpp">
|
||||||
|
<Filter>Source Files\World</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="World.h">
|
<ClInclude Include="World.h">
|
||||||
@@ -77,9 +83,6 @@
|
|||||||
<ClInclude Include="LevelObject.h">
|
<ClInclude Include="LevelObject.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="vector.h">
|
|
||||||
<Filter>Header Files</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="Model.h">
|
<ClInclude Include="Model.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
@@ -107,6 +110,15 @@
|
|||||||
<ClInclude Include="CrystalPoint.h">
|
<ClInclude Include="CrystalPoint.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Interface.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Cursor.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="vector.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="worlds\worlds.json">
|
<None Include="worlds\worlds.json">
|
||||||
|
|||||||
+58
@@ -0,0 +1,58 @@
|
|||||||
|
#include "Cursor.h"
|
||||||
|
#include <GL\freeglut.h>
|
||||||
|
#include <cmath>
|
||||||
|
#include "CrystalPoint.h"
|
||||||
|
|
||||||
|
Cursor* Cursor::instance = NULL;
|
||||||
|
|
||||||
|
Cursor::Cursor()
|
||||||
|
{
|
||||||
|
enabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Cursor::~Cursor()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Cursor* Cursor::getInstance(void)
|
||||||
|
{
|
||||||
|
if (instance == nullptr)
|
||||||
|
instance = new Cursor();
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cursor::enable(bool enable)
|
||||||
|
{
|
||||||
|
enabled = enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Cursor::isEnabled(void)
|
||||||
|
{
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cursor::draw(void)
|
||||||
|
{
|
||||||
|
//Draw Cursor
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glLoadIdentity();
|
||||||
|
glOrtho(0, CrystalPoint::width, CrystalPoint::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();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cursor::update(Vec2f newPosition)
|
||||||
|
{
|
||||||
|
mousePosition = newPosition;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "Vector.h"
|
||||||
|
|
||||||
|
class Cursor
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
Cursor();
|
||||||
|
|
||||||
|
static Cursor* instance;
|
||||||
|
bool enabled;
|
||||||
|
Vec2f mousePosition;
|
||||||
|
public:
|
||||||
|
|
||||||
|
~Cursor();
|
||||||
|
|
||||||
|
static Cursor* getInstance(void);
|
||||||
|
|
||||||
|
void enable(bool enable);
|
||||||
|
bool isEnabled(void);
|
||||||
|
|
||||||
|
void draw(void);
|
||||||
|
void update(Vec2f newPosition);
|
||||||
|
};
|
||||||
|
|
||||||
+1
-1
@@ -39,7 +39,7 @@ HeightMap::HeightMap(const std::string &file, World* world)
|
|||||||
|
|
||||||
if (valueAt(x, y, GREEN) > 0)
|
if (valueAt(x, y, GREEN) > 0)
|
||||||
{
|
{
|
||||||
world->addLevelObject(new LevelObject(world->getObjectFromValue(valueAt(x, y, GREEN)), Vec3f(x, heightAt(x, y), y), Vec3f(0, rand()%360, 0), 1, true));
|
world->addLevelObject(new LevelObject(world->getObjectFromValue(valueAt(x, y, GREEN)).first, Vec3f(x, heightAt(x, y), y), Vec3f(0, rand()%360, 0), 1, world->getObjectFromValue(valueAt(x, y, GREEN)).second));
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec3f normal = ca.cross(ba);
|
Vec3f normal = ca.cross(ba);
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
#include "Interface.h"
|
||||||
|
#include <GL\freeglut.h>
|
||||||
|
#include "CrystalPoint.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "Player.h"
|
||||||
|
|
||||||
|
//Prototype
|
||||||
|
void glutBitmapString(std::string str, int x, int y);
|
||||||
|
|
||||||
|
Interface::Interface()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Interface::~Interface()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Interface::draw()
|
||||||
|
{
|
||||||
|
Player* player = Player::getInstance();
|
||||||
|
|
||||||
|
//Switch view to Ortho
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glLoadIdentity();
|
||||||
|
glOrtho(0, 1000, 1000, 0, -10, 10);
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glLoadIdentity();
|
||||||
|
|
||||||
|
glDisable(GL_LIGHTING);
|
||||||
|
glDisable(GL_DEPTH_TEST);
|
||||||
|
glDisable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
//Draw interface
|
||||||
|
|
||||||
|
//Health bar
|
||||||
|
glBegin(GL_QUADS);
|
||||||
|
glColor4f(0,0,0, 1.0);
|
||||||
|
glVertex2f(250, 980);
|
||||||
|
glVertex2f(250, 965);
|
||||||
|
glVertex2f(750, 965);
|
||||||
|
glVertex2f(750, 980);
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
glBegin(GL_QUADS);
|
||||||
|
glColor4f(1.0f, 0.1f, 0.1f, 1.0);
|
||||||
|
glVertex2f(250, 980);
|
||||||
|
glVertex2f(250, 965);
|
||||||
|
|
||||||
|
glColor4f(1.0f, 0.5f, 0.5f, 1.0);
|
||||||
|
glVertex2f(250 + (player->health / 100 * 500), 965);
|
||||||
|
glVertex2f(250 + (player->health / 100 * 500), 980);
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
//XP bar
|
||||||
|
glBegin(GL_QUADS);
|
||||||
|
glColor4f(0, 0, 0, 1.0);
|
||||||
|
glVertex2f(250, 950);
|
||||||
|
glVertex2f(250, 935);
|
||||||
|
glVertex2f(750, 935);
|
||||||
|
glVertex2f(750, 950);
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
glBegin(GL_QUADS);
|
||||||
|
glColor4f(1.0f, 1.0f, 0.1f, 1.0);
|
||||||
|
glVertex2f(250, 950);
|
||||||
|
glVertex2f(250, 935);
|
||||||
|
|
||||||
|
glColor4f(1.0f, 1.0f, 0.5f, 1.0);
|
||||||
|
glVertex2f(250 + (player->xp / 100 * 500), 935);
|
||||||
|
glVertex2f(250 + (player->xp / 100 * 500), 950);
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
//Text: level
|
||||||
|
glColor4f(1.0f, 1.0f, 0.1f, 1.0);
|
||||||
|
glutBitmapString("Level: " + std::to_string(player->level), 490, 900);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void Interface::update(float deltaTime)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void glutBitmapString(std::string str, int x, int y)
|
||||||
|
{
|
||||||
|
glRasterPos2f(x, y);
|
||||||
|
for (int i = 0; i < str.size(); i++)
|
||||||
|
{
|
||||||
|
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, str[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
class Interface
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Interface();
|
||||||
|
~Interface();
|
||||||
|
|
||||||
|
void draw(void);
|
||||||
|
void update(float deltaTime);
|
||||||
|
};
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
glutDisplayFunc([]() { app->draw(); } );
|
glutDisplayFunc([]() { app->draw(); } );
|
||||||
glutIdleFunc([]() { app->update(); } );
|
glutIdleFunc([]() { app->update(); } );
|
||||||
glutReshapeFunc([](int w, int h) { app->width = w; app->height = h; glViewport(0, 0, w, h); });
|
glutReshapeFunc([](int w, int h) { CrystalPoint::width = w; CrystalPoint::height = h; glViewport(0, 0, w, h); });
|
||||||
|
|
||||||
//Keyboard
|
//Keyboard
|
||||||
glutKeyboardFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = true; });
|
glutKeyboardFunc([](unsigned char c, int, int) { app->keyboardState.keys[c] = true; });
|
||||||
@@ -29,8 +29,7 @@ int main(int argc, char* argv[])
|
|||||||
glutSpecialFunc([](int c, int, int) { app->keyboardState.special[c] = true; });
|
glutSpecialFunc([](int c, int, int) { app->keyboardState.special[c] = true; });
|
||||||
glutSpecialUpFunc([](int c, int, int) { app->keyboardState.special[c] = false; });
|
glutSpecialUpFunc([](int c, int, int) { app->keyboardState.special[c] = false; });
|
||||||
|
|
||||||
//Mouse
|
auto mousemotion = [](int x, int y)
|
||||||
glutPassiveMotionFunc([](int x, int y)
|
|
||||||
{
|
{
|
||||||
if (justMoved)
|
if (justMoved)
|
||||||
{
|
{
|
||||||
@@ -41,11 +40,18 @@ int main(int argc, char* argv[])
|
|||||||
int dy = y - app->height / 2;
|
int dy = y - app->height / 2;
|
||||||
if ((dx != 0 || dy != 0) && abs(dx) < 400 && abs(dy) < 400)
|
if ((dx != 0 || dy != 0) && abs(dx) < 400 && abs(dy) < 400)
|
||||||
{
|
{
|
||||||
app->mouseOffset = app->mouseOffset + Vec2f(dx,dy);
|
app->mouseOffset = app->mouseOffset + Vec2f(dx, dy);
|
||||||
glutWarpPointer(app->width / 2, app->height / 2);
|
glutWarpPointer(app->width / 2, app->height / 2);
|
||||||
justMoved = true;
|
justMoved = true;
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
|
//Mouse
|
||||||
|
glutPassiveMotionFunc(mousemotion);
|
||||||
|
glutMotionFunc(mousemotion);
|
||||||
|
|
||||||
|
CrystalPoint::height = GLUT_WINDOW_HEIGHT;
|
||||||
|
CrystalPoint::width = GLUT_WINDOW_WIDTH;
|
||||||
|
|
||||||
glutMainLoop();
|
glutMainLoop();
|
||||||
return 0;
|
return 0;
|
||||||
@@ -57,7 +63,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);
|
||||||
@@ -86,5 +92,5 @@ void configureOpenGL()
|
|||||||
glEnable(GL_LIGHTING);
|
glEnable(GL_LIGHTING);
|
||||||
glEnable(GL_LIGHT0);
|
glEnable(GL_LIGHT0);
|
||||||
|
|
||||||
glutSetCursor(GLUT_CURSOR_CROSSHAIR);
|
glutSetCursor(GLUT_CURSOR_NONE);
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,9 @@ Player* Player::instance = NULL;
|
|||||||
Player::Player()
|
Player::Player()
|
||||||
{
|
{
|
||||||
speed = 10;
|
speed = 10;
|
||||||
|
health = 50;
|
||||||
|
xp = 75;
|
||||||
|
level = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
Player* Player::getInstance()
|
Player* Player::getInstance()
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ public:
|
|||||||
Model* leftWeapon;
|
Model* leftWeapon;
|
||||||
Model* rightWeapon;
|
Model* rightWeapon;
|
||||||
|
|
||||||
|
float health;
|
||||||
|
float xp;
|
||||||
|
int level;
|
||||||
|
|
||||||
float speed;
|
float speed;
|
||||||
};
|
};
|
||||||
@@ -3,13 +3,19 @@
|
|||||||
#include "Entity.h"
|
#include "Entity.h"
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
#include "Model.h"
|
#include "Model.h"
|
||||||
|
#include "CrystalPoint.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
World::World(const std::string &fileName)
|
World::World(const std::string &fileName)
|
||||||
{
|
{
|
||||||
|
//Store player instance
|
||||||
player = Player::getInstance();
|
player = Player::getInstance();
|
||||||
|
|
||||||
|
//Create the interface
|
||||||
|
interface = new Interface();
|
||||||
|
|
||||||
|
//Open world json file
|
||||||
std::ifstream file(fileName);
|
std::ifstream file(fileName);
|
||||||
if(!file.is_open())
|
if(!file.is_open())
|
||||||
std::cout<<"Error, can't open world file - " << fileName << "\n";
|
std::cout<<"Error, can't open world file - " << fileName << "\n";
|
||||||
@@ -30,60 +36,81 @@ World::World(const std::string &fileName)
|
|||||||
//Load object templates
|
//Load object templates
|
||||||
for (auto objt : v["world"]["object-templates"])
|
for (auto objt : v["world"]["object-templates"])
|
||||||
{
|
{
|
||||||
objecttemplates.push_back(std::pair<int, std::string>(objt["color"], objt["file"]));
|
//collision
|
||||||
|
bool cancollide = true;
|
||||||
|
if (!objt["collision"].isNull())
|
||||||
|
cancollide = objt["collision"].asBool();
|
||||||
|
|
||||||
|
objecttemplates.push_back(std::pair<int, std::pair<std::string, bool>>(objt["color"], std::pair<std::string, bool>(objt["file"], cancollide)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Generate heightmap for this world
|
||||||
heightmap = new HeightMap(v["world"]["heightmap"].asString(), this);
|
heightmap = new HeightMap(v["world"]["heightmap"].asString(), this);
|
||||||
|
|
||||||
|
//Map different texture to heightmap if available
|
||||||
if(!v["world"]["texture"].isNull())
|
if(!v["world"]["texture"].isNull())
|
||||||
heightmap->SetTexture(v["world"]["texture"].asString());
|
heightmap->SetTexture(v["world"]["texture"].asString());
|
||||||
|
|
||||||
|
//Set player starting position
|
||||||
player->position.x = v["player"]["startposition"][0].asFloat();
|
player->position.x = v["player"]["startposition"][0].asFloat();
|
||||||
player->position.y = v["player"]["startposition"][1].asFloat();
|
player->position.y = v["player"]["startposition"][1].asFloat();
|
||||||
player->position.z = v["player"]["startposition"][2].asFloat();
|
player->position.z = v["player"]["startposition"][2].asFloat();
|
||||||
|
|
||||||
|
//Load and place objects into world
|
||||||
for (auto object : v["objects"])
|
for (auto object : v["objects"])
|
||||||
{
|
{
|
||||||
|
//Collision
|
||||||
bool hasCollision = true;
|
bool hasCollision = true;
|
||||||
if (!object["collide"].isNull())
|
if (!object["collide"].isNull())
|
||||||
hasCollision = object["collide"].asBool();
|
hasCollision = object["collide"].asBool();
|
||||||
|
|
||||||
|
//Rotation
|
||||||
Vec3f rotation(0, 0, 0);
|
Vec3f rotation(0, 0, 0);
|
||||||
if(!object["rot"].isNull())
|
if(!object["rot"].isNull())
|
||||||
rotation = Vec3f(object["rot"][0].asFloat(), object["rot"][1].asFloat(), object["rot"][2].asFloat());
|
rotation = Vec3f(object["rot"][0].asFloat(), object["rot"][1].asFloat(), object["rot"][2].asFloat());
|
||||||
|
|
||||||
|
//Scale
|
||||||
float scale = 1;
|
float scale = 1;
|
||||||
if (!object["scale"].isNull())
|
if (!object["scale"].isNull())
|
||||||
scale = object["scale"].asFloat();
|
scale = object["scale"].asFloat();
|
||||||
|
|
||||||
|
//Position
|
||||||
if (object["pos"].isNull())
|
if (object["pos"].isNull())
|
||||||
std::cout << "Invalid world file: objects pos - " << fileName << "\n";
|
std::cout << "Invalid world file: objects pos - " << fileName << "\n";
|
||||||
|
|
||||||
|
//File
|
||||||
|
if (object["file"].isNull())
|
||||||
|
std::cout << "Invalid world file: objects file - " << fileName << "\n";
|
||||||
|
|
||||||
|
//Create
|
||||||
Vec3f position(object["pos"][0].asFloat(), object["pos"][1].asFloat(), object["pos"][2].asFloat());
|
Vec3f position(object["pos"][0].asFloat(), object["pos"][1].asFloat(), object["pos"][2].asFloat());
|
||||||
entities.push_back(new LevelObject(object["file"], position, rotation, scale, hasCollision));
|
entities.push_back(new LevelObject(object["file"].asString(), position, rotation, scale, hasCollision));
|
||||||
}
|
}
|
||||||
|
|
||||||
//Enemies
|
//Load and place enemies into world
|
||||||
for (auto e : v["enemies"])
|
for (auto e : v["enemies"])
|
||||||
{
|
{
|
||||||
std::string fileName = "";
|
//Rotation
|
||||||
if (!e["file"].isNull())
|
|
||||||
fileName = e["file"].asString();
|
|
||||||
|
|
||||||
Vec3f position(0, 0, 0);
|
|
||||||
if (!e["pos"].isNull())
|
|
||||||
position = Vec3f(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
|
|
||||||
|
|
||||||
Vec3f rotation(0, 0, 0);
|
Vec3f rotation(0, 0, 0);
|
||||||
if (!e["rot"].isNull())
|
if (!e["rot"].isNull())
|
||||||
rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
|
rotation = Vec3f(e["rot"][0].asFloat(), e["rot"][1].asFloat(), e["rot"][2].asFloat());
|
||||||
|
|
||||||
|
//Scale
|
||||||
float scale = 1.0f;
|
float scale = 1.0f;
|
||||||
if (!e["scale"].isNull())
|
if (!e["scale"].isNull())
|
||||||
scale = e["scale"].asFloat();
|
scale = e["scale"].asFloat();
|
||||||
|
|
||||||
enemies.push_back(new Enemy(fileName, position, rotation, scale));
|
//Position
|
||||||
|
if (e["pos"].isNull())
|
||||||
|
std::cout << "Invalid world file: enemies pos - " << fileName << "\n";
|
||||||
|
|
||||||
|
//File
|
||||||
|
if (e["file"].isNull())
|
||||||
|
std::cout << "Invalid world file: enemies file - " << fileName << "\n";
|
||||||
|
|
||||||
|
//Create
|
||||||
|
Vec3f position(e["pos"][0].asFloat(), e["pos"][1].asFloat(), e["pos"][2].asFloat());
|
||||||
|
enemies.push_back(new Enemy(e["file"].asString(), position, rotation, scale));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,7 +121,7 @@ World::~World()
|
|||||||
delete heightmap;
|
delete heightmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string World::getObjectFromValue(int val)
|
std::pair<std::string, bool> World::getObjectFromValue(int val)
|
||||||
{
|
{
|
||||||
for (auto i : objecttemplates)
|
for (auto i : objecttemplates)
|
||||||
{
|
{
|
||||||
@@ -116,10 +143,9 @@ void World::draw()
|
|||||||
|
|
||||||
float lightPosition[4] = { 0, 2, 1, 0 };
|
float lightPosition[4] = { 0, 2, 1, 0 };
|
||||||
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
|
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
|
||||||
float lightAmbient[4] = { 0.5, 0.5, 0.5, 1 };
|
float lightAmbient[4] = { 0.2, 0.2, 0.2, 1 };
|
||||||
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
|
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
|
||||||
|
|
||||||
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
|
||||||
heightmap->Draw();
|
heightmap->Draw();
|
||||||
|
|
||||||
for (auto &enemy : enemies)
|
for (auto &enemy : enemies)
|
||||||
@@ -127,6 +153,8 @@ void World::draw()
|
|||||||
|
|
||||||
for (auto &entity : entities)
|
for (auto &entity : entities)
|
||||||
entity->draw();
|
entity->draw();
|
||||||
|
|
||||||
|
interface->draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::update(float elapsedTime)
|
void World::update(float elapsedTime)
|
||||||
|
|||||||
@@ -5,28 +5,30 @@
|
|||||||
#include "Player.h"
|
#include "Player.h"
|
||||||
#include "Enemy.h"
|
#include "Enemy.h"
|
||||||
#include "LevelObject.h"
|
#include "LevelObject.h"
|
||||||
|
#include "Interface.h"
|
||||||
|
|
||||||
class Entity;
|
class Entity;
|
||||||
|
|
||||||
class World
|
class World
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::vector<std::pair<int, std::string>> objecttemplates;
|
std::vector<std::pair<int, std::pair<std::string, bool>>> objecttemplates;
|
||||||
public:
|
|
||||||
World(const std::string &fileName);
|
|
||||||
~World();
|
|
||||||
|
|
||||||
std::vector<Entity*> entities;
|
|
||||||
std::vector<Enemy*> enemies;
|
|
||||||
|
|
||||||
Player* player;
|
Player* player;
|
||||||
HeightMap* heightmap;
|
HeightMap* heightmap;
|
||||||
|
Interface* interface;
|
||||||
|
|
||||||
|
std::vector<Entity*> entities;
|
||||||
|
std::vector<Enemy*> enemies;
|
||||||
|
public:
|
||||||
|
World(const std::string &fileName);
|
||||||
|
~World();
|
||||||
|
|
||||||
void draw();
|
void draw();
|
||||||
void update(float elapsedTime);
|
void update(float elapsedTime);
|
||||||
bool isPlayerPositionValid();
|
bool isPlayerPositionValid();
|
||||||
float getHeight(float x, float y);
|
float getHeight(float x, float y);
|
||||||
void addLevelObject(LevelObject* obj);
|
void addLevelObject(LevelObject* obj);
|
||||||
std::string getObjectFromValue(int i);
|
std::pair<std::string, bool> getObjectFromValue(int i);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 257 KiB After Width: | Height: | Size: 186 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 130 KiB After Width: | Height: | Size: 86 KiB |
@@ -2,6 +2,13 @@
|
|||||||
"world": {
|
"world": {
|
||||||
"heightmap": "worlds/hmcs.png",
|
"heightmap": "worlds/hmcs.png",
|
||||||
"texture": "worlds/hmcstexture.png",
|
"texture": "worlds/hmcstexture.png",
|
||||||
|
"object-templates": [
|
||||||
|
{
|
||||||
|
"color":100,
|
||||||
|
"file": "models/boom/Boom.obj",
|
||||||
|
"collision": false
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"player": {
|
"player": {
|
||||||
"startposition": [ 100, 20, 100 ]
|
"startposition": [ 100, 20, 100 ]
|
||||||
|
|||||||
+2
-1
@@ -4,7 +4,8 @@
|
|||||||
"object-templates": [
|
"object-templates": [
|
||||||
{
|
{
|
||||||
"color":100,
|
"color":100,
|
||||||
"file": "models/boom/Boom.obj"
|
"file": "models/boom/Boom.obj",
|
||||||
|
"collision": false
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 1.9 KiB |
Reference in New Issue
Block a user