Scriptable bullet emitter

This commit is contained in:
MaddoScientisto 2025-03-15 11:44:30 +01:00
commit 4261009c33
17 changed files with 251 additions and 74 deletions

View file

@ -0,0 +1,9 @@
using Godot;
namespace Cirno.Scripts.AttackPatterns;
public interface IScriptHost
{
public Vector2 HomePosition { get; }
public void ChangeSpriteDirection(Vector2 direction);
}

View file

@ -16,23 +16,28 @@ public partial class MovementPattern : AttackPattern
private Tween tween;
private bool isComplete = false;
public override void Start(Boss boss)
protected IScriptHost Boss;
public override void Start(Node2D parent)
{
if (parent is not IScriptHost boss)
return;
Boss = boss;
tween = boss.CreateTween();
tween = parent.CreateTween();
isComplete = false;
Vector2 targetPosition = (Boss?.HomePosition ?? boss.GlobalPosition) + this.relativeTargetPosition;
Vector2 targetPosition = (Boss?.HomePosition ?? parent.GlobalPosition) + this.relativeTargetPosition;
tween.TweenProperty(Boss, "position", targetPosition, moveDuration)
tween.TweenProperty(Parent, "position", targetPosition, moveDuration)
.SetTrans(transitionType)
.SetEase(easeType)
.Finished += () => isComplete = true;
if (shootingPattern != null && !WaitForCompletion)
{
shootingPattern.Start(boss);
shootingPattern.Start(Parent);
}
}

View file

@ -19,12 +19,12 @@ public partial class PatternTest : AttackPattern
private double burstTimer;
private BulletSpawner spawner;
public override void Start(Boss boss)
public override void Start(Node2D parent)
{
Boss = boss;
Parent = parent;
timer = 0;
burstTimer = 0;
spawner = boss.GetNode<BulletSpawner>("BulletSpawner");
spawner = Parent.GetNode<BulletSpawner>("BulletSpawner");
}
public override void UpdatePattern(double delta)
@ -33,7 +33,7 @@ public partial class PatternTest : AttackPattern
burstTimer += delta;
if (timer < duration && burstTimer >= burstInterval)
{
spawner.SpawnBullet(Boss.GlobalPosition, Vector2.Right, bulletSpeed, owner, bulletCount, bulletScene: BulletScene);
spawner.SpawnBullet(Parent.GlobalPosition, Vector2.Right, bulletSpeed, owner, bulletCount, bulletScene: BulletScene);
burstTimer = 0;
}
}

View file

