Spawn objects from file

This commit is contained in:
2016-05-27 10:56:24 +02:00
parent 1c59a3bc6f
commit d83cc6403c
8 changed files with 64 additions and 6 deletions
+1
View File
@@ -184,6 +184,7 @@
<ItemGroup> <ItemGroup>
<None Include="worlds\fire.json" /> <None Include="worlds\fire.json" />
<None Include="worlds\ice.json" /> <None Include="worlds\ice.json" />
<None Include="worlds\small.json" />
<None Include="worlds\worlds.json" /> <None Include="worlds\worlds.json" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+3
View File
@@ -118,5 +118,8 @@
<None Include="worlds\ice.json"> <None Include="worlds\ice.json">
<Filter>Source Files\json</Filter> <Filter>Source Files\json</Filter>
</None> </None>
<None Include="worlds\small.json">
<Filter>Source Files\json</Filter>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>
+19 -2
View File
@@ -2,12 +2,19 @@
#include "stb_image.h" #include "stb_image.h"
#include "vector.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, float scale) HeightMap::HeightMap(const std::string &file, float scale, World* world)
{ {
this->scale = scale; this->scale = scale;
@@ -16,7 +23,12 @@ HeightMap::HeightMap(const std::string &file, float scale)
auto heightAt = [&](int x, int y) auto heightAt = [&](int x, int y)
{ {
return (imgData[(x + y * width) * 4] / 256.0f) * 100.0f; return (imgData[(x + y * width) * 4 ] / 256.0f) * 100.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 y = 0; y < height-1; y++)
@@ -27,6 +39,11 @@ HeightMap::HeightMap(const std::string &file, float scale)
Vec3f ca(0, heightAt(x, y + 1) - heightAt(x, y), 1); Vec3f ca(0, heightAt(x, y + 1) - heightAt(x, y), 1);
Vec3f ba(1, heightAt(x + 1, y) - heightAt(x, y), 0); Vec3f ba(1, heightAt(x + 1, y) - heightAt(x, y), 0);
if (valueAt(x, y, GREEN) > 0)
{
world->addLevelObject(new LevelObject(world->getObjectFromValue(valueAt(x, y, GREEN)), Vec3f(x*scale, heightAt(x, y), y*scale), Vec3f(0, rand()%360, 0), 1, true));
}
Vec3f normal = ca.cross(ba); Vec3f normal = ca.cross(ba);
normal.Normalize(); normal.Normalize();
+3 -1
View File
@@ -5,6 +5,8 @@
#include <vector> #include <vector>
#include <GL/freeglut.h> #include <GL/freeglut.h>
class World;
class HeightMap class HeightMap
{ {
private: private:
@@ -14,7 +16,7 @@ private:
GLuint imageIndex; GLuint imageIndex;
int scale; int scale;
public: public:
HeightMap(const std::string &file, float scale); HeightMap(const std::string &file, float scale, World* world);
~HeightMap(); ~HeightMap();
void Draw(); void Draw();
+26 -2
View File
@@ -1,7 +1,6 @@
#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 "Model.h"
#include <fstream> #include <fstream>
@@ -18,18 +17,27 @@ World::World(const std::string &fileName)
json::Value v = json::readJson(file); json::Value v = json::readJson(file);
file.close(); file.close();
//Check file
if(v["world"].isNull() || v["world"]["heightmap"].isNull()) if(v["world"].isNull() || v["world"]["heightmap"].isNull())
std::cout << "Invalid world file: world - " << fileName << "\n"; std::cout << "Invalid world file: world - " << fileName << "\n";
if (v["player"].isNull() || v["player"]["startposition"].isNull()) if (v["player"].isNull() || v["player"]["startposition"].isNull())
std::cout << "Invalid world file: player - " << fileName << "\n"; std::cout << "Invalid world file: player - " << fileName << "\n";
if (v["objects"].isNull()) if (v["objects"].isNull())
std::cout << "Invalid world file: objects - " << fileName << "\n"; std::cout << "Invalid world file: objects - " << fileName << "\n";
if (v["world"]["object-templates"].isNull())
std::cout << "Invalid world file: object templates - " << fileName << "\n";
float scale = 1.0f; float scale = 1.0f;
if (!v["world"]["scale"].isNull()) if (!v["world"]["scale"].isNull())
scale = v["world"]["scale"].asFloat(); scale = v["world"]["scale"].asFloat();
heightmap = new HeightMap(v["world"]["heightmap"].asString(), scale); //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(), scale, this);
if(!v["world"]["texture"].isNull()) if(!v["world"]["texture"].isNull())
heightmap->SetTexture(v["world"]["texture"].asString()); heightmap->SetTexture(v["world"]["texture"].asString());
@@ -90,6 +98,17 @@ World::~World()
delete heightmap; 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) float World::getHeight(float x, float y)
{ {
return heightmap->GetHeight(x, y); return heightmap->GetHeight(x, y);
@@ -152,6 +171,11 @@ void World::update(float elapsedTime)
} }
} }
void World::addLevelObject(LevelObject* obj)
{
entities.push_back(obj);
}
bool World::isPlayerPositionValid() bool World::isPlayerPositionValid()
{ {
for (auto e : entities) for (auto e : entities)
+5
View File
@@ -4,11 +4,14 @@
#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(const std::string &fileName); World(const std::string &fileName);
~World(); ~World();
@@ -23,5 +26,7 @@ public:
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);
std::string getObjectFromValue(int i);
}; };
+7 -1
View File
@@ -1,7 +1,13 @@
{ {
"world": { "world": {
"heightmap": "worlds/small.png", "heightmap": "worlds/small.png",
"scale": 1 "scale": 1,
"object-templates": [
{
"color":100,
"file": "models/boom/Boom.obj"
}
]
}, },
"player": { "player": {
"startposition": [ 20, 5, 20 ] "startposition": [ 20, 5, 20 ]
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB