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

@ -19,7 +19,7 @@ _name = "Intro"
[sub_resource type="Resource" id="Resource_cfhv5"] [sub_resource type="Resource" id="Resource_cfhv5"]
script = ExtResource("1_ov731") script = ExtResource("1_ov731")
EggIndex = 0 EggIndex = 0
StartingEquipment = Array[ExtResource("2_bkci5")]([]) StartingEquipment = []
[sub_resource type="Resource" id="Resource_0k62o"] [sub_resource type="Resource" id="Resource_0k62o"]
script = ExtResource("2_tnajf") script = ExtResource("2_tnajf")
@ -31,7 +31,7 @@ StartData = SubResource("Resource_cfhv5")
[sub_resource type="Resource" id="Resource_tpb7s"] [sub_resource type="Resource" id="Resource_tpb7s"]
script = ExtResource("1_ov731") script = ExtResource("1_ov731")
EggIndex = 0 EggIndex = 0
StartingEquipment = Array[ExtResource("2_bkci5")]([]) StartingEquipment = []
[sub_resource type="Resource" id="Resource_edoov"] [sub_resource type="Resource" id="Resource_edoov"]
script = ExtResource("2_tnajf") script = ExtResource("2_tnajf")
@ -43,7 +43,7 @@ StartData = SubResource("Resource_tpb7s")
[sub_resource type="Resource" id="Resource_1sw5g"] [sub_resource type="Resource" id="Resource_1sw5g"]
script = ExtResource("1_ov731") script = ExtResource("1_ov731")
EggIndex = 255 EggIndex = 255
StartingEquipment = Array[ExtResource("2_bkci5")]([]) StartingEquipment = []
[sub_resource type="Resource" id="Resource_47bot"] [sub_resource type="Resource" id="Resource_47bot"]
script = ExtResource("2_tnajf") script = ExtResource("2_tnajf")
@ -73,7 +73,7 @@ _name = "Rebel Base"
[sub_resource type="Resource" id="Resource_maxpt"] [sub_resource type="Resource" id="Resource_maxpt"]
script = ExtResource("1_ov731") script = ExtResource("1_ov731")
EggIndex = 0 EggIndex = 0
StartingEquipment = Array[ExtResource("2_bkci5")]([]) StartingEquipment = []
[sub_resource type="Resource" id="Resource_pein5"] [sub_resource type="Resource" id="Resource_pein5"]
script = ExtResource("2_tnajf") script = ExtResource("2_tnajf")
@ -128,7 +128,7 @@ metadata/_custom_type_script = "uid://dnthdjrx78u6m"
[sub_resource type="Resource" id="Resource_7sue8"] [sub_resource type="Resource" id="Resource_7sue8"]
script = ExtResource("1_ov731") script = ExtResource("1_ov731")
EggIndex = 255 EggIndex = 255
StartingEquipment = Array[ExtResource("2_bkci5")]([]) StartingEquipment = []
metadata/_custom_type_script = "uid://mja0rk7n2kln" metadata/_custom_type_script = "uid://mja0rk7n2kln"
[sub_resource type="Resource" id="Resource_ognca"] [sub_resource type="Resource" id="Resource_ognca"]
@ -142,7 +142,7 @@ metadata/_custom_type_script = "uid://dnthdjrx78u6m"
[sub_resource type="Resource" id="Resource_olpjo"] [sub_resource type="Resource" id="Resource_olpjo"]
script = ExtResource("1_ov731") script = ExtResource("1_ov731")
EggIndex = 0 EggIndex = 0
StartingEquipment = Array[ExtResource("2_bkci5")]([]) StartingEquipment = []
metadata/_custom_type_script = "uid://mja0rk7n2kln" metadata/_custom_type_script = "uid://mja0rk7n2kln"
[sub_resource type="Resource" id="Resource_nbnej"] [sub_resource type="Resource" id="Resource_nbnej"]

View file

@ -5,7 +5,7 @@
[resource] [resource]
script = ExtResource("2_8ycgt") script = ExtResource("2_8ycgt")
Name = "Enemy Weapon" Name = null
BulletData = ExtResource("1_eps1c") BulletData = ExtResource("1_eps1c")
RateOfFire = 0.3 RateOfFire = 0.3
BulletCapacity = 4 BulletCapacity = 4
@ -15,6 +15,6 @@ InfiniteAmmo = true
BulletsPerShot = 1 BulletsPerShot = 1
SpreadAngle = 0.0 SpreadAngle = 0.0
RandomSpread = 0.0 RandomSpread = 0.0
ItemKey = "EnemyWeapon" ItemKey = null
AmmoKey = "" AmmoKey = null
_rotationOffset = 0.0 _rotationOffset = 0.0

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,5 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Cirno.Scripts.AttackPatterns;
using Cirno.Scripts.Resources; using Cirno.Scripts.Resources;
using Cirno.Scripts.Resources.ScriptableBullets; using Cirno.Scripts.Resources.ScriptableBullets;
using Cirno.Scripts.UI; using Cirno.Scripts.UI;
@ -7,7 +8,7 @@ using Godot.Collections;
namespace Cirno.Scripts.Actors; 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] public string BossName { get; private set; }
//[Export] private Array<BossPhase> Phases; //[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

