#pragma once #include #include #include "Log.hpp" #include "IEntity.hpp" #define CW 20 #define CH 12 using json = nlohmann::json; class Collider { public: Collider(std::string filename) { std::ifstream ifs(filename); json j; ifs >> j; auto layer = j["layers"][6]; auto layerT1 = j["layers"][4]; m_data.resize(layer["width"]); for (std::size_t i = 0; i < layer["width"]; ++i) { m_data[i].resize(layer["height"]); for (std::size_t j = 0; j < layer["height"]; ++j) { setBlock(i, j, 0); } } for (std::size_t i = 0; i < layer["data"].size(); ++i) { int tile = layer["data"][i]; size_t x = i % layer["width"]; size_t y = i / layer["width"]; if (tile == 16) { setBlock(x, y, 2); } } for (std::size_t i = 0; i < layerT1["data"].size(); ++i) { int tile = layerT1["data"][i]; size_t x = i % layerT1["width"]; size_t y = i / layerT1["width"]; if (tile == 49 || tile == 50) { setBlock(x, y, 1); } } } static bool isWithin(sf::IntRect r, sf::Vector2f pos) { return (pos.x >= r.left * 32.f && pos.x <= r.width * 32.f && pos.y >= r.top * 32.f && pos.y <= r.height * 32.f); } int check(sf::Vector2f pos, float dx, float dy) { float box_x = 5.f; float box_y = 4.f; //return getBlock(pos.x / 32.f, pos.y / 32.f); if (dx < 0.f) { return getBlock((pos.x - box_x + dx) / 32.f, pos.y / 32.f); } else if (dx > 0.f) { return getBlock((pos.x + box_x + dx) / 32.f, pos.y / 32.f); } else if (dy < 0.f) { return getBlock(pos.x / 32.f, (pos.y - box_y + dy) / 32.f); } else { return getBlock(pos.x / 32.f, (pos.y + box_y + 3.f + dy) / 32.f); } } int getBlock(int x, int y) { return m_data[x][y]; } void setBlock(int x, int y, int type) { m_data[x][y] = type; } private: int getTile(int x, int y) { if(x < 0 || y < 0 || x >= CW || y >= CH) { return -1; } return x + CW * y; } std::vector> m_data; float m_localX; };