The main game scene

Now it’s time to bring everything we did together into a playable game scene.

Create a new scene and add a Node named Main. Ensure you create a Node, not a Node2D. Click the “Instance” button and select your saved Player.tscn.

../../_images/instance_scene.png

现在, 将以下节点添加为 Main 的子节点, 并按如下所示对其进行命名(值以秒为单位):

  • Timer (名为 MobTimer)——控制怪物产生的频率

  • Timer (名为 ScoreTimer)——每秒增加分数

  • Timer (名为 StartTimer)——在开始之前给出延迟

  • Position2D (名为 StartPosition) - 表示玩家的起始位置

如下设置每个 Timer 节点的 Wait Time 属性:

  • MobTimer: 0.5

  • ScoreTimer: 1

  • StartTimer: 2

此外, 将 StartTimerOne Shot 属性设置为 On, 并将 StartPosition 节点的 Position 设置为 (240, 450).

生成怪物

Main 节点将产生新的生物, 我们希望它们出现在屏幕边缘的随机位置. 添加一个名为 MobPathPath2D 节点作为 Main 的子级. 当你选择 Path2D 时, 你将在编辑器顶部看到一些新按钮:

../../_images/path2d_buttons.png

选择中间的按钮( 添加点 ), 然后通过点击给四角添加点来绘制路径. 要使点吸附到网格, 请确保同时选中 “使用吸附” 和 “使用网格吸附 “. 该选项可以在” 锁定”按钮左侧找到, 图标为一个磁铁加三个点或一些网格线.

../../_images/grid_snap_button.png

重要

顺时针 的顺序绘制路径, 否则小怪会 向外 而非 向内 生成!

../../_images/draw_path2d.gif

在图像上放置点 4 后, 点击 闭合曲线 按钮, 你的曲线将完成.

现在已经定义了路径, 添加一个 PathFollow2D 节点作为 MobPath 的子节点, 并将其命名为 MobSpawnLocation. 该节点在移动时, 将自动旋转并沿着该路径, 因此我们可以使用它沿路径来选择随机位置和方向.

您的场景应如下所示:

../../_images/main_scene_nodes.png

Main 脚本

将脚本添加到 Main. 在脚本的顶部, 我们使用 export (PackedScene) 来允许我们选择要实例化的 Mob 场景.

GDScript

C#

C++

  1. extends Node
  2. export(PackedScene) var mob_scene
  3. var score
  1. public class Main : Node
  2. {
  3. // Don't forget to rebuild the project so the editor knows about the new export variable.
  4. #pragma warning disable 649
  5. // We assign this in the editor, so we don't need the warning about not being assigned.
  6. [Export]
  7. public PackedScene MobScene;
  8. #pragma warning restore 649
  9. public int Score;
  10. }
  1. // Copy `player.gdns` to `main.gdns` and replace `Player` with `Main`.
  2. // Attach the `main.gdns` file to the Main node.
  3. // Create two files `main.cpp` and `main.hpp` next to `entry.cpp` in `src`.
  4. // This code goes in `main.hpp`. We also define the methods we'll be using here.
  5. #ifndef MAIN_H
  6. #define MAIN_H
  7. #include <AudioStreamPlayer.hpp>
  8. #include <CanvasLayer.hpp>
  9. #include <Godot.hpp>
  10. #include <Node.hpp>
  11. #include <PackedScene.hpp>
  12. #include <PathFollow2D.hpp>
  13. #include <RandomNumberGenerator.hpp>
  14. #include <Timer.hpp>
  15. #include "hud.hpp"
  16. #include "player.hpp"
  17. class Main : public godot::Node {
  18. GODOT_CLASS(Main, godot::Node)
  19. int score;
  20. HUD *_hud;
  21. Player *_player;
  22. godot::Node2D *_start_position;
  23. godot::PathFollow2D *_mob_spawn_location;
  24. godot::Timer *_mob_timer;
  25. godot::Timer *_score_timer;
  26. godot::Timer *_start_timer;
  27. godot::AudioStreamPlayer *_music;
  28. godot::AudioStreamPlayer *_death_sound;
  29. godot::Ref<godot::RandomNumberGenerator> _random;
  30. public:
  31. godot::Ref<godot::PackedScene> mob_scene;
  32. void _init() {}
  33. void _ready();
  34. void game_over();
  35. void new_game();
  36. void _on_MobTimer_timeout();
  37. void _on_ScoreTimer_timeout();
  38. void _on_StartTimer_timeout();
  39. static void _register_methods();
  40. };
  41. #endif // MAIN_H

We also add a call to randomize() here so that the random number generator generates different random numbers each time the game is run:

GDScript

