2020-09-29 01:36:57 +00:00
|
|
|
extends Node
|
|
|
|
|
2020-10-20 13:51:17 +00:00
|
|
|
signal scene_changed
|
|
|
|
|
2020-09-29 01:36:57 +00:00
|
|
|
enum States{
|
|
|
|
INTRO,
|
2020-10-20 13:51:17 +00:00
|
|
|
INTRO_CUTSCENE,
|
|
|
|
TRANSITIONING,
|
2020-09-29 01:36:57 +00:00
|
|
|
MAIN_MENU,
|
|
|
|
GAME_OVER,
|
|
|
|
CONTROLS,
|
2020-10-20 13:51:17 +00:00
|
|
|
NEW_GAME,
|
2020-09-29 01:36:57 +00:00
|
|
|
GAME,
|
|
|
|
PUSE,
|
|
|
|
STATS,
|
2020-10-04 02:50:07 +00:00
|
|
|
CUTSCENE,
|
2020-09-29 01:36:57 +00:00
|
|
|
}
|
|
|
|
var _SAVE_FILE_PATH = "user://data"
|
|
|
|
# Change key in release
|
|
|
|
var _ENCRYPTION_KEY = "fas3uyf076HJsiUDs24dfI9"
|
2020-10-26 12:21:11 +00:00
|
|
|
var is_first_run = false
|
2020-09-29 01:36:57 +00:00
|
|
|
var _SCENES := {
|
|
|
|
States.MAIN_MENU: "res://src/Menu/MainMenu.tscn",
|
2020-10-20 13:51:17 +00:00
|
|
|
States.NEW_GAME: "res://src/GameWorld/GameWorld.tscn",
|
|
|
|
States.INTRO_CUTSCENE: "res://src/CutScenes/IntroCutscene.tscn"
|
2020-09-29 01:36:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 13:51:17 +00:00
|
|
|
var _player_spawn_point;
|
|
|
|
|
2020-10-04 02:50:07 +00:00
|
|
|
var GAME_TIMEOUT_MIN = 10
|
|
|
|
|
2020-10-20 13:51:17 +00:00
|
|
|
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,
|
2020-10-20 13:51:17 +00:00
|
|
|
},
|
|
|
|
"achivements": {
|
|
|
|
"a": false,
|
|
|
|
"b": false
|
2020-09-29 01:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 13:51:17 +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()
|
2020-10-26 12:21:11 +00:00
|
|
|
for key in _data:
|
|
|
|
print("[%s]: %s" % [key, _data[key]])
|
2020-10-04 02:50:07 +00:00
|
|
|
|
|
|
|
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()
|
2020-10-26 12:21:11 +00:00
|
|
|
is_first_run = true
|
2020-09-29 01:36:57 +00:00
|
|
|
save_data()
|
|
|
|
else:
|
2020-10-20 13:51:17 +00:00
|
|
|
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()
|
2020-10-26 12:21:11 +00:00
|
|
|
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
|
|
|
|
|
2020-10-20 13:51:17 +00:00
|
|
|
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
|
2020-10-20 13:51:17 +00:00
|
|
|
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
|
2020-10-20 13:51:17 +00:00
|
|
|
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")
|
2020-10-20 13:51:17 +00:00
|
|
|
# get_tree().change_scene_to(load(_SCENES[_state]))
|
|
|
|
|
2020-10-26 12:21:11 +00:00
|
|
|
|
|
|
|
func set_state(state:int = States.GAME):
|
2020-10-20 13:51:17 +00:00
|
|
|
_state = state
|
2020-10-26 12:21:11 +00:00
|
|
|
|
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()
|
2020-10-04 02:50:07 +00:00
|
|
|
func get_state():
|
|
|
|
return int(_state)
|
2020-09-29 01:36:57 +00:00
|
|
|
func go_to_main_menu():
|
|
|
|
save_data()
|
2020-10-20 13:51:17 +00:00
|
|
|
_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):
|
2020-10-20 13:51:17 +00:00
|
|
|
_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")
|
|
|
|
|
2020-10-26 12:21:11 +00:00
|
|
|
func player_died():
|
2020-09-29 01:36:57 +00:00
|
|
|
_run_data.deaths += 1
|
|
|
|
_data.statistics.deaths += 1
|
2020-10-26 12:21:11 +00:00
|
|
|
|
2020-10-20 13:51:17 +00:00
|
|
|
|
2020-09-29 01:36:57 +00:00
|
|
|
func coin_collected():
|
|
|
|
_run_data.coins += 10
|
|
|
|
_data.statistics.coins += 10
|