diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index dc2da99..e59502e 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp @@ -297,21 +297,6 @@ void Game::Draw(SDL_Renderer* target) { //#endif } -static bool reservedField(const sago::tiled::TileMap& tm, int x, int y) { - if (x < 2 && y > tm.layers.at(6070).height/2-6 && y < tm.layers.at(0).height/2+5) { - return true; - } - if (x > tm.layers.at(0).width-3 && y > tm.layers.at(0).height/2-6 && y < tm.layers.at(0).height/2+5) { - return true; - } - if (y < 2 && x > tm.layers.at(0).width/2-6 && x < tm.layers.at(0).width/2+5) { - return true; - } - if (y > tm.layers.at(0).height-3 && x > tm.layers.at(0).width/2-6 && x < tm.layers.at(0).width/2+5) { - return true; - } - return false; -} void Game::ProcessInput(const SDL_Event& event, bool& processed) { if (data->consoleActive && data->console) { @@ -325,7 +310,7 @@ void Game::ProcessInput(const SDL_Event& event, bool& processed) { int tile_y = data->world_mouse_y/32; if (event.key.keysym.sym == globalData.playerControls.block_create && sago::tiled::tileInBound(data->gameRegion.world.tm, tile_x, tile_y) - && !reservedField(data->gameRegion.world.tm, tile_x, tile_y)) { + && !(data->gameRegion.world.tile_protected(tile_x, tile_y)) ) { int layer_number = 2; // Do not hardcode uint32_t tile = data->drawTile; sago::tiled::setTileOnLayerNumber(data->gameRegion.world.tm, layer_number, tile_x, tile_y, tile); diff --git a/src/saland/model/World.cpp b/src/saland/model/World.cpp index 57f78c4..a400345 100644 --- a/src/saland/model/World.cpp +++ b/src/saland/model/World.cpp @@ -154,9 +154,34 @@ void World::init(std::shared_ptr& world, const std::string& mapFileName } } init_physics(world); + protected_tiles.resize(tm.height*tm.width); + for (int x=0; x < tm.width; ++x){ + for (int y=0; y < tm.height; ++y){ + if (x < 2 && y > tm.layers.at(0).height/2-6 && y < tm.layers.at(0).height/2+5) { + protected_tiles[x+y*tm.width] = true; + } + if (x > tm.layers.at(0).width-3 && y > tm.layers.at(0).height/2-6 && y < tm.layers.at(0).height/2+5) { + protected_tiles[x+y*tm.width] = true; + } + if (y < 2 && x > tm.layers.at(0).width/2-6 && x < tm.layers.at(0).width/2+5) { + protected_tiles[x+y*tm.width] = true; + } + if (y > tm.layers.at(0).height-3 && x > tm.layers.at(0).width/2-6 && x < tm.layers.at(0).width/2+5) { + protected_tiles[x+y*tm.width] = true; + } + } + } } void World::init(std::shared_ptr& world) { init(world, "maps/sample1.tmx"); } +bool World::tile_protected(int x, int y) const { + size_t index = x+y*tm.width; + if (x<0 || y<0 || x>=tm.width || y>=tm.height || index >= protected_tiles.size()) { + //Outside the area. Assume protected + return true; + } + return protected_tiles.at(index); +} diff --git a/src/saland/model/World.hpp b/src/saland/model/World.hpp index 0567ca6..83435cb 100644 --- a/src/saland/model/World.hpp +++ b/src/saland/model/World.hpp @@ -34,11 +34,13 @@ public: void init(std::shared_ptr& world); void init(std::shared_ptr& world, const std::string& mapFileName); void init_physics(std::shared_ptr& world); + bool tile_protected(int x, int y) const; //private: sago::tiled::TileSet ts; sago::tiled::TileMap tm; std::shared_ptr physicsWorld; std::vector managed_bodies; + std::vector protected_tiles; }; #endif /* WORLD_HPP */