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

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using Cirno.Scripts.AttackPatterns;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Resources.ScriptableBullets;
using Cirno.Scripts.UI;
@ -7,7 +8,7 @@ using Godot.Collections;
namespace Cirno.Scripts.Actors;
public partial class Boss : Enemy, IActivable
public partial class Boss : Enemy, IActivable, IScriptHost
{
[Export] public string BossName { get; private set; }
//[Export] private Array<BossPhase> Phases;

View file

@ -0,0 +1,64 @@
using System;
using Cirno.Scripts.AttackPatterns;
using Cirno.Scripts.Resources;
using Godot;
namespace Cirno.Scripts.Actors;
public partial class ScriptableBulletsEmitter : Area2D, IActivable, IScriptHost
{
[Export]
public BulletScript Script { get; private set; }
private bool _isActive = false;
public override void _Ready()
{
_homePosition = this.GlobalPosition;
}
public bool Activate(ActivationType activationType = ActivationType.Toggle)
{
switch (activationType)
{
case ActivationType.Use:
case ActivationType.Toggle:
_isActive = !_isActive;
break;
case ActivationType.Close:
case ActivationType.Enable:
_isActive = true;
break;
case ActivationType.Open:
case ActivationType.Disable:
_isActive = false;
break;
case ActivationType.Destroy:
_isActive = false;
break;
}
if (_isActive)
{
Script.Start(this);
}
return true;
}
public override void _Process(double delta)
{
if (!_isActive) return;
Script.UpdatePhase(delta);
}
private Vector2 _homePosition;
public Vector2 HomePosition => _homePosition;
public void ChangeSpriteDirection(Vector2 direction)
{
}
}

View file

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

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

View file

@ -6,9 +6,9 @@ namespace Cirno.Scripts.Resources;
[GlobalClass]
public abstract partial class AttackPattern : Resource
{
public Boss Boss;
public Node2D Parent;
[Export] public bool WaitForCompletion = true;
public abstract void Start(Boss boss);
public abstract void Start(Node2D parent);
public abstract void UpdatePattern(double delta);
public abstract bool IsComplete();
}

View file

@ -31,7 +31,7 @@ public partial class BossPhase : Resource
if (!currentPattern.WaitForCompletion || currentPattern.IsComplete())
{
currentPatternIndex = (currentPatternIndex + 1) % Patterns.Count;
Patterns[currentPatternIndex].Start(currentPattern.Boss);
Patterns[currentPatternIndex].Start(currentPattern.Parent);
}
}
}

View file

@ -0,0 +1,41 @@
using System.Linq;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Resources;
[GlobalClass]
public partial class BulletScript : Resource
{
[Export]
public Array<AttackPattern> Patterns { get; private set; }
private Node2D _parent;
private int _currentPatternIndex = 0;
private double _patternTimer;
private AttackPattern CurrentPattern => Patterns[_currentPatternIndex];
public void Start(Node2D parent)
{
_parent = parent;
if (Patterns.Count == 0) return;
_currentPatternIndex = 0;
CurrentPattern.Start(parent);
}
public void UpdatePhase(double delta)
{
_patternTimer += delta;
CurrentPattern.UpdatePattern(delta);
if (!CurrentPattern.WaitForCompletion || CurrentPattern.IsComplete())
{
_currentPatternIndex = (_currentPatternIndex + 1) % Patterns.Count;
CurrentPattern.Start(_parent);
}
}
}

View file

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

View file

@ -11,11 +11,11 @@ public partial class PatternGroup : AttackPattern
[Export] private Array<AttackPattern> patterns;
private int currentPatternIndex = 0;
public override void Start(Boss boss)
public override void Start(Node2D parent)
{
Boss = boss;
Parent = parent;
currentPatternIndex = 0;
patterns[currentPatternIndex].Start(boss);
patterns[currentPatternIndex].Start(parent);
}
public override void UpdatePattern(double delta)
@ -29,7 +29,7 @@ public partial class PatternGroup : AttackPattern
currentPatternIndex++;
if (currentPatternIndex < patterns.Count)
{
patterns[currentPatternIndex].Start(Boss);
patterns[currentPatternIndex].Start(Parent);
}
}
}

View file

@ -1,4 +1,5 @@
using Cirno.Scripts.Actors;
using Cirno.Scripts.AttackPatterns;
using Godot;
namespace Cirno.Scripts.Resources;
@ -14,16 +15,22 @@ public partial class SimpleMovementPattern : AttackPattern
private Tween tween;
private bool isComplete = false;
public override void Start(Boss boss)
protected IScriptHost Boss;
public override void Start(Node2D parent)
{
Parent = parent;
if (parent is not IScriptHost boss)
return;
Boss = boss;
tween = boss.CreateTween();
tween = Parent.CreateTween();
isComplete = false;
Vector2 targetPosition = (Boss?.HomePosition ?? boss.GlobalPosition) + relativeTargetPosition;
Vector2 targetPosition = (Boss?.HomePosition ?? Parent.GlobalPosition) + relativeTargetPosition;
boss.ChangeSpriteDirection(-(boss.GlobalPosition - targetPosition));
tween.TweenProperty(Boss, "global_position", targetPosition, moveDuration)
boss.ChangeSpriteDirection(-(Parent.GlobalPosition - targetPosition));
tween.TweenProperty(Parent, "global_position", targetPosition, moveDuration)
.SetTrans(transitionType)
.SetEase(easeType)
.Finished += () =>