Danmaku system

This commit is contained in:
Marco 2025-02-05 19:41:49 +01:00
commit fdec052c16
38 changed files with 924 additions and 9 deletions

54
Scripts/Actors/Boss.cs Normal file
View file

@ -0,0 +1,54 @@
using Cirno.Scripts.Resources;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Actors;
public partial class Boss : Enemy
{
[Export] private Array<BossPhase> Phases;
private int currentPhaseIndex = 0;
private GameManager _gameManager;
private Vector2 _homePosition;
public Vector2 HomePosition => _homePosition;
public GameManager GameManager => _gameManager;
private BossPhase CurrentPhase => Phases[currentPhaseIndex];
public override void _Ready()
{
base._Ready();
_gameManager = GetNode<GameManager>("/root/GameScene");
_homePosition = this.GlobalPosition;
StartPhase(CurrentPhase);
}
public override void _Process(double delta)
{
base._Process(delta);
CurrentPhase.UpdatePhase(delta);
if (_currentHealth <= CurrentPhase.Threshold && currentPhaseIndex + 1 < Phases.Count)
{
currentPhaseIndex++;
StartPhase(CurrentPhase);
}
}
private void StartPhase(BossPhase phase)
{
phase.Start(this);
}
public void TakeDamage(int amount)
{
_currentHealth -= amount;
}
}

View file

@ -0,0 +1,54 @@
using Cirno.Scripts.Actors;
using Cirno.Scripts.Resources;
using Godot;
namespace Cirno.Scripts.AttackPatterns;
[GlobalClass]
public partial class MovementPattern : AttackPattern
{
[Export] private Vector2 relativeTargetPosition;
[Export] private float moveDuration = 2f;
[Export] private Tween.TransitionType transitionType = Tween.TransitionType.Linear;
[Export] private Tween.EaseType easeType = Tween.EaseType.InOut;
[Export] private AttackPattern shootingPattern;
private Tween tween;
private bool isComplete = false;
public override void Start(Boss boss)
{
Boss = boss;
tween = boss.CreateTween();
isComplete = false;
Vector2 targetPosition = (Boss?.HomePosition ?? boss.GlobalPosition) + this.relativeTargetPosition;
tween.TweenProperty(Boss, "position", targetPosition, moveDuration)
.SetTrans(transitionType)
.SetEase(easeType)
.Finished += () => isComplete = true;
if (shootingPattern != null && !WaitForCompletion)
{
shootingPattern.Start(boss);
}
}
public override void UpdatePattern(double delta)
{
if (shootingPattern != null && !WaitForCompletion)
{
shootingPattern.UpdatePattern(delta);
}
}
public override bool IsComplete()
{
if (WaitForCompletion && shootingPattern != null)
return isComplete && shootingPattern.IsComplete();
return isComplete;
}
}

View file

@ -0,0 +1,45 @@
using Cirno.Scripts.Actors;
using Cirno.Scripts.Components;
using Cirno.Scripts.Resources;
using Godot;
namespace Cirno.Scripts.AttackPatterns;
[GlobalClass]
public partial class PatternTest : AttackPattern
{
[Export] public PackedScene BulletScene;
[Export] private float bulletSpeed = 5f;
[Export] private int bulletCount = 12;
[Export] private float duration = 3f;
[Export] private float burstInterval = 0.5f;
[Export] private BulletOwner owner = BulletOwner.Enemy;
private double timer;
private double burstTimer;
private BulletSpawner spawner;
public override void Start(Boss boss)
{
Boss = boss;
timer = 0;
burstTimer = 0;
spawner = boss.GetNode<BulletSpawner>("BulletSpawner");
}
public override void UpdatePattern(double delta)
{
timer += delta;
burstTimer += delta;
if (timer < duration && burstTimer >= burstInterval)
{
spawner.SpawnBullet(Boss.GlobalPosition, Vector2.Right, bulletSpeed, owner, bulletCount, bulletScene: BulletScene);
burstTimer = 0;
}
}
public override bool IsComplete()
{
return timer >= duration;
}
}

View file

@ -0,0 +1,47 @@
using Cirno.Scripts.Actors;
using Cirno.Scripts.Components;
using Cirno.Scripts.Resources;
using Godot;
namespace Cirno.Scripts.AttackPatterns;
[GlobalClass]
public partial class SpiralPattern : AttackPattern
{
[Export] public PackedScene BulletScene;
[Export] private float bulletSpeed = 5f;
[Export] private int bulletCount = 16;
[Export] private float rotationSpeed = 90f;
[Export] private float duration = 5f;
[Export] private float burstInterval = 0.5f;
[Export] private float spread = 360f;
[Export] private BulletOwner owner = BulletOwner.Enemy;
private double timer;
private double burstTimer;
private BulletSpawner spawner;
public override void Start(Boss boss)
{
Boss = boss;
timer = 0;
burstTimer = burstInterval; // start immediately
spawner = boss.GetNode<BulletSpawner>("BulletSpawner");
}
public override void UpdatePattern(double delta)
{
timer += delta;
burstTimer += delta;
if (timer < duration && burstTimer >= burstInterval)
{
spawner.SpawnSpiralPattern(Boss.GlobalPosition, bulletSpeed, owner, bulletCount, rotationSpeed, timer, spread, BulletScene);
burstTimer = 0;
}
}
public override bool IsComplete()
{
return timer >= duration;
}
}

