mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 10:35:34 +00:00
Destroyable props
This commit is contained in:
parent
93469062a1
commit
44ebc70448
52 changed files with 1387 additions and 18428 deletions
150
Scripts/Actors/Destructible3D.cs
Normal file
150
Scripts/Actors/Destructible3D.cs
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
using System.Linq;
|
||||
using Cirno.Scripts.Controllers;
|
||||
using Cirno.Scripts.Resources;
|
||||
using Cirno.Scripts.Utils;
|
||||
using Cirno.Scripts.Weapons;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Cirno.Scripts.Actors;
|
||||
|
||||
public partial class Destructible3D : StaticBody3D, IDestructible
|
||||
{
|
||||
[Export] public bool Indestructible { get; private set; }
|
||||
[Export] public float Health = 1f;
|
||||
|
||||
[Export] public PackedScene DebrisScene { get; set; }
|
||||
|
||||
[Export] public PackedScene ExplosionParticles { get; set; }
|
||||
[Export] public BulletResource ExplosionData { get; set; }
|
||||
|
||||
[Export] public BulletOwner BulletGroup { get; set; } = BulletOwner.None;
|
||||
[Export] public Array<DamageResistance> DamageResistances { get; set; } = [];
|
||||
|
||||
[ExportCategory("On Death Activation")]
|
||||
[Export]
|
||||
public ActivationType ActivationType { get; private set; } = ActivationType.Toggle;
|
||||
[Export]
|
||||
public Node Target { get; private set;}
|
||||
|
||||
[Signal]
|
||||
public delegate void ExplodedEventHandler();
|
||||
|
||||
private float _currentHealth = 0f;
|
||||
|
||||
private bool _isDestroyed = false;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_currentHealth = Health;
|
||||
}
|
||||
|
||||
private void Explode()
|
||||
{
|
||||
//ApplyExplosionDamage();
|
||||
EmitSignal(SignalName.Exploded, this, _currentHealth);
|
||||
ActivateOnDeath();
|
||||
CreateExplosion();
|
||||
CreateParticles();
|
||||
CreateDebris();
|
||||
|
||||
QueueFree();
|
||||
|
||||
//GameManager.Instance.RebakeNavigation();
|
||||
//GameManager.Instance.RecalculateTilemap(this.GlobalPosition);
|
||||
}
|
||||
|
||||
private void ActivateOnDeath()
|
||||
{
|
||||
if (Target is IActivable node)
|
||||
{
|
||||
node.Activate(ActivationType);
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateExplosion()
|
||||
{
|
||||
if (ExplosionData == null) return;
|
||||
|
||||
|
||||
var explosion = PoolingManager.Instance.SpawnBullet<Bullet3D>(ExplosionData);
|
||||
explosion.GlobalPosition = this.GlobalPosition;
|
||||
|
||||
|
||||
explosion.Speed = 0;
|
||||
|
||||
explosion.Initialize(ExplosionData.MakeBullet(new Vector2(this.GlobalPosition.X, this.GlobalPosition.Y)));
|
||||
}
|
||||
|
||||
// private void ApplyExplosionDamage()
|
||||
// {
|
||||
// var spaceState = GetWorld2D().DirectSpaceState;
|
||||
// var query = new PhysicsShapeQueryParameters2D();
|
||||
// var shape = new CircleShape2D { Radius = ExplosionRadius };
|
||||
//
|
||||
// query.SetShape(shape);
|
||||
// query.Transform = new Transform2D(0, GlobalPosition);
|
||||
// query.CollideWithBodies = false;
|
||||
// query.CollideWithAreas = true;
|
||||
// //query.CollisionMask = ;
|
||||
//
|
||||
// var results = spaceState.IntersectShape(query);
|
||||
//
|
||||
// GD.Print($"Shapes in range: {results.Count}");
|
||||
//
|
||||
// foreach (var result in results)
|
||||
// {
|
||||
// if (result.TryGetValue("collider", out var colliderObj))
|
||||
// {
|
||||
//
|
||||
// var collider = colliderObj.As<Node2D>();
|
||||
//
|
||||
//
|
||||
// if (collider.GetParent<Area2D>() is IDestructible destructible)
|
||||
// {
|
||||
// GD.Print($"HITTING {collider.Name}");
|
||||
// destructible.Hit(ExplosionDamage);
|
||||
// }
|
||||
// else {
|
||||
// GD.Print($"Collider {collider.Name} was not idestructible");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
private void CreateDebris()
|
||||
{
|
||||
if (DebrisScene == null) return;
|
||||
this.CreateSibling<Destructible3D>(DebrisScene);
|
||||
}
|
||||
|
||||
private void CreateParticles()
|
||||
{
|
||||
if (ExplosionParticles == null) {
|
||||
return;
|
||||
}
|
||||
var particle = this.CreateSibling<GpuParticles3D>(ExplosionParticles);
|
||||
if (particle == null) return;
|
||||
|
||||
particle.Emitting = true;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void Hit(float damage, DamageType damageType = DamageType.Neutral)
|
||||
{
|
||||
if (_isDestroyed || Indestructible) return;
|
||||
|
||||
var dmg = DamageResistances.Aggregate(damage, (current, resistance) => current * resistance.CalculateDamage(current, damageType));
|
||||
|
||||
_currentHealth -= dmg;
|
||||
if (_currentHealth > 0) return;
|
||||
_isDestroyed = true;
|
||||
Explode();
|
||||
}
|
||||
|
||||
public bool IsDestroyed()
|
||||
{
|
||||
return _isDestroyed;
|
||||
}
|
||||
}
|
||||
1
Scripts/Actors/Destructible3D.cs.uid
Normal file
1
Scripts/Actors/Destructible3D.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://ccxnvbthsvka3
|
||||
Loading…
Add table
Add a link
Reference in a new issue