fix file location
This commit is contained in:
130
script/game_controller.gd
Normal file
130
script/game_controller.gd
Normal file
@@ -0,0 +1,130 @@
|
||||
extends Node
|
||||
|
||||
signal player_hit(bool)
|
||||
signal start_game_one_player
|
||||
signal start_game_two_player
|
||||
signal go_to_menu(String)
|
||||
signal point_gain(int, Vector2)
|
||||
signal go_to_score_saver(int)
|
||||
|
||||
var is_multiplayer := false
|
||||
var actual_score := 0
|
||||
var score :Score
|
||||
|
||||
|
||||
var delay_btw_input = 0.2
|
||||
var current_ui = ""
|
||||
var delay_input := 0.0
|
||||
|
||||
var music = AudioStreamPlayer.new()
|
||||
func _music_finished():
|
||||
music.play()
|
||||
|
||||
func _ready() -> void:
|
||||
music.volume_db = -10
|
||||
music.autoplay = true
|
||||
music.play()
|
||||
music.stream = load("res://audio/exodus.ogg")
|
||||
music.connect("finished", _music_finished)
|
||||
add_child(music)
|
||||
connect("start_game_one_player",start_one_player)
|
||||
connect("start_game_two_player",start_two_player)
|
||||
connect("go_to_score_saver", start_score_saver)
|
||||
connect("go_to_menu", start_menu)
|
||||
if FileAccess.file_exists("user://Score.res"):
|
||||
score = ResourceLoader.load("user://Score.res")
|
||||
else:
|
||||
score = Score.new()
|
||||
|
||||
func _input(event: InputEvent) -> void:
|
||||
if event.is_action_pressed("quit"):
|
||||
get_tree().quit()
|
||||
elif event is InputEventJoypadMotion:
|
||||
var array_ui = ["right", "left", "up", "down"]
|
||||
var max := 0.0
|
||||
var max_ui := ""
|
||||
for ui in array_ui:
|
||||
if max < Input.get_action_strength(ui):
|
||||
max = Input.get_action_strength(ui)
|
||||
max_ui = ui
|
||||
if max < 0.5 :
|
||||
delay_input = delay_btw_input
|
||||
current_ui = ""
|
||||
else:
|
||||
current_ui = max_ui
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
if current_ui != "":
|
||||
if delay_input < delay_btw_input:
|
||||
delay_input += delta
|
||||
else:
|
||||
var uiEvent = InputEventAction.new()
|
||||
uiEvent.action = "ui_" + current_ui
|
||||
uiEvent.pressed = true
|
||||
Input.parse_input_event(uiEvent)
|
||||
delay_input = 0.0
|
||||
|
||||
func start_one_player()->void:
|
||||
get_tree().change_scene_to_file("res://scene/main_game.tscn")
|
||||
|
||||
func start_two_player()->void:
|
||||
is_multiplayer = true
|
||||
get_tree().change_scene_to_file("res://scene/main_game_2_player.tscn")
|
||||
|
||||
func start_menu(user_name : String)->void:
|
||||
if is_multiplayer:
|
||||
var temp = score.score_multiplayer.get(user_name, -1.0)
|
||||
if temp < actual_score:
|
||||
score.score_multiplayer[user_name] = actual_score
|
||||
else:
|
||||
var temp = score.score_singleplayer.get(user_name, -1.0)
|
||||
if temp < actual_score:
|
||||
score.score_singleplayer[user_name] = actual_score
|
||||
user_name = ""
|
||||
actual_score = 0
|
||||
is_multiplayer = false
|
||||
score.save()
|
||||
get_tree().call_deferred("change_scene_to_file", "res://scene/load_screen.tscn")
|
||||
|
||||
func start_score_saver(score: int)->void:
|
||||
actual_score = score
|
||||
get_tree().call_deferred("change_scene_to_file", "res://scene/score_saver.tscn")
|
||||
|
||||
|
||||
func get_scoreboard_singleplayer(n_lignes: int = 0) -> String:
|
||||
return _get_scoreboard(score.score_singleplayer, n_lignes)
|
||||
|
||||
func get_scoreboard_multiplayer(n_lignes: int = 0) -> String:
|
||||
return _get_scoreboard(score.score_multiplayer, n_lignes)
|
||||
|
||||
func _get_scoreboard(dict: Dictionary, n_lignes: int) -> String:
|
||||
var text = ""
|
||||
var tableau_paires = []
|
||||
for key in dict:
|
||||
tableau_paires.append({"key": key, "value": dict[key]})
|
||||
tableau_paires.sort_custom(func(a, b): return a["value"] > b["value"])
|
||||
if n_lignes == 0:
|
||||
n_lignes = clamp( tableau_paires.size(), 5, 1000)
|
||||
for i in range(clamp(n_lignes, 0, tableau_paires.size())):
|
||||
text += str(i+1) + ". " + tableau_paires[i]["key"] + " : " + display_score(tableau_paires[i]["value"])
|
||||
if i < n_lignes-1:
|
||||
text+="\n"
|
||||
if tableau_paires.size() < n_lignes:
|
||||
for i in range(n_lignes - tableau_paires.size()):
|
||||
text += str(tableau_paires.size()+ i+1) + ". " + "---- : ----"
|
||||
if i < n_lignes - tableau_paires.size() -1 :
|
||||
text += "\n"
|
||||
return text
|
||||
|
||||
func display_score(score: int) -> String:
|
||||
var number_str = str(score)
|
||||
var result = ""
|
||||
var counter = 0
|
||||
|
||||
for i in range(number_str.length() - 1, -1, -1):
|
||||
result = number_str[i] + result
|
||||
counter += 1
|
||||
if counter % 3 == 0 and i != 0:
|
||||
result = "_" + result
|
||||
return result
|
||||
Reference in New Issue
Block a user