Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to 4.3 (Support TilemapLayers) #158

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,3 @@ func _set_grid_area(area: Rect2i) -> void:
grid.set_valuexy(x, y, settings.tile)
elif y == -height - 1 and settings.air_layer:
grid.set_valuexy(x, y, null)


Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func _propagate(coords: Vector2i) -> void:

func _get_possible_neighbors(coords: Vector2i, direction: Vector2i) -> Array[WaveFunction2DEntry]:
var possible_neighbors: Array[WaveFunction2DEntry]
var dir_key := ADJACENT_NEIGHBORS.find_key(direction)
var dir_key = ADJACENT_NEIGHBORS.find_key(direction)
for entry in _wave_function[coords]:
for other_entry in _wave_function[coords + direction]:
if other_entry.tile_info.id in entry.get("neighbors_%s" % dir_key):
Expand Down
4 changes: 2 additions & 2 deletions addons/gaea/plugin.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[plugin]

name="Gaea"
description="A procedural generation add-on for Godot 4.2.2"
description="A procedural generation add-on for Godot 4.3"
author="BenjaTK"
version="1.1.3"
version="1.2.0"
script="plugin.gd"
118 changes: 102 additions & 16 deletions addons/gaea/renderers/2D/tilemap_gaea_renderer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ extends GaeaRenderer2D
## Takes [TilemapTileInfo] to determine which tile to place
## in every cell.

enum NodeType {
TILEMAP_LAYERS, ## Use [TileMapLayer]s, with an array of them determining which one is which.
TILEMAP ## Use a single [TileMap] node (not recommended by Godot).
}

@export var node_type: NodeType = NodeType.TILEMAP_LAYERS :
set(value):
node_type = value
notify_property_list_changed()
@export var tile_map_layers: Array[TileMapLayer] :
set(value):
tile_map_layers = value
update_configuration_warnings()
@export var tile_map: TileMap :
set(value):
tile_map = value
Expand All @@ -20,15 +32,38 @@ extends GaeaRenderer2D

