Skip to content

Commit

Permalink
Merge pull request #131 from BenjaTK/code-cleanup
Browse files Browse the repository at this point in the history
Code cleanup (remove unnecesary classes, format using addon and more)
  • Loading branch information
BenjaTK authored Jun 23, 2024
2 parents 7fe2d66 + 6bc48ef commit ee4ff6f
Show file tree
Hide file tree
Showing 71 changed files with 1,015 additions and 452 deletions.
4 changes: 1 addition & 3 deletions addons/gaea/editor/download_update_panel.gd
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
@tool
extends Control


signal failed()
signal failed
signal updated(new_version: String)

const TEMP_FILE_PATH = "user://temp.zip"
Expand All @@ -12,7 +11,6 @@ const TEMP_FILE_PATH = "user://temp.zip"
@onready var download_button: Button = %DownloadButton
@onready var release_notes_button: LinkButton = %ReleaseNotesButton


var next_version_release: Dictionary:
set(value):
next_version_release = value
Expand Down
5 changes: 1 addition & 4 deletions addons/gaea/editor/generator_buttons.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extends EditorProperty


var button := Button.new()


Expand All @@ -11,8 +10,7 @@ func _init() -> void:
container.custom_minimum_size.x = 130
add_child(container)

var generate_button := _add_button(
container, "Generate", preload("./reload.svg"), _on_generate_pressed)
var generate_button := _add_button(container, "Generate", preload("./reload.svg"), _on_generate_pressed)
generate_button.custom_minimum_size.x = 110

_add_button(container, "Clear", preload("./clear.svg"), _on_clear_pressed)
Expand Down Expand Up @@ -42,4 +40,3 @@ func _on_clear_pressed() -> void:
var object = get_edited_object()
if object.has_method("erase"):
object.call("erase")

31 changes: 22 additions & 9 deletions addons/gaea/editor/inspector_plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,27 @@ func _parse_begin(object: Object) -> void:
var generator_buttons := preload("./generator_buttons.gd").new()
add_custom_control(generator_buttons)

if (object is Modifier and object.get("noise") and (object.get("threshold") or (object.get("min") and object.get("max")))) or\
(object is NoiseGeneratorData and object.settings):
var texture_rect := preload("./threshold_visualizer.gd").new()
texture_rect.object = object
if object is Modifier:
if not object.get("noise"):
return

add_custom_control(texture_rect)
if not object.get("threshold"):
return

texture_rect.update()
if object.get("noise"):
object.get("noise").changed.connect(texture_rect.update)
object.changed.connect(texture_rect.update)
if not object.get("min") and object.get("max"):
return
elif object is NoiseGeneratorData:
if not object.settings:
return
else:
return

var texture_rect := preload("./threshold_visualizer.gd").new()
texture_rect.object = object

add_custom_control(texture_rect)

texture_rect.update()
if object.get("noise"):
object.get("noise").changed.connect(texture_rect.update)
object.changed.connect(texture_rect.update)
1 change: 0 additions & 1 deletion addons/gaea/editor/threshold_visualizer.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extends TextureRect


var object: Object


Expand Down
19 changes: 12 additions & 7 deletions addons/gaea/generators/2D/cellular_generator/cellular_generator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ extends GaeaGenerator2D
## @tutorial(Generators): https://benjatk.github.io/Gaea/#/generators/
## @tutorial(CellularGenerator): https://benjatk.github.io/Gaea/#/generators/cellular


@export var settings: CellularGeneratorSettings


Expand All @@ -21,7 +20,7 @@ func generate(starting_grid: GaeaGrid = null) -> void:
return

generation_started.emit()
var time_now :int = Time.get_ticks_msec()
var _time_now: int = Time.get_ticks_msec()

if starting_grid == null:
erase()
Expand All @@ -36,9 +35,9 @@ func generate(starting_grid: GaeaGrid = null) -> void:
next_pass.generate(grid)
return

var time_elapsed :int = Time.get_ticks_msec() - time_now
var _time_elapsed: int = Time.get_ticks_msec() - _time_now
if OS.is_debug_build():
print("%s: Generating took %s seconds" % [name, (float(time_elapsed) / 1000)])
print("%s: Generating took %s seconds" % [name, float(_time_elapsed) / 1000])

grid_updated.emit()
generation_finished.emit()
Expand All @@ -59,9 +58,15 @@ func _smooth() -> void:

