summaryrefslogtreecommitdiff
path: root/scene/2d/tile_map.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/2d/tile_map.cpp')
-rw-r--r--scene/2d/tile_map.cpp99
1 files changed, 55 insertions, 44 deletions
diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp
index 0afead0863..e39c8841cd 100644
--- a/scene/2d/tile_map.cpp
+++ b/scene/2d/tile_map.cpp
@@ -64,13 +64,13 @@ int TileMapPattern::get_cell_source_id(const Vector2i &p_coords) const {
}
Vector2i TileMapPattern::get_cell_atlas_coords(const Vector2i &p_coords) const {
- ERR_FAIL_COND_V(!pattern.has(p_coords), TileSetAtlasSource::INVALID_ATLAS_COORDS);
+ ERR_FAIL_COND_V(!pattern.has(p_coords), TileSetSource::INVALID_ATLAS_COORDS);
return pattern[p_coords].get_atlas_coords();
}
int TileMapPattern::get_cell_alternative_tile(const Vector2i &p_coords) const {
- ERR_FAIL_COND_V(!pattern.has(p_coords), TileSetAtlasSource::INVALID_TILE_ALTERNATIVE);
+ ERR_FAIL_COND_V(!pattern.has(p_coords), TileSetSource::INVALID_TILE_ALTERNATIVE);
return pattern[p_coords].alternative_tile;
}
@@ -113,7 +113,7 @@ void TileMapPattern::clear() {
};
void TileMapPattern::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_cell", "coords", "source_id", "atlas_coords", "alternative_tile"), &TileMapPattern::set_cell, DEFVAL(-1), DEFVAL(TileSetAtlasSource::INVALID_ATLAS_COORDS), DEFVAL(TileSetAtlasSource::INVALID_TILE_ALTERNATIVE));
+ ClassDB::bind_method(D_METHOD("set_cell", "coords", "source_id", "atlas_coords", "alternative_tile"), &TileMapPattern::set_cell, DEFVAL(-1), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(TileSetSource::INVALID_TILE_ALTERNATIVE));
ClassDB::bind_method(D_METHOD("has_cell", "coords"), &TileMapPattern::has_cell);
ClassDB::bind_method(D_METHOD("remove_cell", "coords"), &TileMapPattern::remove_cell);
ClassDB::bind_method(D_METHOD("get_cell_source_id", "coords"), &TileMapPattern::get_cell_source_id);
@@ -314,37 +314,30 @@ void TileMap::set_quadrant_size(int p_size) {
emit_signal("changed");
}
-void TileMap::_fix_cell_transform(Transform2D &xform, const TileMapCell &p_cell, const Vector2 &p_offset, const Size2 &p_sc) {
- Size2 s = p_sc;
- Vector2 offset = p_offset;
+void TileMap::set_collision_visibility_mode(TileMap::VisibilityMode p_show_collision) {
+ show_collision = p_show_collision;
+ _recreate_quadrants();
+ emit_signal("changed");
+}
- // Flip/transpose: update the tile transform.
- TileSetSource *source = *tile_set->get_source(p_cell.source_id);
- TileSetAtlasSource *atlas_source = Object::cast_to<TileSetAtlasSource>(source);
- if (!atlas_source) {
- return;
- }
- TileData *tile_data = Object::cast_to<TileData>(atlas_source->get_tile_data(p_cell.get_atlas_coords(), p_cell.alternative_tile));
- if (tile_data->get_transpose()) {
- SWAP(xform.elements[0].x, xform.elements[0].y);
- SWAP(xform.elements[1].x, xform.elements[1].y);
- SWAP(offset.x, offset.y);
- SWAP(s.x, s.y);
- }
+TileMap::VisibilityMode TileMap::get_collision_visibility_mode() {
+ return show_collision;
+}
- if (tile_data->get_flip_h()) {
- xform.elements[0].x = -xform.elements[0].x;
- xform.elements[1].x = -xform.elements[1].x;
- offset.x = s.x - offset.x;
- }
+void TileMap::set_navigation_visibility_mode(TileMap::VisibilityMode p_show_navigation) {
+ show_navigation = p_show_navigation;
+ _recreate_quadrants();
+ emit_signal("changed");
+}
- if (tile_data->get_flip_v()) {
- xform.elements[0].y = -xform.elements[0].y;
- xform.elements[1].y = -xform.elements[1].y;
- offset.y = s.y - offset.y;
- }
+TileMap::VisibilityMode TileMap::get_navigation_visibility_mode() {
+ return show_navigation;
+}
- xform.elements[2] += offset;
+void TileMap::set_y_sort_enabled(bool p_enable) {
+ Node2D::set_y_sort_enabled(p_enable);
+ _recreate_quadrants();
+ emit_signal("changed");
}
void TileMap::update_dirty_quadrants() {
@@ -520,12 +513,12 @@ void TileMap::set_cell(const Vector2i &p_coords, int p_source_id, const Vector2i
Vector2i atlas_coords = p_atlas_coords;
int alternative_tile = p_alternative_tile;
- if ((source_id == -1 || atlas_coords == TileSetAtlasSource::INVALID_ATLAS_COORDS || alternative_tile == TileSetAtlasSource::INVALID_TILE_ALTERNATIVE) &&
- (source_id != -1 || atlas_coords != TileSetAtlasSource::INVALID_ATLAS_COORDS || alternative_tile != TileSetAtlasSource::INVALID_TILE_ALTERNATIVE)) {
+ if ((source_id == -1 || atlas_coords == TileSetSource::INVALID_ATLAS_COORDS || alternative_tile == TileSetSource::INVALID_TILE_ALTERNATIVE) &&
+ (source_id != -1 || atlas_coords != TileSetSource::INVALID_ATLAS_COORDS || alternative_tile != TileSetSource::INVALID_TILE_ALTERNATIVE)) {
WARN_PRINT("Setting a cell a cell as empty requires both source_id, atlas_coord and alternative_tile to be set to their respective \"invalid\" values. Values were thus changes accordingly.");
source_id = -1;
- atlas_coords = TileSetAtlasSource::INVALID_ATLAS_COORDS;
- alternative_tile = TileSetAtlasSource::INVALID_TILE_ALTERNATIVE;
+ atlas_coords = TileSetSource::INVALID_ATLAS_COORDS;
+ alternative_tile = TileSetSource::INVALID_TILE_ALTERNATIVE;
}
if (!E && source_id == -1) {
@@ -602,7 +595,7 @@ Vector2i TileMap::get_cell_atlas_coords(const Vector2i &p_coords) const {
const Map<Vector2i, TileMapCell>::Element *E = tile_map.find(p_coords);
if (!E) {
- return TileSetAtlasSource::INVALID_ATLAS_COORDS;
+ return TileSetSource::INVALID_ATLAS_COORDS;
}
return E->get().get_atlas_coords();
@@ -613,7 +606,7 @@ int TileMap::get_cell_alternative_tile(const Vector2i &p_coords) const {
const Map<Vector2i, TileMapCell>::Element *E = tile_map.find(p_coords);
if (!E) {
- return TileSetAtlasSource::INVALID_TILE_ALTERNATIVE;
+ return TileSetSource::INVALID_TILE_ALTERNATIVE;
}
return E->get().alternative_tile;
@@ -718,12 +711,17 @@ Map<Vector2i, TileMapQuadrant> &TileMap::get_quadrant_map() {
void TileMap::fix_invalid_tiles() {
ERR_FAIL_COND_MSG(tile_set.is_null(), "Cannot fix invalid tiles if Tileset is not open.");
+
+ Set<Vector2i> coords;
for (Map<Vector2i, TileMapCell>::Element *E = tile_map.front(); E; E = E->next()) {
TileSetSource *source = *tile_set->get_source(E->get().source_id);
if (!source || !source->has_tile(E->get().get_atlas_coords()) || !source->has_alternative_tile(E->get().get_atlas_coords(), E->get().alternative_tile)) {
- set_cell(E->key(), -1, TileSetAtlasSource::INVALID_ATLAS_COORDS, TileSetAtlasSource::INVALID_TILE_ALTERNATIVE);
+ coords.insert(E->key());
}
}
+ for (Set<Vector2i>::Element *E = coords.front(); E; E = E->next()) {
+ set_cell(E->get(), -1, TileSetSource::INVALID_ATLAS_COORDS, TileSetSource::INVALID_TILE_ALTERNATIVE);
+ }
}
void TileMap::_recreate_quadrants() {
@@ -777,6 +775,11 @@ void TileMap::_set_tile_data(const Vector<int> &p_data) {
int offset = (format >= FORMAT_2) ? 3 : 2;
clear();
+
+#ifdef DISABLE_DEPRECATED
+ ERR_FAIL_COND_MSG(format != FORMAT_3, vformat("Cannot handle deprecated TileMap data format version %d. This Godot version was compiled with no support for deprecated data.", format));
+#endif
+
for (int i = 0; i < c; i += offset) {
const uint8_t *ptr = (const uint8_t *)&r[i];
uint8_t local[12];
@@ -806,6 +809,7 @@ void TileMap::_set_tile_data(const Vector<int> &p_data) {
uint16_t alternative_tile = decode_uint16(&local[10]);
set_cell(Vector2i(x, y), source_id, Vector2i(atlas_coords_x, atlas_coords_y), alternative_tile);
} else {
+#ifndef DISABLE_DEPRECATED
uint32_t v = decode_uint32(&local[4]);
v &= (1 << 29) - 1;
@@ -828,6 +832,7 @@ void TileMap::_set_tile_data(const Vector<int> &p_data) {
}
set_cell(Vector2i(x, y), v, Vector2i(coord_x, coord_y), compatibility_alternative_tile);
+#endif
}
}
}
@@ -1716,7 +1721,13 @@ void TileMap::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_quadrant_size", "size"), &TileMap::set_quadrant_size);
ClassDB::bind_method(D_METHOD("get_quadrant_size"), &TileMap::get_quadrant_size);
- ClassDB::bind_method(D_METHOD("set_cell", "coords", "source_id", "atlas_coords", "alternative_tile"), &TileMap::set_cell, DEFVAL(-1), DEFVAL(TileSetAtlasSource::INVALID_ATLAS_COORDS), DEFVAL(TileSetAtlasSource::INVALID_TILE_ALTERNATIVE));
+ ClassDB::bind_method(D_METHOD("set_collision_visibility_mode", "show_collision"), &TileMap::set_collision_visibility_mode);
+ ClassDB::bind_method(D_METHOD("get_collision_visibility_mode"), &TileMap::get_collision_visibility_mode);
+
+ ClassDB::bind_method(D_METHOD("set_navigation_visibility_mode", "show_navigation"), &TileMap::set_navigation_visibility_mode);
+ ClassDB::bind_method(D_METHOD("get_navigation_visibility_mode"), &TileMap::get_navigation_visibility_mode);
+
+ ClassDB::bind_method(D_METHOD("set_cell", "coords", "source_id", "atlas_coords", "alternative_tile"), &TileMap::set_cell, DEFVAL(-1), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(TileSetSource::INVALID_TILE_ALTERNATIVE));
ClassDB::bind_method(D_METHOD("get_cell_source_id", "coords"), &TileMap::get_cell_source_id);
ClassDB::bind_method(D_METHOD("get_cell_atlas_coords", "coords"), &TileMap::get_cell_atlas_coords);
ClassDB::bind_method(D_METHOD("get_cell_alternative_tile", "coords"), &TileMap::get_cell_alternative_tile);
@@ -1740,10 +1751,16 @@ void TileMap::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_set", PROPERTY_HINT_RESOURCE_TYPE, "TileSet"), "set_tileset", "get_tileset");
ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_quadrant_size", PROPERTY_HINT_RANGE, "1,128,1"), "set_quadrant_size", "get_quadrant_size");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "show_collision", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_collision_visibility_mode", "get_collision_visibility_mode");
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "show_navigation", PROPERTY_HINT_ENUM, "Default,Force Show,Force Hide"), "set_navigation_visibility_mode", "get_navigation_visibility_mode");
ADD_PROPERTY_DEFAULT("format", FORMAT_1);
ADD_SIGNAL(MethodInfo("changed"));
+
+ BIND_ENUM_CONSTANT(VISIBILITY_MODE_DEFAULT);
+ BIND_ENUM_CONSTANT(VISIBILITY_MODE_FORCE_HIDE);
+ BIND_ENUM_CONSTANT(VISIBILITY_MODE_FORCE_SHOW);
}
void TileMap::_tile_set_changed() {
@@ -1752,12 +1769,6 @@ void TileMap::_tile_set_changed() {
}
TileMap::TileMap() {
- rect_cache_dirty = true;
- used_size_cache_dirty = true;
- pending_update = false;
- quadrant_size = 16;
- format = FORMAT_1; // Assume lowest possible format if none is present
-
set_notify_transform(true);
set_notify_local_transform(false);
}