diff options
Diffstat (limited to 'scene/resources')
-rw-r--r-- | scene/resources/tile_set.cpp | 190 | ||||
-rw-r--r-- | scene/resources/tile_set.h | 44 |
2 files changed, 172 insertions, 62 deletions
diff --git a/scene/resources/tile_set.cpp b/scene/resources/tile_set.cpp index 489c99e15a..b9d2c503e1 100644 --- a/scene/resources/tile_set.cpp +++ b/scene/resources/tile_set.cpp @@ -53,12 +53,14 @@ bool TileSet::_set(const StringName &p_name, const Variant &p_value) { tile_set_material(id, p_value); else if (what == "modulate") tile_set_modulate(id, p_value); - else if (what == "shape_offset") - tile_set_shape_offset(id, p_value); else if (what == "region") tile_set_region(id, p_value); else if (what == "shape") - tile_set_shape(id, p_value); + tile_set_shape(id, 0, p_value); + else if (what == "shape_offset") + tile_set_shape_offset(id, 0, p_value); + else if (what == "shape_one_way") + tile_set_shape_one_way(id, 0, p_value); else if (what == "shapes") _tile_set_shapes(id, p_value); else if (what == "occluder") @@ -99,12 +101,14 @@ bool TileSet::_get(const StringName &p_name, Variant &r_ret) const { r_ret = tile_get_material(id); else if (what == "modulate") r_ret = tile_get_modulate(id); - else if (what == "shape_offset") - r_ret = tile_get_shape_offset(id); else if (what == "region") r_ret = tile_get_region(id); else if (what == "shape") - r_ret = tile_get_shape(id); + r_ret = tile_get_shape(id, 0); + else if (what == "shape_offset") + r_ret = tile_get_shape_offset(id, 0); + else if (what == "shape_one_way") + r_ret = tile_get_shape_one_way(id, 0); else if (what == "shapes") r_ret = _tile_get_shapes(id); else if (what == "occluder") @@ -123,7 +127,7 @@ bool TileSet::_get(const StringName &p_name, Variant &r_ret) const { void TileSet::_get_property_list(List<PropertyInfo> *p_list) const { - for (Map<int, Data>::Element *E = tile_map.front(); E; E = E->next()) { + for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) { int id = E->key(); String pre = itos(id) + "/"; @@ -138,8 +142,9 @@ void TileSet::_get_property_list(List<PropertyInfo> *p_list) const { p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "occluder", PROPERTY_HINT_RESOURCE_TYPE, "OccluderPolygon2D")); p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "navigation_offset")); p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "navigation", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon")); - p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "shape_offset")); + p_list->push_back(PropertyInfo(Variant::VECTOR2, pre + "shape_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR)); p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D", PROPERTY_USAGE_EDITOR)); + p_list->push_back(PropertyInfo(Variant::BOOL, pre + "shape_one_way", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR)); p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "shapes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR)); } } @@ -147,7 +152,7 @@ void TileSet::_get_property_list(List<PropertyInfo> *p_list) const { void TileSet::create_tile(int p_id) { ERR_FAIL_COND(tile_map.has(p_id)); - tile_map[p_id] = Data(); + tile_map[p_id] = TileData(); _change_notify(""); emit_changed(); } @@ -217,19 +222,6 @@ Vector2 TileSet::tile_get_texture_offset(int p_id) const { return tile_map[p_id].offset; } -void TileSet::tile_set_shape_offset(int p_id, const Vector2 &p_offset) { - - ERR_FAIL_COND(!tile_map.has(p_id)); - tile_map[p_id].shape_offset = p_offset; - emit_changed(); -} - -Vector2 TileSet::tile_get_shape_offset(int p_id) const { - - ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2()); - return tile_map[p_id].shape_offset; -} - void TileSet::tile_set_region(int p_id, const Rect2 &p_region) { ERR_FAIL_COND(!tile_map.has(p_id)); @@ -256,23 +248,82 @@ String TileSet::tile_get_name(int p_id) const { return tile_map[p_id].name; } -void TileSet::tile_set_shape(int p_id, const Ref<Shape2D> &p_shape) { +void TileSet::tile_clear_shapes(int p_id) { + tile_map[p_id].shapes_data.clear(); +} + +void TileSet::tile_add_shape(int p_id, const Ref<Shape2D> &p_shape, const Vector2 &p_offset, bool p_one_way) { + + ERR_FAIL_COND(!tile_map.has(p_id)); + + ShapeData new_data = ShapeData(); + new_data.shape = p_shape; + new_data.shape_offset = p_offset; + new_data.one_way_collision = p_one_way; + + tile_map[p_id].shapes_data.push_back(new_data); +}; +int TileSet::tile_get_shape_count(int p_id) const { + + ERR_FAIL_COND_V(!tile_map.has(p_id), 0); + + return tile_map[p_id].shapes_data.size(); +}; + +void TileSet::tile_set_shape(int p_id, int p_shape_id, const Ref<Shape2D> &p_shape) { ERR_FAIL_COND(!tile_map.has(p_id)); - tile_map[p_id].shapes.resize(1); - tile_map[p_id].shapes[0] = p_shape; + if (tile_map[p_id].shapes_data.size() <= p_shape_id) + tile_map[p_id].shapes_data.resize(p_shape_id + 1); + tile_map[p_id].shapes_data[p_shape_id].shape = p_shape; emit_changed(); } -Ref<Shape2D> TileSet::tile_get_shape(int p_id) const { +Ref<Shape2D> TileSet::tile_get_shape(int p_id, int p_shape_id) const { ERR_FAIL_COND_V(!tile_map.has(p_id), Ref<Shape2D>()); - if (tile_map[p_id].shapes.size() > 0) - return tile_map[p_id].shapes[0]; + if (tile_map[p_id].shapes_data.size() > p_shape_id) + return tile_map[p_id].shapes_data[p_shape_id].shape; return Ref<Shape2D>(); } +void TileSet::tile_set_shape_offset(int p_id, int p_shape_id, const Vector2 &p_offset) { + + ERR_FAIL_COND(!tile_map.has(p_id)); + if (tile_map[p_id].shapes_data.size() <= p_shape_id) + tile_map[p_id].shapes_data.resize(p_shape_id + 1); + tile_map[p_id].shapes_data[p_shape_id].shape_offset = p_offset; + emit_changed(); +} + +Vector2 TileSet::tile_get_shape_offset(int p_id, int p_shape_id) const { + + ERR_FAIL_COND_V(!tile_map.has(p_id), Vector2()); + if (tile_map[p_id].shapes_data.size() > p_shape_id) + return tile_map[p_id].shapes_data[p_shape_id].shape_offset; + + return Vector2(); +} + +void TileSet::tile_set_shape_one_way(int p_id, int p_shape_id, const bool p_one_way) { + + ERR_FAIL_COND(!tile_map.has(p_id)); + if (tile_map[p_id].shapes_data.size() <= p_shape_id) + tile_map[p_id].shapes_data.resize(p_shape_id + 1); + tile_map[p_id].shapes_data[p_shape_id].one_way_collision = p_one_way; + emit_changed(); +} + +bool TileSet::tile_get_shape_one_way(int p_id, int p_shape_id) const { + + ERR_FAIL_COND_V(!tile_map.has(p_id), false); + if (tile_map[p_id].shapes_data.size() > p_shape_id) + return tile_map[p_id].shapes_data[p_shape_id].one_way_collision; + + return false; +} + void TileSet::tile_set_light_occluder(int p_id, const Ref<OccluderPolygon2D> &p_light_occluder) { ERR_FAIL_COND(!tile_map.has(p_id)); @@ -319,31 +370,63 @@ Vector2 TileSet::tile_get_occluder_offset(int p_id) const { return tile_map[p_id].occluder_offset; } -void TileSet::tile_set_shapes(int p_id, const Vector<Ref<Shape2D> > &p_shapes) { +void TileSet::tile_set_shapes(int p_id, const Vector<ShapeData> &p_shapes) { ERR_FAIL_COND(!tile_map.has(p_id)); - tile_map[p_id].shapes = p_shapes; + tile_map[p_id].shapes_data = p_shapes; emit_changed(); } -Vector<Ref<Shape2D> > TileSet::tile_get_shapes(int p_id) const { +Vector<TileSet::ShapeData> TileSet::tile_get_shapes(int p_id) const { + + ERR_FAIL_COND_V(!tile_map.has(p_id), Vector<ShapeData>()); - ERR_FAIL_COND_V(!tile_map.has(p_id), Vector<Ref<Shape2D> >()); - return tile_map[p_id].shapes; + return tile_map[p_id].shapes_data; } void TileSet::_tile_set_shapes(int p_id, const Array &p_shapes) { ERR_FAIL_COND(!tile_map.has(p_id)); - Vector<Ref<Shape2D> > shapes; + Vector<ShapeData> shapes_data; + Vector2 default_offset = tile_get_shape_offset(p_id, 0); + bool default_one_way = tile_get_shape_one_way(p_id, 0); for (int i = 0; i < p_shapes.size(); i++) { - - Ref<Shape2D> s = p_shapes[i]; - if (s.is_valid()) - shapes.push_back(s); + ShapeData s = ShapeData(); + + if (p_shapes[i].get_type() == Variant::OBJECT) { + Ref<Shape2D> shape = p_shapes[i]; + if (shape.is_null()) continue; + + s.shape = shape; + s.shape_offset = default_offset; + s.one_way_collision = default_one_way; + } else if (p_shapes[i].get_type() == Variant::DICTIONARY) { + Dictionary d = p_shapes[i]; + + if (d.has("shape") && d["shape"].get_type() == Variant::OBJECT) + s.shape = d["shape"]; + else + continue; + + if (d.has("shape_offset") && d["shape_offset"].get_type() == Variant::VECTOR2) + s.shape_offset = d["shape_offset"]; + else + s.shape_offset = default_offset; + + if (d.has("one_way") && d["one_way"].get_type() == Variant::BOOL) + s.one_way_collision = d["one_way"]; + else + s.one_way_collision = default_one_way; + + } else { + ERR_EXPLAIN("Expected an array of objects or dictionaries for tile_set_shapes"); + ERR_CONTINUE(true); + } + + shapes_data.push_back(s); } - tile_set_shapes(p_id, shapes); + tile_map[p_id].shapes_data = shapes_data; } Array TileSet::_tile_get_shapes(int p_id) const { @@ -351,9 +434,14 @@ Array TileSet::_tile_get_shapes(int p_id) const { ERR_FAIL_COND_V(!tile_map.has(p_id), Array()); Array arr; - Vector<Ref<Shape2D> > shp = tile_map[p_id].shapes; - for (int i = 0; i < shp.size(); i++) - arr.push_back(shp[i]); + Vector<ShapeData> data = tile_map[p_id].shapes_data; + for (int i = 0; i < data.size(); i++) { + Dictionary shape_data; + shape_data["shape"] = data[i].shape; + shape_data["shape_offset"] = data[i].shape_offset; + shape_data["one_way"] = data[i].one_way_collision; + arr.push_back(shape_data); + } return arr; } @@ -362,7 +450,7 @@ Array TileSet::_get_tiles_ids() const { Array arr; - for (Map<int, Data>::Element *E = tile_map.front(); E; E = E->next()) { + for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) { arr.push_back(E->key()); } @@ -371,7 +459,7 @@ Array TileSet::_get_tiles_ids() const { void TileSet::get_tile_list(List<int> *p_tiles) const { - for (Map<int, Data>::Element *E = tile_map.front(); E; E = E->next()) { + for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) { p_tiles->push_back(E->key()); } @@ -400,7 +488,7 @@ int TileSet::get_last_unused_tile_id() const { int TileSet::find_tile_by_name(const String &p_name) const { - for (Map<int, Data>::Element *E = tile_map.front(); E; E = E->next()) { + for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) { if (p_name == E->get().name) return E->key(); @@ -428,12 +516,16 @@ void TileSet::_bind_methods() { ClassDB::bind_method(D_METHOD("tile_get_material:ShaderMaterial", "id"), &TileSet::tile_get_material); ClassDB::bind_method(D_METHOD("tile_set_texture_offset", "id", "texture_offset"), &TileSet::tile_set_texture_offset); ClassDB::bind_method(D_METHOD("tile_get_texture_offset", "id"), &TileSet::tile_get_texture_offset); - ClassDB::bind_method(D_METHOD("tile_set_shape_offset", "id", "shape_offset"), &TileSet::tile_set_shape_offset); - ClassDB::bind_method(D_METHOD("tile_get_shape_offset", "id"), &TileSet::tile_get_shape_offset); ClassDB::bind_method(D_METHOD("tile_set_region", "id", "region"), &TileSet::tile_set_region); ClassDB::bind_method(D_METHOD("tile_get_region", "id"), &TileSet::tile_get_region); - ClassDB::bind_method(D_METHOD("tile_set_shape", "id", "shape:Shape2D"), &TileSet::tile_set_shape); - ClassDB::bind_method(D_METHOD("tile_get_shape:Shape2D", "id"), &TileSet::tile_get_shape); + ClassDB::bind_method(D_METHOD("tile_set_shape", "id", "shape_id", "shape:Shape2D"), &TileSet::tile_set_shape); + ClassDB::bind_method(D_METHOD("tile_get_shape:Shape2D", "id", "shape_id"), &TileSet::tile_get_shape); + ClassDB::bind_method(D_METHOD("tile_set_shape_offset", "id", "shape_id", "shape_offset"), &TileSet::tile_set_shape_offset); + ClassDB::bind_method(D_METHOD("tile_get_shape_offset", "id", "shape_id"), &TileSet::tile_get_shape_offset); + ClassDB::bind_method(D_METHOD("tile_set_shape_one_way", "id", "shape_id", "one_way"), &TileSet::tile_set_shape_one_way); + ClassDB::bind_method(D_METHOD("tile_get_shape_one_way", "id", "shape_id"), &TileSet::tile_get_shape_one_way); + ClassDB::bind_method(D_METHOD("tile_add_shape", "id", "shape:Shape2D", "shape_offset", "one_way"), &TileSet::tile_add_shape, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("tile_get_shape_count", "id"), &TileSet::tile_get_shape_count); ClassDB::bind_method(D_METHOD("tile_set_shapes", "id", "shapes"), &TileSet::_tile_set_shapes); ClassDB::bind_method(D_METHOD("tile_get_shapes", "id"), &TileSet::_tile_get_shapes); ClassDB::bind_method(D_METHOD("tile_set_navigation_polygon", "id", "navigation_polygon:NavigationPolygon"), &TileSet::tile_set_navigation_polygon); diff --git a/scene/resources/tile_set.h b/scene/resources/tile_set.h index 3c4584161c..c07d82c75a 100644 --- a/scene/resources/tile_set.h +++ b/scene/resources/tile_set.h @@ -40,15 +40,26 @@ class TileSet : public Resource { GDCLASS(TileSet, Resource); - struct Data { +public: + struct ShapeData { + Ref<Shape2D> shape; + Vector2 shape_offset; + bool one_way_collision; + + ShapeData() { + one_way_collision = false; + } + }; + +private: + struct TileData { String name; Ref<Texture> texture; Ref<Texture> normal_map; Vector2 offset; - Vector2 shape_offset; Rect2i region; - Vector<Ref<Shape2D> > shapes; + Vector<ShapeData> shapes_data; Vector2 occluder_offset; Ref<OccluderPolygon2D> occluder; Vector2 navigation_polygon_offset; @@ -57,11 +68,11 @@ class TileSet : public Resource { Color modulate; // Default modulate for back-compat - explicit Data() + explicit TileData() : modulate(1, 1, 1) {} }; - Map<int, Data> tile_map; + Map<int, TileData> tile_map; protected: bool _set(const StringName &p_name, const Variant &p_value); @@ -88,14 +99,24 @@ public: void tile_set_texture_offset(int p_id, const Vector2 &p_offset); Vector2 tile_get_texture_offset(int p_id) const; - void tile_set_shape_offset(int p_id, const Vector2 &p_offset); - Vector2 tile_get_shape_offset(int p_id) const; - void tile_set_region(int p_id, const Rect2 &p_region); Rect2 tile_get_region(int p_id) const; - void tile_set_shape(int p_id, const Ref<Shape2D> &p_shape); - Ref<Shape2D> tile_get_shape(int p_id) const; + void tile_set_shape(int p_id, int p_shape_id, const Ref<Shape2D> &p_shape); + Ref<Shape2D> tile_get_shape(int p_id, int p_shape_id) const; + + void tile_set_shape_offset(int p_id, int p_shape_id, const Vector2 &p_offset); + Vector2 tile_get_shape_offset(int p_id, int p_shape_id) const; + + void tile_set_shape_one_way(int p_id, int p_shape_id, bool p_one_way); + bool tile_get_shape_one_way(int p_id, int p_shape_id) const; + + void tile_clear_shapes(int p_id); + void tile_add_shape(int p_id, const Ref<Shape2D> &p_shape, const Vector2 &p_offset, bool p_one_way = false); + int tile_get_shape_count(int p_id) const; + + void tile_set_shapes(int p_id, const Vector<ShapeData> &p_shapes); + Vector<ShapeData> tile_get_shapes(int p_id) const; void tile_set_material(int p_id, const Ref<ShaderMaterial> &p_material); Ref<ShaderMaterial> tile_get_material(int p_id) const; @@ -115,9 +136,6 @@ public: void tile_set_navigation_polygon(int p_id, const Ref<NavigationPolygon> &p_navigation_polygon); Ref<NavigationPolygon> tile_get_navigation_polygon(int p_id) const; - void tile_set_shapes(int p_id, const Vector<Ref<Shape2D> > &p_shapes); - Vector<Ref<Shape2D> > tile_get_shapes(int p_id) const; - void remove_tile(int p_id); bool has_tile(int p_id) const; |