基本完结
This commit is contained in:
parent
a3d0c1df92
commit
3c157e7772
|
@ -1 +0,0 @@
|
|||
2ddemo
|
|
@ -0,0 +1,71 @@
|
|||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class Hud : CanvasLayer
|
||||
{
|
||||
|
||||
// StartGame 信号
|
||||
[Signal]
|
||||
public delegate void StartGameEventHandler();
|
||||
|
||||
public void ShowMessage(string text)
|
||||
{
|
||||
var message = GetNode<Label>("Message");
|
||||
message.Text = text;
|
||||
message.Show();
|
||||
|
||||
GetNode<Timer>("MessageTimer").Start();
|
||||
}
|
||||
|
||||
// 处理异步
|
||||
async public void ShowGameOver()
|
||||
{
|
||||
|
||||
ShowMessage("Game Over");
|
||||
|
||||
var messageTimer = GetNode<Timer>("MessageTimer");
|
||||
// await 关键字暂停了 ShowGameOver 方法的执行,
|
||||
// 直到 messageTimer 发出超时信号(Timeout)。ToSignal 是 Godot 提供的用于异步等待信号的方法。
|
||||
await ToSignal(messageTimer, Timer.SignalName.Timeout);
|
||||
|
||||
// 显示游戏标题
|
||||
var message = GetNode<Label>("Message");
|
||||
message.Text = "Dodge the Creeps!";
|
||||
message.Show();
|
||||
|
||||
//可以使用场景树的 get_tree().create_timer(2) 函数替代使用 Timer 节点。
|
||||
//这对于延迟非常有用,例如在上述代码中,在这里我们需要在显示“开始”按钮前等待片刻。
|
||||
await ToSignal(GetTree().CreateTimer(1.0), SceneTreeTimer.SignalName.Timeout);
|
||||
GetNode<Button>("StartButton").Show();
|
||||
}
|
||||
|
||||
public void UpdateScore(int score)
|
||||
{
|
||||
GetNode<Label>("ScoreLabel").Text = score.ToString();
|
||||
}
|
||||
|
||||
|
||||
// We also specified this function name in PascalCase in the editor's connection window.
|
||||
private void OnStartButtonPressed()
|
||||
{
|
||||
GetNode<Button>("StartButton").Hide();
|
||||
EmitSignal(SignalName.StartGame);
|
||||
}
|
||||
|
||||
// We also specified this function name in PascalCase in the editor's connection window.
|
||||
private void OnMessageTimerTimeout()
|
||||
{
|
||||
GetNode<Label>("Message").Hide();
|
||||
}
|
||||
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
{
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
}
|
||||
}
|
24
Main.cs
24
Main.cs
|
@ -12,7 +12,7 @@ public partial class Main : Node
|
|||
public override void _Ready()
|
||||
{
|
||||
|
||||
NewGame();
|
||||
|
||||
}
|
||||
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
|
@ -26,11 +26,20 @@ public partial class Main : Node
|
|||
{
|
||||
GetNode<Timer>("MobTimer").Stop();
|
||||
GetNode<Timer>("ScoreTimer").Stop();
|
||||
GetNode<Hud>("HUD").ShowGameOver();
|
||||
|
||||
}
|
||||
|
||||
public void NewGame()
|
||||
{
|
||||
_score = 0;
|
||||
var hud = GetNode<Hud>("HUD");
|
||||
hud.UpdateScore(_score);
|
||||
hud.ShowMessage("Get Ready!");
|
||||
|
||||
// 清楚上一轮游戏的小怪,清除队列call_group() 函数调用组中每个节点上的删除函数——让每个怪物删除其自身。
|
||||
|
||||
GetTree().CallGroup("mobs", Node.MethodName.QueueFree);
|
||||
|
||||
var player = GetNode<Player>("Player");
|
||||
var startPosition = GetNode<Marker2D>("StartPosition");
|
||||
|
@ -45,6 +54,8 @@ public partial class Main : Node
|
|||
private void OnScoreTimerTimeout()
|
||||
{
|
||||
_score++;
|
||||
GetNode<Hud>("HUD").UpdateScore(_score);
|
||||
|
||||
}
|
||||
|
||||
// We also specified this function name in PascalCase in the editor's connection window.
|
||||
|
@ -70,21 +81,32 @@ public partial class Main : Node
|
|||
|
||||
// Choose a random location on Path2D.
|
||||
// 选择一个随机path
|
||||
// 获取路径节点中的一个子节点 PathFollow2D,用于在路径上指定一个位置。PathFollow2D 是沿着预定义路径移动的节点。
|
||||
// ProgressRatio 用于设置 PathFollow2D 在路径上的位置。
|
||||
// GD.Randf() 返回一个 0.0 到 1.0 的随机浮点数,使生成位置在路径的任何随机点上。
|
||||
|
||||
var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
|
||||
mobSpawnLocation.ProgressRatio = GD.Randf();
|
||||
|
||||
// Set the mob's direction perpendicular to the path direction.
|
||||
// direction 变量表示 Mob 的初始方向,基于 mobSpawnLocation 的当前旋转
|
||||
// 并加上 π/2。这是因为 Mob 的方向需要垂直于路径的方向。
|
||||
float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
|
||||
|
||||
// Set the mob's position to a random location.
|
||||
// 将 Mob 的位置设置为 mobSpawnLocation 的当前位置。这意味着 Mob 将在 Path2D 路径上的随机位置生成。
|
||||
mob.Position = mobSpawnLocation.Position;
|
||||
|
||||
// Add some randomness to the direction.
|
||||
// 给 Mob 的方向添加了一些随机性,范围在 -π/4 到 π/4 之间,
|
||||
// 这样 Mob 不会总是沿着同样的方向移动。通过增加随机旋转,使生成的 Mob 朝着一个稍微偏离的方向移动,增加了不可预测性。
|
||||
// 将 Mob 的旋转设置为这个新的方向。
|
||||
direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
|
||||
mob.Rotation = direction;
|
||||
|
||||
// Choose the velocity.
|
||||
// 怪物速度在150-250随机
|
||||
// 使用 Rotated(direction) 方法将速度向量旋转到 Mob 的朝向,使 Mob 朝着设定的方向移动。
|
||||
var velocity = new Vector2((float)GD.RandRange(150.0, 250.0), 0);
|
||||
mob.LinearVelocity = velocity.Rotated(direction);
|
||||
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://c8eip7vknt4no"]
|
||||
|
||||
[ext_resource type="Script" path="res://Hud.cs" id="1_b1a6w"]
|
||||
[ext_resource type="FontFile" uid="uid://77ktpe0gag84" path="res://fonts/Xolonium-Regular.ttf" id="2_p0olu"]
|
||||
|
||||
[node name="HUD" type="CanvasLayer"]
|
||||
script = ExtResource("1_b1a6w")
|
||||
|
||||
[node name="ScoreLabel" type="Label" parent="."]
|
||||
anchors_preset = 5
|
||||
anchor_left = 0.5
|
||||
anchor_right = 0.5
|
||||
offset_left = -22.5
|
||||
offset_right = 22.5
|
||||
offset_bottom = 78.0
|
||||
grow_horizontal = 2
|
||||
theme_override_fonts/font = ExtResource("2_p0olu")
|
||||
theme_override_font_sizes/font_size = 64
|
||||
text = "1"
|
||||
|
||||
[node name="Message" type="Label" parent="."]
|
||||
custom_minimum_size = Vector2(480, 0)
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -88.5
|
||||
offset_top = -39.0
|
||||
offset_right = 88.5
|
||||
offset_bottom = 39.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
theme_override_fonts/font = ExtResource("2_p0olu")
|
||||
theme_override_font_sizes/font_size = 64
|
||||
text = "Dodge The Creeps!"
|
||||
horizontal_alignment = 1
|
||||
vertical_alignment = 1
|
||||
autowrap_mode = 2
|
||||
|
||||
[node name="StartButton" type="Button" parent="."]
|
||||
custom_minimum_size = Vector2(200, 100)
|
||||
anchors_preset = 7
|
||||
anchor_left = 0.5
|
||||
anchor_top = 1.0
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 1.0
|
||||
offset_left = -100.0
|
||||
offset_top = -198.0
|
||||
offset_right = 100.0
|
||||
offset_bottom = -98.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 0
|
||||
theme_override_fonts/font = ExtResource("2_p0olu")
|
||||
theme_override_font_sizes/font_size = 64
|
||||
text = "Start"
|
||||
|
||||
[node name="MessageTimer" type="Timer" parent="."]
|
||||
wait_time = 2.0
|
||||
one_shot = true
|
||||
|
||||
[connection signal="pressed" from="StartButton" to="." method="OnStartButtonPressed"]
|
||||
[connection signal="timeout" from="MessageTimer" to="." method="OnMessageTimerTimeout"]
|
|
@ -1,8 +1,9 @@
|
|||
[gd_scene load_steps=5 format=3 uid="uid://do83xd3leoud0"]
|
||||
[gd_scene load_steps=6 format=3 uid="uid://do83xd3leoud0"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://d00fo6b251wcw" path="res://player.tscn" id="1_8xvyb"]
|
||||
[ext_resource type="Script" path="res://Main.cs" id="1_o5srj"]
|
||||
[ext_resource type="PackedScene" uid="uid://caloretbkmfga" path="res://mob.tscn" id="2_852fk"]
|
||||
[ext_resource type="PackedScene" uid="uid://c8eip7vknt4no" path="res://hud.tscn" id="4_ippxg"]
|
||||
|
||||
[sub_resource type="Curve2D" id="Curve2D_ibmf5"]
|
||||
_data = {
|
||||
|
@ -35,7 +36,10 @@ curve = SubResource("Curve2D_ibmf5")
|
|||
position = Vector2(-1, -1)
|
||||
rotation = 0.000868055
|
||||
|
||||
[node name="HUD" parent="." instance=ExtResource("4_ippxg")]
|
||||
|
||||
[connection signal="Hit" from="Player" to="." method="GameOver"]
|
||||
[connection signal="timeout" from="MobTimer" to="." method="OnMobTimerTimeout"]
|
||||
[connection signal="timeout" from="ScoreTimer" to="." method="OnScoreTimerTimeout"]
|
||||
[connection signal="timeout" from="StartTimer" to="." method="OnStartTimerTimeout"]
|
||||
[connection signal="StartGame" from="HUD" to="." method="NewGame"]
|
||||
|
|
Loading…
Reference in New Issue