diff --git a/CMakeLists.txt b/CMakeLists.txt index 226efcf..6c0c0da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,7 @@ pkg_search_module(SDL2GFX REQUIRED SDL2_gfx) include_directories("src/Libs/include") -file(GLOB SOURCES "src/*.cpp" "src/*/*.cpp" "src/Libs/*.c") +file(GLOB SOURCES "src/*.cpp" "src/*/*.cpp" "src/*/*/*.cpp" "src/Libs/*.c") add_executable(saland ${SOURCES}) diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index d1623f3..71d57aa 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp @@ -1,6 +1,7 @@ #include #include "Game.hpp" +#include "model/World.hpp" #include "../sagotmx/tmx_struct.h" #include "../sago/SagoMisc.hpp" #include "globals.hpp" @@ -39,8 +40,7 @@ struct Game::GameImpl { float center_x = 0; float center_y = 0; bool drawCollision = true; - sago::tiled::TileSet ts; - sago::tiled::TileMap tm; + World world; int topx = 0.0; int topy = 0.0; int world_mouse_x = 0; //Mouse cooridinates relative to the world @@ -51,17 +51,13 @@ struct Game::GameImpl { Game::Game() { data.reset(new Game::GameImpl()); - std::string tsx_file = sago::GetFileContent("maps/terrain.tsx"); - std::string tmx_file = sago::GetFileContent("maps/sample1.tmx"); - data->ts = sago::tiled::string2tileset(tsx_file); - data->tm = sago::tiled::string2tilemap(tmx_file); - data->tm.tileset.alternativeSource = &data->ts; + data->world.init(); data->lastUpdate = SDL_GetTicks(); data->human.reset(new Human()); } Game::~Game() { - std::string data2save = sago::tiled::tilemap2string(data->tm); + std::string data2save = sago::tiled::tilemap2string(data->world.tm); sago::WriteFileContent("maps/sample1.tmx", data2save); } @@ -107,8 +103,8 @@ void Game::Draw(SDL_Renderer* target) { data->topy = -10; } SDL_Texture* texture = globalData.spriteHolder->GetDataHolder().getTexturePtr("terrain"); - for (size_t i = 0; i < data->tm.layers.size(); ++i ) { - DrawLayer(target, texture, data->tm, i, data->topx, data->topy); + for (size_t i = 0; i < data->world.tm.layers.size(); ++i ) { + DrawLayer(target, texture, data->world.tm, i, data->topx, data->topy); } int mousebox_x = data->world_mouse_x - data->world_mouse_x%32 - data->topx; int mousebox_y = data->world_mouse_y - data->world_mouse_y%32 - data->topy; @@ -129,10 +125,27 @@ void Game::ProcessInput(const SDL_Event& event, bool& processed) { } } +static std::vector > getTouchingTiles(float x, float y, float radius) { + const int tileSize = 32; + std::vector > res; + std::vector > candidates; + for (int i = floor((x-radius) / tileSize); i < (x+radius)/ tileSize ; i++ ) { + for (int j = floor((y-radius) / tileSize); j < (y+radius)/ tileSize ; j++ ) { + candidates.push_back(std::make_pair(i,j)); + } + } + res = candidates; + return res; +} + static bool MoveEntity( Placeable* entity, float destX, float destY ) { bool canMove = true; float sourceX = entity->X; float sourceY = entity->Y; + const auto& touchingTilesNew = getTouchingTiles(destX, destY, entity->Radius); + for (const auto& tile : touchingTilesNew) { + + } entity->X = destX; entity->Y = destY; if ( !canMove ) { diff --git a/src/saland/Game.hpp b/src/saland/Game.hpp index 917ee15..7be731f 100644 --- a/src/saland/Game.hpp +++ b/src/saland/Game.hpp @@ -4,7 +4,6 @@ #include #include "../sago/GameStateInterface.hpp" -#include "../sagotmx/tmx_struct.h" class Game : public sago::GameStateInterface { public: diff --git a/src/saland/model/World.cpp b/src/saland/model/World.cpp new file mode 100644 index 0000000..892f6ab --- /dev/null +++ b/src/saland/model/World.cpp @@ -0,0 +1,13 @@ +#include "World.hpp" + +World::World() { +} + +void World::init() { + std::string tsx_file = sago::GetFileContent("maps/terrain.tsx"); + std::string tmx_file = sago::GetFileContent("maps/sample1.tmx"); + ts = sago::tiled::string2tileset(tsx_file); + tm = sago::tiled::string2tilemap(tmx_file); + tm.tileset.alternativeSource = &ts; +} + diff --git a/src/saland/model/World.hpp b/src/saland/model/World.hpp new file mode 100644 index 0000000..c6438c5 --- /dev/null +++ b/src/saland/model/World.hpp @@ -0,0 +1,17 @@ +#ifndef WORLD_HPP +#define WORLD_HPP + +#include "../../sagotmx/tmx_struct.h" +#include "../../sago/SagoMisc.hpp" + +class World { +public: + World(); + void init(); +//private: + sago::tiled::TileSet ts; + sago::tiled::TileMap tm; +}; + +#endif /* WORLD_HPP */ +