for cell in grid.get_cells(settings.tile.layer):
var dead_neighbors_count: int = grid.get_amount_of_empty_neighbors(cell, settings.tile.layer)
if grid.get_value(cell, settings.tile.layer) != null and dead_neighbors_count > settings.max_floor_empty_neighbors:
if (
grid.get_value(cell, settings.tile.layer) != null
and dead_neighbors_count > settings.max_floor_empty_neighbors
):
_temp_grid.set_value(cell, null)
elif grid.get_value(cell, settings.tile.layer) == null and dead_neighbors_count <= settings.min_empty_neighbors:
elif (
grid.get_value(cell, settings.tile.layer) == null
and dead_neighbors_count <= settings.min_empty_neighbors
):
_temp_grid.set_value(cell, settings.tile)

grid = _temp_grid
Expand All @@ -73,7 +78,7 @@ func _smooth() -> void:


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

if not settings:
warnings.append("Needs CellularGeneratorSettings to work.")
Expand Down
18 changes: 5 additions & 13 deletions addons/gaea/generators/2D/chunk_aware_generator_2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
class_name ChunkAwareGenerator2D
extends GaeaGenerator2D


## Emitted when any update to a chunk is made. Either erasing it or generating it.
signal chunk_updated(chunk_position: Vector2i)
## Emitted when a chunk is finished generated. [signal chunk_updated] is also called.
signal chunk_generation_finished(chunk_position: Vector2i)
## Emitted when a chunk is erased. [signal chunk_updated] is also called.
signal chunk_erased(chunk_position: Vector2i)


## The size of the Chunks. [br]
## [b]Warning: Cannot be set to 0[/b]
@export var chunk_size: Vector2i = Vector2i(16, 16)
Expand All @@ -36,8 +34,8 @@ func erase_chunk(chunk_position: Vector2i) -> void:
for layer in grid.get_layer_count():
grid.erase(Vector2i(x, y), layer)

(func(): chunk_updated.emit(chunk_position)).call_deferred() # deferred for threadability
(func(): chunk_erased.emit(chunk_position)).call_deferred() # deferred for threadability
(func(): chunk_updated.emit(chunk_position)).call_deferred() # deferred for threadability
(func(): chunk_erased.emit(chunk_position)).call_deferred() # deferred for threadability


func _apply_modifiers_chunk(modifiers: Array[Modifier2D], chunk_position: Vector2i) -> void:
Expand All @@ -63,21 +61,15 @@ func unload_chunk(chunk_position: Vector2i) -> void:

### Utils ###


func has_chunk(chunk_position: Vector2i) -> bool:
return generated_chunks.has(chunk_position)


func get_chunk_axis_range(position: int, axis_size: int) -> Array:
return range(
position * axis_size,
(position + 1) * axis_size,
1
)
return range(position * axis_size, (position + 1) * axis_size, 1)


## Returns the coordinates of the chunk containing the cell at the given [param map_position].
func map_to_chunk(map_position: Vector2i) -> Vector2i:
return Vector2i(
floori(float(map_position.x) / chunk_size.x),
floori(float(map_position.y) / chunk_size.y)
)
return Vector2i(floori(float(map_position.x) / chunk_size.x), floori(float(map_position.y) / chunk_size.y))
1 change: 0 additions & 1 deletion addons/gaea/generators/2D/generator_2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
class_name GaeaGenerator2D
extends GaeaGenerator


## Used to transform a world position into a map position,
## mainly used by the [ChunkLoader]. May also be used by
## a [GaeaRenderer]. Otherwise doesn't affect generation.
Expand Down
3 changes: 1 addition & 2 deletions addons/gaea/generators/2D/generator_settings_2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ class_name GeneratorSettings2D
extends Resource
## @tutorial(Gaea's Structure): https://benjatk.github.io/Gaea/#/structure


@export var modifiers: Array[Modifier2D] # TODO: Replace with custom control for easier editing. Similar to Blender.
@export var modifiers: Array[Modifier2D] # TODO: Replace with custom control for easier editing. Similar to Blender.


func _init() -> void:
Expand Down
8 changes: 6 additions & 2 deletions addons/gaea/generators/2D/grid_2d.gd
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
class_name GaeaGrid2D
extends GaeaGrid

const SURROUNDING := [
Vector2i.RIGHT, Vector2i.LEFT,
Vector2i.UP, Vector2i.DOWN,
Vector2i(1, 1), Vector2i(1, -1),
Vector2i(-1, -1), Vector2i(-1, 1)
]

const SURROUNDING := [Vector2i.RIGHT, Vector2i.LEFT, Vector2i.UP, Vector2i.DOWN,
Vector2i(1, 1), Vector2i(1, -1), Vector2i(-1, -1), Vector2i(-1, 1)]


