Skip to content

Commit 7206f2d

Browse files
committed
mono demo for dodge the creeps tutorial
1 parent 01d2f78 commit 7206f2d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1648
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectGuid>{7829C155-1795-447D-A78E-8775E3CF134E}</ProjectGuid>
7+
<OutputType>Library</OutputType>
8+
<OutputPath>.mono\temp\bin\$(Configuration)</OutputPath>
9+
<RootNamespace>DodgeTheCreepsCS</RootNamespace>
10+
<AssemblyName>DodgeTheCreepsCS</AssemblyName>
11+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
12+
<BaseIntermediateOutputPath>.mono\temp\obj</BaseIntermediateOutputPath>
13+
<IntermediateOutputPath>$(BaseIntermediateOutputPath)\$(Configuration)</IntermediateOutputPath>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>portable</DebugType>
18+
<Optimize>false</Optimize>
19+
<DefineConstants>DEBUG;</DefineConstants>
20+
<ErrorReport>prompt</ErrorReport>
21+
<WarningLevel>4</WarningLevel>
22+
<ConsolePause>false</ConsolePause>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>portable</DebugType>
26+
<Optimize>true</Optimize>
27+
<ErrorReport>prompt</ErrorReport>
28+
<WarningLevel>4</WarningLevel>
29+
<ConsolePause>false</ConsolePause>
30+
</PropertyGroup>
31+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Tools|AnyCPU' ">
32+
<DebugSymbols>true</DebugSymbols>
33+
<DebugType>portable</DebugType>
34+
<Optimize>false</Optimize>
35+
<DefineConstants>DEBUG;TOOLS;</DefineConstants>
36+
<ErrorReport>prompt</ErrorReport>
37+
<WarningLevel>4</WarningLevel>
38+
<ConsolePause>false</ConsolePause>
39+
</PropertyGroup>
40+
<ItemGroup>
41+
<Reference Include="GodotSharp">
42+
<HintPath>$(ProjectDir)\.mono\assemblies\GodotSharp.dll</HintPath>
43+
<Private>False</Private>
44+
</Reference>
45+
<Reference Include="GodotSharpEditor" Condition=" '$(Configuration)' == 'Tools' ">
46+
<HintPath>$(ProjectDir)\.mono\assemblies\GodotSharpEditor.dll</HintPath>
47+
<Private>False</Private>
48+
</Reference>
49+
<Reference Include="System" />
50+
</ItemGroup>
51+
<ItemGroup>
52+
<Compile Include="HUD.cs" />
53+
<Compile Include="Main.cs" />
54+
<Compile Include="Mob.cs" />
55+
<Compile Include="Player.cs" />
56+
<Compile Include="Properties\AssemblyInfo.cs" />
57+
</ItemGroup>
58+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
59+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio 2012
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DodgeTheCreepsCS", "DodgeTheCreepsCS.csproj", "{7829C155-1795-447D-A78E-8775E3CF134E}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
Tools|Any CPU = Tools|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{7829C155-1795-447D-A78E-8775E3CF134E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{7829C155-1795-447D-A78E-8775E3CF134E}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
{7829C155-1795-447D-A78E-8775E3CF134E}.Release|Any CPU.ActiveCfg = Release|Any CPU
15+
{7829C155-1795-447D-A78E-8775E3CF134E}.Release|Any CPU.Build.0 = Release|Any CPU
16+
{7829C155-1795-447D-A78E-8775E3CF134E}.Tools|Any CPU.ActiveCfg = Tools|Any CPU
17+
{7829C155-1795-447D-A78E-8775E3CF134E}.Tools|Any CPU.Build.0 = Tools|Any CPU
18+
EndGlobalSection
19+
EndGlobal

mono/DodgeTheCreepsCS/HUD.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Godot;
2+
using System;
3+
4+
public class HUD : CanvasLayer
5+
{
6+
[Signal]
7+
public delegate void StartGame();
8+
9+
public void ShowMessage(string text)
10+
{
11+
var messageLabel = GetNode<Label>("MessageLabel");
12+
messageLabel.Text = text;
13+
messageLabel.Show();
14+
15+
GetNode<Timer>("MessageTimer").Start();
16+
}
17+
18+
async public void ShowGameOver()
19+
{
20+
ShowMessage("Game Over");
21+
22+
var messageTimer = GetNode<Timer>("MessageTimer");
23+
await ToSignal(messageTimer, "timeout");
24+
25+
var messageLabel = GetNode<Label>("MessageLabel");
26+
messageLabel.Text = "Dodge the\nCreeps!";
27+
messageLabel.Show();
28+
29+
GetNode<Button>("StartButton").Show();
30+
}
31+
32+
public void UpdateScore(int score)
33+
{
34+
GetNode<Label>("ScoreLabel").Text = score.ToString();
35+
}
36+
37+
public void OnStartButtonPressed()
38+
{
39+
GetNode<Button>("StartButton").Hide();
40+
EmitSignal("StartGame");
41+
}
42+
43+
public void OnMessageTimerTimeout()
44+
{
45+
GetNode<Label>("MessageLabel").Hide();
46+
}
47+
}

mono/DodgeTheCreepsCS/HUD.tscn

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
[gd_scene load_steps=6 format=2]
2+
3+
[ext_resource path="res://HUD.cs" type="Script" id=1]
4+
[ext_resource path="res://fonts/Xolonium-Regular.ttf" type="DynamicFontData" id=2]
5+
6+
[sub_resource type="DynamicFont" id=1]
7+
8+
size = 64
9+
outline_size = 0
10+
outline_color = Color( 1, 1, 1, 1 )
11+
use_mipmaps = true
12+
use_filter = false
13+
font_data = ExtResource( 2 )
14+
15+
[sub_resource type="DynamicFont" id=2]
16+
17+
size = 64
18+
outline_size = 0
19+
outline_color = Color( 1, 1, 1, 1 )
20+
use_mipmaps = true
21+
use_filter = false
22+
font_data = ExtResource( 2 )
23+
24+
[sub_resource type="DynamicFont" id=3]
25+
26+
size = 64
27+
outline_size = 0
28+
outline_color = Color( 1, 1, 1, 1 )
29+
use_mipmaps = true
30+
use_filter = false
31+
font_data = ExtResource( 2 )
32+
33+
[node name="HUD" type="CanvasLayer"]
34+
layer = 1
35+
offset = Vector2( 0, 0 )
36+
rotation = 0.0
37+
scale = Vector2( 1, 1 )
38+
transform = Transform2D( 1, 0, 0, 1, 0, 0 )
39+
script = ExtResource( 1 )
40+
41+
[node name="ScoreLabel" type="Label" parent="."]
42+
anchor_left = 0.5
43+
anchor_top = 0.0
44+
anchor_right = 0.5
45+
anchor_bottom = 0.0
46+
margin_left = -25.0
47+
margin_right = 25.0
48+
margin_bottom = 100.0
49+
rect_pivot_offset = Vector2( 0, 0 )
50+
rect_clip_content = false
51+
mouse_filter = 2
52+
mouse_default_cursor_shape = 0
53+
size_flags_horizontal = 1
54+
size_flags_vertical = 4
55+
custom_fonts/font = SubResource( 1 )
56+
text = "0
57+
"
58+
align = 1
59+
percent_visible = 1.0
60+
lines_skipped = 0
61+
max_lines_visible = -1
62+
63+
[node name="MessageLabel" type="Label" parent="."]
64+
anchor_left = 0.5
65+
anchor_top = 0.5
66+
anchor_right = 0.5
67+
anchor_bottom = 0.5
68+
margin_left = -200.0
69+
margin_top = -150.0
70+
margin_right = 200.0
71+
rect_pivot_offset = Vector2( 0, 0 )
72+
rect_clip_content = false
73+
mouse_filter = 2
74+
mouse_default_cursor_shape = 0
75+
size_flags_horizontal = 1
76+
size_flags_vertical = 4
77+
custom_fonts/font = SubResource( 2 )
78+
text = "Dodge the
79+
Creeps!"
80+
align = 1
81+
valign = 1
82+
percent_visible = 1.0
83+
lines_skipped = 0
84+
max_lines_visible = -1
85+
86+
[node name="StartButton" type="Button" parent="."]
87+
anchor_left = 0.5
88+
anchor_top = 1.0
89+
anchor_right = 0.5
90+
anchor_bottom = 1.0
91+
margin_left = -100.0
92+
margin_top = -200.0
93+
margin_right = 100.0
94+
margin_bottom = -100.0
95+
rect_pivot_offset = Vector2( 0, 0 )
96+
rect_clip_content = false
97+
focus_mode = 2
98+
mouse_filter = 0
99+
mouse_default_cursor_shape = 0
100+
size_flags_horizontal = 1
101+
size_flags_vertical = 1
102+
custom_fonts/font = SubResource( 3 )
103+
toggle_mode = false
104+
enabled_focus_mode = 2
105+
shortcut = null
106+
group = null
107+
text = "Start"
108+
flat = false
109+
align = 1
110+
111+
[node name="MessageTimer" type="Timer" parent="."]
112+
process_mode = 1
113+
wait_time = 2.0
114+
one_shot = true
115+
autostart = false
116+
117+
[connection signal="pressed" from="StartButton" to="." method="OnStartButtonPressed"]
118+
[connection signal="timeout" from="MessageTimer" to="." method="OnMessageTimerTimeout"]

mono/DodgeTheCreepsCS/Main.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using Godot;
2+
using System;
3+
4+
public class Main : Node
5+
{
6+
[Export]
7+
public PackedScene Mob;
8+
9+
private int _score;
10+
11+
// We use 'System.Random' as an alternative to GDScript's random methods.
12+
private Random _random = new Random();
13+
14+
public override void _Ready()
15+
{
16+
}
17+
18+
// We'll use this later because C# doesn't support GDScript's randi().
19+
private float RandRange(float min, float max)
20+
{
21+
return (float)_random.NextDouble() * (max - min) + min;
22+
}
23+
24+
public void GameOver()
25+
{
26+
GetNode<Timer>("MobTimer").Stop();
27+
GetNode<Timer>("ScoreTimer").Stop();
28+
29+
GetNode<HUD>("HUD").ShowGameOver();
30+
31+
GetNode<AudioStreamPlayer>("Music").Stop();
32+
GetNode<AudioStreamPlayer>("DeathSound").Play();
33+
}
34+
35+
public void NewGame()
36+
{
37+
_score = 0;
38+
39+
var player = GetNode<Player>("Player");
40+
var startPosition = GetNode<Position2D>("StartPosition");
41+
player.Start(startPosition.Position);
42+
43+
GetNode<Timer>("StartTimer").Start();
44+
45+
var hud = GetNode<HUD>("HUD");
46+
hud.UpdateScore(_score);
47+
hud.ShowMessage("Get Ready!");
48+
49+
GetNode<AudioStreamPlayer>("Music").Play();
50+
}
51+
52+
public void OnStartTimerTimeout()
53+
{
54+
GetNode<Timer>("MobTimer").Start();
55+
GetNode<Timer>("ScoreTimer").Start();
56+
}
57+
58+
public void OnScoreTimerTimeout()
59+
{
60+
_score++;
61+
62+
GetNode<HUD>("HUD").UpdateScore(_score);
63+
}
64+
65+
public void OnMobTimerTimeout()
66+
{
67+
// Choose a random location on Path2D.
68+
var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
69+
mobSpawnLocation.SetOffset(_random.Next());
70+
71+
// Create a Mob instance and add it to the scene.
72+
var mobInstance = (RigidBody2D)Mob.Instance();
73+
AddChild(mobInstance);
74+
75+
// Set the mob's direction perpendicular to the path direction.
76+
float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
77+
78+
// Set the mob's position to a random location.
79+
mobInstance.Position = mobSpawnLocation.Position;
80+
81+
// Add some randomness to the direction.
82+
direction += RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
83+
mobInstance.Rotation = direction;
84+
85+
// Choose the velocity.
86+
mobInstance.SetLinearVelocity(new Vector2(RandRange(150f, 250f), 0).Rotated(direction));
87+
}
88+
}

0 commit comments

Comments
 (0)