diff --git a/MB_FYP/scenes/ai_fighter.tscn b/MB_FYP/scenes/ai_fighter.tscn index e0c9e16..13496fe 100644 --- a/MB_FYP/scenes/ai_fighter.tscn +++ b/MB_FYP/scenes/ai_fighter.tscn @@ -1,16 +1,20 @@ -[gd_scene load_steps=4 format=3 uid="uid://3e6fmds2x8q5"] +[gd_scene load_steps=6 format=3 uid="uid://3e6fmds2x8q5"] [ext_resource type="Texture2D" uid="uid://soden53qtfxf" path="res://assets/Ships/Fighters/Enemy/enemyFighter.png" id="1_4l75b"] [ext_resource type="Script" path="res://script/ai_fighter.cs" id="1_kyds1"] +[ext_resource type="FontFile" uid="uid://ryhimaxr7tr4" path="res://assets/Fonts/Kenney Mini Square.ttf" id="3_8bw2f"] [sub_resource type="CircleShape2D" id="CircleShape2D_6vq6f"] radius = 41.0488 +[sub_resource type="LabelSettings" id="LabelSettings_lim1q"] +font = ExtResource("3_8bw2f") +font_size = 40 + [node name="AI_Fighter" type="CharacterBody2D"] +collision_mask = 13 motion_mode = 1 script = ExtResource("1_kyds1") -type = 1 -faction = 2 [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource("CircleShape2D_6vq6f") @@ -22,3 +26,24 @@ texture = ExtResource("1_4l75b") position = Vector2(0, -58) [node name="EffectSpawn" type="Node2D" parent="."] + +[node name="HealthDisplay" type="Node2D" parent="."] +position = Vector2(-30, 90) +scale = Vector2(1.2, 1.2) + +[node name="HealthLabel" type="Label" parent="HealthDisplay"] +offset_right = 65.0 +offset_bottom = 50.0 +text = "100" +label_settings = SubResource("LabelSettings_lim1q") +horizontal_alignment = 1 + +[node name="HPLabel" type="Label" parent="HealthDisplay"] +offset_left = 62.0 +offset_top = -2.0 +offset_right = 127.0 +offset_bottom = 48.0 +scale = Vector2(0.5, 0.5) +text = "HP" +label_settings = SubResource("LabelSettings_lim1q") +horizontal_alignment = 1 diff --git a/MB_FYP/scenes/game.tscn b/MB_FYP/scenes/game.tscn index 635d8f0..9c4ec97 100644 --- a/MB_FYP/scenes/game.tscn +++ b/MB_FYP/scenes/game.tscn @@ -22,11 +22,9 @@ position = Vector2(1228, 195) [node name="Asteroid2" parent="Asteroids" instance=ExtResource("3_b8wlr")] position = Vector2(332, 661) -size = 1 [node name="Asteroid3" parent="Asteroids" instance=ExtResource("3_b8wlr")] position = Vector2(1450, 641) -size = 2 [node name="Asteroid4" parent="Asteroids" instance=ExtResource("3_b8wlr")] position = Vector2(769, 202) @@ -41,20 +39,20 @@ position = Vector2(387, 230) [node name="Player" parent="Ships/Friendly" instance=ExtResource("1_1w06w")] position = Vector2(959, 539) scale = Vector2(0.6, 0.6) -collision_layer = 8 -type = 2 [node name="AI_Fighter" parent="Ships/Friendly" instance=ExtResource("5_nkk10")] -position = Vector2(1151, 378) +position = Vector2(1229, 430) scale = Vector2(0.6, 0.6) -faction = 1 +type = 0 +Faction = 1 [node name="Enemy" type="Node" parent="Ships"] [node name="AI_Fighter" parent="Ships/Enemy" instance=ExtResource("5_nkk10")] -position = Vector2(992, 205) +position = Vector2(952, 214) scale = Vector2(0.6, 0.6) type = 0 +Faction = 2 [connection signal="Exploded" from="Asteroids/Asteroid" to="." method="OnAsteroidExploded"] [connection signal="Exploded" from="Asteroids/Asteroid2" to="." method="OnAsteroidExploded"] diff --git a/MB_FYP/scenes/player.tscn b/MB_FYP/scenes/player.tscn index e3e5762..4573051 100644 --- a/MB_FYP/scenes/player.tscn +++ b/MB_FYP/scenes/player.tscn @@ -8,10 +8,9 @@ radius = 41.0488 [node name="Player" type="CharacterBody2D"] -collision_mask = 5 +collision_mask = 13 motion_mode = 1 script = ExtResource("1_lhmq0") -color = 1 [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource("CircleShape2D_btpq3") diff --git a/MB_FYP/scenes/ship.tscn b/MB_FYP/scenes/ship.tscn index 566acec..5d84334 100644 --- a/MB_FYP/scenes/ship.tscn +++ b/MB_FYP/scenes/ship.tscn @@ -7,6 +7,7 @@ radius = 41.0488 [node name="Ship" type="CharacterBody2D"] +collision_mask = 13 motion_mode = 1 script = ExtResource("1_u8ww1") diff --git a/MB_FYP/script/Laser.cs b/MB_FYP/script/Laser.cs index 1f5b895..8e8b0e4 100644 --- a/MB_FYP/script/Laser.cs +++ b/MB_FYP/script/Laser.cs @@ -6,6 +6,8 @@ public partial class Laser : Area2D [Export] public int Speed { get; set;} = 2000; + public ship Shooter; + public Vector2 MovementVector { get; set; } = new Vector2(0, -1); @@ -13,6 +15,11 @@ public partial class Laser : Area2D { QueueFree(); } + + public override void _Ready() + { + Connect("body_entered", new Callable(this, nameof(OnBodyEntered))); + } public override void _PhysicsProcess(double delta) { GlobalPosition += MovementVector.Rotated(Rotation) * Speed * (float)delta; @@ -26,4 +33,37 @@ public partial class Laser : Area2D QueueFree(); } } + + private bool ValidTargetFaction(ship body) + { + return Shooter.Faction switch + { + ship.ShipFaction.PLAYER => body.Faction is ship.ShipFaction.ENEMY or ship.ShipFaction.ACE, + ship.ShipFaction.ENEMY => body.Faction is ship.ShipFaction.FRIENDLY or ship.ShipFaction.PLAYER, + ship.ShipFaction.FRIENDLY => body.Faction is ship.ShipFaction.ENEMY or ship.ShipFaction.ACE, + ship.ShipFaction.ACE => body.Faction is ship.ShipFaction.FRIENDLY or ship.ShipFaction.PLAYER, + _ => throw new ArgumentOutOfRangeException() // thank you rider very cool + }; + } + + private void OnBodyEntered(Node body) + { + GD.Print("Laser hit something: ", body.Name); + + if (body is ship target) + { + if (Shooter != null && !ValidTargetFaction(target)) + { + GD.Print("friendly fire"); + QueueFree(); + return; // prevent friendly fire + } + else + { + GD.Print("Hit type: ", body.GetType()); + target.ShipDamage(Shooter.Damage); + QueueFree(); + } + } + } } diff --git a/MB_FYP/script/ai_fighter.cs b/MB_FYP/script/ai_fighter.cs index ce39194..734d96b 100644 --- a/MB_FYP/script/ai_fighter.cs +++ b/MB_FYP/script/ai_fighter.cs @@ -11,6 +11,9 @@ public partial class ai_fighter : ship [Export] public float EngageDistance = 300f; + private Label HealthLabel; + private Label HPLabel; + //retreat logic private float previousDistance = 0f; private float stuckTime = 0f; @@ -24,10 +27,19 @@ public partial class ai_fighter : ship SetShipStats(); SetupVisual(); Sprite.Texture = GD.Load(spritePath); + + HealthLabel = GetNode