View file

@ -0,0 +1,54 @@
using Cirno.Scripts.Actors;
using Cirno.Scripts.Components;
using Cirno.Scripts.Resources;
namespace Cirno.Scripts.AttackPatterns;
using Godot;
[GlobalClass]
public partial class TargetedPattern : AttackPattern
{
[Export] public PackedScene BulletScene;
[Export] private float bulletSpeed = 5f;
[Export] private float duration = 3f;
[Export] private float burstInterval = 0.5f;
[Export] private int bulletsPerShot = 1;
[Export] private float spread = 0f;
[Export] private BulletOwner owner = BulletOwner.Enemy;
[Export] private Resource modifier;
private double timer;
private double burstTimer;
private BulletSpawner spawner;
//private Node2D player;
private GameManager _gameManager;
public override void Start(Boss boss)
{
_gameManager = boss.GetNode<GameManager>("/root/GameScene");
Boss = boss;
timer = 0;
burstTimer = 0;
spawner = boss.GetNode<BulletSpawner>("BulletSpawner");
}
public override void UpdatePattern(double delta)
{
timer += delta;
burstTimer += delta;
if (timer < duration && burstTimer >= burstInterval && _gameManager.PlayerPosition.HasValue)
{
spawner.SpawnTargetedBullet(Boss.GlobalPosition, _gameManager.PlayerPosition.Value, bulletSpeed, owner, BulletScene, bulletsPerShot, spread, modifier as IBulletModifier);
burstTimer = 0;
}
}
public override bool IsComplete()
{
return timer >= duration;
}
}

View file

@ -42,7 +42,6 @@ public partial class Bullet : Area2D
//Debug.WriteLine($"Bullet Shot at direction {direction.X} {direction.Y}");
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{

View file

@ -0,0 +1,53 @@
using Cirno.Scripts.Resources;
using Godot;
namespace Cirno.Scripts.Components;
public partial class BulletSpawner : Node2D
{
[Export] public PackedScene BulletScene;
private GameManager _gameManager;
public override void _Ready()
{
_gameManager = GetNode<GameManager>("/root/GameScene");
}
public void SpawnBullet(Vector2 position, Vector2 direction, float speed, BulletOwner owner, int count = 1, float angleOffset = 0, float spread = 0, PackedScene bulletScene = null, IBulletModifier modifier = null)
{
for (int i = 0; i < count; i++)
{
var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletScene ?? BulletScene, position);
//var bullet = BulletScene.Instantiate<Bullet>();
bullet.Position = position;
bullet.Owner = owner;
//bullet.Speed = speed;
float modifiedSpeed = modifier?.ModifySpeed(speed, i) ?? speed;
bullet.Speed = modifiedSpeed;
Vector2 baseDirection = direction == Vector2.Zero ? Vector2.Right : direction.Normalized();
float baseAngle = Mathf.Atan2(baseDirection.Y, baseDirection.X);
//float angle = angleOffset + (360 / count) * i;
float angle = baseAngle + Mathf.DegToRad(angleOffset + (spread / count) * i);
Vector2 bulletDirection = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
//Vector2 bulletDirection = new Vector2(Mathf.Cos(Mathf.DegToRad(angle)), Mathf.Sin(Mathf.DegToRad(angle)));
bullet.SetDirection(bulletDirection);
//GetParent().AddChild(bullet);
}
}
public void SpawnTargetedBullet(Vector2 position, Vector2 target, float speed, BulletOwner owner, PackedScene bulletScene = null, int burstCount = 1, float spread = 0, IBulletModifier modifier = null)
{
Vector2 direction = (target - position).Normalized();
SpawnBullet(position, direction, speed, owner, burstCount, spread: spread, bulletScene: bulletScene, modifier: modifier);
}
public void SpawnSpiralPattern(Vector2 position, float speed, BulletOwner owner, int bulletCount, float rotationSpeed, double time, float spread, PackedScene bulletScene = null)
{
float angleOffset = (float)(rotationSpeed * time);
SpawnBullet(position, Vector2.Right, speed, owner, bulletCount, angleOffset, spread, bulletScene: bulletScene);
}
}

View file

