Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Créer l'ennemi

Il est temps maintenant de créer des ennemis que notre joueur devra esquiver. Leur comportement ne sera pas très complexe : des monstres vont apparaître au hasard aux bords de l'écran et se déplacer dans une direction aléatoire en ligne droite, puis disparaître lorsqu'ils sortent de l'écran.

Nous allons créer une scène Mob, que nous pouvons ensuite instancier pour créer un nombre quelconque de monstres indépendants dans le jeu.

Configuration du nœud

Click Scene -> New Scene from the top menu and add the following nodes:

N'oubliez pas de configurer les enfants pour qu'ils ne puissent pas être sélectionnés, comme vous l'avez fait avec la scène Player.

Select the Mob node and set it's Gravity Scale property in the RigidBody2D section of the inspector to 0. This will prevent the mob from falling downwards.

In addition, under the CollisionObject2D section just beneath the RigidBody2D section, expand the Collision group and uncheck the 1 inside the Mask property. This will ensure the mobs do not collide with each other.

../../_images/set_collision_mask.webp

Set up the AnimatedSprite2D like you did for the player. This time, we have 3 animations: fly, swim, and walk. There are two images for each animation in the art folder.

The Animation Speed property has to be set for each individual animation. Adjust it to 3 for all 3 animations.

../../_images/mob_animations.webp

You can use the "Play Animation" buttons on the right of the Animation Speed input field to preview your animations.

Nous allons choisir une de ces animations au hasard pour que les monstres aient une certaine variété.

Like the player images, these mob images need to be scaled down. Set the AnimatedSprite2D's Scale property to (0.75, 0.75).

As in the Player scene, add a CapsuleShape2D for the collision. To align the shape with the image, you'll need to set the Rotation property to 90 (under "Transform" in the Inspector).

Enregistrer la scène.

Script de l'ennemi

Ajoutez un script au Mob comme ceci :

extends RigidBody2D

Maintenant, regardons le reste du script. Dans _ready(), nous jouons l'animation et choisissons au hasard un des trois types d'animation :

func _ready():
    var mob_types = $AnimatedSprite2D.sprite_frames.get_animation_names()
    $AnimatedSprite2D.play(mob_types[randi() % mob_types.size()])

First, we get the list of animation names from the AnimatedSprite2D's sprite_frames property. This returns an Array containing all three animation names: ["walk", "swim", "fly"].

Nous devons ensuite choisir un nombre aléatoire entre 0 et 2 pour sélectionner l'un de ces noms dans la liste (les indices des listes commencent à 0). La fonction randi() % n sélectionne un nombre entier aléatoire entre 0 et n-1.

The last piece is to make the mobs delete themselves when they leave the screen. Connect the screen_exited() signal of the VisibleOnScreenNotifier2D node to the Mob and add this code:

func _on_visible_on_screen_notifier_2d_screen_exited():
    queue_free()

Cela complète la scène Mob.

Le joueur et les ennemis étant prêts, dans la partie suivante, nous allons les réunir dans une nouvelle scène. Nous ferons en sorte que les ennemis apparaissent de manière aléatoire sur le plateau de jeu et avancent, transformant notre projet en un jeu jouable.