04.creating_the_enemy.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. .. _doc_your_first_2d_game_creating_the_enemy:
  2. Creating the enemy
  3. ==================
  4. Now it's time to make the enemies our player will have to dodge. Their behavior
  5. will not be very complex: mobs will spawn randomly at the edges of the screen,
  6. choose a random direction, and move in a straight line.
  7. We'll create a ``Mob`` scene, which we can then *instance* to create any number
  8. of independent mobs in the game.
  9. Node setup
  10. ~~~~~~~~~~
  11. Click Scene -> New Scene and add the following nodes:
  12. - :ref:`RigidBody2D <class_RigidBody2D>` (named ``Mob``)
  13. - :ref:`AnimatedSprite <class_AnimatedSprite>`
  14. - :ref:`CollisionShape2D <class_CollisionShape2D>`
  15. - :ref:`VisibilityNotifier2D <class_VisibilityNotifier2D>`
  16. Don't forget to set the children so they can't be selected, like you did with
  17. the Player scene.
  18. In the :ref:`RigidBody2D <class_RigidBody2D>` properties, set ``Gravity Scale``
  19. to ``0``, so the mob will not fall downward. In addition, under the
  20. :ref:`CollisionObject2D <class_CollisionObject2D>` section, click the ``Mask`` property and uncheck the first
  21. box. This will ensure the mobs do not collide with each other.
  22. .. image:: img/set_collision_mask.png
  23. Set up the :ref:`AnimatedSprite <class_AnimatedSprite>` like you did for the
  24. player. This time, we have 3 animations: ``fly``, ``swim``, and ``walk``. There
  25. are two images for each animation in the art folder.
  26. Adjust the "Speed (FPS)" to ``3`` for all animations.
  27. .. image:: img/mob_animations.gif
  28. Set the ``Playing`` property in the Inspector to "On".
  29. We'll select one of these animations randomly so that the mobs will have some
  30. variety.
  31. Like the player images, these mob images need to be scaled down. Set the
  32. ``AnimatedSprite``'s ``Scale`` property to ``(0.75, 0.75)``.
  33. As in the ``Player`` scene, add a ``CapsuleShape2D`` for the collision. To align
  34. the shape with the image, you'll need to set the ``Rotation Degrees`` property
  35. to ``90`` (under "Transform" in the Inspector).
  36. Save the scene.
  37. Enemy script
  38. ~~~~~~~~~~~~
  39. Add a script to the ``Mob`` like this:
  40. .. tabs::
  41. .. code-tab:: gdscript GDScript
  42. extends RigidBody2D
  43. .. code-tab:: csharp
  44. public class Mob : RigidBody2D
  45. {
  46. // Don't forget to rebuild the project.
  47. }
  48. .. code-tab:: cpp
  49. // Copy `player.gdns` to `mob.gdns` and replace `Player` with `Mob`.
  50. // Attach the `mob.gdns` file to the Mob node.
  51. // Create two files `mob.cpp` and `mob.hpp` next to `entry.cpp` in `src`.
  52. // This code goes in `mob.hpp`. We also define the methods we'll be using here.
  53. #ifndef MOB_H
  54. #define MOB_H
  55. #include <AnimatedSprite.hpp>
  56. #include <Godot.hpp>
  57. #include <RigidBody2D.hpp>
  58. class Mob : public godot::RigidBody2D {
  59. GODOT_CLASS(Mob, godot::RigidBody2D)
  60. godot::AnimatedSprite *_animated_sprite;
  61. public:
  62. void _init() {}
  63. void _ready();
  64. void _on_VisibilityNotifier2D_screen_exited();
  65. static void _register_methods();
  66. };
  67. #endif // MOB_H
  68. Now let's look at the rest of the script. In ``_ready()`` we play the animation
  69. and randomly choose one of the three animation types:
  70. .. tabs::
  71. .. code-tab:: gdscript GDScript
  72. func _ready():
  73. $AnimatedSprite.playing = true
  74. var mob_types = $AnimatedSprite.frames.get_animation_names()
  75. $AnimatedSprite.animation = mob_types[randi() % mob_types.size()]
  76. .. code-tab:: csharp
  77. public override void _Ready()
  78. {
  79. var animSprite = GetNode<AnimatedSprite>("AnimatedSprite");
  80. animSprite.Playing = true;
  81. string[] mobTypes = animSprite.Frames.GetAnimationNames();
  82. animSprite.Animation = mobTypes[GD.Randi() % mobTypes.Length];
  83. }
  84. .. code-tab:: cpp
  85. // This code goes in `mob.cpp`.
  86. #include "mob.hpp"
  87. #include <RandomNumberGenerator.hpp>
  88. #include <SpriteFrames.hpp>
  89. void Mob::_ready() {
  90. godot::Ref<godot::RandomNumberGenerator> random = godot::RandomNumberGenerator::_new();
  91. random->randomize();
  92. _animated_sprite = get_node<godot::AnimatedSprite>("AnimatedSprite");
  93. _animated_sprite->_set_playing(true);
  94. godot::PoolStringArray mob_types = _animated_sprite->get_sprite_frames()->get_animation_names();
  95. _animated_sprite->set_animation(mob_types[random->randi() % mob_types.size()]);
  96. }
  97. First, we get the list of animation names from the AnimatedSprite's ``frames``
  98. property. This returns an Array containing all three animation names: ``["walk",
  99. "swim", "fly"]``.
  100. We then need to pick a random number between ``0`` and ``2`` to select one of
  101. these names from the list (array indices start at ``0``). ``randi() % n``
  102. selects a random integer between ``0`` and ``n-1``.
  103. .. note:: You must use ``randomize()`` if you want your sequence of "random"
  104. numbers to be different every time you run the scene. We're going to
  105. use ``randomize()`` in our ``Main`` scene, so we won't need it here.
  106. The last piece is to make the mobs delete themselves when they leave the screen.
  107. Connect the ``screen_exited()`` signal of the ``VisibilityNotifier2D`` node and
  108. add this code:
  109. .. tabs::
  110. .. code-tab:: gdscript GDScript
  111. func _on_VisibilityNotifier2D_screen_exited():
  112. queue_free()
  113. .. code-tab:: csharp
  114. public void OnVisibilityNotifier2DScreenExited()
  115. {
  116. QueueFree();
  117. }
  118. .. code-tab:: cpp
  119. // This code goes in `mob.cpp`.
  120. void Mob::_on_VisibilityNotifier2D_screen_exited() {
  121. queue_free();
  122. }
  123. This completes the `Mob` scene.
  124. With the player and enemies ready, in the next part, we'll bring them together
  125. in a new scene. We'll make enemies spawn randomly around the game board and move
  126. forward, turning our project into a playable game.