@ -8,6 +8,12 @@ using Godot.Collections;
public partial class Enemy : CharacterBody2D
{
private InteractionController _cachedPlayer;
public InteractionController CachedPlayer
{
get => _cachedPlayer;
protected set => _cachedPlayer = value;
}
private EnemyState _currentState = EnemyState.Idle;
[Export] public float Health = 4f;
@ -20,7 +26,7 @@ public partial class Enemy : CharacterBody2D
[Export] public Weapon EquippedWeapon;
private float _currentHealth = 0f;
protected float _currentHealth = 0f;
private bool _isDestroyed = false;
@ -52,9 +58,12 @@ public partial class Enemy : CharacterBody2D
_navigationAgent = GetNodeOrNull<NavigationAgent2D>("NavigationAgent2D");
_alarmManager = GetNode<AlarmManager>("/root/GameScene/AlarmManager");
_alarmManager.AlarmEnabled += AlarmManagerOnAlarmEnabled;
_alarmManager = GetNodeOrNull<AlarmManager>("/root/GameScene/AlarmManager");
if (_alarmManager != null)
{
_alarmManager.AlarmEnabled += AlarmManagerOnAlarmEnabled;
}
}
private void AlarmManagerOnAlarmEnabled(Vector2 location)

View file

@ -7,9 +7,13 @@ public partial class GameManager : Node2D
private Hud _hud;
private PlayerMovement _player;
public PlayerMovement Player => _player;
private Node2D _cameraTarget;
public Vector2? PlayerPosition => _player?.GlobalPosition ?? null;
[Export]
public PackedScene PlayerTemplate { get; set; }

View file

@ -0,0 +1,13 @@
using Cirno.Scripts.Actors;
using Godot;
namespace Cirno.Scripts.Resources;
public abstract partial class AttackPattern : Resource
{
public Boss Boss;
[Export] public bool WaitForCompletion = true;
public abstract void Start(Boss boss);
public abstract void UpdatePattern(double delta);
public abstract bool IsComplete();
}

View file

@ -0,0 +1,35 @@
using Cirno.Scripts.Actors;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Resources;
[GlobalClass]
public partial class BossPhase : Resource
{
[Export] public int Threshold;
[Export] public Array<AttackPattern> Patterns;
private int currentPatternIndex = 0;
private double patternTimer;
public void Start(Boss boss)
{
currentPatternIndex = 0;
Patterns[currentPatternIndex].Start(boss);
}
public void UpdatePhase(double delta)
{
patternTimer += delta;
var currentPattern = Patterns[currentPatternIndex];
currentPattern.UpdatePattern(delta);
if (!currentPattern.WaitForCompletion || currentPattern.IsComplete())
{
currentPatternIndex = (currentPatternIndex + 1) % Patterns.Count;
Patterns[currentPatternIndex].Start(currentPattern.Boss);
}
}
}

View file

@ -0,0 +1,14 @@
using Godot;
namespace Cirno.Scripts.Resources;
[GlobalClass]
public partial class DecreasingSpeedModifier : Resource, IBulletModifier
{
[Export] private float decreaseRate = 0.1f;
public float ModifySpeed(float baseSpeed, int bulletIndex)
{
return Mathf.Max(0, baseSpeed - (decreaseRate * bulletIndex));
}
}

View file

@ -0,0 +1,6 @@
namespace Cirno.Scripts.Resources;
public interface IBulletModifier
{
float ModifySpeed(float baseSpeed, int bulletIndex);
}

View file

@ -0,0 +1,42 @@
using Godot;
using System.Collections.Generic;
using Cirno.Scripts.Actors;
using Godot.Collections;
namespace Cirno.Scripts.Resources;
[GlobalClass]
public partial class PatternGroup : AttackPattern
{
[Export] private Array<AttackPattern> patterns;
private int currentPatternIndex = 0;
public override void Start(Boss boss)
{
Boss = boss;
currentPatternIndex = 0;
patterns[currentPatternIndex].Start(boss);
}
public override void UpdatePattern(double delta)
{
if (currentPatternIndex < patterns.Count)
{
patterns[currentPatternIndex].UpdatePattern(delta);
if (!patterns[currentPatternIndex].WaitForCompletion || patterns[currentPatternIndex].IsComplete())
{
currentPatternIndex++;
if (currentPatternIndex < patterns.Count)
{
patterns[currentPatternIndex].Start(Boss);
}
}
}
}
public override bool IsComplete()
{
return currentPatternIndex >= patterns.Count;
}
}

View file

@ -0,0 +1,37 @@
using Cirno.Scripts.Actors;
using Godot;
namespace Cirno.Scripts.Resources;
[GlobalClass]
public partial class SimpleMovementPattern : AttackPattern
{
[Export] private Vector2 relativeTargetPosition;
[Export] private float moveDuration = 2f;
[Export] private Tween.TransitionType transitionType = Tween.TransitionType.Linear;
[Export] private Tween.EaseType easeType = Tween.EaseType.InOut;
private Tween tween;
private bool isComplete = false;
public override void Start(Boss boss)
{
Boss = boss;
tween = boss.CreateTween();
isComplete = false;
Vector2 targetPosition = (Boss?.HomePosition ?? boss.GlobalPosition) + relativeTargetPosition;
tween.TweenProperty(Boss, "position", targetPosition, moveDuration)
.SetTrans(transitionType)
.SetEase(easeType)
.Finished += () => isComplete = true;
}
public override void UpdatePattern(double delta) { }
public override bool IsComplete()
{
return isComplete;
}
}

View file

@ -97,7 +97,6 @@ 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, BulletScene, _muzzle.GlobalPosition);