Added respawn logic ... and with it also a complete rehaul of how managing scene instances work to make it work

This commit is contained in:
2026-03-01 15:40:56 +01:00
parent 51ee5029e9
commit 6d8e8e3734
14 changed files with 194 additions and 36 deletions

View File

@@ -1,22 +1,19 @@
extends Node
# Main scene
var mainSceneInstance
# Screen
var screen_size = Vector2(1920.0, 1080.0) # Default screen size (this is a float for some reason)
var viewport_size
@onready var extent: Rect2 = get_viewport().get_visible_rect()
# Main menu & game spawning
@onready
var mainMenuScene = preload("res://main_menu.tscn").instantiate()
@onready
var gameScene = preload("res://molecular/molecular_stage.tscn").instantiate()
# utils.
var rng = RandomNumberGenerator.new()
var eps: float = 1e-4
# managers
@onready
var foodManager = gameScene.get_node("FoodManager")
var foodManager: FoodManager2D
# A world "current"
# polar
@@ -33,24 +30,26 @@ var t: float = 0.0
func _ready() -> void:
# Start game
mainSceneInstance = get_tree().root.get_child(-1)
# Create viewport
viewport_size = get_viewport().get_visible_rect().size
# Create game (main menu)
add_child(mainMenuScene)
# initial world current
get_new_flow()
func _physics_process(delta: float) -> void:
t += delta
# Flow current change
if abs(fmod(t, flowT)) < eps:
get_new_flow()
func start_game() -> void:
add_child(gameScene)
func switch_scene(name:String) -> void:
if name == "respawn":
mainSceneInstance.go_to_respawn_scene()
# TODO: This needs to be called from a script inheriting a CharacterBody2D (e.g. the player)
# Alternative would be to pass the player reference to this script (which might be better?)
@@ -67,15 +66,14 @@ func get_boundaried_position(position):
position.x = wrapf(position.x, 0, screen_size.x)
position.y = wrapf(position.y, 0, screen_size.y)
return position
func get_new_flow():
flow_dir = rng.randf()*2*PI
flow_mag = rng.randf()
flow_x = flow_mag * cos(flow_dir)
flow_y = flow_mag * sin(flow_dir)
func calc_distance(one, two) -> float:
var candidate = one.distance_to(two)

View File

@@ -0,0 +1,3 @@
[gd_scene format=3 uid="uid://bsrxph8oa7uuj"]
[node name="Main" type="Node" unique_id=1994328839]

View File

@@ -1,7 +0,0 @@
extends Control
func _on_play_button_pressed() -> void:
print("Starting game by pressing button...")
GameManager.start_game()
self.queue_free()

View File

@@ -1 +0,0 @@
uid://dxc66bci2ivrj

View File

