mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-04 15:00:09 +01:00
fix Dodge the Creeps
This commit is contained in:
@@ -1,26 +1,5 @@
|
||||
<Project Sdk="Godot.NET.Sdk/3.2.3">
|
||||
<Project Sdk="Godot.NET.Sdk/3.2.3">
|
||||
<PropertyGroup>
|
||||
<ProjectGuid>{75CB0382-CCCC-4A4D-ABF0-C6CD04D9F832}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<RootNamespace>DodgetheCreepswithC</RootNamespace>
|
||||
<AssemblyName>Dodge the Creeps with C#</AssemblyName>
|
||||
<GodotProjectGeneratorVersion>1.0.7374.16792</GodotProjectGeneratorVersion>
|
||||
<TargetFramework>net472</TargetFramework>
|
||||
<!--The following properties were overriden during migration to prevent errors.
|
||||
Enabling them may require other manual changes to the project and its files.-->
|
||||
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<Deterministic>false</Deterministic>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="HUD.cs" />
|
||||
<Compile Include="Main.cs" />
|
||||
<Compile Include="Mob.cs" />
|
||||
<Compile Include="Player.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
</Project>
|
||||
@@ -1,5 +1,4 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public class HUD : CanvasLayer
|
||||
{
|
||||
@@ -15,7 +14,7 @@ public class HUD : CanvasLayer
|
||||
GetNode<Timer>("MessageTimer").Start();
|
||||
}
|
||||
|
||||
async public void ShowGameOver()
|
||||
public async void ShowGameOver()
|
||||
{
|
||||
ShowMessage("Game Over");
|
||||
|
||||
@@ -37,7 +36,7 @@ public class HUD : CanvasLayer
|
||||
public void OnStartButtonPressed()
|
||||
{
|
||||
GetNode<Button>("StartButton").Hide();
|
||||
EmitSignal("StartGame");
|
||||
EmitSignal(nameof(StartGame));
|
||||
}
|
||||
|
||||
public void OnMessageTimerTimeout()
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public 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 mob;
|
||||
private PackedScene _mobScene;
|
||||
#pragma warning restore 649
|
||||
|
||||
private int _score;
|
||||
|
||||
// We use 'System.Random' as an alternative to GDScript's random methods.
|
||||
private Random _random = new Random();
|
||||
|
||||
// We'll use this later because C# doesn't support GDScript's randi().
|
||||
private float RandRange(float min, float max)
|
||||
public override void _Ready()
|
||||
{
|
||||
return (float)_random.NextDouble() * (max - min) + min;
|
||||
GD.Randomize();
|
||||
}
|
||||
|
||||
public void GameOver()
|
||||
@@ -30,6 +28,9 @@ public class Main : Node
|
||||
|
||||
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;
|
||||
|
||||
var player = GetNode<Player>("Player");
|
||||
@@ -60,16 +61,16 @@ public class Main : Node
|
||||
|
||||
public 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 RigidBody2D, since they appear later on the line.
|
||||
// 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.
|
||||
|
||||
// Choose a random location on Path2D.
|
||||
var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
|
||||
mobSpawnLocation.SetOffset(_random.Next());
|
||||
mobSpawnLocation.Offset = GD.Randi();
|
||||
|
||||
// Create a Mob instance and add it to the scene.
|
||||
var mobInstance = (RigidBody2D)mob.Instance();
|
||||
var mobInstance = (Mob)_mobScene.Instance();
|
||||
AddChild(mobInstance);
|
||||
|
||||
// Set the mob's direction perpendicular to the path direction.
|
||||
@@ -79,12 +80,10 @@ public class Main : Node
|
||||
mobInstance.Position = mobSpawnLocation.Position;
|
||||
|
||||
// Add some randomness to the direction.
|
||||
direction += RandRange(-Mathf.Tau / 8, Mathf.Tau / 8);
|
||||
direction += (float)GD.RandRange(-Mathf.Tau / 8, Mathf.Tau / 8);
|
||||
mobInstance.Rotation = direction;
|
||||
|
||||
// Choose the velocity.
|
||||
mobInstance.SetLinearVelocity(new Vector2(RandRange(150f, 250f), 0).Rotated(direction));
|
||||
|
||||
GetNode("HUD").Connect("StartGame", mobInstance, "OnStartGame");
|
||||
mobInstance.LinearVelocity = new Vector2((float)GD.RandRange(mobInstance.minSpeed, mobInstance.maxSpeed), 0).Rotated(direction);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ _data = {
|
||||
|
||||
[node name="Main" type="Node"]
|
||||
script = ExtResource( 1 )
|
||||
mob = ExtResource( 2 )
|
||||
_mobScene = ExtResource( 2 )
|
||||
|
||||
[node name="ColorRect" type="ColorRect" parent="."]
|
||||
anchor_right = 1.0
|
||||
|
||||
@@ -4,19 +4,17 @@ using System;
|
||||
public class Mob : RigidBody2D
|
||||
{
|
||||
[Export]
|
||||
public int minSpeed; // Minimum speed range.
|
||||
public int minSpeed;
|
||||
|
||||
[Export]
|
||||
public int maxSpeed; // Maximum speed range.
|
||||
|
||||
private String[] _mobTypes = { "walk", "swim", "fly" };
|
||||
|
||||
// C# doesn't have GDScript's random methods, so we use System.Random insetad.
|
||||
private static Random _random = new Random();
|
||||
public int maxSpeed;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
GetNode<AnimatedSprite>("AnimatedSprite").Animation = _mobTypes[_random.Next(0, _mobTypes.Length)];
|
||||
var animSprite = GetNode<AnimatedSprite>("AnimatedSprite");
|
||||
animSprite.Playing = true;
|
||||
string[] mobTypes = animSprite.Frames.GetAnimationNames();
|
||||
animSprite.Animation = mobTypes[GD.Randi() % mobTypes.Length];
|
||||
}
|
||||
|
||||
public void OnVisibilityScreenExited()
|
||||
|
||||
@@ -30,7 +30,9 @@ animations = [ {
|
||||
radius = 35.8898
|
||||
height = 29.3103
|
||||
|
||||
[node name="Mob" type="RigidBody2D"]
|
||||
[node name="Mob" type="RigidBody2D" groups=[
|
||||
"mobs",
|
||||
]]
|
||||
collision_mask = 0
|
||||
gravity_scale = 0.0
|
||||
script = ExtResource( 1 )
|
||||
@@ -44,8 +46,6 @@ maxSpeed = 250
|
||||
scale = Vector2( 0.75, 0.75 )
|
||||
frames = SubResource( 1 )
|
||||
animation = "walk"
|
||||
frame = 1
|
||||
playing = true
|
||||
|
||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
|
||||
rotation = 1.5708
|
||||
|
||||
@@ -5,14 +5,17 @@ public class Player : Area2D
|
||||
[Signal]
|
||||
public delegate void Hit();
|
||||
|
||||
// These only need to be accessed in this script, so we can make them private.
|
||||
// Private variables in C# in Godot have their name starting with an
|
||||
// underscore and also have the "private" keyword instead of "public".
|
||||
[Export]
|
||||
public int speed; // How fast the player will move (pixels/sec).
|
||||
private int _speed = 400; // How fast the player will move (pixels/sec).
|
||||
|
||||
private Vector2 _screenSize; // Size of the game window.
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_screenSize = GetViewport().GetSize();
|
||||
_screenSize = GetViewportRect().Size;
|
||||
Hide();
|
||||
}
|
||||
|
||||
@@ -26,7 +29,7 @@ public class Player : Area2D
|
||||
|
||||
if (velocity.Length() > 0)
|
||||
{
|
||||
velocity = velocity.Normalized() * speed;
|
||||
velocity = velocity.Normalized() * _speed;
|
||||
animatedSprite.Play();
|
||||
}
|
||||
else
|
||||
@@ -59,14 +62,14 @@ public class Player : Area2D
|
||||
Position = pos;
|
||||
Show();
|
||||
// Must be deferred as we can't change physics properties on a physics callback.
|
||||
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("Disabled", false);
|
||||
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", false);
|
||||
}
|
||||
|
||||
public void OnPlayerBodyEntered(PhysicsBody2D body)
|
||||
{
|
||||
Hide(); // Player disappears after being hit.
|
||||
EmitSignal("Hit");
|
||||
EmitSignal(nameof(Hit));
|
||||
// Must be deferred as we can't change physics properties on a physics callback.
|
||||
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("Disabled", true);
|
||||
GetNode<CollisionShape2D>("CollisionShape2D").SetDeferred("disabled", true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ script = ExtResource( 1 )
|
||||
__meta__ = {
|
||||
"_edit_group_": true
|
||||
}
|
||||
speed = 400
|
||||
_speed = 400
|
||||
|
||||
[node name="AnimatedSprite" type="AnimatedSprite" parent="."]
|
||||
scale = Vector2( 0.5, 0.5 )
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
using System.Reflection;
|
||||
|
||||
// Information about this assembly is defined by the following attributes.
|
||||
// Change them to the values specific to your project.
|
||||
|
||||
[assembly: AssemblyTitle("Dodge the Creeps with C#")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("")]
|
||||
[assembly: AssemblyCopyright("")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
|
||||
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
|
||||
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
|
||||
|
||||
[assembly: AssemblyVersion("1.0.*")]
|
||||
|
||||
// The following attributes are used to specify the signing key for the assembly,
|
||||
// if desired. See the Mono documentation for more information about signing.
|
||||
|
||||
//[assembly: AssemblyDelaySign(false)]
|
||||
//[assembly: AssemblyKeyFile("")]
|
||||
Reference in New Issue
Block a user