diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2017-11-22 15:31:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-22 15:31:46 +0100 |
commit | e38efe22c7626de34912cf2346f915ccca56f77b (patch) | |
tree | b608f291065a4b485bec91e20245c78391cc0a5b /scene/2d | |
parent | 012b39706364a8eb1ed576470a0d35c8ed498ad0 (diff) | |
parent | bcfb0a09f89f6734e2404acd68505dc45b402ee6 (diff) |
Merge pull request #12301 from MarianoGnu/master
Implement auto-tiling
Diffstat (limited to 'scene/2d')
-rw-r--r-- | scene/2d/tile_map.cpp | 247 | ||||
-rw-r--r-- | scene/2d/tile_map.h | 29 |
2 files changed, 243 insertions, 33 deletions
diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp index f067b5a187..f8bc27ccf6 100644 --- a/scene/2d/tile_map.cpp +++ b/scene/2d/tile_map.cpp @@ -365,6 +365,11 @@ void TileMap::_update_dirty_quadrants() { } Rect2 r = tile_set->tile_get_region(c.id); + if (tile_set->tile_get_is_autotile(c.id)) { + int spacing = tile_set->autotile_get_spacing(c.id); + r.size = tile_set->autotile_get_size(c.id); + r.position += (r.size + Vector2(spacing, spacing)) * Vector2(c.autotile_coord_x, c.autotile_coord_y); + } Size2 s = tex->get_size(); if (r == Rect2()) @@ -456,21 +461,23 @@ void TileMap::_update_dirty_quadrants() { for (int i = 0; i < shapes.size(); i++) { Ref<Shape2D> shape = shapes[i].shape; if (shape.is_valid()) { - Transform2D xform; - xform.set_origin(offset.floor()); - - Vector2 shape_ofs = tile_set->tile_get_shape_offset(c.id, i); - - _fix_cell_transform(xform, c, shape_ofs + center_ofs, s); - - if (debug_canvas_item.is_valid()) { - vs->canvas_item_add_set_transform(debug_canvas_item, xform); - shape->draw(debug_canvas_item, debug_collision_color); + if (!tile_set->tile_get_is_autotile(c.id) || (shapes[i].autotile_coord.x == c.autotile_coord_x && shapes[i].autotile_coord.y == c.autotile_coord_y)) { + Transform2D xform; + xform.set_origin(offset.floor()); + + Vector2 shape_ofs = tile_set->tile_get_shape_offset(c.id, i); + + _fix_cell_transform(xform, c, shape_ofs + center_ofs, s); + + if (debug_canvas_item.is_valid()) { + vs->canvas_item_add_set_transform(debug_canvas_item, xform); + shape->draw(debug_canvas_item, debug_collision_color); + } + ps->body_add_shape(q.body, shape->get_rid(), xform); + ps->body_set_shape_metadata(q.body, shape_idx, Vector2(E->key().x, E->key().y)); + ps->body_set_shape_as_one_way_collision(q.body, shape_idx, shapes[i].one_way_collision); + shape_idx++; } - ps->body_add_shape(q.body, shape->get_rid(), xform); - ps->body_set_shape_metadata(q.body, shape_idx, Vector2(E->key().x, E->key().y)); - ps->body_set_shape_as_one_way_collision(q.body, shape_idx, shapes[i].one_way_collision); - shape_idx++; } } @@ -479,9 +486,17 @@ void TileMap::_update_dirty_quadrants() { } if (navigation) { - Ref<NavigationPolygon> navpoly = tile_set->tile_get_navigation_polygon(c.id); + Ref<NavigationPolygon> navpoly; + Vector2 npoly_ofs; + if (tile_set->tile_get_is_autotile(c.id)) { + navpoly = tile_set->autotile_get_navigation_polygon(c.id, Vector2(c.autotile_coord_x, c.autotile_coord_y)); + npoly_ofs = Vector2(); + } else { + navpoly = tile_set->tile_get_navigation_polygon(c.id); + npoly_ofs = tile_set->tile_get_navigation_polygon_offset(c.id); + } + if (navpoly.is_valid()) { - Vector2 npoly_ofs = tile_set->tile_get_navigation_polygon_offset(c.id); Transform2D xform; xform.set_origin(offset.floor() + q.pos); _fix_cell_transform(xform, c, npoly_ofs + center_ofs, s); @@ -495,10 +510,17 @@ void TileMap::_update_dirty_quadrants() { } } - Ref<OccluderPolygon2D> occluder = tile_set->tile_get_light_occluder(c.id); + Ref<OccluderPolygon2D> occluder; + Vector2 occluder_ofs; + if (tile_set->tile_get_is_autotile(c.id)) { + occluder = tile_set->autotile_get_light_occluder(c.id, Vector2(c.autotile_coord_x, c.autotile_coord_y)); + occluder_ofs = tile_set->tile_get_occluder_offset(c.id); + } else { + occluder = tile_set->tile_get_light_occluder(c.id); + occluder_ofs = Vector2(); + } if (occluder.is_valid()) { - Vector2 occluder_ofs = tile_set->tile_get_occluder_offset(c.id); Transform2D xform; xform.set_origin(offset.floor() + q.pos); _fix_cell_transform(xform, c, occluder_ofs + center_ofs, s); @@ -656,7 +678,7 @@ void TileMap::set_cellv(const Vector2 &p_pos, int p_tile, bool p_flip_x, bool p_ set_cell(p_pos.x, p_pos.y, p_tile, p_flip_x, p_flip_y, p_transpose); } -void TileMap::set_cell(int p_x, int p_y, int p_tile, bool p_flip_x, bool p_flip_y, bool p_transpose) { +void TileMap::set_cell(int p_x, int p_y, int p_tile, bool p_flip_x, bool p_flip_y, bool p_transpose, Vector2 p_autotile_coord) { PosKey pk(p_x, p_y); @@ -702,15 +724,105 @@ void TileMap::set_cell(int p_x, int p_y, int p_tile, bool p_flip_x, bool p_flip_ c.flip_h = p_flip_x; c.flip_v = p_flip_y; c.transpose = p_transpose; + c.autotile_coord_x = (uint16_t)p_autotile_coord.x; + c.autotile_coord_y = (uint16_t)p_autotile_coord.y; _make_quadrant_dirty(Q); used_size_cache_dirty = true; } int TileMap::get_cellv(const Vector2 &p_pos) const { + return get_cell(p_pos.x, p_pos.y); } +void TileMap::make_bitmask_area_dirty(const Vector2 &p_pos) { + + for (int x = p_pos.x - 1; x <= p_pos.x + 1; x++) { + for (int y = p_pos.y - 1; x <= p_pos.y + 1; y++) { + PosKey p(x, y); + if (dirty_bitmask.find(p) == NULL) { + dirty_bitmask.push_back(p); + } + } + } +} + +void TileMap::update_bitmask_area(const Vector2 &p_pos) { + + for (int x = p_pos.x - 1; x <= p_pos.x + 1; x++) { + for (int y = p_pos.y - 1; y <= p_pos.y + 1; y++) { + update_cell_bitmask(x, y); + } + } +} + +void TileMap::update_cell_bitmask(int p_x, int p_y) { + + PosKey p(p_x, p_y); + Map<PosKey, Cell>::Element *E = tile_map.find(p); + if (E != NULL) { + int id = get_cell(p_x, p_y); + if (tile_set->tile_get_is_autotile(id)) { + uint16_t mask = 0; + if (tile_set->autotile_get_bitmask_mode(id) == TileSet::BITMASK_2X2) { + if (tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y))) { + mask |= TileSet::BIND_TOPLEFT; + } + if (tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y))) { + mask |= TileSet::BIND_TOPRIGHT; + } + if (tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y))) { + mask |= TileSet::BIND_BOTTOMLEFT; + } + if (tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y))) { + mask |= TileSet::BIND_BOTTOMRIGHT; + } + } else if (tile_set->autotile_get_bitmask_mode(id) == TileSet::BITMASK_3X3) { + if (tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y))) { + mask |= TileSet::BIND_TOPLEFT; + } + if (tile_set->is_tile_bound(id, get_cell(p_x, p_y - 1))) { + mask |= TileSet::BIND_TOP; + } + if (tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y - 1)) && tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y))) { + mask |= TileSet::BIND_TOPRIGHT; + } + if (tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y))) { + mask |= TileSet::BIND_LEFT; + } + mask |= TileSet::BIND_CENTER; + if (tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y))) { + mask |= TileSet::BIND_RIGHT; + } + if (tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x - 1, p_y))) { + mask |= TileSet::BIND_BOTTOMLEFT; + } + if (tile_set->is_tile_bound(id, get_cell(p_x, p_y + 1))) { + mask |= TileSet::BIND_BOTTOM; + } + if (tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x, p_y + 1)) && tile_set->is_tile_bound(id, get_cell(p_x + 1, p_y))) { + mask |= TileSet::BIND_BOTTOMRIGHT; + } + } + Vector2 coord = tile_set->autotile_get_subtile_for_bitmask(id, mask, this, Vector2(p_x, p_y)); + E->get().autotile_coord_x = (int)coord.x; + E->get().autotile_coord_y = (int)coord.y; + } else { + E->get().autotile_coord_x = 0; + E->get().autotile_coord_y = 0; + } + } +} + +void TileMap::update_dirty_bitmask() { + + while (dirty_bitmask.size() > 0) { + update_cell_bitmask(dirty_bitmask[0].x, dirty_bitmask[0].y); + dirty_bitmask.pop_front(); + } +} + int TileMap::get_cell(int p_x, int p_y) const { PosKey pk(p_x, p_y); @@ -756,6 +868,30 @@ bool TileMap::is_cell_transposed(int p_x, int p_y) const { return E->get().transpose; } +int TileMap::get_cell_autotile_coord_x(int p_x, int p_y) const { + + PosKey pk(p_x, p_y); + + const Map<PosKey, Cell>::Element *E = tile_map.find(pk); + + if (!E) + return 0; + + return E->get().autotile_coord_x; +} + +int TileMap::get_cell_autotile_coord_y(int p_x, int p_y) const { + + PosKey pk(p_x, p_y); + + const Map<PosKey, Cell>::Element *E = tile_map.find(pk); + + if (!E) + return 0; + + return E->get().autotile_coord_y; +} + void TileMap::_recreate_quadrants() { _clear_quadrants(); @@ -823,11 +959,13 @@ void TileMap::_set_tile_data(const PoolVector<int> &p_data) { int c = p_data.size(); PoolVector<int>::Read r = p_data.read(); - for (int i = 0; i < c; i += 2) { + int offset = (format == FORMAT_2_1_5) ? 3 : 2; + + for (int i = 0; i < c; i += offset) { const uint8_t *ptr = (const uint8_t *)&r[i]; - uint8_t local[8]; - for (int j = 0; j < 8; j++) + uint8_t local[12]; + for (int j = 0; j < ((format == FORMAT_2_1_5) ? 12 : 8); j++) local[j] = ptr[j]; #ifdef BIG_ENDIAN_ENABLED @@ -836,6 +974,11 @@ void TileMap::_set_tile_data(const PoolVector<int> &p_data) { SWAP(local[1], local[2]); SWAP(local[4], local[7]); SWAP(local[5], local[6]); + //TODO: ask someone to check this... + if (FORMAT == FORMAT_2_1_5) { + SWAP(local[8], local[11]); + SWAP(local[9], local[10]); + } #endif int16_t x = decode_uint16(&local[0]); @@ -845,24 +988,28 @@ void TileMap::_set_tile_data(const PoolVector<int> &p_data) { bool flip_v = v & (1 << 30); bool transpose = v & (1 << 31); v &= (1 << 29) - 1; - + int16_t coord_x; + int16_t coord_y; + if (format == FORMAT_2_1_5) { + coord_x = decode_uint16(&local[8]); + coord_y = decode_uint16(&local[10]); + } /* if (x<-20 || y <-20 || x>4000 || y>4000) continue; */ - set_cell(x, y, v, flip_h, flip_v, transpose); + set_cell(x, y, v, flip_h, flip_v, transpose, Vector2(coord_x, coord_y)); } } PoolVector<int> TileMap::_get_tile_data() const { PoolVector<int> data; - data.resize(tile_map.size() * 2); + data.resize(tile_map.size() * 3); PoolVector<int>::Write w = data.write(); int idx = 0; for (const Map<PosKey, Cell>::Element *E = tile_map.front(); E; E = E->next()) { - uint8_t *ptr = (uint8_t *)&w[idx]; encode_uint16(E->key().x, &ptr[0]); encode_uint16(E->key().y, &ptr[2]); @@ -873,9 +1020,10 @@ PoolVector<int> TileMap::_get_tile_data() const { val |= (1 << 30); if (E->get().transpose) val |= (1 << 31); - encode_uint32(val, &ptr[4]); - idx += 2; + encode_uint16(E->get().autotile_coord_x, &ptr[8]); + encode_uint16(E->get().autotile_coord_y, &ptr[10]); + idx += 3; } w = PoolVector<int>::Write(); @@ -1119,10 +1267,50 @@ Vector2 TileMap::_map_to_world(int p_x, int p_y, bool p_ignore_ofs) const { } return ret; } + +bool TileMap::_set(const StringName &p_name, const Variant &p_value) { + + if (p_name == "format") { + if (p_value.get_type() == Variant::INT) { + format = (DataFormat)(p_value.operator int64_t()); + return true; + } + } else if (p_name == "tile_data") { + if (p_value.is_array()) { + _set_tile_data(p_value); + return true; + } + return false; + } + return false; +} + +bool TileMap::_get(const StringName &p_name, Variant &r_ret) const { + + if (p_name == "format") { + r_ret = FORMAT_2_1_5; + return true; + } else if (p_name == "tile_data") { + r_ret = _get_tile_data(); + return true; + } + return false; +} + +void TileMap::_get_property_list(List<PropertyInfo> *p_list) const { + + PropertyInfo p(Variant::INT, "format", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR); + p_list->push_back(p); + + p = PropertyInfo(Variant::OBJECT, "tile_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR); + p_list->push_back(p); +} + Vector2 TileMap::map_to_world(const Vector2 &p_pos, bool p_ignore_ofs) const { return _map_to_world(p_pos.x, p_pos.y, p_ignore_ofs); } + Vector2 TileMap::world_to_map(const Vector2 &p_pos) const { Vector2 ret = get_cell_transform().affine_inverse().xform(p_pos); @@ -1357,8 +1545,6 @@ void TileMap::_bind_methods() { ADD_GROUP("Occluder", "occluder_"); ADD_PROPERTY(PropertyInfo(Variant::INT, "occluder_light_mask", PROPERTY_HINT_LAYERS_2D_RENDER), "set_occluder_light_mask", "get_occluder_light_mask"); - ADD_GROUP("", ""); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tile_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_tile_data", "_get_tile_data"); ADD_SIGNAL(MethodInfo("settings_changed")); @@ -1398,6 +1584,7 @@ TileMap::TileMap() { y_sort_mode = false; occluder_light_mask = 1; clip_uv = false; + format = FORMAT_2_1_4; //Always initialize with the lowest format fp_adjust = 0.00001; tile_origin = TILE_ORIGIN_TOP_LEFT; diff --git a/scene/2d/tile_map.h b/scene/2d/tile_map.h index 9e14ec838a..a0ca2e6a35 100644 --- a/scene/2d/tile_map.h +++ b/scene/2d/tile_map.h @@ -60,6 +60,11 @@ public: }; private: + enum DataFormat { + FORMAT_2_1_4 = 0, + FORMAT_2_1_5 + }; + Ref<TileSet> tile_set; Size2i cell_size; int quadrant_size; @@ -81,6 +86,8 @@ private: //using a more precise comparison so the regions can be sorted later bool operator<(const PosKey &p_k) const { return (y == p_k.y) ? x < p_k.x : y < p_k.y; } + bool operator==(const PosKey &p_k) const { return (y == p_k.y && x == p_k.x); } + PosKey(int16_t p_x, int16_t p_y) { x = p_x; y = p_y; @@ -98,13 +105,17 @@ private: bool flip_h : 1; bool flip_v : 1; bool transpose : 1; + int16_t autotile_coord_x : 16; + int16_t autotile_coord_y : 16; }; - uint32_t _u32t; - Cell() { _u32t = 0; } + uint64_t _u64t; + Cell() { _u64t = 0; } }; Map<PosKey, Cell> tile_map; + List<PosKey> dirty_bitmask; + struct Quadrant { Vector2 pos; @@ -167,6 +178,7 @@ private: float bounce; uint32_t collision_layer; uint32_t collision_mask; + DataFormat format; TileOrigin tile_origin; @@ -198,6 +210,10 @@ private: _FORCE_INLINE_ Vector2 _map_to_world(int p_x, int p_y, bool p_ignore_ofs = false) const; protected: + bool _set(const StringName &p_name, const Variant &p_value); + bool _get(const StringName &p_name, Variant &r_ret) const; + void _get_property_list(List<PropertyInfo> *p_list) const; + void _notification(int p_what); static void _bind_methods(); @@ -220,17 +236,24 @@ public: void set_center_y(bool p_enable); bool get_center_y() const; - void set_cell(int p_x, int p_y, int p_tile, bool p_flip_x = false, bool p_flip_y = false, bool p_transpose = false); + void set_cell(int p_x, int p_y, int p_tile, bool p_flip_x = false, bool p_flip_y = false, bool p_transpose = false, Vector2 p_autotile_coord = Vector2()); int get_cell(int p_x, int p_y) const; bool is_cell_x_flipped(int p_x, int p_y) const; bool is_cell_y_flipped(int p_x, int p_y) const; bool is_cell_transposed(int p_x, int p_y) const; + int get_cell_autotile_coord_x(int p_x, int p_y) const; + int get_cell_autotile_coord_y(int p_x, int p_y) const; void set_cellv(const Vector2 &p_pos, int p_tile, bool p_flip_x = false, bool p_flip_y = false, bool p_transpose = false); int get_cellv(const Vector2 &p_pos) const; Rect2 _edit_get_rect() const; + void make_bitmask_area_dirty(const Vector2 &p_pos); + void update_bitmask_area(const Vector2 &p_pos); + void update_cell_bitmask(int p_x, int p_y); + void update_dirty_bitmask(); + void set_collision_layer(uint32_t p_layer); uint32_t get_collision_layer() const; |