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)