source: Studio/Globals.gd@ 49e3bfa

sandbox
Last change on this file since 49e3bfa was ef2ca13, checked in by Kevin Chapman <victory2b@…>, 3 years ago

Enabled the death Popup when player reaches 0 heath.

  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[f7b09c8]1extends Node
2
3# The path to the title screen scene
4const MAIN_MENU_PATH = "res://Main_Menu.tscn"
5
6
7# ------------------------------------
8# All of the GUI/UI related variables
9
10# The popup scene, and a variable to hold the popup
11const POPUP_SCENE = preload("res://Pause_Popup.tscn")
12var popup = null
[994b0da]13var DEAD_SCENE = preload("res://Dead_Popup.tscn")
[3c8ca52]14
[f7b09c8]15# A canvas layer node so our GUI/UI is always drawn on top
16var canvas_layer = null
17
18# The debug display scene, and a variable to hold the debug display
19const DEBUG_DISPLAY_SCENE = preload("res://Debug_Display.tscn")
20var debug_display = null
21
22# ------------------------------------
23
24
25# A variable to hold all of the respawn points in a level
26var respawn_points = null
27
28# A variable to hold the mouse sensitivity (so Player.gd can load it)
29var mouse_sensitivity = 0.08
30
31
32# ------------------------------------
33# All of the audio files.
34
35# You will need to provide your own sound files.
36# One site I'd recommend is GameSounds.xyz. I'm using Sonniss' GDC Game Audio bundle for 2017.
37# The tracks I've used (with some minor editing) are as follows:
38# Gamemaster audio gun sound pack:
39# gun_revolver_pistol_shot_04,
40# gun_semi_auto_rifle_cock_02,
41# gun_submachine_auto_shot_00_automatic_preview_01
42var audio_clips = {
43 "pistol_shot":null, #preload("res://path_to_your_audio_here!")
44 "rifle_shot":null, #preload("res://path_to_your_audio_here!")
45 "gun_cock":null, #preload("res://path_to_your_audio_here!")
46}
47
48# The simple audio player scene
49const SIMPLE_AUDIO_PLAYER_SCENE = preload("res://Simple_Audio_Player.tscn")
50
51# A list to hold all of the created audio nodes
52var created_audio = []
53
[aa1db83]54# Config file key bindings
55var keybind_filepath = "res://config/keybinds.ini"
56var keybind_configfile
57var keybinds = {}
[f7b09c8]58
[aa1db83]59# ------------------------------------
[f7b09c8]60
61func _ready():
62 # Randomize the random number generator, so we get random values
63 randomize()
64
65 # Make a new canvas layer.
66 # This is so our popup always appears on top of everything else
67 canvas_layer = CanvasLayer.new()
68 add_child(canvas_layer)
69
[aa1db83]70 # Loads in the keybindings
71 keybind_configfile = ConfigFile.new()
72 if keybind_configfile.load(keybind_filepath) == OK:
73 for key in keybind_configfile.get_section_keys("keybinds"):
74 var key_value = keybind_configfile.get_value("keybinds", key)
75 if str(key_value) != "":
76 keybinds[key] = key_value
77 else:
78 keybinds[key] = null
79 print(key, " : ", OS.get_scancode_string(key_value))
80 else:
81 print("Keybinds Config File Not Found")
82
83 set_game_binds()
84
[994b0da]85func player_dead():
[ef2ca13]86 if popup == null:
87 # Make a new popup scene
88 popup = DEAD_SCENE.instance()
89
[994b0da]90 # Connect the signals
[ef2ca13]91 popup.get_node("Button_quit").connect("pressed", self, "popup_quit")
92 popup.get_node("Button_respawn").connect("pressed", self, "popup_closed")
93
[994b0da]94 # Add it as a child, and make it pop up in the center of the screen
[ef2ca13]95 canvas_layer.add_child(popup)
96 popup.popup_centered()
97
[994b0da]98 # Make sure the mouse is visible
99 Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
[ef2ca13]100
[994b0da]101 # Pause the game
102 get_tree().paused = true
103
[aa1db83]104# Set the keybinds
105func set_game_binds():
106 for key in keybinds.keys():
107 var value = keybinds[key]
108
109 var actionlist = InputMap.get_action_list(key)
110 if !actionlist.empty():
111 InputMap.action_erase_event(key, actionlist[0])
112
113 if value != null:
114 var new_key = InputEventKey.new()
115 new_key.set_scancode(value)
116 InputMap.action_add_event(key, new_key)
117
118func write_config():
119 for key in keybinds.keys():
120 var key_value = keybinds[key]
121 if key_value != null:
122 keybind_configfile.set_value("keybinds", key, key_value)
123 else:
124 keybind_configfile.set_value("keybinds", key, "")
125 keybind_configfile.save(keybind_filepath)
[f7b09c8]126
127func get_respawn_position():
128 # If we do not have any respawn points, return origin
129 if respawn_points == null:
130 return Vector3(0, 0, 0)
131 # If we have respawn points, get a random one and return it's global position
132 else:
133 var respawn_point = rand_range(0, respawn_points.size()-1)
134 return respawn_points[respawn_point].global_transform.origin
135
136
137func load_new_scene(new_scene_path):
138 # Set respawn points to null so when/if we get to a level with no respawn points,
139 # we do not respawn at the respawn points in the level prior.
140 respawn_points = null
141
142 # Delete all of the sounds
143 for sound in created_audio:
144 if (sound != null):
145 sound.queue_free()
146 created_audio.clear()
147
148 # Change scenes
149 get_tree().change_scene(new_scene_path)
150
151
152func _process(delta):
153 # If ui_cancel is pressed, we want to open a popup if one is not already open
154 if Input.is_action_just_pressed("ui_cancel"):
155 if popup == null:
156 # Make a new popup scene
157 popup = POPUP_SCENE.instance()
158
159 # Connect the signals
160 popup.get_node("Button_quit").connect("pressed", self, "popup_quit")
161 popup.connect("popup_hide", self, "popup_closed")
162 popup.get_node("Button_resume").connect("pressed", self, "popup_closed")
163
164 # Add it as a child, and make it pop up in the center of the screen
165 canvas_layer.add_child(popup)
166 popup.popup_centered()
167
168 # Make sure the mouse is visible
169 Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
170
171 # Pause the game
172 get_tree().paused = true
173
174
175func popup_closed():
176 # Unpause the game
177 get_tree().paused = false
178
179 # If we have a popup, destoy it
180 if popup != null:
181 popup.queue_free()
182 popup = null
[ef2ca13]183 # Re-captures mouse when returning to game
184 if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
185 Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
[f7b09c8]186
187func popup_quit():
188 get_tree().paused = false
189
190 # Make sure the mouse is visible
191 Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
192
193 # If we have a popup, destoy it
194 if popup != null:
195 popup.queue_free()
196 popup = null
197
198 # Go back to the title screen scene
199 load_new_scene(MAIN_MENU_PATH)
200
201
202func set_debug_display(display_on):
203 # If we are turning off the debug display
204 if display_on == false:
205 # If we have a debug display, then free it and set debug_display to null
206 if debug_display != null:
207 debug_display.queue_free()
208 debug_display = null
209 # If we are turning on the debug display
210 else:
211 # If we do not have a debug display, instance/spawn a new one and set it to debug_display
212 if debug_display == null:
213 debug_display = DEBUG_DISPLAY_SCENE.instance()
214 canvas_layer.add_child(debug_display)
215
216
217func play_sound(sound_name, loop_sound=false, sound_position=null):
218 # If we have a audio clip with with the name sound_name
219 if audio_clips.has(sound_name):
220 # Make a new simple audio player and set it's looping variable to the loop_sound
221 var new_audio = SIMPLE_AUDIO_PLAYER_SCENE.instance()
222 new_audio.should_loop = loop_sound
223
224 # Add it as a child and add it to created_audio
225 add_child(new_audio)
226 created_audio.append(new_audio)
227
228 # Send the newly created simple audio player the audio stream and sound position
229 new_audio.play_sound(audio_clips[sound_name], sound_position)
230
231 # If we do not have an audio clip with the name sound_name, print a error message
232 else:
233 print ("ERROR: cannot play sound that does not exist in audio_clips!")
234
235
Note: See TracBrowser for help on using the repository browser.