Skip to content

Commit

Permalink
Updated Dodge the Creeps C# to Godot mono 4.2 (#1000)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivan Shakhov <[email protected]>
Co-authored-by: Aaron Franke <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2024
1 parent 4f866f2 commit fbef18f
Show file tree
Hide file tree
Showing 25 changed files with 425 additions and 358 deletions.
2 changes: 1 addition & 1 deletion 2d/dodge_the_creeps/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func start(pos):
$CollisionShape2D.disabled = false


func _on_Player_body_entered(_body):
func _on_body_entered(_body):
hide() # Player disappears after being hit.
hit.emit()
# Must be deferred as we can't change physics properties on a physics callback.
Expand Down
2 changes: 1 addition & 1 deletion 2d/dodge_the_creeps/Player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ process_material = SubResource("7")
texture = ExtResource("2")
speed_scale = 2.0

[connection signal="body_entered" from="." to="." method="_on_Player_body_entered"]
[connection signal="body_entered" from="." to="." method="_on_body_entered"]
11 changes: 9 additions & 2 deletions mono/dodge_the_creeps/Dodge the Creeps with C#.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<Project Sdk="Godot.NET.Sdk/4.0.0-dev5">
<Project Sdk="Godot.NET.Sdk/4.2.1">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<RootNamespace>DodgeTheCreeps</RootNamespace>
</PropertyGroup>
<ItemGroup>
<None Include="**/*.tscn" />
</ItemGroup>
<ItemGroup>
<None Include="project.godot" />
</ItemGroup>
</Project>
11 changes: 5 additions & 6 deletions mono/dodge_the_creeps/HUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public partial class HUD : CanvasLayer
{
[Signal]
public delegate void StartGame();
public delegate void StartGameEventHandler();

public void ShowMessage(string text)
{
Expand All @@ -19,11 +19,10 @@ public async void ShowGameOver()
ShowMessage("Game Over");

var messageTimer = GetNode<Timer>("MessageTimer");
await ToSignal(messageTimer, "timeout");
await ToSignal(messageTimer, Timer.SignalName.Timeout);

var messageLabel = GetNode<Label>("MessageLabel");
messageLabel.Text = "Dodge the\nCreeps!";
messageLabel.Show();
ShowMessage("Dodge the\nCreeps!");
await ToSignal(GetTree().CreateTimer(1.0), SceneTreeTimer.SignalName.Timeout);

GetNode<Button>("StartButton").Show();
}
Expand All @@ -36,7 +35,7 @@ public void UpdateScore(int score)
public void OnStartButtonPressed()
{
GetNode<Button>("StartButton").Hide();
EmitSignal(nameof(StartGame));
EmitSignal(SignalName.StartGame);
}

public void OnMessageTimerTimeout()
Expand Down
63 changes: 32 additions & 31 deletions mono/dodge_the_creeps/HUD.tscn
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
[gd_scene load_steps=6 format=2]
[gd_scene load_steps=5 format=3 uid="uid://b0ljm4vbkww0f"]

[ext_resource path="res://HUD.cs" type="Script" id=1]
[ext_resource path="res://fonts/Xolonium-Regular.ttf" type="FontData" id=2]
[ext_resource type="Script" path="res://HUD.cs" id="1"]
[ext_resource type="FontFile" uid="uid://cu4g4pt1v4fyv" path="res://fonts/Xolonium-Regular.ttf" id="2"]

[sub_resource type="Font" id=1]
size = 64
use_mipmaps = true
font_data = ExtResource( 2 )
[sub_resource type="InputEventAction" id="InputEventAction_vmrao"]
action = &"start_game"

[sub_resource type="Font" id=2]
size = 64
use_mipmaps = true
font_data = ExtResource( 2 )

[sub_resource type="Font" id=3]
size = 64
use_mipmaps = true
font_data = ExtResource( 2 )
[sub_resource type="Shortcut" id="Shortcut_b2h4v"]
events = [SubResource("InputEventAction_vmrao")]

[node name="HUD" type="CanvasLayer"]
script = ExtResource( 1 )
script = ExtResource("1")

[node name="ScoreLabel" type="Label" parent="."]
anchors_preset = 5
anchor_left = 0.5
anchor_right = 0.5
offset_left = -25.0
offset_right = 25.0
offset_bottom = 100.0
custom_fonts/font = SubResource( 1 )
text = "0
"
align = 1
grow_horizontal = 2
theme_override_fonts/font = ExtResource("2")
theme_override_font_sizes/font_size = 64
text = "0"
horizontal_alignment = 1

[node name="MessageLabel" type="Label" parent="."]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -200.0
offset_top = -150.0
offset_right = 200.0
custom_fonts/font = SubResource( 2 )
offset_left = -240.0
offset_top = -79.5
offset_right = 240.0
offset_bottom = 79.5
grow_horizontal = 2
grow_vertical = 2
theme_override_fonts/font = ExtResource("2")
theme_override_font_sizes/font_size = 64
text = "Dodge the
Creeps!"
align = 1
valign = 1
Creeps"
horizontal_alignment = 1
autowrap_mode = 2

[node name="StartButton" type="Button" parent="."]
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 = -200.0
offset_top = -140.0
offset_right = 100.0
offset_bottom = -100.0
custom_fonts/font = SubResource( 3 )
offset_bottom = -40.0
theme_override_fonts/font = ExtResource("2")
theme_override_font_sizes/font_size = 65
shortcut = SubResource("Shortcut_b2h4v")
text = "Start"

[node name="MessageTimer" type="Timer" parent="."]
Expand Down
47 changes: 25 additions & 22 deletions mono/dodge_the_creeps/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ public partial class Main : Node
#pragma warning disable 649
// We assign this in the editor, so we don't need the warning about not being assigned.
[Export]
public PackedScene mobScene;
public PackedScene MobScene { get; set; }
#pragma warning restore 649

public int score;

public override void _Ready()
{
GD.Randomize();
}
private int _score;

public void GameOver()
{
Expand All @@ -30,48 +25,53 @@ public void NewGame()
{
// Note that for calling Godot-provided methods with strings,
// we have to use the original Godot snake_case name.
GetTree().CallGroup("mobs", "queue_free");
score = 0;
GetTree().CallGroup("mobs", Node.MethodName.QueueFree);
_score = 0;

var player = GetNode<Player>("Player");
var startPosition = GetNode<Position2D>("StartPosition");
var startPosition = GetNode<Marker2D>("StartPosition");
player.Start(startPosition.Position);

GetNode<Timer>("StartTimer").Start();

var hud = GetNode<HUD>("HUD");
hud.UpdateScore(score);
hud.UpdateScore(_score);
hud.ShowMessage("Get Ready!");

GetNode<AudioStreamPlayer>("Music").Play();
}

public void OnStartTimerTimeout()
private void OnStartTimerTimeout()
{
GetNode<Timer>("MobTimer").Start();
GetNode<Timer>("ScoreTimer").Start();
}

public void OnScoreTimerTimeout()
private void OnScoreTimerTimeout()
{
score++;
_score++;

GetNode<HUD>("HUD").UpdateScore(score);
GetNode<HUD>("HUD").UpdateScore(_score);
}

public void OnMobTimerTimeout()
// We also specified this function name in PascalCase in the editor's connection window.
private void OnMobTimerTimeout()
{
// Note: Normally it is best to use explicit types rather than the `var`
// keyword. However, var is acceptable to use here because the types are
// obviously PathFollow2D and Mob, since they appear later on the line.
// obviously Mob and PathFollow2D, since they appear later on the line.

if (MobScene is null)
{
GD.PrintErr("Mob scene is not set. You need to set it in the inspector.");
return;
}
// Create a new instance of the Mob scene.
Mob mob = MobScene.Instantiate<Mob>();

// Choose a random location on Path2D.
var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
mobSpawnLocation.Offset = GD.Randi();

// Create a Mob instance and add it to the scene.
var mob = (Mob)mobScene.Instantiate();
AddChild(mob);
mobSpawnLocation.ProgressRatio = GD.Randf();

// Set the mob's direction perpendicular to the path direction.
float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
Expand All @@ -86,5 +86,8 @@ public void OnMobTimerTimeout()
// Choose the velocity for the mob.
var velocity = new Vector2((float)GD.RandRange(150.0, 250.0), 0);
mob.LinearVelocity = velocity.Rotated(direction);

// Spawn the mob by adding it to the Main scene.
AddChild(mob);
}
}
34 changes: 18 additions & 16 deletions mono/dodge_the_creeps/Main.tscn
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
[gd_scene load_steps=8 format=2]
[gd_scene load_steps=8 format=3 uid="uid://oq3nlri51jl3"]

[ext_resource path="res://Main.cs" type="Script" id=1]
[ext_resource path="res://Mob.tscn" type="PackedScene" id=2]
[ext_resource path="res://Player.tscn" type="PackedScene" id=3]
[ext_resource path="res://HUD.tscn" type="PackedScene" id=4]
[ext_resource path="res://art/House In a Forest Loop.ogg" type="AudioStream" id=5]
[ext_resource path="res://art/gameover.wav" type="AudioStream" id=6]
[ext_resource type="Script" path="res://Main.cs" id="1_t4q5g"]
[ext_resource type="PackedScene" uid="uid://ixe1g4hv46xs" path="res://Mob.tscn" id="2_06wge"]
[ext_resource type="PackedScene" uid="uid://u1nbrhmt1vqu" path="res://Player.tscn" id="3_ouh2a"]
[ext_resource type="PackedScene" uid="uid://b0ljm4vbkww0f" path="res://HUD.tscn" id="4_f8bkj"]
[ext_resource type="AudioStream" uid="uid://cwm86fnmnh0sh" path="res://art/House In a Forest Loop.ogg" id="5_dnvwy"]
[ext_resource type="AudioStream" uid="uid://i4rhnwpnljso" path="res://art/gameover.wav" id="5_r2snl"]

[sub_resource type="Curve2D" id=1]
[sub_resource type="Curve2D" id="1"]
_data = {
"points": PackedVector2Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 480, 0, 0, 0, 0, 0, 480, 720, 0, 0, 0, 0, 0, 720, 0, 0, 0, 0, 0, 0)
}
point_count = 5

