Skip to content

Commit a83f2e8

Browse files
authored
Added support for tilemap patterns to TilemapRenderer and TilemapMaterial (#414)
1 parent f73fd88 commit a83f2e8

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

addons/gaea/nodes/renderers/tilemap_renderer.gd

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func _render(grid: GaeaGrid) -> void:
1515

1616
for layer_idx in grid.get_layers_count():
1717
var terrains: Dictionary[TileMapGaeaMaterial, Array] = {}
18+
var patterns: Dictionary[TileMapGaeaMaterial, Array] = {}
1819
if tile_map_layers.size() <= layer_idx or not is_instance_valid(tile_map_layers.get(layer_idx)):
1920
continue
2021

@@ -25,12 +26,22 @@ func _render(grid: GaeaGrid) -> void:
2526
tile_map_layers[layer_idx].set_cell(Vector2i(cell.x, cell.y), value.source_id, value.atlas_coord, value.alternative_tile)
2627
elif value.type == TileMapGaeaMaterial.Type.TERRAIN:
2728
terrains.get_or_add(value, []).append(Vector2i(cell.x, cell.y))
29+
elif value.type == TileMapGaeaMaterial.Type.PATTERN:
30+
patterns.get_or_add(value, []).append(Vector2i(cell.x, cell.y))
2831

29-
for material: TileMapGaeaMaterial in terrains:
32+
for terrain_material: TileMapGaeaMaterial in terrains:
3033
tile_map_layers[layer_idx].set_cells_terrain_connect(
31-
terrains.get(material), material.terrain_set, material.terrain
34+
terrains.get(terrain_material), terrain_material.terrain_set, terrain_material.terrain
3235
)
3336

37+
for pattern_material: TileMapGaeaMaterial in patterns:
38+
var pattern := tile_map_layers[layer_idx].tile_set.get_pattern(pattern_material.pattern_index)
39+
for cell in patterns.get(pattern_material):
40+
tile_map_layers[layer_idx].set_pattern(
41+
cell + pattern_material.pattern_offset,
42+
pattern
43+
)
44+
3445

3546
func _on_area_erased(area: AABB) -> void:
3647
for x in range(area.position.x, area.end.x):

addons/gaea/resources/materials/data/tilemap_material.gd

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ extends GaeaMaterial
55

66
enum Type {
77
SINGLE_CELL, ## Tile is just a single cell in the TileMap. Requires a [param source_id] and a [param atlas_coord]. Can optionally be an [param alternative_tile].
8-
TERRAIN ## Tile is a terrain from a terrain set. Allows for autotiling. Requires a [param terrain_set] and a [param terrain]
8+
TERRAIN, ## Tile is a terrain from a terrain set. Allows for autotiling. Requires a [param terrain_set] and a [param terrain]
9+
PATTERN ## Tile is a pattern of cell. Requires a [param pattern_index] and a [param pattern_offset].
910
}
1011

1112
## Determines how the [TileMapGaeaRenderer] uses this material.
@@ -28,16 +29,22 @@ enum Type {
2829
@export var terrain_set: int = 0
2930
## Terrain in the terrain set determined previously.
3031
@export var terrain: int = 0
32+
## Pattern index in the pattern list of the [TileSet].
33+
@export var pattern_index: int = 0
34+
## Pattern offset use to shift the tiles from the origin (Top left corner of the pattern).
35+
@export var pattern_offset: Vector2i = Vector2i.ZERO
3136

3237

3338
func _validate_property(property: Dictionary) -> void:
34-
match type:
35-
Type.SINGLE_CELL:
36-
if property.name.begins_with("terrain"):
37-
property.usage = PROPERTY_USAGE_NONE
38-
Type.TERRAIN:
39-
if property.name in ["source_id", "atlas_coord", "alternative_tile"]:
40-
property.usage = PROPERTY_USAGE_NONE
39+
if type != Type.SINGLE_CELL:
40+
if property.name in ["source_id", "atlas_coord", "alternative_tile"]:
41+
property.usage = PROPERTY_USAGE_NONE
42+
if type != Type.TERRAIN:
43+
if property.name.begins_with("terrain"):
44+
property.usage = PROPERTY_USAGE_NONE
45+
if type != Type.PATTERN:
46+
if property.name.begins_with("pattern"):
47+
property.usage = PROPERTY_USAGE_NONE
4148

4249

4350
func _is_data() -> bool:

0 commit comments

Comments
 (0)