player collision with asteroids, health displayed on hud

This commit is contained in:
rsxri 2024-08-15 18:12:48 +01:00
parent 3de5c44ebc
commit c942179ebd
10 changed files with 113 additions and 19 deletions

View file

@ -15,3 +15,5 @@ texture = ExtResource("2_rmklb")
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2(1, -1) position = Vector2(1, -1)
shape = ExtResource("3_pxcia") shape = ExtResource("3_pxcia")
[connection signal="body_entered" from="." to="." method="OnBodyEntered"]

View file

@ -16,7 +16,8 @@ script = ExtResource("1_dukjm")
position = Vector2(800, 450) position = Vector2(800, 450)
scale = Vector2(0.6, 0.6) scale = Vector2(0.6, 0.6)
collision_layer = 8 collision_layer = 8
type = 2 color = 1
type = 1
[node name="Lasers" type="Node" parent="."] [node name="Lasers" type="Node" parent="."]
@ -39,6 +40,7 @@ position = Vector2(769, 202)
[node name="Asteroid5" parent="Asteroids" instance=ExtResource("3_b8wlr")] [node name="Asteroid5" parent="Asteroids" instance=ExtResource("3_b8wlr")]
position = Vector2(387, 230) position = Vector2(387, 230)
[connection signal="HealthUpdate" from="Player" to="." method="OnPlayerHealthUpdate"]
[connection signal="LaserShot" from="Player" to="." method="OnPlayerLaserShot"] [connection signal="LaserShot" from="Player" to="." method="OnPlayerLaserShot"]
[connection signal="Exploded" from="Asteroids/Asteroid" to="." method="OnAsteroidExploded"] [connection signal="Exploded" from="Asteroids/Asteroid" to="." method="OnAsteroidExploded"]
[connection signal="Exploded" from="Asteroids/Asteroid2" to="." method="OnAsteroidExploded"] [connection signal="Exploded" from="Asteroids/Asteroid2" to="." method="OnAsteroidExploded"]

7
MB_FYP/scenes/hud.tres Normal file
View file

@ -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

View file

@ -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="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"] [ext_resource type="LabelSettings" uid="uid://lnjklxelhfuv" path="res://scenes/hud.tres" id="2_ns78v"]
[sub_resource type="LabelSettings" id="LabelSettings_588nb"]
font = ExtResource("2_2d36h")
font_size = 48
[node name="HUD" type="Control"] [node name="HUD" type="Control"]
layout_mode = 3 layout_mode = 3
@ -21,4 +17,13 @@ offset_top = 15.0
offset_right = 363.0 offset_right = 363.0
offset_bottom = 90.0 offset_bottom = 90.0
text = "SCORE: 9999" 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")

View file

@ -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="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"] [ext_resource type="Script" path="res://script/camera.cs" id="3_ui7sm"]
[sub_resource type="CircleShape2D" id="CircleShape2D_btpq3"] [sub_resource type="CircleShape2D" id="CircleShape2D_btpq3"]
radius = 41.0488 radius = 41.0488
[node name="Player" type="CharacterBody2D"] [node name="Player" type="CharacterBody2D"]
collision_mask = 5
motion_mode = 1 motion_mode = 1
script = ExtResource("1_lhmq0") script = ExtResource("1_lhmq0")
@ -14,6 +16,7 @@ script = ExtResource("1_lhmq0")
shape = SubResource("CircleShape2D_btpq3") shape = SubResource("CircleShape2D_btpq3")
[node name="ShipSprite" type="Sprite2D" parent="."] [node name="ShipSprite" type="Sprite2D" parent="."]
texture = ExtResource("2_3806w")
[node name="LaserSpawn" type="Node2D" parent="."] [node name="LaserSpawn" type="Node2D" parent="."]
position = Vector2(0, -58) position = Vector2(0, -58)

View file

