05.the_main_game_scene.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. .. _doc_your_first_2d_game_the_main_game_scene:
  2. The main game scene
  3. ===================
  4. Now it's time to bring everything we did together into a playable game scene.
  5. Create a new scene and add a :ref:`Node <class_Node>` named ``Main``.
  6. (The reason we are using Node instead of Node2D is because this node will
  7. be a container for handling game logic. It does not require 2D functionality itself.)
  8. Click the **Instance** button (represented by a chain link icon) and select your saved
  9. ``Player.tscn``.
  10. .. image:: img/instance_scene.png
  11. Now, add the following nodes as children of ``Main``, and name them as shown
  12. (values are in seconds):
  13. - :ref:`Timer <class_Timer>` (named ``MobTimer``) - to control how often mobs
  14. spawn
  15. - :ref:`Timer <class_Timer>` (named ``ScoreTimer``) - to increment the score
  16. every second
  17. - :ref:`Timer <class_Timer>` (named ``StartTimer``) - to give a delay before
  18. starting
  19. - :ref:`Marker2D <class_Marker2D>` (named ``StartPosition``) - to indicate
  20. the player's start position
  21. Set the ``Wait Time`` property of each of the ``Timer`` nodes as follows:
  22. - ``MobTimer``: ``0.5``
  23. - ``ScoreTimer``: ``1``
  24. - ``StartTimer``: ``2``
  25. In addition, set the ``One Shot`` property of ``StartTimer`` to "On" and set
  26. ``Position`` of the ``StartPosition`` node to ``(240, 450)``.
  27. Spawning mobs
  28. ~~~~~~~~~~~~~
  29. The Main node will be spawning new mobs, and we want them to appear at a random
  30. location on the edge of the screen. Add a :ref:`Path2D <class_Path2D>` node
  31. named ``MobPath`` as a child of ``Main``. When you select ``Path2D``, you will
  32. see some new buttons at the top of the editor:
  33. .. image:: img/path2d_buttons.png
  34. Select the middle one ("Add Point") and draw the path by clicking to add the
  35. points at the corners shown. To have the points snap to the grid, make sure "Use
  36. Grid Snap" and "Use Smart Snap" are both selected. These options can be found to the
  37. left of the "Lock" button, appearing as a magnet next to some dots and
  38. intersecting lines, respectively.
  39. .. image:: img/grid_snap_button.png
  40. .. important:: Draw the path in *clockwise* order, or your mobs will spawn
  41. pointing *outwards* instead of *inwards*!
  42. .. image:: img/draw_path2d.gif
  43. After placing point ``4`` in the image, click the "Close Curve" button and your
  44. curve will be complete.
  45. Now that the path is defined, add a :ref:`PathFollow2D <class_PathFollow2D>`
  46. node as a child of ``MobPath`` and name it ``MobSpawnLocation``. This node will
  47. automatically rotate and follow the path as it moves, so we can use it to select
  48. a random position and direction along the path.
  49. Your scene should look like this:
  50. .. image:: img/main_scene_nodes.png
  51. Main script
  52. ~~~~~~~~~~~
  53. Add a script to ``Main``. At the top of the script, we use
  54. ``@export var mob_scene: PackedScene`` to allow us to choose the Mob scene we want
  55. to instance.
  56. .. tabs::
  57. .. code-tab:: gdscript GDScript
  58. extends Node
  59. @export var mob_scene: PackedScene
  60. var score
  61. .. code-tab:: csharp
  62. public class Main : Node
  63. {
  64. // Don't forget to rebuild the project so the editor knows about the new export variable.
  65. #pragma warning disable 649
  66. // We assign this in the editor, so we don't need the warning about not being assigned.
  67. [Export]
  68. public PackedScene MobScene;
  69. #pragma warning restore 649
  70. public int Score;
  71. }
  72. .. code-tab:: cpp
  73. // Copy `player.gdns` to `main.gdns` and replace `Player` with `Main`.
  74. // Attach the `main.gdns` file to the Main node.
  75. // Create two files `main.cpp` and `main.hpp` next to `entry.cpp` in `src`.
  76. // This code goes in `main.hpp`. We also define the methods we'll be using here.
  77. #ifndef MAIN_H
  78. #define MAIN_H
  79. #include <AudioStreamPlayer.hpp>
  80. #include <CanvasLayer.hpp>
  81. #include <Godot.hpp>
  82. #include <Node.hpp>
  83. #include <PackedScene.hpp>
  84. #include <PathFollow2D.hpp>
  85. #include <RandomNumberGenerator.hpp>
  86. #include <Timer.hpp>
  87. #include "hud.hpp"
  88. #include "player.hpp"
  89. class Main : public godot::Node {
  90. GODOT_CLASS(Main, godot::Node)
  91. int score;
  92. HUD *_hud;
  93. Player *_player;
  94. godot::Node2D *_start_position;
  95. godot::PathFollow2D *_mob_spawn_location;
  96. godot::Timer *_mob_timer;
  97. godot::Timer *_score_timer;
  98. godot::Timer *_start_timer;
  99. godot::AudioStreamPlayer *_music;
  100. godot::AudioStreamPlayer *_death_sound;
  101. godot::Ref<godot::RandomNumberGenerator> _random;
  102. public:
  103. godot::Ref<godot::PackedScene> mob_scene;
  104. void _init() {}
  105. void _ready();
  106. void game_over();
  107. void new_game();
  108. void _on_MobTimer_timeout();
  109. void _on_ScoreTimer_timeout();
  110. void _on_StartTimer_timeout();
  111. static void _register_methods();
  112. };
  113. #endif // MAIN_H
  114. We also add a call to ``randomize()`` here so that the random number
  115. generator generates different random numbers each time the game is run:
  116. .. tabs::
  117. .. code-tab:: gdscript GDScript
  118. func _ready():
  119. randomize()
  120. .. code-tab:: csharp
  121. public override void _Ready()
  122. {
  123. GD.Randomize();
  124. }
  125. .. code-tab:: cpp
  126. // This code goes in `main.cpp`.
  127. #include "main.hpp"
  128. #include <SceneTree.hpp>
  129. #include "mob.hpp"
  130. void Main::_ready() {
  131. _hud = get_node<HUD>("HUD");
  132. _player = get_node<Player>("Player");
  133. _start_position = get_node<godot::Node2D>("StartPosition");
  134. _mob_spawn_location = get_node<godot::PathFollow2D>("MobPath/MobSpawnLocation");
  135. _mob_timer = get_node<godot::Timer>("MobTimer");
  136. _score_timer = get_node<godot::Timer>("ScoreTimer");
  137. _start_timer = get_node<godot::Timer>("StartTimer");
  138. // Uncomment these after adding the nodes in the "Sound effects" section of "Finishing up".
  139. //_music = get_node<godot::AudioStreamPlayer>("Music");
  140. //_death_sound = get_node<godot::AudioStreamPlayer>("DeathSound");
  141. _random = (godot::Ref<godot::RandomNumberGenerator>)godot::RandomNumberGenerator::_new();
  142. _random->randomize();
  143. }
  144. Click the ``Main`` node and you will see the ``Mob Scene`` property in the Inspector
  145. under "Script Variables".
  146. You can assign this property's value in two ways:
  147. - Drag ``Mob.tscn`` from the "FileSystem" dock and drop it in the **Mob Scene**
  148. property.
  149. - Click the down arrow next to "[empty]" and choose "Load". Select ``Mob.tscn``.
  150. Next, select the ``Player`` node in the Scene dock, and access the Node dock on
  151. the sidebar. Make sure to have the Signals tab selected in the Node dock.
  152. You should see a list of the signals for the ``Player`` node. Find and
  153. double-click the ``hit`` signal in the list (or right-click it and select
  154. "Connect..."). This will open the signal connection dialog. We want to make a
  155. new function named ``game_over``, which will handle what needs to happen when a
  156. game ends. Type "game_over" in the "Receiver Method" box at the bottom of the
  157. signal connection dialog and click "Connect". Add the following code to the new
  158. function, as well as a ``new_game`` function that will set everything up for a
  159. new game:
  160. .. tabs::
  161. .. code-tab:: gdscript GDScript
  162. func game_over():
  163. $ScoreTimer.stop()
  164. $MobTimer.stop()
  165. func new_game():
  166. score = 0
  167. $Player.start($StartPosition.position)
  168. $StartTimer.start()
  169. .. code-tab:: csharp
  170. public void GameOver()
  171. {
  172. GetNode<Timer>("MobTimer").Stop();
  173. GetNode<Timer>("ScoreTimer").Stop();
  174. }
  175. public void NewGame()
  176. {
  177. Score = 0;
  178. var player = GetNode<Player>("Player");
  179. var startPosition = GetNode<Position2D>("StartPosition");
  180. player.Start(startPosition.Position);
  181. GetNode<Timer>("StartTimer").Start();
  182. }
  183. .. code-tab:: cpp
  184. // This code goes in `main.cpp`.
  185. void Main::game_over() {
  186. _score_timer->stop();
  187. _mob_timer->stop();
  188. }
  189. void Main::new_game() {
  190. score = 0;
  191. _player->start(_start_position->get_position());
  192. _start_timer->start();
  193. }
  194. Now connect the ``timeout()`` signal of each of the Timer nodes (``StartTimer``,
  195. ``ScoreTimer`` , and ``MobTimer``) to the main script. ``StartTimer`` will start
  196. the other two timers. ``ScoreTimer`` will increment the score by 1.
  197. .. tabs::
  198. .. code-tab:: gdscript GDScript
  199. func _on_ScoreTimer_timeout():
  200. score += 1
  201. func _on_StartTimer_timeout():
  202. $MobTimer.start()
  203. $ScoreTimer.start()
  204. .. code-tab:: csharp
  205. public void OnScoreTimerTimeout()
  206. {
  207. Score++;
  208. }
  209. public void OnStartTimerTimeout()
  210. {
  211. GetNode<Timer>("MobTimer").Start();
  212. GetNode<Timer>("ScoreTimer").Start();
  213. }
  214. .. code-tab:: cpp
  215. // This code goes in `main.cpp`.
  216. void Main::_on_ScoreTimer_timeout() {
  217. score += 1;
  218. }
  219. void Main::_on_StartTimer_timeout() {
  220. _mob_timer->start();
  221. _score_timer->start();
  222. }
  223. // Also add this to register all methods and the mob scene property.
  224. void Main::_register_methods() {
  225. godot::register_method("_ready", &Main::_ready);
  226. godot::register_method("game_over", &Main::game_over);
  227. godot::register_method("new_game", &Main::new_game);
  228. godot::register_method("_on_MobTimer_timeout", &Main::_on_MobTimer_timeout);
  229. godot::register_method("_on_ScoreTimer_timeout", &Main::_on_ScoreTimer_timeout);
  230. godot::register_method("_on_StartTimer_timeout", &Main::_on_StartTimer_timeout);
  231. godot::register_property("mob_scene", &Main::mob_scene, (godot::Ref<godot::PackedScene>)nullptr);
  232. }
  233. In ``_on_MobTimer_timeout()``, we will create a mob instance, pick a random
  234. starting location along the ``Path2D``, and set the mob in motion. The
  235. ``PathFollow2D`` node will automatically rotate as it follows the path, so we
  236. will use that to select the mob's direction as well as its position.
  237. When we spawn a mob, we'll pick a random value between ``150.0`` and
  238. ``250.0`` for how fast each mob will move (it would be boring if they were
  239. all moving at the same speed).
  240. Note that a new instance must be added to the scene using ``add_child()``.
  241. .. tabs::
  242. .. code-tab:: gdscript GDScript
  243. func _on_MobTimer_timeout():
  244. # Create a new instance of the Mob scene.
  245. var mob = mob_scene.instantiate()
  246. # Choose a random location on Path2D.
  247. var mob_spawn_location = get_node("MobPath/MobSpawnLocation")
  248. mob_spawn_location.offset = randi()
  249. # Set the mob's direction perpendicular to the path direction.
  250. var direction = mob_spawn_location.rotation + PI / 2
  251. # Set the mob's position to a random location.
  252. mob.position = mob_spawn_location.position
  253. # Add some randomness to the direction.
  254. direction += randf_range(-PI / 4, PI / 4)
  255. mob.rotation = direction
  256. # Choose the velocity for the mob.
  257. var velocity = Vector2(randf_range(150.0, 250.0), 0.0)
  258. mob.linear_velocity = velocity.rotated(direction)
  259. # Spawn the mob by adding it to the Main scene.
  260. add_child(mob)
  261. .. code-tab:: csharp
  262. public void OnMobTimerTimeout()
  263. {
  264. // Note: Normally it is best to use explicit types rather than the `var`
  265. // keyword. However, var is acceptable to use here because the types are
  266. // obviously Mob and PathFollow2D, since they appear later on the line.
  267. // Create a new instance of the Mob scene.
  268. var mob = (Mob)MobScene.Instance();
  269. // Choose a random location on Path2D.
  270. var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
  271. mobSpawnLocation.Offset = GD.Randi();
  272. // Set the mob's direction perpendicular to the path direction.
  273. float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
  274. // Set the mob's position to a random location.
  275. mob.Position = mobSpawnLocation.Position;
  276. // Add some randomness to the direction.
  277. direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
  278. mob.Rotation = direction;
  279. // Choose the velocity.
  280. var velocity = new Vector2((float)GD.RandRange(150.0, 250.0), 0);
  281. mob.LinearVelocity = velocity.Rotated(direction);
  282. // Spawn the mob by adding it to the Main scene.
  283. AddChild(mob);
  284. }
  285. .. code-tab:: cpp
  286. // This code goes in `main.cpp`.
  287. void Main::_on_MobTimer_timeout() {
  288. // Create a new instance of the Mob scene.
  289. godot::Node *mob = mob_scene->instance();
  290. // Choose a random location on Path2D.
  291. _mob_spawn_location->set_offset((real_t)_random->randi());
  292. // Set the mob's direction perpendicular to the path direction.
  293. real_t direction = _mob_spawn_location->get_rotation() + (real_t)Math_PI / 2;
  294. // Set the mob's position to a random location.
  295. mob->set("position", _mob_spawn_location->get_position());
  296. // Add some randomness to the direction.
  297. direction += _random->randf_range((real_t)-Math_PI / 4, (real_t)Math_PI / 4);
  298. mob->set("rotation", direction);
  299. // Choose the velocity for the mob.
  300. godot::Vector2 velocity = godot::Vector2(_random->randf_range(150.0, 250.0), 0.0);
  301. mob->set("linear_velocity", velocity.rotated(direction));
  302. // Spawn the mob by adding it to the Main scene.
  303. add_child(mob);
  304. }
  305. .. important:: Why ``PI``? In functions requiring angles, Godot uses *radians*,
  306. not degrees. Pi represents a half turn in radians, about
  307. ``3.1415`` (there is also ``TAU`` which is equal to ``2 * PI``).
  308. If you're more comfortable working with degrees, you'll need to
  309. use the ``deg2rad()`` and ``rad2deg()`` functions to convert
  310. between the two.
  311. Testing the scene
  312. ~~~~~~~~~~~~~~~~~
  313. Let's test the scene to make sure everything is working. Add this ``new_game``
  314. call to ``_ready()``:
  315. .. tabs::
  316. .. code-tab:: gdscript GDScript
  317. func _ready():
  318. randomize()
  319. new_game()
  320. .. code-tab:: csharp
  321. public override void _Ready()
  322. {
  323. NewGame();
  324. }
  325. .. code-tab:: cpp
  326. // This code goes in `main.cpp`.
  327. void Main::_ready() {
  328. new_game();
  329. }
  330. Let's also assign ``Main`` as our "Main Scene" - the one that runs automatically
  331. when the game launches. Press the "Play" button and select ``Main.tscn`` when
  332. prompted.
  333. .. tip:: If you had already set another scene as the "Main Scene", you can right
  334. click ``Main.tscn`` in the FileSystem dock and select "Set As Main Scene".
  335. You should be able to move the player around, see mobs spawning, and see the
  336. player disappear when hit by a mob.
  337. When you're sure everything is working, remove the call to ``new_game()`` from
  338. ``_ready()``.
  339. What's our game lacking? Some user interface. In the next lesson, we'll add a
  340. title screen and display the player's score.