C#

C++

  1. func _ready():
  2. randomize()
  1. public override void _Ready()
  2. {
  3. GD.Randomize();
  4. }
  1. // This code goes in `main.cpp`.
  2. #include "main.hpp"
  3. #include <SceneTree.hpp>
  4. #include "mob.hpp"
  5. void Main::_ready() {
  6. _hud = get_node<HUD>("HUD");
  7. _player = get_node<Player>("Player");
  8. _start_position = get_node<godot::Node2D>("StartPosition");
  9. _mob_spawn_location = get_node<godot::PathFollow2D>("MobPath/MobSpawnLocation");
  10. _mob_timer = get_node<godot::Timer>("MobTimer");
  11. _score_timer = get_node<godot::Timer>("ScoreTimer");
  12. _start_timer = get_node<godot::Timer>("StartTimer");
  13. // Uncomment these after adding the nodes in the "Sound effects" section of "Finishing up".
  14. //_music = get_node<godot::AudioStreamPlayer>("Music");
  15. //_death_sound = get_node<godot::AudioStreamPlayer>("DeathSound");
  16. _random = (godot::Ref<godot::RandomNumberGenerator>)godot::RandomNumberGenerator::_new();
  17. _random->randomize();
  18. }

Click the Main node and you will see the Mob Scene property in the Inspector under “Script Variables”.

有两种方法来给这个属性赋值:

  • 从 “文件系统” 面板中拖动 Mob.tscnMob 属性中.

  • 单击 “「空」” 旁边的下拉箭头按钮, 选择 “载入”, 接着选择 Mob.tscn .

在场景树中选择 Player 节点, 然后选择 节点(Node) 选项卡(位于右侧属性旁), 确保已选择 信号(Signals) .

你可以看到 Player 的信号列表. 找到 hit 信号并双击(或右键选择 “连接信号…”). 我们将在打开的界面创建 game_over 函数, 用来处理游戏结束时发生的事情. 在 连接信号到方法 窗口底部的 接收方法 框中键入 game_over . 添加以下代码, 以及 new_game 函数以设置新游戏的所需内容:

GDScript

C#

C++

  1. func game_over():
  2. $ScoreTimer.stop()
  3. $MobTimer.stop()
  4. func new_game():
  5. score = 0
  6. $Player.start($StartPosition.position)
  7. $StartTimer.start()
  1. public void GameOver()
  2. {
  3. GetNode<Timer>("MobTimer").Stop();
  4. GetNode<Timer>("ScoreTimer").Stop();
  5. }
  6. public void NewGame()
  7. {
  8. Score = 0;
  9. var player = GetNode<Player>("Player");
  10. var startPosition = GetNode<Position2D>("StartPosition");
  11. player.Start(startPosition.Position);
  12. GetNode<Timer>("StartTimer").Start();
  13. }
  1. // This code goes in `main.cpp`.
  2. void Main::game_over() {
  3. _score_timer->stop();
  4. _mob_timer->stop();
  5. }
  6. void Main::new_game() {
  7. score = 0;
  8. _player->start(_start_position->get_position());
  9. _start_timer->start();
  10. }

现在将每个 Timer 节点( StartTimer , ScoreTimerMobTimer )的 timeout() 信号连接到 main 脚本。 StartTimer 将启动其他两个计时器.。 ScoreTimer 将使得分加1。

GDScript

C#

C++

  1. func _on_ScoreTimer_timeout():
  2. score += 1
  3. func _on_StartTimer_timeout():
  4. $MobTimer.start()
  5. $ScoreTimer.start()
  1. public void OnScoreTimerTimeout()
  2. {
  3. Score++;
  4. }
  5. public void OnStartTimerTimeout()
  6. {
  7. GetNode<Timer>("MobTimer").Start();
  8. GetNode<Timer>("ScoreTimer").Start();
  9. }
  1. // This code goes in `main.cpp`.
  2. void Main::_on_ScoreTimer_timeout() {
  3. score += 1;
  4. }
  5. void Main::_on_StartTimer_timeout() {
  6. _mob_timer->start();
  7. _score_timer->start();
  8. }
  9. // Also add this to register all methods and the mob scene property.
  10. void Main::_register_methods() {
  11. godot::register_method("_ready", &Main::_ready);
  12. godot::register_method("game_over", &Main::game_over);
  13. godot::register_method("new_game", &Main::new_game);
  14. godot::register_method("_on_MobTimer_timeout", &Main::_on_MobTimer_timeout);
  15. godot::register_method("_on_ScoreTimer_timeout", &Main::_on_ScoreTimer_timeout);
  16. godot::register_method("_on_StartTimer_timeout", &Main::_on_StartTimer_timeout);
  17. godot::register_property("mob_scene", &Main::mob_scene, (godot::Ref<godot::PackedScene>)nullptr);
  18. }