@ -17,22 +17,27 @@ public partial class MovementPattern : AttackPattern
private Tween tween; private Tween tween;
private bool isComplete = false; 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; Boss = boss;
tween = boss.CreateTween(); tween = parent.CreateTween();
isComplete = false; 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) .SetTrans(transitionType)
.SetEase(easeType) .SetEase(easeType)
.Finished += () => isComplete = true; .Finished += () => isComplete = true;
if (shootingPattern != null && !WaitForCompletion) 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 double burstTimer;
private BulletSpawner spawner; private BulletSpawner spawner;
public override void Start(Boss boss) public override void Start(Node2D parent)
{ {
Boss = boss; Parent = parent;
timer = 0; timer = 0;
burstTimer = 0; burstTimer = 0;
spawner = boss.GetNode<BulletSpawner>("BulletSpawner"); spawner = Parent.GetNode<BulletSpawner>("BulletSpawner");
} }
public override void UpdatePattern(double delta) public override void UpdatePattern(double delta)
@ -33,7 +33,7 @@ public partial class PatternTest : AttackPattern
burstTimer += delta; burstTimer += delta;
if (timer < duration && burstTimer >= burstInterval) 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; burstTimer = 0;
} }
} }

View file

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

View file

@ -23,16 +23,14 @@ public partial class TargetedPattern : AttackPattern
private double burstTimer; private double burstTimer;
private BulletSpawner spawner; private BulletSpawner spawner;
//private Node2D player; //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; timer = 0;
burstTimer = 0; burstTimer = 0;
spawner = boss.GetNode<BulletSpawner>("BulletSpawner"); spawner = Parent.GetNode<BulletSpawner>("BulletSpawner");
} }
@ -40,9 +38,9 @@ public partial class TargetedPattern : AttackPattern
{ {
timer += delta; timer += delta;
burstTimer += 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; burstTimer = 0;
} }
} }

View file

@ -6,9 +6,9 @@ namespace Cirno.Scripts.Resources;
[GlobalClass] [GlobalClass]
public abstract partial class AttackPattern : Resource public abstract partial class AttackPattern : Resource
{ {
public Boss Boss; public Node2D Parent;
[Export] public bool WaitForCompletion = true; [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 void UpdatePattern(double delta);
public abstract bool IsComplete(); public abstract bool IsComplete();
} }

View file

@ -31,7 +31,7 @@ public partial class BossPhase : Resource
if (!currentPattern.WaitForCompletion || currentPattern.IsComplete()) if (!currentPattern.WaitForCompletion || currentPattern.IsComplete())
{ {
currentPatternIndex = (currentPatternIndex + 1) % Patterns.Count; 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; [Export] private Array<AttackPattern> patterns;
private int currentPatternIndex = 0; private int currentPatternIndex = 0;
public override void Start(Boss boss) public override void Start(Node2D parent)
{ {
Boss = boss; Parent = parent;
currentPatternIndex = 0; currentPatternIndex = 0;
patterns[currentPatternIndex].Start(boss); patterns[currentPatternIndex].Start(parent);
} }
public override void UpdatePattern(double delta) public override void UpdatePattern(double delta)
@ -29,7 +29,7 @@ public partial class PatternGroup : AttackPattern
currentPatternIndex++; currentPatternIndex++;
if (currentPatternIndex < patterns.Count) 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.Actors;
using Cirno.Scripts.AttackPatterns;
using Godot; using Godot;
namespace Cirno.Scripts.Resources; namespace Cirno.Scripts.Resources;
@ -14,16 +15,22 @@ public partial class SimpleMovementPattern : AttackPattern
private Tween tween; private Tween tween;
private bool isComplete = false; 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; Boss = boss;
tween = boss.CreateTween(); tween = Parent.CreateTween();
isComplete = false; isComplete = false;
Vector2 targetPosition = (Boss?.HomePosition ?? boss.GlobalPosition) + relativeTargetPosition; Vector2 targetPosition = (Boss?.HomePosition ?? Parent.GlobalPosition) + relativeTargetPosition;
boss.ChangeSpriteDirection(-(boss.GlobalPosition - targetPosition)); boss.ChangeSpriteDirection(-(Parent.GlobalPosition - targetPosition));
tween.TweenProperty(Boss, "global_position", targetPosition, moveDuration) tween.TweenProperty(Parent, "global_position", targetPosition, moveDuration)
.SetTrans(transitionType) .SetTrans(transitionType)
.SetEase(easeType) .SetEase(easeType)
.Finished += () => .Finished += () =>