@ -4,7 +4,7 @@ using System;
public partial class Laser : Area2D public partial class Laser : Area2D
{ {
[Export] [Export]
public int Speed { get; set;} = 500; public int Speed { get; set;} = 1000;
public Vector2 MovementVector { get; set; } = new Vector2(0, -1); public Vector2 MovementVector { get; set; } = new Vector2(0, -1);

View file

@ -73,6 +73,15 @@ public partial class asteroid : Area2D
QueueFree(); 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) public override void _PhysicsProcess(double delta)
{ {

View file

@ -11,6 +11,8 @@ public partial class game : Node2D
public Label ScoreLabel = null; public Label ScoreLabel = null;
public Label HealthLabel = null;
public hud h; public hud h;
@ -22,7 +24,7 @@ public partial class game : Node2D
{ {
HUD = GetNode<Control>("UI/HUD"); HUD = GetNode<Control>("UI/HUD");
ScoreLabel = GetNode<Label>("UI/HUD/Score"); ScoreLabel = GetNode<Label>("UI/HUD/Score");
//HealthLabel = GetNode<Label>("UI/HUD/Health");
Asteroids = GetNode<Node>("Asteroids"); Asteroids = GetNode<Node>("Asteroids");
var a = new asteroid(); var a = new asteroid();
@ -33,9 +35,8 @@ public partial class game : Node2D
Lasers = GetNode<Node>("Lasers"); Lasers = GetNode<Node>("Lasers");
Player = GetNode<CharacterBody2D>("Player"); Player = GetNode<CharacterBody2D>("Player");
var p = new player(); //var p = new player();
p.LaserShot += OnPlayerLaserShot; //p.LaserShot += OnPlayerLaserShot;
} }
public override void _Process(double delta) public override void _Process(double delta)
@ -66,6 +67,15 @@ public partial class game : Node2D
ScoreLabel.Text = "SCORE: " + score.ToString(); ScoreLabel.Text = "SCORE: " + score.ToString();
} }
public void UpdateHealthLabel(int health)
{
if (HealthLabel == null)
{
HealthLabel = GetNode<Label>("UI/HUD/Health");
}
HealthLabel.Text = "HEALTH: " + health.ToString();
}
//Signals and Connections //Signals and Connections
public void OnPlayerLaserShot(Area2D Laser) public void OnPlayerLaserShot(Area2D Laser)
{ {
@ -74,6 +84,11 @@ public partial class game : Node2D
GD.Print(Player.Position); GD.Print(Player.Position);
} }
public void OnPlayerHealthUpdate(int health)
{
UpdateHealthLabel(health);
}
public void OnAsteroidExploded(Vector2 pos, int size) public void OnAsteroidExploded(Vector2 pos, int size)
{ {
//score hardcoded for now, will make implementation of enemy scores easier later on //score hardcoded for now, will make implementation of enemy scores easier later on

View file

@ -5,6 +5,9 @@ public partial class player : ship // Inherits from base ship class
{ {
public enum ShipColor{RED, GREEN, BLUE} public enum ShipColor{RED, GREEN, BLUE}
[Signal]
public delegate void HealthUpdateEventHandler(int health);
[Export] [Export]
public ShipColor color; public ShipColor color;
@ -23,14 +26,20 @@ public partial class player : ship // Inherits from base ship class
//move into selection statement for toggling between fa off and on //move into selection statement for toggling between fa off and on
if(Input.GetAxis("strafe_left", "strafe_right") == 0){ if(Input.GetAxis("strafe_left", "strafe_right") == 0){
Velocity = Velocity.MoveToward(Vector2.Zero, 1.5f); Velocity = Velocity.MoveToward(Vector2.Zero, 2.5f);
} }
else if(Input.GetAxis("down", "up") == 0){ else if(Input.GetAxis("down", "up") == 0){
Velocity = Velocity.MoveToward(Vector2.Zero, 1.5f); Velocity = Velocity.MoveToward(Vector2.Zero, 2.5f);
} }
} }
public override void _Ready() public override void ShipDamage(int damage)
{
base.ShipDamage(damage);
EmitSignal(SignalName.HealthUpdate, Health);
}
public override void _Ready()
{ {
Sprite = GetNode<Sprite2D>("ShipSprite"); Sprite = GetNode<Sprite2D>("ShipSprite");
string spritePath = ""; string spritePath = "";
@ -70,6 +79,10 @@ public partial class player : ship // Inherits from base ship class
LaserSpawn = GetNode<Node2D>("LaserSpawn"); LaserSpawn = GetNode<Node2D>("LaserSpawn");
//Connect("body_entered" +=)
SetShipStats();
Health = MaxHealth;
EmitSignal(SignalName.HealthUpdate, Health);
} }
public override void _Process(double delta) public override void _Process(double delta)
@ -78,6 +91,12 @@ public partial class player : ship // Inherits from base ship class
{ {
ShootLaser(); ShootLaser();
} }
if (Health < 0)
{
GD.Print("dead lol");
}
} }
public override void _PhysicsProcess(double delta) public override void _PhysicsProcess(double delta)
{ // every frame { // every frame

View file

@ -1,3 +1,4 @@
using System.Collections;
using Godot; using Godot;
public partial class ship : CharacterBody2D public partial class ship : CharacterBody2D
@ -10,9 +11,13 @@ public partial class ship : CharacterBody2D
//[Export] //[Export]
//public Vector2 ScreenSize; //public Vector2 ScreenSize;
[Export] [Export]
public int Health { get; set; } = 100;
[Export]
public int MaxHealth { get; set; } = 100;
[Export]
public int MaxSpeed { get; set;} = 300; public int MaxSpeed { get; set;} = 300;
[Export] [Export]
public int MainSpeed { get; set; } = 10; public int MainSpeed { get; set; } = 20;
[Export] [Export]
public int StrafeSpeed { get; set; } = 5; public int StrafeSpeed { get; set; } = 5;
[Export] [Export]
@ -34,6 +39,33 @@ public partial class ship : CharacterBody2D
EmitSignal(SignalName.LaserShot, Laser); EmitSignal(SignalName.LaserShot, Laser);
} }
public void SetShipStats()
{
switch (type)
{
case ShipType.INTERCEPTOR:
MaxSpeed = 450;
MainSpeed = 35;
StrafeSpeed = 10;
RotationSpeed = 4f;
MaxHealth = 75;
break;
case ShipType.GUARDIAN:
MaxSpeed = 200;
MainSpeed = 10;
StrafeSpeed = 3;
RotationSpeed = 1f;
MaxHealth = 250;
break;
}
}
public virtual void ShipDamage(int damage)
{
Health -= damage;
}
public override void _Ready() public override void _Ready()
{ {
//ScreenSize = GetViewportRect().Size; //ScreenSize = GetViewportRect().Size;