In _on_MobTimer_timeout(), we will create a mob instance, pick a random starting location along the Path2D, and set the mob in motion. The PathFollow2D node will automatically rotate as it follows the path, so we will use that to select the mob’s direction as well as its position. When we spawn a mob, we’ll pick a random value between 150.0 and 250.0 for how fast each mob will move (it would be boring if they were all moving at the same speed).

注意,必须使用 add_child() 将新实例添加到场景中。

GDScript

C#

C++

  1. func _on_MobTimer_timeout():
  2. # Choose a random location on Path2D.
  3. var mob_spawn_location = get_node("MobPath/MobSpawnLocation");
  4. mob_spawn_location.offset = randi()
  5. # Create a Mob instance and add it to the scene.
  6. var mob = mob_scene.instance()
  7. add_child(mob)
  8. # Set the mob's direction perpendicular to the path direction.
  9. var direction = mob_spawn_location.rotation + PI / 2
  10. # Set the mob's position to a random location.
  11. mob.position = mob_spawn_location.position
  12. # Add some randomness to the direction.
  13. direction += rand_range(-PI / 4, PI / 4)
  14. mob.rotation = direction
  15. # Choose the velocity.
  16. var velocity = Vector2(rand_range(150.0, 250.0), 0.0)
  17. mob.linear_velocity = velocity.rotated(direction)
  1. public void OnMobTimerTimeout()
  2. {
  3. // Note: Normally it is best to use explicit types rather than the `var`
  4. // keyword. However, var is acceptable to use here because the types are
  5. // obviously PathFollow2D and Mob, since they appear later on the line.
  6. // Choose a random location on Path2D.
  7. var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
  8. mobSpawnLocation.Offset = GD.Randi();
  9. // Create a Mob instance and add it to the scene.
  10. var mob = (Mob)MobScene.Instance();
  11. AddChild(mob);
  12. // Set the mob's direction perpendicular to the path direction.
  13. float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
  14. // Set the mob's position to a random location.
  15. mob.Position = mobSpawnLocation.Position;
  16. // Add some randomness to the direction.
  17. direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
  18. mob.Rotation = direction;
  19. // Choose the velocity.
  20. var velocity = new Vector2((float)GD.RandRange(150.0, 250.0), 0);
  21. mob.LinearVelocity = velocity.Rotated(direction);
  22. }
  1. // This code goes in `main.cpp`.
  2. void Main::_on_MobTimer_timeout() {
  3. // Choose a random location on Path2D.
  4. _mob_spawn_location->set_offset((real_t)_random->randi());
  5. // Create a Mob instance and add it to the scene.
  6. godot::Node *mob = mob_scene->instance();
  7. add_child(mob);
  8. // Set the mob's direction perpendicular to the path direction.
  9. real_t direction = _mob_spawn_location->get_rotation() + (real_t)Math_PI / 2;
  10. // Set the mob's position to a random location.
  11. mob->set("position", _mob_spawn_location->get_position());
  12. // Add some randomness to the direction.
  13. direction += _random->randf_range((real_t)-Math_PI / 4, (real_t)Math_PI / 4);
  14. mob->set("rotation", direction);
  15. // Choose the velocity for the mob.
  16. godot::Vector2 velocity = godot::Vector2(_random->randf_range(150.0, 250.0), 0.0);
  17. mob->set("linear_velocity", velocity.rotated(direction));
  18. }

重要

Why PI? In functions requiring angles, Godot uses radians, not degrees. Pi represents a half turn in radians, about 3.1415 (there is also TAU which is equal to 2 * PI). If you’re more comfortable working with degrees, you’ll need to use the deg2rad() and rad2deg() functions to convert between the two.

测试场景

Let’s test the scene to make sure everything is working. Add this new_game call to _ready():

GDScript

C#

C++

  1. func _ready():
  2. randomize()
  3. new_game()
  1. public override void _Ready()
  2. {
  3. NewGame();
  4. }
  1. // This code goes in `main.cpp`.
  2. void Main::_ready() {
  3. new_game();
  4. }

让我们同时指定 Main 作为我们的“主场景”——游戏启动时自动运行的场景。按下“运行”按钮,当弹出提示时选择 Main.tscn

你应该可以四处移动游戏角色,观察敌人的生成,以及玩家被敌人击中时会消失。

当你确定一切正常时,在 _ready() 中移除对 new_game() 的调用。

What’s our game lacking? Some user interface. In the next lesson, we’ll add a title screen and display the player’s score.