platformer-game-test/src/Singletons/GameState.gd

150 lines
3.3 KiB
GDScript3
Raw Normal View History

2020-09-29 01:36:57 +00:00
extends Node
signal scene_changed
2020-09-29 01:36:57 +00:00
enum States{
INTRO,
INTRO_CUTSCENE,
TRANSITIONING,
2020-09-29 01:36:57 +00:00
MAIN_MENU,
GAME_OVER,
CONTROLS,
NEW_GAME,
2020-09-29 01:36:57 +00:00
GAME,
PUSE,
STATS,
CUTSCENE,
2020-09-29 01:36:57 +00:00
}
var _SAVE_FILE_PATH = "user://data"
# Change key in release
var _ENCRYPTION_KEY = "fas3uyf076HJsiUDs24dfI9"
var is_first_run = false
2020-09-29 01:36:57 +00:00
var _SCENES := {
States.MAIN_MENU: "res://src/Menu/MainMenu.tscn",
States.NEW_GAME: "res://src/GameWorld/GameWorld.tscn",
States.INTRO_CUTSCENE: "res://src/CutScenes/IntroCutscene.tscn"
2020-09-29 01:36:57 +00:00
}
var _player_spawn_point;
var GAME_TIMEOUT_MIN = 10
var _default_data := {
2020-09-29 01:36:57 +00:00
"statistics": {
"deaths": 0,
"clears": 0,
"runs": 0,
"total_time_played": 0,
"orbs": 0,
"coins": 0,
},
"heigh_score": {
"deaths": 0,
"score": 0,
"cleard": false,
"coins": 0,
"orbs": 0,
"time": 0,
},
"achivements": {
"a": false,
"b": false
2020-09-29 01:36:57 +00:00
}
}
var _data := _default_data
2020-09-29 01:36:57 +00:00
var _run_data:= {
"deaths": 0,
"coins": 0,
"orbs": 0,
"time": 0,
"score": 0
}
var _state = States.INTRO
func _ready() -> void:
load_data()
for key in _data:
print("[%s]: %s" % [key, _data[key]])
func _physics_process(delta: float) -> void:
if _state == States.GAME:
_run_data.time += delta
_data.statistics.total_time_played += delta
2020-09-29 01:36:57 +00:00
func load_data():
var data_file = File.new()
var e = data_file.open_encrypted_with_pass(_SAVE_FILE_PATH, File.READ, _ENCRYPTION_KEY)
if e != 0:
data_file.close()
is_first_run = true
2020-09-29 01:36:57 +00:00
save_data()
else:
var saved_data:Dictionary = data_file.get_var()
for key in saved_data.keys():
if _data.has(key):
if typeof(saved_data[key]) == TYPE_DICTIONARY:
for sub_key in saved_data[key]:
var subdict:Dictionary = _data[key];
if subdict.has(sub_key): _data[key][sub_key] = saved_data[key][sub_key]
else: _data[key] = saved_data[key]
2020-09-29 01:36:57 +00:00
data_file.close()
pass
func save_data():
var data_file = File.new()
data_file.open_encrypted_with_pass(_SAVE_FILE_PATH, File.WRITE, _ENCRYPTION_KEY)
data_file.store_var(_data)
2020-09-29 01:36:57 +00:00
data_file.close()
pass
func start_new_game(from_main_menu:bool=false, change_music:bool=true):
2020-09-29 01:36:57 +00:00
for key in _run_data.keys():
_run_data[key] = 0
var state = States.INTRO_CUTSCENE if from_main_menu else States.NEW_GAME
if not from_main_menu: _data.statistics.runs += 1
2020-09-29 01:36:57 +00:00
save_data()
2020-10-21 02:33:58 +00:00
var delay = 3 if state == States.INTRO_CUTSCENE else 1
if change_music: AudioManager.play_music(AudioManager.Music.PreGame, delay)
_change_scene(state, delay)
2020-10-21 02:33:58 +00:00
yield(self, "scene_changed")
# get_tree().change_scene_to(load(_SCENES[_state]))
func set_state(state:int = States.GAME):
_state = state
2020-09-29 01:36:57 +00:00
func get_heigh_score():
return _data.heigh_score.duplicate()
func get_statistics():
return _data.statistics.duplicate()
func get_run_data():
return _run_data.duplicate()
func get_state():
return int(_state)
2020-09-29 01:36:57 +00:00
func go_to_main_menu():
save_data()
_change_scene(States.MAIN_MENU)
yield(self, "scene_changed")
2020-10-21 02:33:58 +00:00
func _change_scene(state:int,duration:float=1):
_state = States.TRANSITIONING
Stage.fade_out(duration/2)
yield(Stage, "fade_finished")
get_tree().change_scene_to(load(_SCENES[state]))
Stage.fade_in(duration/2)
yield(Stage, "fade_finished")
_state = state
emit_signal("scene_changed")
func player_died():
2020-09-29 01:36:57 +00:00
_run_data.deaths += 1
_data.statistics.deaths += 1
2020-09-29 01:36:57 +00:00
func coin_collected():
_run_data.coins += 10
_data.statistics.coins += 10