summaryrefslogtreecommitdiff
path: root/scene/resources
diff options
context:
space:
mode:
authorGilles Roudière <gilles.roudiere@gmail.com>2021-06-30 12:03:22 +0200
committerGilles Roudière <gilles.roudiere@gmail.com>2021-07-21 12:29:51 +0200
commit047e9b19f80e1f65d207d163c9c67574f7484ade (patch)
treee948216522e1cf717f56c377d0b62e7b2312db13 /scene/resources
parent88bf6e1c6d3e711976304bb16ac51719e8e82f72 (diff)
Import old tiles in SINGLE_TILE mode as atlases
Diffstat (limited to 'scene/resources')
-rw-r--r--scene/resources/tile_set.cpp159
-rw-r--r--scene/resources/tile_set.h30
2 files changed, 164 insertions, 25 deletions
diff --git a/scene/resources/tile_set.cpp b/scene/resources/tile_set.cpp
index deb5a50eb2..b00d05c17c 100644
--- a/scene/resources/tile_set.cpp
+++ b/scene/resources/tile_set.cpp
@@ -1539,7 +1539,7 @@ const Vector2i TileSetSource::INVALID_ATLAS_COORDS = Vector2i(-1, -1);
const int TileSetSource::INVALID_TILE_ALTERNATIVE = -1;
#ifndef DISABLE_DEPRECATED
-void TileSet::compatibility_conversion() {
+void TileSet::_compatibility_conversion() {
for (Map<int, CompatibilityTileData *>::Element *E = compatibility_data.front(); E; E = E->next()) {
CompatibilityTileData *ctd = E->value();
@@ -1551,13 +1551,94 @@ void TileSet::compatibility_conversion() {
// Handle each tile as a new source. Not optimal but at least it should stay compatible.
switch (ctd->tile_mode) {
- case 0: // SINGLE_TILE
- // TODO
- break;
- case 1: // AUTO_TILE
+ case COMPATIBILITY_TILE_MODE_SINGLE_TILE: {
+ atlas_source->set_margins(ctd->region.get_position());
+ atlas_source->set_texture_region_size(ctd->region.get_size());
+
+ Vector2i coords = Vector2i(0, 0);
+ for (int flags = 0; flags < 8; flags++) {
+ bool flip_h = flags & 1;
+ bool flip_v = flags & 2;
+ bool transpose = flags & 4;
+
+ int alternative_tile = 0;
+ if (!atlas_source->has_tile(coords)) {
+ atlas_source->create_tile(coords);
+ } else {
+ alternative_tile = atlas_source->create_alternative_tile(coords);
+ }
+
+ // Add to the mapping.
+ Array key_array;
+ key_array.push_back(flip_h);
+ key_array.push_back(flip_v);
+ key_array.push_back(transpose);
+
+ Array value_array;
+ value_array.push_back(source_id);
+ value_array.push_back(coords);
+ value_array.push_back(alternative_tile);
+
+ if (!compatibility_tilemap_mapping.has(E->key())) {
+ compatibility_tilemap_mapping[E->key()] = Map<Array, Array>();
+ }
+ compatibility_tilemap_mapping[E->key()][key_array] = value_array;
+ compatibility_tilemap_mapping_tile_modes[E->key()] = COMPATIBILITY_TILE_MODE_SINGLE_TILE;
+ print_line(vformat("Added conversion from:%s to%s", key_array, value_array));
+
+ TileData *tile_data = Object::cast_to<TileData>(atlas_source->get_tile_data(coords, alternative_tile));
+
+ tile_data->set_flip_h(flip_h);
+ tile_data->set_flip_v(flip_v);
+ tile_data->set_transpose(transpose);
+ tile_data->tile_set_material(ctd->material);
+ tile_data->set_modulate(ctd->modulate);
+ tile_data->set_z_index(ctd->z_index);
+
+ if (ctd->occluder.is_valid()) {
+ if (get_occlusion_layers_count() < 1) {
+ set_occlusion_layers_count(1);
+ }
+ tile_data->set_occluder(0, ctd->occluder);
+ }
+ if (ctd->navigation.is_valid()) {
+ if (get_navigation_layers_count() < 1) {
+ set_navigation_layers_count(1);
+ }
+ tile_data->set_navigation_polygon(0, ctd->autotile_navpoly_map[coords]);
+ }
+
+ tile_data->set_z_index(ctd->z_index);
+
+ // Add the shapes.
+ if (ctd->shapes.size() > 0) {
+ if (get_physics_layers_count() < 1) {
+ set_physics_layers_count(1);
+ }
+ }
+ for (int k = 0; k < ctd->shapes.size(); k++) {
+ CompatibilityShapeData csd = ctd->shapes[k];
+ if (csd.autotile_coords == coords) {
+ Ref<ConvexPolygonShape2D> convex_shape = csd.shape; // Only ConvexPolygonShape2D are supported, which is the default type used by the 3.x editor
+ if (convex_shape.is_valid()) {
+ Vector<Vector2> polygon = convex_shape->get_points();
+ for (int point_index = 0; point_index < polygon.size(); point_index++) {
+ polygon.write[point_index] = csd.transform.xform(polygon[point_index]);
+ }
+ tile_data->set_collision_polygons_count(0, tile_data->get_collision_polygons_count(0) + 1);
+ int index = tile_data->get_collision_polygons_count(0) - 1;
+ tile_data->set_collision_polygon_one_way(0, index, csd.one_way);
+ tile_data->set_collision_polygon_one_way_margin(0, index, csd.one_way_margin);
+ tile_data->set_collision_polygon_points(0, index, polygon);
+ }
+ }
+ }
+ }
+ } break;
+ case COMPATIBILITY_TILE_MODE_AUTO_TILE: {
// TODO
- break;
- case 2: // ATLAS_TILE
+ } break;
+ case COMPATIBILITY_TILE_MODE_ATLAS_TILE: {
atlas_source->set_margins(ctd->region.get_position());
atlas_source->set_separation(Vector2i(ctd->autotile_spacing, ctd->autotile_spacing));
atlas_source->set_texture_region_size(ctd->autotile_tile_size);
@@ -1578,6 +1659,26 @@ void TileSet::compatibility_conversion() {
} else {
alternative_tile = atlas_source->create_alternative_tile(coords);
}
+
+ // Add to the mapping.
+ Array key_array;
+ key_array.push_back(coords);
+ key_array.push_back(flip_h);
+ key_array.push_back(flip_v);
+ key_array.push_back(transpose);
+
+ Array value_array;
+ value_array.push_back(source_id);
+ value_array.push_back(coords);
+ value_array.push_back(alternative_tile);
+
+ if (!compatibility_tilemap_mapping.has(E->key())) {
+ compatibility_tilemap_mapping[E->key()] = Map<Array, Array>();
+ }
+ compatibility_tilemap_mapping[E->key()][key_array] = value_array;
+ compatibility_tilemap_mapping_tile_modes[E->key()] = COMPATIBILITY_TILE_MODE_ATLAS_TILE;
+ print_line(vformat("Added conversion from:%s to%s", key_array, value_array));
+
TileData *tile_data = Object::cast_to<TileData>(atlas_source->get_tile_data(coords, alternative_tile));
tile_data->set_flip_h(flip_h);
@@ -1641,7 +1742,7 @@ void TileSet::compatibility_conversion() {
}
}
}
- break;
+ } break;
}
// Offset all shapes
@@ -1655,9 +1756,6 @@ void TileSet::compatibility_conversion() {
convex->set_points(points);
}
}
-
- // Add the mapping to the map
- compatibility_source_mapping.insert(E->key(), source_id);
}
// Reset compatibility data
@@ -1666,6 +1764,43 @@ void TileSet::compatibility_conversion() {
}
compatibility_data = Map<int, CompatibilityTileData *>();
}
+
+Array TileSet::compatibility_tilemap_map(int p_tile_id, Vector2i p_coords, bool p_flip_h, bool p_flip_v, bool p_transpose) {
+ Array cannot_convert_array;
+ cannot_convert_array.push_back(-1);
+ cannot_convert_array.push_back(TileSetAtlasSource::INVALID_ATLAS_COORDS);
+ cannot_convert_array.push_back(TileSetAtlasSource::INVALID_TILE_ALTERNATIVE);
+
+ if (!compatibility_tilemap_mapping.has(p_tile_id)) {
+ return cannot_convert_array;
+ }
+
+ int tile_mode = compatibility_tilemap_mapping_tile_modes[p_tile_id];
+ switch (tile_mode) {
+ case COMPATIBILITY_TILE_MODE_SINGLE_TILE: {
+ Array a;
+ a.push_back(p_flip_h);
+ a.push_back(p_flip_v);
+ a.push_back(p_transpose);
+ return compatibility_tilemap_mapping[p_tile_id][a];
+ }
+ case COMPATIBILITY_TILE_MODE_AUTO_TILE:
+ return cannot_convert_array;
+ break;
+ case COMPATIBILITY_TILE_MODE_ATLAS_TILE: {
+ Array a;
+ a.push_back(p_coords);
+ a.push_back(p_flip_h);
+ a.push_back(p_flip_v);
+ a.push_back(p_transpose);
+ return compatibility_tilemap_mapping[p_tile_id][a];
+ }
+ default:
+ return cannot_convert_array;
+ break;
+ }
+};
+
#endif // DISABLE_DEPRECATED
bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
@@ -1831,7 +1966,7 @@ bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
ctd->z_index = p_value;
// TODO: remove the conversion from here, it's not where it should be done
- compatibility_conversion();
+ _compatibility_conversion();
} else {
return false;
}
diff --git a/scene/resources/tile_set.h b/scene/resources/tile_set.h
index dbf6dbabe6..4dcde52098 100644
--- a/scene/resources/tile_set.h
+++ b/scene/resources/tile_set.h
@@ -79,15 +79,15 @@ private:
Vector2 tex_offset;
Ref<ShaderMaterial> material;
Rect2 region;
- int tile_mode;
- Color modulate;
+ int tile_mode = 0;
+ Color modulate = Color(1, 1, 1);
// Atlas or autotiles data
- int autotile_bitmask_mode;
+ int autotile_bitmask_mode = 0;
Vector2 autotile_icon_coordinate;
Size2i autotile_tile_size = Size2i(16, 16);
- int autotile_spacing;
+ int autotile_spacing = 0;
Map<Vector2i, int> autotile_bitmask_flags;
Map<Vector2i, Ref<OccluderPolygon2D>> autotile_occluder_map;
Map<Vector2i, Ref<NavigationPolygon>> autotile_navpoly_map;
@@ -99,20 +99,24 @@ private:
Vector2 occluder_offset;
Ref<NavigationPolygon> navigation;
Vector2 navigation_offset;
- int z_index;
+ int z_index = 0;
};
- Map<int, CompatibilityTileData *> compatibility_data = Map<int, CompatibilityTileData *>();
- Map<int, int> compatibility_source_mapping = Map<int, int>();
+ enum CompatibilityTileMode {
+ COMPATIBILITY_TILE_MODE_SINGLE_TILE = 0,
+ COMPATIBILITY_TILE_MODE_AUTO_TILE,
+ COMPATIBILITY_TILE_MODE_ATLAS_TILE,
+ };
-private:
- void compatibility_conversion();
+ Map<int, CompatibilityTileData *> compatibility_data;
+ Map<int, int> compatibility_tilemap_mapping_tile_modes;
+ Map<int, Map<Array, Array>> compatibility_tilemap_mapping;
-public:
- int compatibility_get_source_for_tile_id(int p_old_source) {
- return compatibility_source_mapping[p_old_source];
- };
+ void _compatibility_conversion();
+public:
+ // Format of output array [source_id, atlas_coords, alternative]
+ Array compatibility_tilemap_map(int p_tile_id, Vector2i p_coords, bool p_flip_h, bool p_flip_v, bool p_transpose);
#endif // DISABLE_DEPRECATED
public: