Bullet resource

This commit is contained in:
Marco 2025-02-12 16:20:55 +01:00
commit 07f6e58ebd
19 changed files with 236 additions and 50 deletions

View file

@ -1,6 +1,7 @@
using Godot;
using System;
using System.Diagnostics;
using Cirno.Scripts;
using Cirno.Scripts.Components;
using Cirno.Scripts.Resources;
@ -111,7 +112,7 @@ public partial class Bullet : Area2D
private void _on_visible_on_screen_notifier_2d_screen_exited()
{
//Debug.WriteLine("Destroy bullet out of screen");
QueueFree();
Destroy();
}
private void _on_body_entered(Node2D body)
@ -119,7 +120,7 @@ public partial class Bullet : Area2D
if (body.IsInGroup("Solid"))
{
//Debug.WriteLine("Collision");
QueueFree();
Destroy();
}
//// Do not Collide with body for purpose of destroying bullets
// else if (body.IsInGroup("Destroyable"))
@ -134,7 +135,7 @@ public partial class Bullet : Area2D
{
if (area.IsInGroup("Solid"))
{
QueueFree();
Destroy();
return;
}
@ -144,9 +145,21 @@ public partial class Bullet : Area2D
destructible.Hit(Damage, DamageType);
QueueFree();
Destroy();
}
}
public void Destroy()
{
if (_bulletInfo?.DestructionParticlesScene != null)
{
var particle = this.CreateSibling<AutodeleteParticle>(_bulletInfo.DestructionParticlesScene);
particle.Init();
}
QueueFree();
}
}
public enum BulletOwner

View file

@ -0,0 +1,27 @@
using Godot;
namespace Cirno.Scripts.Components;
public partial class AutodeleteParticle : GpuParticles2D
{
[Export]
public double LifeTime { get; private set; }
private double _timer = 0;
public void Init()
{
this.Emitting = true;
}
public override void _Process(double delta)
{
_timer += delta;
if (_timer >= LifeTime || this.Emitting == false)
{
QueueFree();
}
}
}

View file

@ -91,6 +91,7 @@ public class BulletInfo
//public double Time { get; set; }
public float Spread { get; set; }
public PackedScene BulletScene { get; set; }
public PackedScene DestructionParticlesScene { get; set; }
public IBulletModifier Modifier { get; set; }
public List<TimeModifier> TimeModifiers { get; set; } = new List<TimeModifier>();
}

View file

@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts.Components;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Resources;
[GlobalClass]
public partial class BulletResource : Resource
{
[Export]
public PackedScene BulletScene { get; set; }
[Export] public PackedScene DestructionParticlesScene { get; set; }
[Export] public float BulletSpeed = 100f;
[Export] public float BulletDamage = 1;
[Export] public float LifeTime = 10f;
[Export] public BulletOwner Owner = BulletOwner.None;
[Export] public DamageType DamageType = DamageType.Neutral;
[Export]
public Resource Modifier;
[Export] public Array<Resource> TimeModifiers;
public BulletInfo MakeBullet(Vector2 position, int count = 1, float spread = 0f, float rotationOffset = 0f)
{
return new BulletInfo()
{
Position = position,
Direction = Vector2.Right,
Speed = BulletSpeed,
Owner = Owner,
DamageType = DamageType,
Damage = BulletDamage,
BulletCount = count,
Spread = spread,
BulletScene = BulletScene,
RotationOffset = rotationOffset,
Modifier = Modifier as IBulletModifier,
LifeTime = LifeTime,
DestructionParticlesScene = DestructionParticlesScene,
TimeModifiers = TimeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().ToList() ??
new List<TimeModifier>()
};
}
}

View file

@ -12,8 +12,12 @@ public partial class WeaponResource : Resource
[Export]
public string Name { get; set; }
[Export]
public PackedScene BulletScene { get; set; }
[Export] public BulletResource BulletData { get; private set; }
//[Export]
//public PackedScene BulletScene { get; set; }
//[Export] public PackedScene DestructionParticlesScene { get; set; }
[Export] public double RateOfFire = 0.4f;
@ -33,35 +37,38 @@ public partial class WeaponResource : Resource
#region Bullet spawn data
[Export] public string AmmoKey;
[Export] public float BulletSpeed = 100f;
[Export] public float BulletDamage = 1;
[Export] public float LifeTime = 10f;
//[Export] public float BulletSpeed = 100f;
//[Export] public float BulletDamage = 1;
//[Export] public float LifeTime = 10f;
[Export] private float _rotationOffset = 0f;
[Export] private BulletOwner owner = BulletOwner.None;
[Export] private DamageType _damageType = DamageType.Neutral;
[Export] private Resource _modifier;
[Export] private Array<Resource> _timeModifiers;
//[Export] private BulletOwner owner = BulletOwner.None;
//[Export] private DamageType _damageType = DamageType.Neutral;
//[Export] private Resource _modifier;
//[Export] private Array<Resource> _timeModifiers;
#endregion
public BulletInfo MakeBullet(Vector2 position)
{
return new BulletInfo()
{
Position = position,
Direction = Vector2.Right,
Speed = BulletSpeed,
Owner = owner,
DamageType = _damageType,
Damage = BulletDamage,
BulletCount = BulletsPerShot,
Spread = SpreadAngle,
BulletScene = BulletScene,
RotationOffset = _rotationOffset,
Modifier = _modifier as IBulletModifier,
LifeTime = LifeTime,
TimeModifiers = _timeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().ToList() ??
new List<TimeModifier>()
};
return BulletData.MakeBullet(position, BulletsPerShot, SpreadAngle, _rotationOffset);
// return new BulletInfo()
// {
// Position = position,
// Direction = Vector2.Right,
// Speed = BulletSpeed,
// Owner = owner,
// DamageType = _damageType,
// Damage = BulletDamage,
// BulletCount = BulletsPerShot,
// Spread = SpreadAngle,
// BulletScene = BulletScene,
// RotationOffset = _rotationOffset,
// Modifier = _modifier as IBulletModifier,
// LifeTime = LifeTime,
// DestructionParticlesScene = DestructionParticlesScene,
// TimeModifiers = _timeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().ToList() ??
// new List<TimeModifier>()
// };
}
}

View file

@ -99,7 +99,7 @@ public partial class Weapon : Node2D
// Rotate the ShootDirection by the spread angle
Vector2 spreadDirection = ShootDirection.Rotated(Mathf.DegToRad(spreadOffset));
var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, WeaponData.BulletScene, _muzzle.GlobalPosition);
var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, WeaponData.BulletData.BulletScene, _muzzle.GlobalPosition);
if (bullet == null)
{
@ -111,7 +111,7 @@ public partial class Weapon : Node2D
//bullet.SetDirection(ShootDirection);
bullet.SetDirection(spreadDirection);
bullet.Speed = WeaponData.BulletSpeed;
bullet.Speed = WeaponData.BulletData.BulletSpeed;
}
LoadedAmmo -= 1;