@@ -1,6 +1,14 @@
[gd_scene format=3 uid="uid://drgv154ei1vrl"]
[ext_resource type="Script" uid="uid://dxc66bci2ivrj" path="res://main_menu.gd" id="1_06t4h"]
[sub_resource type="GDScript" id="GDScript_rhts7"]
script/source = "extends Control
func _on_play_button_pressed() -> void:
print(\"Starting game by pressing button...\")
GameManager.start_game()
self.queue_free()
"
[sub_resource type="LabelSettings" id="LabelSettings_rhts7"]
font_size = 64
@@ -12,7 +20,7 @@ anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_06t4h")
script = SubResource("GDScript_rhts7")
[node name="PlayButton" type="Button" parent="." unique_id=1831335357]
layout_mode = 1
@@ -42,5 +50,3 @@ text = "The Main Menu"
label_settings = SubResource("LabelSettings_rhts7")
horizontal_alignment = 1
vertical_alignment = 1
[connection signal="pressed" from="PlayButton" to="." method="_on_play_button_pressed"]

View File

@@ -0,0 +1,61 @@
extends Node
# Main menu & game spawning
@onready
var mainMenuScene = preload("res://main_menu.tscn")
var mainMenuSceneInstance: Node
@onready
var gameScene = preload("res://molecular/molecular_stage.tscn")
var gameSceneInstance: Node
@onready
var respawnScene = preload("res://respawn_menu.tscn")
var respawnSceneInstance: Node
# Currently active scene (instance)
var currentSceneInstance:Node
# UI effects
var fadeEffect: CanvasLayer
func _ready() -> void:
# Instantiate effects
fadeEffect = $UI/Fade
fadeEffect.visible = false
# Create game (main menu)
print("Creating Main menu...")
mainMenuSceneInstance = mainMenuScene.instantiate()
currentSceneInstance = mainMenuSceneInstance
add_child(currentSceneInstance)
# Link MainMenu button to start_game
print("Linking Play button...")
mainMenuSceneInstance.get_node("PlayButton").connect("pressed", start_game)
func start_game() -> void:
print("Starting game...")
# Instatiate
currentSceneInstance.queue_free()
gameSceneInstance = gameScene.instantiate()
currentSceneInstance = gameSceneInstance
add_child(currentSceneInstance)
# Populate GameManager with game scene
GameManager.foodManager = gameSceneInstance.get_node("FoodManager")
func go_to_respawn_scene() -> void:
fadeEffect.visible = true
await fadeEffect.fade(1.0, 1.5).finished
print("Switching to Respawn scene.")
currentSceneInstance.queue_free()
respawnSceneInstance = respawnScene.instantiate()
respawnSceneInstance.get_node("RespawnButton").connect("pressed", start_game)
currentSceneInstance = respawnSceneInstance
add_child(currentSceneInstance)
await fadeEffect.fade(0.0, 1.5).finished
fadeEffect.visible = false

View File

@@ -0,0 +1 @@
uid://wiv0plsu04s

View File

@@ -1,6 +1,11 @@
[gd_scene format=3 uid="uid://b8gt8os2m28lv"]
[ext_resource type="Script" uid="uid://vsbibc5fanou" path="res://game_manager.gd" id="1_0f027"]
[ext_resource type="Script" uid="uid://wiv0plsu04s" path="res://main_scene.gd" id="1_2c62f"]
[ext_resource type="PackedScene" uid="uid://3eg3a8ceom4l" path="res://molecular/fade.tscn" id="2_2c62f"]
[node name="MainScene" type="Node" unique_id=1897047571]
script = ExtResource("1_0f027")
script = ExtResource("1_2c62f")
[node name="UI" type="Node2D" parent="." unique_id=1032224503]
[node name="Fade" parent="UI" unique_id=853660457 instance=ExtResource("2_2c62f")]

View File

@@ -0,0 +1,11 @@
extends CanvasLayer
@onready var color_rect: ColorRect = $ColorRect
func _ready() -> void:
color_rect.color.a = 0
func fade(target_alpha: float, duration: float):
var tween = create_tween()
tween.tween_property(color_rect, "color:a", target_alpha, duration)
return tween

View File

@@ -0,0 +1 @@
uid://bwf56ehqo32gr

View File

@@ -0,0 +1,14 @@
[gd_scene format=3 uid="uid://3eg3a8ceom4l"]
[ext_resource type="Script" uid="uid://bwf56ehqo32gr" path="res://molecular/fade.gd" id="1_rx8rx"]
[node name="Fade" type="CanvasLayer" unique_id=853660457]
script = ExtResource("1_rx8rx")
[node name="ColorRect" type="ColorRect" parent="." unique_id=1320853444]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
color = Color(0, 0, 0, 0)

View File

@@ -103,14 +103,25 @@ func handle_damage(dmg: int, src: Node) -> void:
healthPoints -= dmg
hasiframes = true
invulnerable_cooldown_timer.start()
if healthPoints <= 0:
print("Player died. Respawn button not implemented.")
isAlive = false
if isAlive and healthPoints <= 0:
die()
func collect_resource(damnt: int) -> void:
resources += damnt
print("Resource collected! Total: ", resources)
func die() -> void:
print("Player died.")
isAlive = false
GameManager.switch_scene("respawn")
# TODO: Is this used or should it be removed? (currently unused)
func respawm() -> void:
healthPoints = maxHealth
isAlive = true
hasiframes = true
attack_cooldown_timer.start()

View File

@@ -100,7 +100,6 @@ limit_right = 1152
limit_bottom = 648
[node name="UI" type="Node2D" parent="." unique_id=1500495475]
visible = false
[node name="CanvasLayer" type="CanvasLayer" parent="UI" unique_id=633018012]

View File

@@ -0,0 +1,56 @@
[gd_scene format=3 uid="uid://dg72ceo3c1u3q"]
[sub_resource type="LabelSettings" id="LabelSettings_3i7aq"]
font_size = 64
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_3i7aq"]
content_margin_left = 4.0
content_margin_top = 4.0
content_margin_right = 4.0
content_margin_bottom = 4.0
bg_color = Color(0, 0, 0, 1)
corner_radius_top_left = 3
corner_radius_top_right = 3
corner_radius_bottom_right = 3
corner_radius_bottom_left = 3
corner_detail = 5
[node name="RespawnMenu" type="Control" unique_id=1895105935]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="YouDiedLabel" type="Label" parent="." unique_id=1672619325]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -66.0
offset_top = -146.695
offset_right = 66.0
offset_bottom = -123.69501
grow_horizontal = 2
grow_vertical = 2
text = "Oh, no! You died!"
label_settings = SubResource("LabelSettings_3i7aq")
[node name="RespawnButton" type="Button" parent="." unique_id=1508413909]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -160.0
offset_top = -63.0
offset_right = 162.0
offset_bottom = -5.0
grow_horizontal = 2
grow_vertical = 2
theme_override_styles/normal = SubResource("StyleBoxFlat_3i7aq")
text = "Respawn"