source: Studio/Globals.gd@ aa1db83

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

Made Keybindings ini file and Mana bar.

  • Property mode set to 100644
File size: 6.3 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
13
14# A canvas layer node so our GUI/UI is always drawn on top
15var canvas_layer = null
16
17# The debug display scene, and a variable to hold the debug display
18const DEBUG_DISPLAY_SCENE = preload("res://Debug_Display.tscn")
19var debug_display = null
20
21# ------------------------------------
22
23
24# A variable to hold all of the respawn points in a level
25var respawn_points = null
26
27# A variable to hold the mouse sensitivity (so Player.gd can load it)
28var mouse_sensitivity = 0.08
29# A variable to hold the joypad sensitivity (so Player.gd can load it)
30var joypad_sensitivity = 2
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
86# Set the keybinds
87func set_game_binds():
88 for key in keybinds.keys():
89 var value = keybinds[key]
90
91 var actionlist = InputMap.get_action_list(key)
92 if !actionlist.empty():
93 InputMap.action_erase_event(key, actionlist[0])
94
95 if value != null:
96 var new_key = InputEventKey.new()
97 new_key.set_scancode(value)
98 InputMap.action_add_event(key, new_key)
99
100func write_config():
101 for key in keybinds.keys():
102 var key_value = keybinds[key]
103 if key_value != null:
104 keybind_configfile.set_value("keybinds", key, key_value)
105 else:
106 keybind_configfile.set_value("keybinds", key, "")
107 keybind_configfile.save(keybind_filepath)
108
109func get_respawn_position():
110 # If we do not have any respawn points, return origin
111 if respawn_points == null:
112 return Vector3(0, 0, 0)
113 # If we have respawn points, get a random one and return it's global position
114 else:
115 var respawn_point = rand_range(0, respawn_points.size()-1)
116 return respawn_points[respawn_point].global_transform.origin
117
118
119func load_new_scene(new_scene_path):
120 # Set respawn points to null so when/if we get to a level with no respawn points,
121 # we do not respawn at the respawn points in the level prior.
122 respawn_points = null
123
124 # Delete all of the sounds
125 for sound in created_audio:
126 if (sound != null):
127 sound.queue_free()
128 created_audio.clear()
129
130 # Change scenes
131 get_tree().change_scene(new_scene_path)
132
133
134func _process(delta):
135 # If ui_cancel is pressed, we want to open a popup if one is not already open
136 if Input.is_action_just_pressed("ui_cancel"):
137 if popup == null:
138 # Make a new popup scene
139 popup = POPUP_SCENE.instance()
140
141 # Connect the signals
142 popup.get_node("Button_quit").connect("pressed", self, "popup_quit")
143 popup.connect("popup_hide", self, "popup_closed")
144 popup.get_node("Button_resume").connect("pressed", self, "popup_closed")
145
146 # Add it as a child, and make it pop up in the center of the screen
147 canvas_layer.add_child(popup)
148 popup.popup_centered()
149
150 # Make sure the mouse is visible
151 Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
152
153 # Pause the game
154 get_tree().paused = true
155
156
157func popup_closed():
158 # Unpause the game
159 get_tree().paused = false
160
161 # If we have a popup, destoy it
162 if popup != null:
163 popup.queue_free()
164 popup = null
165
166func popup_quit():
167 get_tree().paused = false
168
169 # Make sure the mouse is visible
170 Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
171
172 # If we have a popup, destoy it
173 if popup != null:
174 popup.queue_free()
175 popup = null
176
177 # Go back to the title screen scene
178 load_new_scene(MAIN_MENU_PATH)
179
180
181func set_debug_display(display_on):
182 # If we are turning off the debug display
183 if display_on == false:
184 # If we have a debug display, then free it and set debug_display to null
185 if debug_display != null:
186 debug_display.queue_free()
187 debug_display = null
188 # If we are turning on the debug display
189 else:
190 # If we do not have a debug display, instance/spawn a new one and set it to debug_display
191 if debug_display == null:
192 debug_display = DEBUG_DISPLAY_SCENE.instance()
193 canvas_layer.add_child(debug_display)
194
195
196func play_sound(sound_name, loop_sound=false, sound_position=null):
197 # If we have a audio clip with with the name sound_name
198 if audio_clips.has(sound_name):
199 # Make a new simple audio player and set it's looping variable to the loop_sound
200 var new_audio = SIMPLE_AUDIO_PLAYER_SCENE.instance()
201 new_audio.should_loop = loop_sound
202
203 # Add it as a child and add it to created_audio
204 add_child(new_audio)
205 created_audio.append(new_audio)
206
207 # Send the newly created simple audio player the audio stream and sound position
208 new_audio.play_sound(audio_clips[sound_name], sound_position)
209
210 # If we do not have an audio clip with the name sound_name, print a error message
211 else:
212 print ("ERROR: cannot play sound that does not exist in audio_clips!")
213
214
Note: See TracBrowser for help on using the repository browser.