diff --git a/Scenes/fragola.cs b/Scenes/fragola.cs index f5bd6420..c5f5f55c 100644 --- a/Scenes/fragola.cs +++ b/Scenes/fragola.cs @@ -7,9 +7,14 @@ public partial class fragola : RigidBody2D [Signal] public delegate void HitEventHandler(); + private AnimatedSprite2D _animatedSprite; + + private bool _isAlive = true; + // Called when the node enters the scene tree for the first time. public override void _Ready() { + _animatedSprite = GetNode("./AnimatedSprite2D"); } // Called every frame. 'delta' is the elapsed time since the previous frame. @@ -20,15 +25,34 @@ public partial class fragola : RigidBody2D private void _on_body_entered(Node body) { Debug.WriteLine("Collision"); - Hide(); + EmitSignal(SignalName.Hit); - // Must be deferred as we can't change physics properties on a physics callback. - GetNode("CollisionShape2D").SetDeferred(CollisionShape2D.PropertyName.Disabled, true); + } private void _on_area_2d_area_entered(Area2D area) { // Replace with function body. Debug.WriteLine("Collision area"); EmitSignal(SignalName.Hit); + + if (_isAlive) + { + Explode(); + } + } + + private void Explode() + { + _isAlive = false; + _animatedSprite.Play("explode"); + + //Hide(); + DisableCollision(); + } + + private void DisableCollision() + { + // Must be deferred as we can't change physics properties on a physics callback. + GetNode("CollisionShape2D").SetDeferred(CollisionShape2D.PropertyName.Disabled, true); } } diff --git a/Scenes/player.tscn b/Scenes/player.tscn index c8d76e2b..46d825e8 100644 --- a/Scenes/player.tscn +++ b/Scenes/player.tscn @@ -138,7 +138,6 @@ collision_layer = 2 collision_mask = 3 script = ExtResource("1_m27vu") Speed = 2000 -CrosshairDistance = 20.0 BulletScene = ExtResource("2_ov36d") Muzzle = NodePath("Muzzle") metadata/_edit_group_ = true diff --git a/Scenes/test.tscn b/Scenes/test.tscn index 544b0db9..de4f821a 100644 --- a/Scenes/test.tscn +++ b/Scenes/test.tscn @@ -6160,6 +6160,7 @@ script = ExtResource("6_t8ide") [node name="Player" parent="." instance=ExtResource("2_8mh54")] position = Vector2(7, 1) +CrosshairDistance = 10.0 [node name="ReferenceRect" type="ReferenceRect" parent="."] visible = false diff --git a/Scripts/Bullet.cs b/Scripts/Bullet.cs index 8e16eb9c..5ae51210 100644 --- a/Scripts/Bullet.cs +++ b/Scripts/Bullet.cs @@ -9,12 +9,24 @@ public partial class Bullet : Area2D private Vector2 _direction = Vector2.Right; + //public delegate void BulletHitEventHandler(Node Body); + //public event BulletHitEventHandler BulletHit; + // Called when the node enters the scene tree for the first time. public override void _Ready() { - + //this.Connect("body_entered", new Callable(this, nameof(OnBodyEntered))); } + //private void OnBodyEntered(Node body) + //{ + // When a body is entered, invoke the event and pass the collided body + // BulletHit?.Invoke(body); + + // Then remove the bullet + // QueueFree(); + //} + public void SetDirection(Vector2 direction) { var normalized = direction.Normalized(); diff --git a/Scripts/PlayerMovement.cs b/Scripts/PlayerMovement.cs index a3ea4976..dfa9d4cb 100644 --- a/Scripts/PlayerMovement.cs +++ b/Scripts/PlayerMovement.cs @@ -55,7 +55,7 @@ public partial class PlayerMovement : CharacterBody2D { if (Input.IsActionJustPressed("shoot")) { - Debug.WriteLine("Shoot"); + //Debug.WriteLine("Shoot"); Bullet bullet = BulletScene.Instantiate(); Owner.AddChild(bullet); bullet.Transform = Muzzle.GlobalTransform;