diff --git a/Scenes/bullet.tscn b/Scenes/bullet.tscn index c030b3a6..0fec1b02 100644 --- a/Scenes/bullet.tscn +++ b/Scenes/bullet.tscn @@ -6,7 +6,7 @@ [sub_resource type="CircleShape2D" id="CircleShape2D_jxptd"] radius = 2.23607 -[node name="Bullet" type="Area2D"] +[node name="Bullet" type="Area2D" groups=["bullets"]] script = ExtResource("1_jvxw3") Speed = 200.0 metadata/_edit_group_ = true diff --git a/Scenes/fragola.cs b/Scenes/fragola.cs new file mode 100644 index 00000000..f5bd6420 --- /dev/null +++ b/Scenes/fragola.cs @@ -0,0 +1,34 @@ +using Godot; +using System; +using System.Diagnostics; + +public partial class fragola : RigidBody2D +{ + [Signal] + public delegate void HitEventHandler(); + + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + } + + // Called every frame. 'delta' is the elapsed time since the previous frame. + public override void _Process(double delta) + { + } + + 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); + } +} diff --git a/Scenes/fragola.tscn b/Scenes/fragola.tscn index 63e3646d..f83a9e07 100644 --- a/Scenes/fragola.tscn +++ b/Scenes/fragola.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=10 format=3 uid="uid://rp4jhx0tuh24"] +[gd_scene load_steps=12 format=3 uid="uid://rp4jhx0tuh24"] [ext_resource type="Texture2D" uid="uid://cy5usvdeabi0t" path="res://Sprites/fragola.png" id="1_5wy11"] +[ext_resource type="Script" path="res://Scenes/fragola.cs" id="1_773fi"] [sub_resource type="AtlasTexture" id="AtlasTexture_j6kkb"] atlas = ExtResource("1_5wy11") @@ -63,11 +64,15 @@ animations = [{ [sub_resource type="CircleShape2D" id="CircleShape2D_bbnjh"] radius = 7.0 -[node name="Fragola" type="RigidBody2D"] +[sub_resource type="CircleShape2D" id="CircleShape2D_svktn"] +radius = 7.0 + +[node name="Fragola" type="RigidBody2D" groups=["enemies"]] collision_layer = 4 collision_mask = 7 mass = 0.03 gravity_scale = 0.0 +script = ExtResource("1_773fi") metadata/_edit_group_ = true [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] @@ -78,3 +83,11 @@ animation = &"idle" shape = SubResource("CircleShape2D_bbnjh") [node name="Fragola" type="Node2D" parent="."] + +[node name="Area2D" type="Area2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] +shape = SubResource("CircleShape2D_svktn") + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] +[connection signal="area_entered" from="Area2D" to="." method="_on_area_2d_area_entered"] diff --git a/Scenes/player.tscn b/Scenes/player.tscn index 706bd6d0..c8d76e2b 100644 --- a/Scenes/player.tscn +++ b/Scenes/player.tscn @@ -171,4 +171,4 @@ y_sort_enabled = true sprite_frames = SubResource("SpriteFrames_q0rt3") animation = &"walk_left" -[node name="Crosshair" parent="." instance=ExtResource("6_l43rf")] +[node name="Crosshair" parent="Smoothing2D" instance=ExtResource("6_l43rf")] diff --git a/Scripts/Bullet.cs b/Scripts/Bullet.cs index 9cc2fa5e..8e16eb9c 100644 --- a/Scripts/Bullet.cs +++ b/Scripts/Bullet.cs @@ -21,7 +21,7 @@ public partial class Bullet : Area2D _direction = normalized; - Debug.WriteLine($"Bullet Shot at direction {direction.X} {direction.Y}"); + //Debug.WriteLine($"Bullet Shot at direction {direction.X} {direction.Y}"); } @@ -33,7 +33,7 @@ public partial class Bullet : Area2D private void _on_visible_on_screen_notifier_2d_screen_exited() { - Debug.WriteLine("Destroy bullet out of screen"); + //Debug.WriteLine("Destroy bullet out of screen"); QueueFree(); } diff --git a/Scripts/PlayerMovement.cs b/Scripts/PlayerMovement.cs index 5bb4726b..a3ea4976 100644 --- a/Scripts/PlayerMovement.cs +++ b/Scripts/PlayerMovement.cs @@ -27,7 +27,7 @@ public partial class PlayerMovement : CharacterBody2D public override void _Ready() { _animatedSprite = GetNode("./Smoothing2D/AnimatedSprite2D"); - _crosshair = GetNode("./Crosshair"); + _crosshair = GetNode("./Smoothing2D/Crosshair"); _movementDirection = Vector2.Zero; _facingDirection = Vector2.Zero;