Browse Source

Adding a generation UI

pull/1/head
Ryan Reed 1 year ago
parent
commit
b1000fbf0b
3 changed files with 191 additions and 25 deletions
  1. +5
    -0
      project.godot
  2. +68
    -10
      world.gd
  3. +118
    -15
      world.tscn

+ 5
- 0
project.godot View File

@ -22,3 +22,8 @@ Pause={
, 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":4194313,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
Menu={
"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":4194305,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}

+ 68
- 10
world.gd View File

@ -11,12 +11,22 @@ enum CellStates {
ALIVE,
}
## UI
@onready var debug_ui: MarginContainer = $UI/Debug
@onready var debug_generation_counter: Label = $UI/Debug/VBoxContainer/GenerationCounter
@onready var debug_living_cells_counter: Label = $UI/Debug/VBoxContainer/LivingCellsCounter
@onready var debug_world_seed: Label = $UI/Debug/VBoxContainer/WorldSeed
@onready var generation_ui: MarginContainer = $UI/WorldGeneration
@onready var generation_seed: LineEdit = $UI/WorldGeneration/VBoxContainer/Seed/Input
@onready var generation_world_size_x: LineEdit = $UI/WorldGeneration/VBoxContainer/WorldSize/Input_x
@onready var generation_world_size_y: LineEdit = $UI/WorldGeneration/VBoxContainer/WorldSize/Input_y
@onready var gneration_cell_size_x: LineEdit = $UI/WorldGeneration/VBoxContainer/CellSize/Input_x
@onready var gneration_cell_size_y: LineEdit = $UI/WorldGeneration/VBoxContainer/CellSize/Input_y
## Other
@onready var camera: Camera2D = $Camera2D
@onready var generation_counter: Label = $CanvasLayer/Debug/VBoxContainer/GenerationCounter
@onready var generation_timer: Timer = $GenerationTimer
@onready var living_cells_counter: Label = $CanvasLayer/Debug/VBoxContainer/LivingCellsCounter
@onready var world_seed_label: Label = $CanvasLayer/Debug/VBoxContainer/WorldSeed
@export var world_seed: int
@export var cell_size: Vector2 = Vector2(16, 16)
@ -25,17 +35,46 @@ enum CellStates {
var cell_instance
var generation: int = 1
var is_paused: int = false
var total_living: int = 0
var world: Array = []
func _ready() -> void:
if not world_seed:
world_seed = randi()
seed(world_seed)
world_seed_label.text = "World Seeed: %s" % world_seed
update_generation_ui()
debug_ui.visible = false
generation_ui.visible = true
func _input(event: InputEvent) -> void:
if event.is_action_pressed("Pause"):
is_paused = !is_paused
if event.is_action_pressed("Menu"):
is_paused = true
generation_ui.visible = true
update_generation_ui()
#
# UI
#
## Update the Gneration UI with the current settings
func update_generation_ui() -> void:
generation_seed.text = str(world_seed)
generation_world_size_x.text = str(world_size.x)
generation_world_size_y.text = str(world_size.y)
gneration_cell_size_x.text = str(cell_size.x)
gneration_cell_size_x.text = str(cell_size.y)
#
# Conway Specific
#
func start_conway() -> void:
debug_world_seed.text = "World Seeed: %s" % world_seed
is_paused = false
generate_world()
generation_timer.start()
# Center the camera on the world
camera.position.x = world_size.x * cell_size.x / 2
@ -66,7 +105,9 @@ func cell_is_alive(pos: Vector2) -> bool:
func count_living_neighbors(pos: Vector2) -> int:
var count := 0
@warning_ignore("incompatible_ternary")
var x_min = 0 if pos.x - 1 < 0 else pos.x - 1
@warning_ignore("incompatible_ternary")
var y_min = 0 if pos.y - 1 < 0 else pos.y - 1
var x_max = world_size.x if pos.x + 2 > world_size.x else pos.x + 2
var y_max = world_size.y if pos.y + 2 > world_size.y else pos.y + 2
@ -80,6 +121,8 @@ func count_living_neighbors(pos: Vector2) -> int:
## Loop through the world and create or kill cells depending on rules
func process_generation() -> void:
if is_paused: return
generation += 1
total_living = 0
@ -116,6 +159,7 @@ func create_cell(pos: Vector2) -> RID:
return cell_instance
## Remove the cell from the RenderingServer, if it exists
func kill_cell(pos: Vector2) -> CellStates:
if world[pos.x][pos.y] is RID:
RenderingServer.free_rid(world[pos.x][pos.y])
@ -138,7 +182,21 @@ func generate_world() -> void:
func _on_generation_timer_timeout() -> void:
process_generation()
generation_counter.text = "Generation: %s" % generation
living_cells_counter.text = "Living Cells: %s" % total_living
debug_generation_counter.text = "Generation: %s" % generation
debug_living_cells_counter.text = "Living Cells: %s" % total_living
generation_timer.start()
func _on_generation_submit_pressed() -> void:
if not generation_seed.text.strip_edges():
world_seed = randi()
else:
world_seed = int(generation_seed.text)
world_size = Vector2(int(generation_world_size_x.text), int(generation_world_size_y.text))
cell_size = Vector2(int(gneration_cell_size_x.text), int(gneration_cell_size_x.text))
debug_ui.visible = true
generation_ui.visible = false
seed(world_seed)
randomize()
start_conway()

+ 118
- 15
world.tscn View File

@ -7,39 +7,142 @@
[node name="World" type="Node2D"]
position = Vector2(-152, 0)
script = ExtResource("1_wavft")
world_seed = 123
cell_texture = ExtResource("2_8r6bn")
[node name="CanvasLayer" type="CanvasLayer" parent="."]
[node name="Camera2D" type="Camera2D" parent="."]
position = Vector2(464, 200)
[node name="GenerationTimer" type="Timer" parent="."]
wait_time = 0.05
one_shot = true
[node name="Debug" type="MarginContainer" parent="CanvasLayer"]
offset_right = 40.0
offset_bottom = 40.0
[node name="UI" type="CanvasLayer" parent="."]
[node name="VBoxContainer" type="VBoxContainer" parent="CanvasLayer/Debug"]
[node name="Debug" type="MarginContainer" parent="UI"]
visible = false
offset_right = 106.0
offset_bottom = 116.0
[node name="VBoxContainer" type="VBoxContainer" parent="UI/Debug"]
layout_mode = 2
[node name="FPSCounter" parent="CanvasLayer/Debug/VBoxContainer" instance=ExtResource("3_ves6s")]
[node name="FPSCounter" parent="UI/Debug/VBoxContainer" instance=ExtResource("3_ves6s")]
layout_mode = 2
text = "FPS: 0"
[node name="WorldSeed" type="Label" parent="CanvasLayer/Debug/VBoxContainer"]
[node name="WorldSeed" type="Label" parent="UI/Debug/VBoxContainer"]
layout_mode = 2
text = "World Seed: 0"
[node name="GenerationCounter" type="Label" parent="CanvasLayer/Debug/VBoxContainer"]
[node name="GenerationCounter" type="Label" parent="UI/Debug/VBoxContainer"]
layout_mode = 2
text = "Generation: 1"
[node name="LivingCellsCounter" type="Label" parent="CanvasLayer/Debug/VBoxContainer"]
[node name="LivingCellsCounter" type="Label" parent="UI/Debug/VBoxContainer"]
layout_mode = 2
text = "Living Cells: 0"
[node name="Camera2D" type="Camera2D" parent="."]
position = Vector2(464, 200)
[node name="WorldGeneration" type="MarginContainer" parent="UI"]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -46.0
offset_top = -25.5
offset_right = 46.0
offset_bottom = 25.5
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 4
size_flags_vertical = 4
theme_override_constants/margin_left = 10
theme_override_constants/margin_top = 10
theme_override_constants/margin_right = 10
theme_override_constants/margin_bottom = 10
[node name="GenerationTimer" type="Timer" parent="."]
wait_time = 0.05
one_shot = true
autostart = true
[node name="VBoxContainer" type="VBoxContainer" parent="UI/WorldGeneration"]
layout_mode = 2
[node name="Seed" type="HBoxContainer" parent="UI/WorldGeneration/VBoxContainer"]
layout_mode = 2
theme_override_constants/separation = 6
[node name="Label" type="Label" parent="UI/WorldGeneration/VBoxContainer/Seed"]
custom_minimum_size = Vector2(120, 0)
layout_mode = 2
text = "Seed"
[node name="Input" type="LineEdit" parent="UI/WorldGeneration/VBoxContainer/Seed"]
custom_minimum_size = Vector2(200, 0)
layout_mode = 2
placeholder_text = "Empty for Random"
[node name="WorldSize" type="HBoxContainer" parent="UI/WorldGeneration/VBoxContainer"]
layout_mode = 2
theme_override_constants/separation = 6
[node name="Label" type="Label" parent="UI/WorldGeneration/VBoxContainer/WorldSize"]
custom_minimum_size = Vector2(120, 0)
layout_mode = 2
text = "World Size"
[node name="Label_x" type="Label" parent="UI/WorldGeneration/VBoxContainer/WorldSize"]
layout_mode = 2
text = "X:"
[node name="Input_x" type="LineEdit" parent="UI/WorldGeneration/VBoxContainer/WorldSize"]
custom_minimum_size = Vector2(20, 0)
layout_mode = 2
text = "32"
max_length = 4
[node name="Label_y" type="Label" parent="UI/WorldGeneration/VBoxContainer/WorldSize"]
layout_mode = 2
text = "Y:"
[node name="Input_y" type="LineEdit" parent="UI/WorldGeneration/VBoxContainer/WorldSize"]
custom_minimum_size = Vector2(20, 0)
layout_mode = 2
text = "32"
max_length = 4
[node name="CellSize" type="HBoxContainer" parent="UI/WorldGeneration/VBoxContainer"]
layout_mode = 2
theme_override_constants/separation = 6
[node name="Label" type="Label" parent="UI/WorldGeneration/VBoxContainer/CellSize"]
custom_minimum_size = Vector2(120, 0)
layout_mode = 2
text = "Cell Size"
[node name="Label_x" type="Label" parent="UI/WorldGeneration/VBoxContainer/CellSize"]
layout_mode = 2
text = "X:"
[node name="Input_x" type="LineEdit" parent="UI/WorldGeneration/VBoxContainer/CellSize"]
custom_minimum_size = Vector2(20, 0)
layout_mode = 2
text = "16"
max_length = 4
[node name="Label_y" type="Label" parent="UI/WorldGeneration/VBoxContainer/CellSize"]
layout_mode = 2
text = "Y:"
[node name="Input_y" type="LineEdit" parent="UI/WorldGeneration/VBoxContainer/CellSize"]
custom_minimum_size = Vector2(20, 0)
layout_mode = 2
text = "16"
max_length = 4
[node name="Submit" type="HBoxContainer" parent="UI/WorldGeneration/VBoxContainer"]
layout_mode = 2
[node name="Button" type="Button" parent="UI/WorldGeneration/VBoxContainer/Submit"]
layout_mode = 2
text = "Start"
[connection signal="timeout" from="GenerationTimer" to="." method="_on_generation_timer_timeout"]
[connection signal="pressed" from="UI/WorldGeneration/VBoxContainer/Submit/Button" to="." method="_on_generation_submit_pressed"]

Loading…
Cancel
Save