## Sets the value at the given position to [param value].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func generate(starting_grid: GaeaGrid = null) -> void:
push_error("%s doesn't have a settings resource" % name)
return

var time_now :int = Time.get_ticks_msec()
var _time_now: int = Time.get_ticks_msec()

generation_started.emit()

Expand All @@ -35,9 +35,9 @@ func generate(starting_grid: GaeaGrid = null) -> void:
next_pass.generate(grid)
return

var time_elapsed :int = Time.get_ticks_msec() - time_now
var _time_elapsed: int = Time.get_ticks_msec() - _time_now
if OS.is_debug_build():
print("%s: Generating took %s seconds" % [name, float(time_elapsed) / 1000 ])
print("%s: Generating took %s seconds" % [name, float(_time_elapsed) / 1000])

grid_updated.emit()
generation_finished.emit()
Expand Down Expand Up @@ -69,16 +69,15 @@ func generate_chunk(chunk_position: Vector2i, starting_grid: GaeaGrid = null) ->
next_pass.generate_chunk(chunk_position, grid)
return

(func(): chunk_updated.emit(chunk_position)).call_deferred() # deferred for threadability
(func(): chunk_generation_finished.emit(chunk_position)).call_deferred() # deferred for threadability
(func(): chunk_updated.emit(chunk_position)).call_deferred() # deferred for threadability
(func(): chunk_generation_finished.emit(chunk_position)).call_deferred() # deferred for threadability


func _set_grid() -> void:
var max_height: int = 0
for x in range(settings.world_length):
max_height = maxi(
floor(settings.noise.get_noise_1d(x) * settings.height_intensity + settings.height_offset),
max_height
floor(settings.noise.get_noise_1d(x) * settings.height_intensity + settings.height_offset), max_height
)

var area := Rect2i(
Expand Down
15 changes: 6 additions & 9 deletions addons/gaea/generators/2D/noise_generator/noise_generator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func generate(starting_grid: GaeaGrid = null) -> void:

generation_started.emit()

var time_now :int = Time.get_ticks_msec()
var _time_now: int = Time.get_ticks_msec()

settings.noise.seed = seed

Expand All @@ -36,9 +36,9 @@ func generate(starting_grid: GaeaGrid = null) -> void:
next_pass.generate(grid)
return

var time_elapsed: int = Time.get_ticks_msec() - time_now
var _time_elapsed: int = Time.get_ticks_msec() - _time_now
if OS.is_debug_build():
print("%s: Generating took %s seconds" % [name, float(time_elapsed) / 1000])
print("%s: Generating took %s seconds" % [name, float(_time_elapsed) / 1000])

grid_updated.emit()
generation_finished.emit()
Expand Down Expand Up @@ -69,19 +69,16 @@ func generate_chunk(chunk_position: Vector2i, starting_grid: GaeaGrid = null) ->
next_pass.generate_chunk(chunk_position, grid)
return

(func (): chunk_updated.emit(chunk_position)).call_deferred() # Deferred for thread-safety.
(func (): chunk_generation_finished.emit(chunk_position)).call_deferred() # Deferred for thread-safety.
(func(): chunk_updated.emit(chunk_position)).call_deferred() # Deferred for thread-safety.
(func(): chunk_generation_finished.emit(chunk_position)).call_deferred() # Deferred for thread-safety.


func _set_grid() -> void:
_set_grid_area(Rect2i(Vector2i.ZERO, Vector2i(settings.world_size)))


func _set_grid_chunk(chunk_position: Vector2i) -> void:
_set_grid_area(Rect2i(
chunk_position * chunk_size,
chunk_size
))
_set_grid_area(Rect2i(chunk_position * chunk_size, chunk_size))


func _set_grid_area(rect: Rect2i) -> void:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ extends Resource
@export_group("Thresholds")
# Note: i just dont want to call them threshold_min and threshold_max, it seems repetitive for me
## The minimum threshold
@export_range(-1.0, 1.0) var min: float = -1.0 :
@export_range(-1.0, 1.0) var min: float = -1.0:
set(value):
min = value
if min > max:
max = min
emit_changed()
## The maximum threshold
@export_range(-1.0, 1.0) var max: float = 1.0 :
@export_range(-1.0, 1.0) var max: float = 1.0:
set(value):
max = value
if max < min:
min = max
emit_changed()

var settings: NoiseGeneratorSettings :
var settings: NoiseGeneratorSettings:
set(value):
settings = value
settings.noise.changed.connect(emit_changed)
Loading

0 comments on commit ee4ff6f

Please sign in to comment.