From c942179ebd7b05a01b1cc1e29b246c03b0ad3d38 Mon Sep 17 00:00:00 2001 From: rsxri Date: Thu, 15 Aug 2024 18:12:48 +0100 Subject: [PATCH] player collision with asteroids, health displayed on hud --- MB_FYP/scenes/asteroid.tscn | 2 ++ MB_FYP/scenes/game.tscn | 4 +++- MB_FYP/scenes/hud.tres | 7 +++++++ MB_FYP/scenes/hud.tscn | 19 ++++++++++++------- MB_FYP/scenes/player.tscn | 5 ++++- MB_FYP/script/Laser.cs | 2 +- MB_FYP/script/asteroid.cs | 9 +++++++++ MB_FYP/script/game.cs | 23 +++++++++++++++++++---- MB_FYP/script/player.cs | 27 +++++++++++++++++++++++---- MB_FYP/script/ship.cs | 34 +++++++++++++++++++++++++++++++++- 10 files changed, 113 insertions(+), 19 deletions(-) create mode 100644 MB_FYP/scenes/hud.tres diff --git a/MB_FYP/scenes/asteroid.tscn b/MB_FYP/scenes/asteroid.tscn index 0ba5883..5bf4bd1 100644 --- a/MB_FYP/scenes/asteroid.tscn +++ b/MB_FYP/scenes/asteroid.tscn @@ -15,3 +15,5 @@ texture = ExtResource("2_rmklb") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] position = Vector2(1, -1) shape = ExtResource("3_pxcia") + +[connection signal="body_entered" from="." to="." method="OnBodyEntered"] diff --git a/MB_FYP/scenes/game.tscn b/MB_FYP/scenes/game.tscn index 6fc187f..b7252c5 100644 --- a/MB_FYP/scenes/game.tscn +++ b/MB_FYP/scenes/game.tscn @@ -16,7 +16,8 @@ script = ExtResource("1_dukjm") position = Vector2(800, 450) scale = Vector2(0.6, 0.6) collision_layer = 8 -type = 2 +color = 1 +type = 1 [node name="Lasers" type="Node" parent="."] @@ -39,6 +40,7 @@ position = Vector2(769, 202) [node name="Asteroid5" parent="Asteroids" instance=ExtResource("3_b8wlr")] position = Vector2(387, 230) +[connection signal="HealthUpdate" from="Player" to="." method="OnPlayerHealthUpdate"] [connection signal="LaserShot" from="Player" to="." method="OnPlayerLaserShot"] [connection signal="Exploded" from="Asteroids/Asteroid" to="." method="OnAsteroidExploded"] [connection signal="Exploded" from="Asteroids/Asteroid2" to="." method="OnAsteroidExploded"] diff --git a/MB_FYP/scenes/hud.tres b/MB_FYP/scenes/hud.tres new file mode 100644 index 0000000..26cb12d --- /dev/null +++ b/MB_FYP/scenes/hud.tres @@ -0,0 +1,7 @@ +[gd_resource type="LabelSettings" load_steps=2 format=3 uid="uid://lnjklxelhfuv"] + +[ext_resource type="FontFile" uid="uid://be6xxaq8drt5q" path="res://assets/Fonts/Kenney Pixel Square.ttf" id="1_3xllx"] + +[resource] +font = ExtResource("1_3xllx") +font_size = 48 diff --git a/MB_FYP/scenes/hud.tscn b/MB_FYP/scenes/hud.tscn index 48596c8..d6ffd22 100644 --- a/MB_FYP/scenes/hud.tscn +++ b/MB_FYP/scenes/hud.tscn @@ -1,11 +1,7 @@ -[gd_scene load_steps=4 format=3 uid="uid://b6myj160l6vf8"] +[gd_scene load_steps=3 format=3 uid="uid://b6myj160l6vf8"] [ext_resource type="Script" path="res://script/hud.cs" id="1_aksh1"] -[ext_resource type="FontFile" uid="uid://be6xxaq8drt5q" path="res://assets/Fonts/Kenney Pixel Square.ttf" id="2_2d36h"] - -[sub_resource type="LabelSettings" id="LabelSettings_588nb"] -font = ExtResource("2_2d36h") -font_size = 48 +[ext_resource type="LabelSettings" uid="uid://lnjklxelhfuv" path="res://scenes/hud.tres" id="2_ns78v"] [node name="HUD" type="Control"] layout_mode = 3 @@ -21,4 +17,13 @@ offset_top = 15.0 offset_right = 363.0 offset_bottom = 90.0 text = "SCORE: 9999" -label_settings = SubResource("LabelSettings_588nb") +label_settings = ExtResource("2_ns78v") + +[node name="Health" type="Label" parent="."] +layout_mode = 0 +offset_left = 17.0 +offset_top = 95.0 +offset_right = 384.0 +offset_bottom = 167.0 +text = "Health:" +label_settings = ExtResource("2_ns78v") diff --git a/MB_FYP/scenes/player.tscn b/MB_FYP/scenes/player.tscn index 1e03191..9f5f959 100644 --- a/MB_FYP/scenes/player.tscn +++ b/MB_FYP/scenes/player.tscn @@ -1,12 +1,14 @@ -[gd_scene load_steps=4 format=3 uid="uid://ckh362yqjkpi0"] +[gd_scene load_steps=5 format=3 uid="uid://ckh362yqjkpi0"] [ext_resource type="Script" path="res://script/player.cs" id="1_lhmq0"] +[ext_resource type="Texture2D" uid="uid://ofevjaw7ld0a" path="res://assets/Player/Fighter/ShipBlue.png" id="2_3806w"] [ext_resource type="Script" path="res://script/camera.cs" id="3_ui7sm"] [sub_resource type="CircleShape2D" id="CircleShape2D_btpq3"] radius = 41.0488 [node name="Player" type="CharacterBody2D"] +collision_mask = 5 motion_mode = 1 script = ExtResource("1_lhmq0") @@ -14,6 +16,7 @@ script = ExtResource("1_lhmq0") shape = SubResource("CircleShape2D_btpq3") [node name="ShipSprite" type="Sprite2D" parent="."] +texture = ExtResource("2_3806w") [node name="LaserSpawn" type="Node2D" parent="."] position = Vector2(0, -58) diff --git a/MB_FYP/script/Laser.cs b/MB_FYP/script/Laser.cs index b99be99..f592730 100644 --- a/MB_FYP/script/Laser.cs +++ b/MB_FYP/script/Laser.cs @@ -4,7 +4,7 @@ using System; public partial class Laser : Area2D { [Export] - public int Speed { get; set;} = 500; + public int Speed { get; set;} = 1000; public Vector2 MovementVector { get; set; } = new Vector2(0, -1); diff --git a/MB_FYP/script/asteroid.cs b/MB_FYP/script/asteroid.cs index dddd3be..3ae617e 100644 --- a/MB_FYP/script/asteroid.cs +++ b/MB_FYP/script/asteroid.cs @@ -73,6 +73,15 @@ public partial class asteroid : Area2D QueueFree(); } + public void OnBodyEntered(CharacterBody2D body) + { + if (body is player player) + { + player.ShipDamage(30); + GD.Print("player asteroid collide"); + } + } + public override void _PhysicsProcess(double delta) { diff --git a/MB_FYP/script/game.cs b/MB_FYP/script/game.cs index 231aa7f..dbf83de 100644 --- a/MB_FYP/script/game.cs +++ b/MB_FYP/script/game.cs @@ -11,6 +11,8 @@ public partial class game : Node2D public Label ScoreLabel = null; + public Label HealthLabel = null; + public hud h; @@ -22,8 +24,8 @@ public partial class game : Node2D { HUD = GetNode("UI/HUD"); ScoreLabel = GetNode