Player can die now

This commit is contained in:
MaddoScientisto 2024-08-18 17:38:32 +02:00
commit 360b4dfe6a
6 changed files with 87 additions and 24 deletions

View file

@ -59,11 +59,12 @@ public partial class Bullet : Area2D
//Debug.WriteLine("Collision");
QueueFree();
}
else if (body.IsInGroup("Destroyable"))
{
Debug.WriteLine("Collision with destroyable object body");
QueueFree();
}
//// Do not Collide with body for purpose of destroying bullets
// else if (body.IsInGroup("Destroyable"))
// {
// Debug.WriteLine("Collision with destroyable object body");
// QueueFree();
// }
}

View file

@ -15,6 +15,8 @@ public partial class Enemy : Area2D, IDestructible
[Export] public double RateOfFire = 0.4f;
[Export] public float BulletSpeed = 100f;
private float _currentHealth = 0f;
private bool _isDestroyed = false;
@ -71,6 +73,7 @@ public partial class Enemy : Area2D, IDestructible
// bullet.Transform = this.GlobalTransform;
// bullet.Position = this.Position;
bullet.SetDirection((_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized());
bullet.Speed = BulletSpeed;
_cooldownTimer.Start(RateOfFire);
}
}
@ -117,6 +120,14 @@ public partial class Enemy : Area2D, IDestructible
}
private void Explode()
{
Debug.WriteLine("Ded");
//CreateParticles();
//CreateDebris();
QueueFree();
}
public void Hit(float damage)
{
if (_isDestroyed) return;
@ -127,14 +138,6 @@ public partial class Enemy : Area2D, IDestructible
Explode();
}
private void Explode()
{
Debug.WriteLine("Ded");
//CreateParticles();
//CreateDebris();
QueueFree();
}
public bool IsDestroyed()
{
return _isDestroyed;

View file

@ -3,7 +3,7 @@ using System;
using System.Diagnostics;
using Cirno.Scripts;
public partial class PlayerMovement : CharacterBody2D
public partial class PlayerMovement : CharacterBody2D, IDestructible
{
[Export]
public int Speed { get; set; } = 400;
@ -32,10 +32,25 @@ public partial class PlayerMovement : CharacterBody2D
private Sprite2D _crosshair;
private Timer _cooldownTimer;
[Export] public float Health = 4f;
private float _currentHealth = 0f;
[Export] public double RateOfFire = 0.4f;
[Export] public float BulletSpeed = 300f;
private bool _isDestroyed = false;
public override void _Ready()
{
_currentHealth = Health;
_animatedSprite = GetNode<AnimatedSprite2D>("./Smoothing2D/AnimatedSprite2D");
_crosshair = GetNode<Sprite2D>("./Smoothing2D/Crosshair");
_cooldownTimer = GetNode<Timer>("./ShootTimer");
_movementDirection = Vector2.Zero;
_facingDirection = Vector2.Zero;
@ -171,4 +186,37 @@ public partial class PlayerMovement : CharacterBody2D
}
}
}
private void Explode()
{
Debug.WriteLine("Ded");
//CreateParticles();
//CreateDebris();
QueueFree();
}
public void Hit(float damage)
{
GD.Print($"Player damaged for {damage}");
if (_isDestroyed) return;
_currentHealth -= damage;
if (!(_currentHealth <= 0)) return;
_isDestroyed = true;
Explode();
}
public bool IsDestroyed()
{
return _isDestroyed;
}
private void _on_damage_hit_box_area_entered(Area2D area) {
if (area is Bullet bullet) {
GD.Print("Received damage manually");
this.Hit(bullet.Damage);
bullet.QueueFree();
}
}
}