aboutsummaryrefslogtreecommitdiffstats
path: root/godot/scenes/Game.gd
blob: 6d74fd6468822e49d16d182ae0604977df59567f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
extends Spatial

const NUM_LEVELS = 3
const PAR = [5,8,15]

# level control
var current_level_id = 0
var post_game = false

# stroke control
var current_strokes = 0
var strokes_per_level = [0,0,0]

var scenes = [
	preload("res://scenes/levels/level1/level1.tscn"), 
	preload("res://scenes/levels/level2/level2.tscn"),
	preload("res://scenes/levels/level3/level3.tscn")
	]

# Called when the node enters the scene tree for the first time.
func _ready():
	current_strokes = 0
	$JinglePlayer.stream.loop_mode = AudioStreamSample.LOOP_DISABLED
	load_scene_by_index(0)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if Input.is_action_just_pressed("ui_cancel"):
		close_scoreboard()
		get_tree().paused = true
		$PausePopup.show()
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		


func _on_QuitButton_pressed():
	get_tree().quit()


func _on_ResumeButton_pressed():
	$PausePopup.hide()
	get_tree().paused = false
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)


func _on_MainMenuButton_pressed():
	get_tree().paused = false
	get_tree().change_scene("res://scenes/levels/MainMenu.tscn")


func end_level():
	open_scoreboard()
	evaluate_player(current_strokes, PAR[current_level_id])
	$BGMPLayer.stream_paused = true
	$JinglePlayer.play()
	post_game = true


func add_stroke():
	current_strokes += 1


func revoke_stroke():
	current_strokes -= 1


func open_scoreboard():
	strokes_per_level[current_level_id] = current_strokes
	$Scoreboard.update_values(strokes_per_level, PAR)
	$Scoreboard.show()


func close_scoreboard():
	$Scoreboard.hide()


func is_post_game():
	return post_game


func next_level():
	post_game = false

	current_level_id += 1
	current_strokes = 0
	
	if current_level_id >= NUM_LEVELS:
		get_tree().change_scene("res://scenes/levels/MainMenu.tscn")
		return 
	
	load_scene_by_index(current_level_id)
	
	close_scoreboard()
	$Evaluation.hide()
	
	$BGMPLayer.stream_paused = false
	$JinglePlayer.stop()


func evaluate_player(strokes, par):
	if strokes <= 1:
		$Evaluation/EvaluationLabel.text = "HOLE IN ONE"
		$Evaluation.show()
		return 
	
	var diff = strokes - par
	if diff < -2:
		$Evaluation/EvaluationLabel.text = "%d" % diff
	elif diff == -2:
		$Evaluation/EvaluationLabel.text = "EAGLE"
	elif diff == -1: 
		$Evaluation/EvaluationLabel.text = "BIRDIE"
	elif diff == 0:
		$Evaluation/EvaluationLabel.text = "PAR"
	elif diff == 1:
		$Evaluation/EvaluationLabel.text = "BOGEY"
	elif diff == 2:
		$Evaluation/EvaluationLabel.text = "DOUBLE BOGEY"
	else:
		$Evaluation/EvaluationLabel.text = "+ %d" % diff
	
	$Evaluation.show()


func _on_JinglePlayer_finished():
	$BGMPLayer.stream_paused = false


func load_scene_by_index(index):
	# clear level
	for n in $LoadedLevel.get_children():
		$LoadedLevel.remove_child(n)
		n.queue_free()
	
	# add new level
	$LoadedLevel
	var instance = scenes[index].instance()
	$LoadedLevel.add_child(instance)