185 lines
5.2 KiB
GDScript
185 lines
5.2 KiB
GDScript
extends Node
|
|
|
|
const WINDOW_TITLE_INPUT = "GUI/Settings/Menus/TabContainer/Test/ScrollContainer/VBoxContainer/TitleBox/Title"
|
|
|
|
#### Environment
|
|
var time_scale = 0.01
|
|
|
|
#
|
|
##### Character
|
|
#var character_name = "player"
|
|
#var character_color = null
|
|
#var character_gender = null
|
|
#var character_scale = 1
|
|
#var character_slot = null
|
|
|
|
#### BG loader
|
|
onready var progress_texture = $background_loader/center_container/texture_progress
|
|
var loader
|
|
var wait_frames
|
|
var time_max = 10 # msec
|
|
var current_scene = null
|
|
var old_scene = null
|
|
var parent_scene = null
|
|
var current_map = null
|
|
var current_start_position = "start_position"
|
|
|
|
var current_state = "login"
|
|
|
|
var font_size= 14
|
|
|
|
func _ready():
|
|
var root = get_tree().get_root()
|
|
# current_scene = root.get_child(root.get_child_count() -1)
|
|
|
|
change_title()
|
|
get_tree().get_root().connect("size_changed", self, "on_window_size_changed")
|
|
|
|
# change_level( "res://login_scene/login_scene.tscn", get_tree().get_root() )
|
|
|
|
character.get_node( "infos_spatial" ).hide()
|
|
|
|
########
|
|
#### change title ####
|
|
func on_window_size_changed():
|
|
change_title()
|
|
|
|
func change_title():
|
|
var title = "Khanat"
|
|
|
|
if has_node( WINDOW_TITLE_INPUT ):
|
|
var title_node = get_node( WINDOW_TITLE_INPUT )
|
|
if title_node and not title_node.text.strip_edges() == "":
|
|
title = title_node.text.strip_edges()
|
|
|
|
title += " (" + String(OS.get_window_size().x) + "x" + String(OS.get_window_size().y) + ")"
|
|
OS.set_window_title( title )
|
|
########
|
|
|
|
|
|
|
|
########
|
|
#### change level
|
|
func unload_scene( scene ):
|
|
scene.queue_free()
|
|
|
|
func load_scene( scene_path ):
|
|
loader = ResourceLoader.load_interactive( scene_path )
|
|
if loader == null: # check for errors
|
|
return false
|
|
return true
|
|
|
|
func change_level( p_next_state, p_next_scene_path, p_parent_next_scene = null, p_old_scene = null, p_start_position = "start_position" ):
|
|
|
|
if not load_scene( p_next_scene_path ):
|
|
print( "Erreur lors du chargement de la scene: "+str(p_next_scene_path) )
|
|
return
|
|
|
|
character.hide()
|
|
# get_node("background_loader/center_container").rect_min_size = get_viewport().size
|
|
progress_texture.rect_min_size = get_viewport().size
|
|
get_node("background_loader").show()
|
|
|
|
self.current_start_position = p_start_position
|
|
|
|
parent_scene = p_parent_next_scene
|
|
old_scene = p_old_scene
|
|
|
|
set_process(true)
|
|
|
|
if old_scene:
|
|
unload_scene( old_scene )
|
|
|
|
wait_frames = 1
|
|
self.current_state = p_next_state
|
|
########
|
|
|
|
|
|
func _process( time ):
|
|
########
|
|
#### LOADER
|
|
if loader == null:
|
|
# no need to process anymore
|
|
set_process(false)
|
|
return
|
|
|
|
if wait_frames > 0: # wait for frames to let the "loading" animation to show up
|
|
wait_frames -= 1
|
|
return
|
|
|
|
var t = OS.get_ticks_msec()
|
|
while OS.get_ticks_msec() < t + time_max: # use "time_max" to control how much time we block this thread
|
|
# poll your loader
|
|
var err = loader.poll()
|
|
# update_progress()
|
|
|
|
if err == ERR_FILE_EOF: # load finished
|
|
# update_progress()
|
|
var resource = loader.get_resource()
|
|
loader = null
|
|
set_new_scene( resource )
|
|
break
|
|
elif err == OK:
|
|
update_progress()
|
|
pass
|
|
else: # error during loading
|
|
show_error()
|
|
loader = null
|
|
break
|
|
########
|
|
|
|
func _input( event ):
|
|
|
|
if current_state == "game" and event is InputEventKey and not event.is_echo():
|
|
if event.is_action_pressed("test_change_map_1"):
|
|
global.change_level( "game", "res://test_scene/test_scene.tscn", null, current_map )
|
|
elif event.is_action_pressed("test_change_map_2"):
|
|
global.change_level( "game", "res://test_grid_map/test_grid_map.tscn", null, current_map )
|
|
elif event.is_action_pressed("test_change_map_3"):
|
|
global.change_level( "game", "res://game_scene/game_scene.tscn", null, current_map )
|
|
|
|
|
|
|
|
|
|
func update_progress():
|
|
|
|
var progress = (float(loader.get_stage()) / loader.get_stage_count()) * progress_texture.max_value
|
|
|
|
progress_texture.value = progress
|
|
|
|
|
|
func set_new_scene( scene_resource ):
|
|
progress_texture.value = 0
|
|
|
|
var scene = scene_resource.instance()
|
|
# current_scene = scene_resource.instance()
|
|
if parent_scene:
|
|
parent_scene.add_child(scene)
|
|
else:
|
|
get_tree().get_root().add_child(scene)
|
|
|
|
print( "scene: "+str(scene.name) )
|
|
print( "start position: " + str(self.current_start_position ) )
|
|
if scene.has_node( self.current_start_position ):
|
|
print( "OK" )
|
|
character.update( scene.get_node( self.current_start_position ) )
|
|
|
|
current_map = scene
|
|
|
|
get_node("background_loader").hide()
|
|
character.show()
|
|
|
|
|
|
func _on_login_scene_character_creation_finished():
|
|
|
|
self.change_level( "game", "res://game_scene/game_scene.tscn", null, get_tree().get_root().get_node("login_scene") )
|
|
|
|
|
|
func _on_logout_button_pressed():
|
|
self.change_level( "login", "res://login_scene/login_scene.tscn", null, current_map )
|
|
get_tree().paused = false
|
|
########
|
|
|
|
|
|
func _on_GUI_logout_button_pressed():
|
|
pass # replace with function body
|