Merge pull request #7651 from raulsntos/dotnet/fix-instancing-with-signals

This commit is contained in:
Max Hilbrunner
2023-07-22 17:23:14 +02:00
committed by Max Hilbrunner
parent 4c8c5489a0
commit 1681f2d1d5

View File

@@ -41,7 +41,7 @@ given velocity:
public partial class Bullet : Area2D
{
Vector2 Velocity = new Vector2();
public Vector2 Velocity { get; set; } = Vector2.Zero;
public override void _PhysicsProcess(double delta)
{
@@ -111,7 +111,7 @@ Here is the code for the player using signals to emit the bullet:
public partial class Player : Sprite2D
{
[Signal]
delegate void ShootEventHandler(PackedScene bullet, Vector2 direction, Vector2 location);
public delegate void ShootEventHandler(PackedScene bullet, float direction, Vector2 location);
private PackedScene _bullet = GD.Load<PackedScene>("res://bullet.tscn");
@@ -147,13 +147,13 @@ In the main scene, we then connect the player's signal (it will appear in the
.. code-tab:: csharp
public void OnPlayerShoot(PackedScene bullet, Vector2 direction, Vector2 location)
private void OnPlayerShoot(PackedScene bullet, float direction, Vector2 location)
{
var bulletInstance = (Bullet)bullet.Instantiate();
AddChild(bulletInstance);
bulletInstance.Rotation = direction;
bulletInstance.Position = location;
bulletInstance.Velocity = bulletInstance.Velocity.Rotated(direction);
var spawnedBullet = bullet.Instantiate<Bullet>();
AddChild(spawnedBullet);
spawnedBullet.Rotation = direction;
spawnedBullet.Position = location;
spawnedBullet.Velocity = spawnedBullet.Velocity.Rotated(direction);
}
Now the bullets will maintain their own movement independent of the player's