source: Studio/Globals.gd@ 994b0da

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

(WIP) Got dead menu to show up, but it does not show the mouse even though the code is the same as other menus that work.

  • Property mode set to 100644
File size: 6.8 KB
Line 
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
13var DEAD_SCENE = preload("res://Dead_Popup.tscn")
14var dead_popup = null
15
16# A canvas layer node so our GUI/UI is always drawn on top
17var canvas_layer = null
18
19# The debug display scene, and a variable to hold the debug display
20const DEBUG_DISPLAY_SCENE = preload("res://Debug_Display.tscn")
21var debug_display = null
22
23# ------------------------------------
24
25
26# A variable to hold all of the respawn points in a level
27var respawn_points = null
28
29# A variable to hold the mouse sensitivity (so Player.gd can load it)
30var mouse_sensitivity = 0.08
31
32
33# ------------------------------------
34# All of the audio files.
35
36# You will need to provide your own sound files.
37# One site I'd recommend is GameSounds.xyz. I'm using Sonniss' GDC Game Audio bundle for 2017.
38# The tracks I've used (with some minor editing) are as follows:
39# Gamemaster audio gun sound pack:
40# gun_revolver_pistol_shot_04,
41# gun_semi_auto_rifle_cock_02,
42# gun_submachine_auto_shot_00_automatic_preview_01
43var audio_clips = {
44 "pistol_shot":null, #preload("res://path_to_your_audio_here!")
45 "rifle_shot":null, #preload("res://path_to_your_audio_here!")
46 "gun_cock":null, #preload("res://path_to_your_audio_here!")
47}
48
49# The simple audio player scene
50const SIMPLE_AUDIO_PLAYER_SCENE = preload("res://Simple_Audio_Player.tscn")
51
52# A list to hold all of the created audio nodes
53var created_audio = []
54
55# Config file key bindings
56var keybind_filepath = "res://config/keybinds.ini"
57var keybind_configfile
58var keybinds = {}
59
60# ------------------------------------
61
62func _ready():
63 # Randomize the random number generator, so we get random values
64 randomize()
65
66 # Make a new canvas layer.
67 # This is so our popup always appears on top of everything else
68 canvas_layer = CanvasLayer.new()
69 add_child(canvas_layer)
70
71 # Loads in the keybindings
72 keybind_configfile = ConfigFile.new()
73 if keybind_configfile.load(keybind_filepath) == OK:
74 for key in keybind_configfile.get_section_keys("keybinds"):
75 var key_value = keybind_configfile.get_value("keybinds", key)
76 if str(key_value) != "":
77 keybinds[key] = key_value
78 else:
79 keybinds[key] = null
80 print(key, " : ", OS.get_scancode_string(key_value))
81 else:
82 print("Keybinds Config File Not Found")
83
84 set_game_binds()
85
86func player_dead():
87 # Make a new popup scene
88 dead_popup = DEAD_SCENE.instance()
89
90 # Connect the signals
91 dead_popup.get_node("Button_quit").connect("pressed", self, "popup_quit")
92 dead_popup.get_node("Button_respawn").connect("pressed", self, "popup_closed")
93
94 # Add it as a child, and make it pop up in the center of the screen
95 canvas_layer.add_child(dead_popup)
96 dead_popup.popup_centered()
97
98 # Make sure the mouse is visible
99 Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
100
101 # Pause the game
102 get_tree().paused = true
103
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)
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
183
184func popup_quit():
185 get_tree().paused = false
186
187 # Make sure the mouse is visible
188 Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
189
190 # If we have a popup, destoy it
191 if popup != null:
192 popup.queue_free()
193 popup = null
194
195 # Go back to the title screen scene
196 load_new_scene(MAIN_MENU_PATH)
197
198
199func set_debug_display(display_on):
200 # If we are turning off the debug display
201 if display_on == false:
202 # If we have a debug display, then free it and set debug_display to null
203 if debug_display != null:
204 debug_display.queue_free()
205 debug_display = null
206 # If we are turning on the debug display
207 else:
208 # If we do not have a debug display, instance/spawn a new one and set it to debug_display
209 if debug_display == null:
210 debug_display = DEBUG_DISPLAY_SCENE.instance()
211 canvas_layer.add_child(debug_display)
212
213
214func play_sound(sound_name, loop_sound=false, sound_position=null):
215 # If we have a audio clip with with the name sound_name
216 if audio_clips.has(sound_name):
217 # Make a new simple audio player and set it's looping variable to the loop_sound
218 var new_audio = SIMPLE_AUDIO_PLAYER_SCENE.instance()
219 new_audio.should_loop = loop_sound
220
221 # Add it as a child and add it to created_audio
222 add_child(new_audio)
223 created_audio.append(new_audio)
224
225 # Send the newly created simple audio player the audio stream and sound position
226 new_audio.play_sound(audio_clips[sound_name], sound_position)
227
228 # If we do not have an audio clip with the name sound_name, print a error message
229 else:
230 print ("ERROR: cannot play sound that does not exist in audio_clips!")
231
232
Note: See TracBrowser for help on using the repository browser.