Destroyable props

This commit is contained in:
Marco 2025-06-19 17:55:23 +02:00
commit 44ebc70448
52 changed files with 1387 additions and 18428 deletions

View 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;
}
}

View file

@ -0,0 +1 @@
uid://ccxnvbthsvka3

View file

@ -51,11 +51,11 @@ public partial class Bullet : Area2D, IBullet
_collisionShape2D = GetNode<CollisionShape2D>("CollisionShape2D");
}
public void Initialize(BulletInfo bulletInfo, GameManager gameManager)
public void Initialize(BulletInfo bulletInfo)
{
_bulletInfo = bulletInfo;
_gameManager = gameManager;
_gameManager = GameManager.Instance;
_elapsedTime = 0f;

View file

@ -43,7 +43,7 @@ public partial class DeathAnimationHandler : ActorModule
var explosion = _actor.CreateSibling<Bullet>(ExplosionData.BulletScene);
explosion.Speed = 0;
explosion.Initialize(ExplosionData.MakeBullet(_actor.GlobalPosition), GameManager.Instance);
explosion.Initialize(ExplosionData.MakeBullet(_actor.GlobalPosition));
}
private void CreateParticles()

View file

@ -34,7 +34,7 @@ public partial class BulletSpawner : Node2D
bulletInfo = bulletInfo.Modifier.ModifyBullet(bulletInfo, i, bulletInfo.BulletCount);
}
bullet.Initialize(bulletInfo, _gameManager);
bullet.Initialize(bulletInfo);
// float modifiedSpeed = bulletInfo.Modifier?.ModifySpeed(bulletInfo.Speed, i, bulletInfo.BulletCount) ?? bulletInfo.Speed;
// bullet.Speed = modifiedSpeed;

View file

@ -238,7 +238,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
//var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, item.WeaponData.BulletData.BulletScene, this.GlobalPosition);
bullet.Initialize(bulletData, _gameManager);
bullet.Initialize(bulletData);
//bullet.SetDirection(ShootDirection);
bullet.SetDirection(_facingDirection);

View file

@ -25,7 +25,7 @@ public partial class SpiderbombEffectResource : ItemEffectResource
var bulletData = item.WeaponData.MakeBullet(parent.Machine.MainObject.GlobalPosition);
bullet.Initialize(bulletData, GameManager.Instance);
bullet.Initialize(bulletData);
bullet.SetDirection(parent.FacingDirection);
bullet.RotateSpriteDegrees(-90);
//bullet.SetDirection(_facingDirection);

View file

@ -0,0 +1,24 @@
using Godot;
namespace Cirno.Scripts.Utils;
[Tool]
public partial class MapProxy3D : Node3D
{
[ExportToolButton("Rebuild")] public Callable RebuildButton => Callable.From(Rebuild);
public void Rebuild()
{
if (!Engine.IsEditorHint()) return;
var children = GetChildren();
foreach (var child in children)
{
if (child.HasMethod("verify_and_build"))
{
child.Call("verify_and_build");
}
}
}
}

View file

@ -0,0 +1 @@
uid://crpgy1o73rtlx

64
Scripts/Utils/Tools3D.cs Normal file
View file