[node name="Main" type="Node"]
script = ExtResource( 1 )
mobScene = ExtResource( 2 )
script = ExtResource("1_t4q5g")
MobScene = ExtResource("2_06wge")

[node name="ColorRect" type="ColorRect" parent="."]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
color = Color(0.219608, 0.372549, 0.380392, 1)

[node name="Player" parent="." instance=ExtResource( 3 )]
[node name="Player" parent="." instance=ExtResource("3_ouh2a")]

[node name="MobTimer" type="Timer" parent="."]
wait_time = 0.5
Expand All @@ -32,21 +34,21 @@ wait_time = 0.5
wait_time = 2.0
one_shot = true

[node name="StartPosition" type="Position2D" parent="."]
[node name="StartPosition" type="Marker2D" parent="."]
position = Vector2(240, 450)

[node name="MobPath" type="Path2D" parent="."]
curve = SubResource( 1 )
curve = SubResource("1")

[node name="MobSpawnLocation" type="PathFollow2D" parent="MobPath"]

[node name="HUD" parent="." instance=ExtResource( 4 )]
[node name="HUD" parent="." instance=ExtResource("4_f8bkj")]

[node name="Music" type="AudioStreamPlayer" parent="."]
stream = ExtResource( 5 )
stream = ExtResource("5_dnvwy")

[node name="DeathSound" type="AudioStreamPlayer" parent="."]
stream = ExtResource( 6 )
stream = ExtResource("5_r2snl")

[connection signal="Hit" from="Player" to="." method="GameOver"]
[connection signal="timeout" from="MobTimer" to="." method="OnMobTimerTimeout"]
Expand Down
11 changes: 5 additions & 6 deletions mono/dodge_the_creeps/Mob.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using Godot;

public partial class Mob : RigidDynamicBody2D
public partial class Mob : RigidBody2D
{
public override void _Ready()
{
var animSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
animSprite.Playing = true;
string[] mobTypes = animSprite.Frames.GetAnimationNames();
animSprite.Animation = mobTypes[GD.Randi() % mobTypes.Length];
var animatedSprite = GetNode<AnimatedSprite2D>("AnimatedSprite2D");
string[] mobTypes = animatedSprite.SpriteFrames.GetAnimationNames();
animatedSprite.Play(mobTypes[GD.Randi() % mobTypes.Length]);
}

public void OnVisibilityScreenExited()
public void OnVisibleOnScreenNotifier2DScreenExited()
{
QueueFree();
}
Expand Down
Loading

0 comments on commit fbef18f

Please sign in to comment.