func _ready() -> void:
super()
if Vector2i(Vector2(tile_map.tile_set.tile_size) * tile_map.scale) != generator.tile_size:
push_warning("TileMap's tile size doesn't match with generator's tile size, can cause generation issues.
The generator's tile size has been set to the TileMap's tile size.")
generator.tile_size = Vector2(tile_map.tile_set.tile_size) * tile_map.scale
match node_type:
NodeType.TILEMAP:
if not is_instance_valid(tile_map):
push_error("TilemapGaeaRenderer needs TileMap to work.")
return

if Vector2i(Vector2(tile_map.tile_set.tile_size) * tile_map.scale) != generator.tile_size:
push_warning("TileMap's tile size doesn't match with generator's tile size, can cause generation issues.
The generator's tile size has been set to the TileMap's tile size.")
generator.tile_size = Vector2(tile_map.tile_set.tile_size) * tile_map.scale
NodeType.TILEMAP_LAYERS:
if tile_map_layers.is_empty():
push_error("TilemapGaeaRenderer needs at least one TileMapLayer to work.")
return

var layer: TileMapLayer = tile_map_layers.front()
if Vector2i(Vector2(layer.tile_set.tile_size) * layer.scale) != generator.tile_size:
push_warning("The TileMapLayer's tile size doesn't match with generator's tile size, can cause generation issues.
The generator's tile size has been set to the layer's tile size. (Only layer 0 checked)")
generator.tile_size = Vector2(layer.tile_set.tile_size) * layer.scale


func _draw_area(area: Rect2i) -> void:
var terrains: Dictionary

if not is_instance_valid(tile_map) and node_type == NodeType.TILEMAP:
push_error("Invalid TileMap, can't draw area.")
return
elif tile_map_layers.is_empty() and node_type == NodeType.TILEMAP_LAYERS:
push_error("No TileMapLayers assigned, can't draw area.")
return

for x in range(area.position.x, area.end.x + 1):
for y in range(area.position.y, area.end.y + 1):
var tile_position := Vector2i(x, y)
Expand All @@ -40,8 +75,8 @@ func _draw_area(area: Rect2i) -> void:
break

if not has_cell_in_position:
for l in range(tile_map.get_layers_count()):
tile_map.call_thread_safe("erase_cell", l, Vector2i(x, y)) # thread_safe paces these calls out when threaded.
for l in range(_get_tilemap_layers_count()):
_erase_tilemap_cell(l, Vector2i(x, y))
continue

for layer in range(generator.grid.get_layer_count()):
Expand All @@ -53,37 +88,88 @@ func _draw_area(area: Rect2i) -> void:

match tile_info.type:
TilemapTileInfo.Type.SINGLE_CELL:
tile_map.call_thread_safe("set_cell", # thread_safe paces these calls out when threaded.
tile_info.tilemap_layer, tile, tile_info.source_id,
tile_info.atlas_coord, tile_info.alternative_tile
)
_set_tile(tile_position, tile_info)

TilemapTileInfo.Type.TERRAIN:
if not terrains.has(tile_info):
terrains[tile_info] = [tile]
else:
terrains[tile_info].append(tile)

for tile_info in terrains:
tile_map.set_cells_terrain_connect.call_deferred(
tile_info.tilemap_layer, terrains[tile_info],
tile_info.terrain_set, tile_info.terrain, !terrain_gap_fix
)
_set_terrain(terrains[tile_info], tile_info)

(func(): area_rendered.emit(area)).call_deferred()


func _draw() -> void:
if clear_tile_map_on_draw:
tile_map.clear()
if node_type == NodeType.TILEMAP:
tile_map.clear()
else:
for layer in tile_map_layers:
layer.clear()
super._draw()


func _set_tile(cell: Vector2i, tile_info: TilemapTileInfo) -> void:
match node_type:
NodeType.TILEMAP:
tile_map.call_thread_safe("set_cell", # thread_safe paces these calls out when threaded.
tile_info.tilemap_layer, cell, tile_info.source_id,
tile_info.atlas_coord, tile_info.alternative_tile
)
NodeType.TILEMAP_LAYERS:
tile_map_layers[tile_info.tilemap_layer].call_thread_safe("set_cell",
cell, tile_info.source_id,
tile_info.atlas_coord, tile_info.alternative_tile
)


func _set_terrain(cells: Array, tile_info: TilemapTileInfo) -> void:
match node_type:
NodeType.TILEMAP:
tile_map.set_cells_terrain_connect.call_deferred(
tile_info.tilemap_layer, cells,
tile_info.terrain_set, tile_info.terrain, !terrain_gap_fix
)
NodeType.TILEMAP_LAYERS:
tile_map_layers[tile_info.tilemap_layer].set_cells_terrain_connect.call_deferred(
cells, tile_info.terrain_set, tile_info.terrain, !terrain_gap_fix
)


func _get_tilemap_layers_count() -> int:
match node_type:
NodeType.TILEMAP:
return tile_map.get_layers_count()
NodeType.TILEMAP_LAYERS:
return tile_map_layers.size()
return 0


func _erase_tilemap_cell(layer: int, cell: Vector2i) -> void:
match node_type:
NodeType.TILEMAP:
tile_map.call_thread_safe("erase_cell", layer, cell) # thread_safe paces these calls out when threaded.
NodeType.TILEMAP_LAYERS:
tile_map_layers[layer].call_thread_safe("erase_cell", cell)

func _get_configuration_warnings() -> PackedStringArray:
var warnings: PackedStringArray

warnings.append_array(super._get_configuration_warnings())

if not is_instance_valid(tile_map):
if not is_instance_valid(tile_map) and node_type == NodeType.TILEMAP:
warnings.append("Needs a TileMap to work.")
elif tile_map_layers.is_empty() and node_type == NodeType.TILEMAP_LAYERS:
warnings.append("Needs at least one TileMapLayer to work.")

return warnings


func _validate_property(property: Dictionary) -> void:
if property.name == "tile_map" and node_type == NodeType.TILEMAP_LAYERS:
property.usage = PROPERTY_USAGE_NONE
elif property.name == "tile_map_layers" and node_type == NodeType.TILEMAP:
property.usage = PROPERTY_USAGE_NONE
8 changes: 4 additions & 4 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ config_version=5
config/name="Gaea"
config/tags=PackedStringArray("addon")
run/main_scene="res://scenes/test scene/test_scene.tscn"
config/features=PackedStringArray("4.2", "Forward Plus")
config/features=PackedStringArray("4.3", "Forward Plus")
config/icon="res://icon.svg"

[debug]
Expand All @@ -40,17 +40,17 @@ enabled=PackedStringArray("res://addons/gaea/plugin.cfg", "res://addons/gdscript

move_left={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"echo":false,"script":null)
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null)
]
}
move_right={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"echo":false,"script":null)
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null)
]
}
jump={
"deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"echo":false,"script":null)
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":32,"key_label":0,"unicode":32,"location":0,"echo":false,"script":null)
]
}

Expand Down
23 changes: 10 additions & 13 deletions scenes/demos/cellular/cellular_demo.tscn

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions scenes/demos/heightmap/chunks_heightmap_demo.tscn

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions scenes/demos/heightmap/heightmap_demo.tscn

Large diffs are not rendered by default.

19 changes: 11 additions & 8 deletions scenes/demos/heightmap/terraria-like_generation_settings.tres
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
[gd_resource type="Resource" script_class="HeightmapGenerator2DSettings" load_steps=31 format=3 uid="uid://db18nmw5jkbtc"]
[gd_resource type="Resource" script_class="HeightmapGenerator2DSettings" load_steps=34 format=3 uid="uid://db18nmw5jkbtc"]

