diff --git a/februarygodotgame/Readme.md b/februarygodotgame/Readme.md index f9a3516..c6eb505 100644 --- a/februarygodotgame/Readme.md +++ b/februarygodotgame/Readme.md @@ -73,3 +73,54 @@ if direction: 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! +