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

@ -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();
}
}
}