StickerClone/core/sceneInstance.gd

79 lines
2.2 KiB
GDScript

extends Node2D
@export var InitialPlayer :PackedScene
@export var InitialMap :PackedScene
@onready var current_scene = $CurrentScene
@onready var next_scene = $NextScene
#@onready var scene_transition = $ScreenTransition/AnimationPlayer
var player:Object
func scene_clean(scene):
if (scene != null and scene.get_child_count() > 0):
scene.get_child(0).queue_free()
func _init():
scene_clean(current_scene)
scene_clean(next_scene)
func _process(_delta):
if($CurrentScene.get_child_count() >0):
if($CurrentScene.get_child(0).position != Vector2(0,0)):
breakpoint
func transition_to_scene(new_scene: PackedScene, spawn_location: Vector2, spawn_direction = 1):
#screen_transition.play('FadeToBlack')
if (new_scene):
if (current_scene.get_child_count() == 0 ):
current_scene.add_child(new_scene.instantiate())
else:
next_scene.add_child(new_scene.instantiate())
#region Anim Map
if (next_scene.get_child(0) != null):
match spawn_direction:
0:
$AnimationPlayer.play("travel_left",-1,1)
1:
$AnimationPlayer.play("travel_left",-1,1)
2:
$AnimationPlayer.play("travel_up",-1,1)
3:
$AnimationPlayer.play("travel_down",-1,1)
#endregion
#region Spawn Player
if (player):
player.reparent(next_scene.get_child(0))
player.teleport(spawn_location, spawn_direction)
else:
player = $CurrentScene.find_child("player",true)
if (player):
player.teleport(spawn_location, spawn_direction)
else: #Spawn the player if there is none
if (InitialPlayer):
player = InitialPlayer.instantiate()
player.set_name("player")
current_scene.get_child(0).add_child(player)
else:
printerr("No Initial player found")
#endregion
else:
printerr("No New Scene")
func _on_ready():
transition_to_scene(InitialMap,Vector2(0,0),1)
func on_travel_end():
player.Traveling = false
print("end travel")
func _on_animation_player_animation_finished(anim_name):
print("anim finished")
current_scene.get_child(0).queue_free()
current_scene.position = Vector2(0,0)
if (next_scene.get_child_count() > 0):
next_scene.get_child(0).reparent(current_scene)
scene_clean(next_scene)
print("level unloaded")
await get_tree().create_timer(1).connect("timeout",Callable(self,"on_travel_end"))