commit 6649c916
Not beutifull but water is now places correctly.
Changed files
| M | src/terrain/WaterHandler.cpp before |
| M | src/terrain/WaterHandler.hpp before |
diff --git a/src/terrain/WaterHandler.cpp b/src/terrain/WaterHandler.cpp
index 4fe3286..818124f 100644
--- a/src/terrain/WaterHandler.cpp
+++ b/src/terrain/WaterHandler.cpp
@@ -24,12 +24,107 @@ https://github.com/sago007/saland
#include "WaterHandler.hpp"
#include <iostream>
+WaterHandler::WaterHandler() {
+ setupTiles(default_tile);
+}
+
+void WaterHandler::setupTiles(uint32_t start_tile) {
+ tile_map["11111110"] = 29;
+ tile_map["11111011"] = 30;
+ tile_map["11011111"] = 61;
+ tile_map["01111111"] = 62;
+
+ tile_map["00001011"] = 92;
+ tile_map["00011111"] = 93;
+ tile_map["00010110"] = 94;
+
+ tile_map["01101011"] = 124;
+ tile_map["11111111"] = 125;
+ tile_map["11010110"] = 126;
+
+ tile_map["01101000"] = 156;
+ tile_map["11111000"] = 157;
+ tile_map["11010000"] = 158;
+
+ tiles = {28, 29, 30, 60, 61, 62, 92, 93, 94, 124, 125, 126, 156, 157, 158, 188, 189, 190};
+}
+
+uint32_t WaterHandler::getTile(sago::tiled::TileMap& tm, int x, int y) {
+ std::string theString = stringForTileSurrounding(tm, x, y);
+ uint32_t tile = tile_map[theString];
+ if (tile > 0) {
+ return tile;
+ }
+ if (theString[1] == '1' && theString[2] == '1' && theString[4] == '1' && theString[6] == '1' && theString[7] == '1') {
+ return tile_map["01101011"];
+ }
+ if (theString[0] == '1' && theString[1] == '1' && theString[3] == '1' && theString[5] == '1' && theString[6] == '1') {
+ return tile_map["11010110"];
+ }
+ if (theString[3] == '1' && theString[4] == '1' && theString[5] == '1' && theString[6] == '1' && theString[7] == '1') {
+ return tile_map["00011111"];
+ }
+ if (theString[0] == '1' && theString[1] == '1' && theString[2] == '1' && theString[3] == '1' && theString[4] == '1') {
+ return tile_map["11111000"];
+ }
+
+ if (theString[1] == '1' && theString[2] == '1' && theString[4] == '1') {
+ return tile_map["01101000"];
+ }
+ if (theString[0] == '1' && theString[1] == '1' && theString[3] == '1') {
+ return tile_map["11010000"];
+ }
+
+ if (theString[3] == '1' && theString[5] == '1' && theString[6] == '1') {
+ return tile_map["00010110"];
+ }
+ if (theString[4] == '1' && theString[6] == '1' && theString[7] == '1') {
+ return tile_map["00001011"];
+ }
+ return default_tile;
+}
+
void WaterHandler::updateTile(sago::tiled::TileMap& tm, int x, int y) {
+ if (x < 0 || y < 0 || x > tm.width || y > tm.height) {
+ //Out of bound. Do nothing
+ return;
+ }
int layer_number = 2; // Do not hardcode
- uint32_t tile = default_tile;
uint32_t current_tile = sago::tiled::getTileFromLayer(tm, tm.layers.at(layer_number), x, y);
- std::cout << "tile: " << current_tile << "\n";
- if (tiles.find(current_tile) != tiles.end()) {
- sago::tiled::setTileOnLayerNumber(tm, layer_number, x, y, tile);
+ std::cout << "tile: " << current_tile << ", surrounding: " << stringForTileSurrounding(tm, x, y) << "\n";
+ if (isWaterTile(current_tile)) {
+ uint32_t tile = getTile(tm, x, y);
+ if (tile != current_tile) {
+ sago::tiled::setTileOnLayerNumber(tm, layer_number, x, y, tile);
+ updateTile(tm, x-1, y-1);
+ updateTile(tm, x, y-1);
+ updateTile(tm, x+1, y-1);
+ updateTile(tm, x-1, y);
+ updateTile(tm, x+1, y);
+ updateTile(tm, x-1, y+1);
+ updateTile(tm, x, y+1);
+ updateTile(tm, x+1, y+1);
+ }
}
-}
\ No newline at end of file
+}
+
+bool WaterHandler::isWaterTile(uint32_t tile) const {
+ return tiles.find(tile) != tiles.end();
+}
+
+bool WaterHandler::isWater(const sago::tiled::TileMap& tm, int x, int y) const {
+ if (x < 0 || y < 0 || x > tm.width || y > tm.height) {
+ //Assume that "water" is around the map
+ return true;
+ }
+ int layer_number = 2; // Do not hardcode
+ uint32_t current_tile = sago::tiled::getTileFromLayer(tm, tm.layers.at(layer_number), x, y);
+ return tiles.find(current_tile) != tiles.end();
+}
+
+std::string WaterHandler::stringForTileSurrounding(const sago::tiled::TileMap& tm, int x, int y) const {
+ std::string ret = std::to_string(isWater(tm, x-1, y-1)) + std::to_string(isWater(tm, x, y-1)) + std::to_string(isWater(tm, x+1, y-1))
+ + std::to_string(isWater(tm, x-1, y)) + std::to_string(isWater(tm, x+1, y))
+ + std::to_string(isWater(tm, x-1, y+1)) + std::to_string(isWater(tm, x, y+1)) + std::to_string(isWater(tm, x+1, y+1));
+ return ret;
+}
diff --git a/src/terrain/WaterHandler.hpp b/src/terrain/WaterHandler.hpp
index 5d0f4f4..5605b8b 100644
--- a/src/terrain/WaterHandler.hpp
+++ b/src/terrain/WaterHandler.hpp
@@ -28,10 +28,24 @@ https://github.com/sago007/saland
#include "../sagotmx/tmx_struct.h"
struct WaterHandler {
- std::unordered_set<uint32_t> tiles = {28, 29, 30, 60, 61, 62, 92, 93, 94, 188, 189, 190};
+ std::unordered_set<uint32_t> tiles;
+ std::map<std::string, uint32_t> tile_map;
uint32_t default_tile = 28;
- void updateTile(sago::tiled::TileMap& world, int x, int y);
+ WaterHandler();
+
+ void setupTiles(uint32_t start_tile);
+
+ uint32_t getTile(sago::tiled::TileMap& tm, int x, int y);
+
+ void updateTile(sago::tiled::TileMap& tm, int x, int y);
+
+ bool isWaterTile(uint32_t tile) const;
+
+ bool isWater(const sago::tiled::TileMap& tm, int x, int y) const;
+
+ std::string stringForTileSurrounding(const sago::tiled::TileMap& tm, int x, int y) const;
+
};
#endif //TERRAIN_WATERHANDLER_HPP