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