.. | ||
assets | ||
scenes | ||
scripts | ||
.gitattributes | ||
.gitignore | ||
icon.svg | ||
icon.svg.import | ||
project.godot | ||
Readme.md |
Stretch Goals
For this game I set two stretch goals for myself based on popular requests:
- Sound Integration
- Bad Guy Death
Sound Integration
I wanted two kinds of sounds in the game, namely music tracks and sound effects for things like footsteps while running.
For both, I was able to use the normal AudioStreamPlayer node.
Game Music
I implemented one AudioStreamPlayer as a child of the SceneManager. This is a good place to control the playing of background audio tracks.
Two audio tracks are in assets/audio/music. These are ogg-vorbis encoded music tracks.
I want to have the ability to play a different track of my choosing per game level. So I add the resource paths to the GameController.
var soundtracks = ["res://assets/audio/music/CrashingOut.ogg","res://assets/audio/music/DragAndDreadTheme.ogg", "res://assets/audio/music/CrashingOut.ogg"]
The SceneManager already calls the GameController's reset function when it loads up, so I gave that function a return value passing the track that the SceneManager should play for that level.
func reset():
countdown = timers[currentLevel]
player.health = player.max_health
coinsCollected = 0
return(soundtracks[currentLevel])
This is important, because the SceneManager should not try to play a track until the level has fully loaded.
To play the track, the SceneManager needs to load the track using the string supplied by the GameController.
func _ready() -> void:
buildLevel()
var track = Gamecontroller.reset()
var audiofile = load(track)
audio_stream_player.stream = audiofile
audio_stream_player.play()
We can now have an audio track per level!
Possible improvements to this would be to have the sound tracks have a transition like a cross-fade.
Game SFX
An AudioStreamPlayer node was added as a child of the Player scene.
Audio files for SFX were added directly to the player, using preload.
@onready var audio_stream_player: AudioStreamPlayer = $AudioStreamPlayer
const GRASS_RUNNING = preload("res://assets/audio/footsteps/Grass Running.wav")
It was then relatively easy to have the footsteps audio play, considering that our code is already tracking if the player is running in order to play the "run" animation. The update looks like this:
if direction:
if not isJumping:
playerGraphic.play("run")
if not audio_stream_player.playing:
audio_stream_player.stream=GRASS_RUNNING
audio_stream_player.playing=true
Bad Guy Death
To be able to damage the bad guys, a few changes need to be made.
First, the SceneManager needs a loop to tell the GameController about each bad guy.
for obj in badguys.get_children():
if obj is Slime:
totalBadguys +=1
obj.playerDamageSignal.connect(Gamecontroller.playerDamage)
Gamecontroller.addEnemyToLevel(obj)
Notice that each enemy is given to a function in the GameController called addEnemyToLevel
The addEnemyToLevel
uses a Dictionary object to assign some stats to each enemy, using the enemy itself as a key.
A dictionary object is powerful and easy to create:
var enemiesDict = {}
Then the function stores an info object for each enemy like so:
func addEnemyToLevel(enemy):
#assign a health using the enemy as a key
var enemyStat = {
"health": slime.health,
"damage": slime.meleeDamage
}
enemiesDict[enemy] = enemyStat
Notice how we use the slime CustomResource to get the default stats for slime creatures?
To retrieve the stats of an individual enemy from the dictionary, we just have to use the enemy object itself as a key.
We can do this in the bulletHit
function, where we can add one additional check. If the bullet is hitting a Slime object, we can use that enemy object to retreive their health from the dictionary. After that it is easy to reduce the health, and optionally destroy them if their health goes to zero or below.
if body is Slime:
#reduce health of slime
enemiesDict[body]["health"] -=30
if enemiesDict[body]["health"] <=0:
#killed the slime
destroySignal.emit(body)
And so we can now shoot the bad guys!