diff --git a/CrystalPoint.vcxproj b/CrystalPoint.vcxproj index f8cc70d..97671cc 100644 --- a/CrystalPoint.vcxproj +++ b/CrystalPoint.vcxproj @@ -184,6 +184,7 @@ + diff --git a/CrystalPoint.vcxproj.filters b/CrystalPoint.vcxproj.filters index e75e5d2..43a3b3d 100644 --- a/CrystalPoint.vcxproj.filters +++ b/CrystalPoint.vcxproj.filters @@ -118,5 +118,8 @@ Source Files\json + + Source Files\json + \ No newline at end of file diff --git a/HeightMap.cpp b/HeightMap.cpp index 042c229..f6752a6 100644 --- a/HeightMap.cpp +++ b/HeightMap.cpp @@ -2,12 +2,19 @@ #include "stb_image.h" #include "vector.h" +#include "LevelObject.h" + #include #include #include +#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; @@ -16,7 +23,12 @@ HeightMap::HeightMap(const std::string &file, float scale) 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++) @@ -27,6 +39,11 @@ HeightMap::HeightMap(const std::string &file, float scale) 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) + { + 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); normal.Normalize(); diff --git a/HeightMap.h b/HeightMap.h index 4c3c445..0f3a141 100644 --- a/HeightMap.h +++ b/HeightMap.h @@ -5,6 +5,8 @@ #include #include +class World; + class HeightMap { private: @@ -14,7 +16,7 @@ private: GLuint imageIndex; int scale; public: - HeightMap(const std::string &file, float scale); + HeightMap(const std::string &file, float scale, World* world); ~HeightMap(); void Draw(); diff --git a/World.cpp b/World.cpp index a2d54ad..1a9e533 100644 --- a/World.cpp +++ b/World.cpp @@ -1,7 +1,6 @@ #include "World.h" #include #include "Entity.h" -#include "LevelObject.h" #include "json.h" #include "Model.h" #include @@ -18,18 +17,27 @@ World::World(const std::string &fileName) json::Value v = json::readJson(file); file.close(); + //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"; float scale = 1.0f; if (!v["world"]["scale"].isNull()) 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(objt["color"], objt["file"])); + } + + heightmap = new HeightMap(v["world"]["heightmap"].asString(), scale, this); if(!v["world"]["texture"].isNull()) heightmap->SetTexture(v["world"]["texture"].asString()); @@ -90,6 +98,17 @@ 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); @@ -152,6 +171,11 @@ void World::update(float elapsedTime) } } +void World::addLevelObject(LevelObject* obj) +{ + entities.push_back(obj); +} + bool World::isPlayerPositionValid() { for (auto e : entities) diff --git a/World.h b/World.h index 0df2a06..de9c803 100644 --- a/World.h +++ b/World.h @@ -4,11 +4,14 @@ #include "HeightMap.h" #include "Player.h" #include "Enemy.h" +#include "LevelObject.h" class Entity; class World { +private: + std::vector> objecttemplates; public: World(const std::string &fileName); ~World(); @@ -23,5 +26,7 @@ public: void update(float elapsedTime); bool isPlayerPositionValid(); float getHeight(float x, float y); + void addLevelObject(LevelObject* obj); + std::string getObjectFromValue(int i); }; diff --git a/worlds/small.json b/worlds/small.json index e69e432..bd9ab49 100644 --- a/worlds/small.json +++ b/worlds/small.json @@ -1,7 +1,13 @@ { "world": { "heightmap": "worlds/small.png", - "scale": 1 + "scale": 1, + "object-templates": [ + { + "color":100, + "file": "models/boom/Boom.obj" + } + ] }, "player": { "startposition": [ 20, 5, 20 ] diff --git a/worlds/small.png b/worlds/small.png index fd75ea1..72f885b 100644 Binary files a/worlds/small.png and b/worlds/small.png differ