[ext_resource type="Script" path="res://addons/gaea/modifiers/2D/modifier_2d.gd" id="1_fksxq"]
[ext_resource type="Script" path="res://addons/gaea/modifiers/2D/heightmap_painter.gd" id="1_qj0ns"]
[ext_resource type="Script" path="res://addons/gaea/tile_info/tilemap_tile_info.gd" id="2_6f082"]
[ext_resource type="Script" path="res://addons/gaea/modifiers/2D/carver.gd" id="3_w1n11"]
[ext_resource type="Script" path="res://addons/gaea/modifiers/2D/noise_painter.gd" id="4_xyqnb"]
[ext_resource type="Script" path="res://addons/gaea/generators/2D/heightmap_generator/heightmap_generator_2d_settings.gd" id="5_haojk"]
[ext_resource type="Script" path="res://addons/gaea/modifiers/2D/conditions/offset_condition_2d.gd" id="5_yd776"]
[ext_resource type="Script" path="res://addons/gaea/modifiers/conditions/chance_condition.gd" id="6_ei8am"]
[ext_resource type="Script" path="res://addons/gaea/modifiers/conditions/condition.gd" id="6_q4bb5"]
[ext_resource type="Script" path="res://addons/gaea/modifiers/2D/advanced_modifier.gd" id="7_ei7y7"]
[ext_resource type="Script" path="res://addons/gaea/tile_info/random_tile_info.gd" id="8_uaynf"]
[ext_resource type="Script" path="res://addons/gaea/tile_info/tile_info.gd" id="11_ixuoq"]

[sub_resource type="Resource" id="Resource_6kmi4"]
script = ExtResource("2_6f082")
Expand Down Expand Up @@ -56,7 +59,7 @@ filter_type = 0

[sub_resource type="FastNoiseLite" id="FastNoiseLite_1mckf"]
noise_type = 3
seed = -764944331
seed = 1407555525
frequency = 0.012
fractal_type = 2
fractal_octaves = 3
Expand All @@ -77,7 +80,7 @@ affected_layers = Array[int]([0])
filter_type = 0

[sub_resource type="FastNoiseLite" id="FastNoiseLite_kyspj"]
seed = -1324448828
seed = 848051028
frequency = 0.0617
fractal_octaves = 2
fractal_lacunarity = 2.95
Expand Down Expand Up @@ -108,7 +111,7 @@ enabled = true
filter_type = 0

[sub_resource type="FastNoiseLite" id="FastNoiseLite_vkmxs"]
seed = -1330625371
seed = 841874485
frequency = 0.0511
fractal_lacunarity = 0.565
domain_warp_type = 2
Expand Down Expand Up @@ -186,7 +189,7 @@ layer = 0

[sub_resource type="Resource" id="Resource_piada"]
script = ExtResource("8_uaynf")
tiles = Array[Resource("res://addons/gaea/tile_info/tile_info.gd")]([SubResource("Resource_a6kch"), SubResource("Resource_6jmcy"), SubResource("Resource_nbea2")])
tiles = Array[ExtResource("11_ixuoq")]([SubResource("Resource_a6kch"), SubResource("Resource_6jmcy"), SubResource("Resource_nbea2")])
use_weights = true
weight_0 = 0.4
weight_1 = 1.0
Expand All @@ -197,7 +200,7 @@ layer = 0
[sub_resource type="Resource" id="Resource_f4y1w"]
resource_name = "Vegetation"
script = ExtResource("7_ei7y7")
conditions = Array[Resource("res://addons/gaea/modifiers/conditions/condition.gd")]([SubResource("Resource_06osm"), SubResource("Resource_g0joc")])
conditions = Array[ExtResource("6_q4bb5")]([SubResource("Resource_06osm"), SubResource("Resource_g0joc")])
tile = SubResource("Resource_piada")
salt = 3487287303
enabled = true
Expand All @@ -207,7 +210,7 @@ filter_layers = Array[int]([])

[sub_resource type="FastNoiseLite" id="FastNoiseLite_mwtbk"]
noise_type = 3
seed = 1061969268
seed = -1060498172

[sub_resource type="Resource" id="Resource_hitrj"]
resource_name = "Ground"
Expand All @@ -230,4 +233,4 @@ height_offset = 160
height_intensity = 25
min_height = -32
air_layer = true
modifiers = Array[Resource("res://addons/gaea/modifiers/2D/modifier_2d.gd")]([SubResource("Resource_pu43q"), SubResource("Resource_855mm"), SubResource("Resource_q887n"), SubResource("Resource_2w3lo"), SubResource("Resource_08u4d"), SubResource("Resource_f4y1w")])
modifiers = Array[ExtResource("1_fksxq")]([SubResource("Resource_pu43q"), SubResource("Resource_855mm"), SubResource("Resource_q887n"), SubResource("Resource_2w3lo"), SubResource("Resource_08u4d"), SubResource("Resource_f4y1w")])
Loading