@ -0,0 +1,64 @@
using Godot;
namespace Cirno.Scripts.Utils;
public static class Tools3D
{
public static T CreateChild<T>(this Node3D node, PackedScene prefab) where T : Node3D
{
return CreateChild<T>(node, prefab, node.GlobalPosition);
}
public static T CreateChild<T>(this Node3D node, PackedScene prefab, Vector3 position) where T : Node3D
{
return CreateChildOf<T>(node, node, prefab, position);
// if (prefab == null) return null;
// var newInstance = prefab.Instantiate<T>();
// node.GetParent().CallDeferred("add_child", newInstance);
// // Need to use parent instead of owner because tilemap scenes have no owner
// //node.Owner.CallDeferred("add_child", newInstance);
// newInstance.Transform = node.GlobalTransform;
// newInstance.Position = position;
//
// return newInstance;
}
public static T CreateSibling<T>(this Node3D node, PackedScene prefab) where T : Node3D
{
return CreateChildOf<T>(node, node.GetParent<Node3D>(), prefab, node.GlobalPosition);
}
public static T CreateSibling<T>(this Node3D node, PackedScene prefab, Vector3 position) where T : Node3D
{
return CreateChildOf<T>(node, node.GetParent<Node3D>(), prefab, position);
}
public static T CreateChildOf<T>(this Node3D node, Node3D parentNode, PackedScene prefab) where T : Node3D
{
return CreateChildOf<T>(node, parentNode, prefab, node.GlobalPosition);
}
public static T CreateChildOf<T>(this Node3D node, Node3D parentNode, PackedScene prefab, Vector3 position) where T : Node3D
{
if (prefab == null)
{
GD.PrintErr("Tried to instantiate a null prefab");
return null;
}
if (parentNode == null)
{
GD.PrintErr("Tried to instantiate child of a null parent");
return null;
}
var newInstance = prefab.Instantiate<T>();
//node.GetParent().CallDeferred("add_child", newInstance);
parentNode.CallDeferred("add_child", newInstance);
//newInstance.Transform = node.GlobalTransform;
newInstance.Position = parentNode.ToLocal(position);
return newInstance;
}
}

View file

@ -0,0 +1 @@
uid://frydabuff8ab

View file

@ -189,7 +189,7 @@ public partial class Weapon : Node2D
GetBulletStrengthMultiplier(bulletData.Damage, bulletData.OriginalBulletResource.MaxDamage, 20);
}
bullet.Initialize(bulletData, _gameManager);
bullet.Initialize(bulletData);
//bullet.SetDirection(ShootDirection);
bullet.SetDirection(spreadDirection);

View file

@ -26,8 +26,6 @@ public partial class Bullet3D : Area3D, IBullet
private List<ModifierWrapper> _modifiers = new();
private GameManager _gameManager;
public bool IsGrazed { get; set; } = false;
public bool IsFrozen { get; private set; } = false;
@ -49,11 +47,10 @@ public partial class Bullet3D : Area3D, IBullet
_collisionShape = GetNode<CollisionShape3D>("CollisionShape");
}
public void Initialize(BulletInfo bulletInfo, GameManager gameManager)
public void Initialize(BulletInfo bulletInfo)
{
_bulletInfo = bulletInfo;
_gameManager = gameManager;
_elapsedTime = 0f;
@ -163,12 +160,12 @@ public partial class Bullet3D : Area3D, IBullet
public void FacePlayer()
{
if (_gameManager.Player != null)
{
//_direction = (_gameManager.PlayerPosition.Value - this.GlobalPosition).Normalized();
RotateBullet(0); // quick hack to rotate lasers
//LookAt(player.GlobalPosition);
}
// if (_gameManager.Player != null)
// {
// //_direction = (_gameManager.PlayerPosition.Value - this.GlobalPosition).Normalized();
// RotateBullet(0); // quick hack to rotate lasers
// //LookAt(player.GlobalPosition);
// }
}
@ -236,6 +233,16 @@ public partial class Bullet3D : Area3D, IBullet
private void _on_body_entered(Node3D body)
{
if (body.IsInGroup("Destroyable") && body is IDestructible destructible &&
CanHit(BulletOwner, destructible.BulletGroup))
{
// hit
destructible.Hit(Damage, DamageType);
RequestCollisionDestruction();
return;
}
if (body.IsInGroup("Solid"))
{
//Debug.WriteLine("Collision");

View file

@ -18,7 +18,7 @@ public interface IBullet
public delegate void OnDestroyEventHandler();
public void Initialize(BulletInfo bulletInfo, GameManager gameManager);
public void Initialize(BulletInfo bulletInfo);
public void Enable();
public void Disable(bool hideSprite = true);

View file

@ -182,7 +182,7 @@ public partial class Weapon3D : Node3D
GetBulletStrengthMultiplier(bulletData.Damage, bulletData.OriginalBulletResource.MaxDamage, 20);
}
bullet.Initialize(bulletData, _gameManager);
bullet.Initialize(bulletData);
//bullet.SetDirection(ShootDirection);
bullet.SetDirection(spreadDirection);