@ -13,19 +13,19 @@ namespace Cirno.Scripts.AttackPatterns;
public partial class SpiralPattern : AttackPattern
{
[Export] public BulletResource BulletResource { get; set; }
[Export] public PackedScene BulletScene;
[Export] private float _bulletLifeTime = 20f; // Switch to res
[Export] private bool _destroyOnCollision = false; // Switch to res
[Export] private float bulletSpeed = 5f; // Switch to res
//Export] public PackedScene BulletScene;
//[Export] private float _bulletLifeTime = 20f; // Switch to res
//[Export] private bool _destroyOnCollision = false; // Switch to res
//[Export] private float bulletSpeed = 5f; // Switch to res
[Export] private int bulletCount = 16;
[Export] private float rotationSpeed = 0f;
[Export] private float _rotationOffset = 0f;
[Export] private float duration = 5f;
[Export] private float burstInterval = 0.5f;
[Export] private float spread = 360f;
[Export] private BulletOwner owner = BulletOwner.Enemy; // Switch to res
[Export] private DamageType _damageType = DamageType.Neutral; // Switch to res
[Export] private float _bulletDamage = 1f; // Switch to res
//[Export] private BulletOwner owner = BulletOwner.Enemy; // Switch to res
//[Export] private DamageType _damageType = DamageType.Neutral; // Switch to res
//[Export] private float _bulletDamage = 1f; // Switch to res
[ExportGroup("Modifiers")]
[Export] private BulletCreationModifier _modifier;
[ExportGroup("Modifiers")]
@ -37,12 +37,12 @@ public partial class SpiralPattern : AttackPattern
private double burstTimer;
private BulletSpawner spawner;
public override void Start(Boss boss)
public override void Start(Node2D parent)
{
Boss = boss;
Parent = parent;
timer = 0;
burstTimer = burstInterval; // start immediately
spawner = boss.GetNode<BulletSpawner>("BulletSpawner");
spawner = parent.GetNode<BulletSpawner>("BulletSpawner");
}
public override void UpdatePattern(double delta)
@ -55,12 +55,12 @@ public partial class SpiralPattern : AttackPattern
Vector2 direction = BulletResource.Direction;
if (_targetPlayer && Boss.GameManager.PlayerPosition.HasValue)
if (_targetPlayer && GameManager.Instance.PlayerPosition.HasValue)
{
direction = (Boss.GameManager.PlayerPosition.Value - Boss.GlobalPosition).Normalized();
direction = (GameManager.Instance.PlayerPosition.Value - Parent.GlobalPosition).Normalized();
}
var bullet = BulletResource.MakeBullet(Boss.GlobalPosition, bulletCount, spread, angleOffset);
var bullet = BulletResource.MakeBullet(Parent.GlobalPosition, bulletCount, spread, angleOffset);
bullet.Direction = direction;
//spawner.SpawnBullet(MakeBullet(Boss.GlobalPosition, direction, angleOffset));
@ -94,25 +94,27 @@ public partial class SpiralPattern : AttackPattern
{
var bl = BulletResource.MakeBullet(position, bulletCount, angleOffset);
bl.Direction = direction;
return bl;
return new BulletInfo()
{
Position = position,
Direction = direction,
LifeTime = _bulletLifeTime,
DestroyOnCollision = _destroyOnCollision,
Speed = bulletSpeed,
Owner = owner,
DamageType = _damageType,
Damage = _bulletDamage,
BulletCount = bulletCount,
Spread = spread,
BulletScene = BulletScene,
RotationOffset = angleOffset,
Modifier = _modifier,
TimeModifiers = _timeModifiers.ToList()// _timeModifiers.Select(x => x.MakeClone()).ToList()
// TimeModifiers = _timeModifiers?.Where(mod => mod != null).ToList() ?? new List<TimeModifier>()
};
// return new BulletInfo()
// {
// Position = position,
// Direction = direction,
// LifeTime = _bulletLifeTime,
// DestroyOnCollision = _destroyOnCollision,
// Speed = bulletSpeed,
// Owner = owner,
// DamageType = _damageType,
// Damage = _bulletDamage,
// BulletCount = bulletCount,
// Spread = spread,
// BulletScene = BulletScene,
// RotationOffset = angleOffset,
// Modifier = _modifier,
// TimeModifiers = _timeModifiers.ToList()// _timeModifiers.Select(x => x.MakeClone()).ToList()
// // TimeModifiers = _timeModifiers?.Where(mod => mod != null).ToList() ?? new List<TimeModifier>()
// };
}
public override bool IsComplete()

View file

@ -23,16 +23,14 @@ public partial class TargetedPattern : AttackPattern
private double burstTimer;
private BulletSpawner spawner;
//private Node2D player;
private GameManager _gameManager;
public override void Start(Boss boss)
public override void Start(Node2D parent)
{
_gameManager = boss.GetNode<GameManager>("/root/GameScene");
Boss = boss;
Parent = parent;
timer = 0;
burstTimer = 0;
spawner = boss.GetNode<BulletSpawner>("BulletSpawner");
spawner = Parent.GetNode<BulletSpawner>("BulletSpawner");
}
@ -40,9 +38,9 @@ public partial class TargetedPattern : AttackPattern
{
timer += delta;
burstTimer += delta;
if (timer < duration && burstTimer >= burstInterval && _gameManager.PlayerPosition.HasValue)
if (timer < duration && burstTimer >= burstInterval && GameManager.Instance.PlayerPosition.HasValue)
{
spawner.SpawnTargetedBullet(Boss.GlobalPosition, _gameManager.PlayerPosition.Value, bulletSpeed, owner, BulletScene, bulletsPerShot, spread, modifier);
spawner.SpawnTargetedBullet(Parent.GlobalPosition, GameManager.Instance.PlayerPosition.Value, bulletSpeed, owner, BulletScene, bulletsPerShot, spread, modifier);
burstTimer = 0;
}
}