From 17ae15b41c5ec912c36956dbc38e5a95af0ac648 Mon Sep 17 00:00:00 2001 From: Andy Belle-Isle Date: Sun, 6 Oct 2019 04:07:50 -0400 Subject: Started working on reading collision maps for worlds --- src/texture.cpp | 2 ++ src/texture.hpp | 2 -- src/world.cpp | 21 +++++++++++++++------ src/world.hpp | 26 ++++++++++++++++++++++++-- 4 files changed, 41 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/texture.cpp b/src/texture.cpp index 2870b9f..b013bef 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -21,6 +21,8 @@ #include "texture.hpp" +#include + #include #include diff --git a/src/texture.hpp b/src/texture.hpp index 3daebbd..b03d649 100644 --- a/src/texture.hpp +++ b/src/texture.hpp @@ -20,8 +20,6 @@ #ifndef TEXTURE_HPP_ #define TEXTURE_HPP_ -#include - #include #include diff --git a/src/world.cpp b/src/world.cpp index 7b82d32..8c15ccc 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -55,7 +55,6 @@ World::World(sol::object param) // If a generate function is defined, call it if (generate != sol::nil) generate(this); - std::cout << "flamingo" << std::endl; } // TODO @@ -121,12 +120,22 @@ unsigned int World::setSeed(unsigned int s) /* PHYSICS */ double World::getHeight(double x, double y, double z) { - (void)x; (void)y; - (void)z; - - double Y = 10.0f; - return Y; + double Y = 0.0f; + for (auto &l : solidLayers) { + if (z == l.drawLayer) { + int wx = x*unitSize; + + int h = 0.0; + for (auto b : l.hitbox[wx]) { + if (b) + Y = h; + h++; + } + return Y; + } + } + return 0; } /********* diff --git a/src/world.hpp b/src/world.hpp index a6d2f1d..fe4e400 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -26,6 +26,8 @@ #include #include +#include + #include "texture.hpp" #include "events/render.hpp" @@ -80,12 +82,32 @@ public: class SolidLayer : public Layer { + friend class World; private: - // hitbox something something + std::vector> hitbox; public: SolidLayer(float z, sol::table tab) : Layer(z, tab) { if (tab["hitbox"] != nullptr) { - std::cout << "hitbox: " << std::string(tab["hitbox"]) << std::endl; + int width, height; + unsigned char* box = + SOIL_load_image(std::string(tab["hitbox"]).c_str(), + &width, &height, 0, + SOIL_LOAD_RGBA); + + for (int w = 0; w < width; w++) { + hitbox.push_back(std::vector(height)); + for (int h = 0; h < height; h++) { + unsigned char* c = &box[(h) + (height*w*4)]; + // we want to read the red channel + if (c[3]) { + hitbox[w][h] = true; + } + else + hitbox[w][h] = false; + } + } + + SOIL_free_image_data(box); } } }; -- cgit v1.2.3