From 4e6257dbe3dab85e48982fbd2fda5f0e05535ad9 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 9 Feb 2025 11:48:30 +0100 Subject: [PATCH] Time modifiers --- Resources/BossPhases/Rumia_SP1.tres | 2 + Scripts/AttackPatterns/SpiralPattern.cs | 7 ++- Scripts/Bullet.cs | 73 ++++++++++++++++++++++--- Scripts/Components/BulletSpawner.cs | 9 ++- Scripts/Resources/TimeModifier.cs | 19 +++++++ project.godot | 2 +- 6 files changed, 101 insertions(+), 11 deletions(-) create mode 100644 Scripts/Resources/TimeModifier.cs diff --git a/Resources/BossPhases/Rumia_SP1.tres b/Resources/BossPhases/Rumia_SP1.tres index 6fe0cdde..d4cf5751 100644 --- a/Resources/BossPhases/Rumia_SP1.tres +++ b/Resources/BossPhases/Rumia_SP1.tres @@ -36,6 +36,7 @@ burstInterval = 1.0 spread = 180.0 owner = 2 _modifier = SubResource("Resource_l3ln6") +_timeModifiers = Array[Resource]([]) WaitForCompletion = true [sub_resource type="Resource" id="Resource_7yi74"] @@ -50,6 +51,7 @@ burstInterval = 1.0 spread = 180.0 owner = 2 _modifier = SubResource("Resource_l3ln6") +_timeModifiers = null WaitForCompletion = true [sub_resource type="Resource" id="Resource_gm1rv"] diff --git a/Scripts/AttackPatterns/SpiralPattern.cs b/Scripts/AttackPatterns/SpiralPattern.cs index cdcec8a9..b3a15557 100644 --- a/Scripts/AttackPatterns/SpiralPattern.cs +++ b/Scripts/AttackPatterns/SpiralPattern.cs @@ -1,7 +1,10 @@ -using Cirno.Scripts.Actors; +using System.Collections.Generic; +using System.Linq; +using Cirno.Scripts.Actors; using Cirno.Scripts.Components; using Cirno.Scripts.Resources; using Godot; +using Godot.Collections; namespace Cirno.Scripts.AttackPatterns; @@ -18,6 +21,7 @@ public partial class SpiralPattern : AttackPattern [Export] private float spread = 360f; [Export] private BulletOwner owner = BulletOwner.Enemy; [Export] private Resource _modifier; + [Export] private Array _timeModifiers; private double timer; private double burstTimer; @@ -50,6 +54,7 @@ public partial class SpiralPattern : AttackPattern BulletScene = BulletScene, RotationOffset = _rotationOffset, Modifier = _modifier as IBulletModifier, + TimeModifiers = _timeModifiers?.Where(mod => mod is TimeModifier).Cast().ToList() ?? new List() }); // spawner.SpawnSpiralPattern(Boss.GlobalPosition, bulletSpeed, owner, bulletCount, rotationSpeed, timer, spread, BulletScene); diff --git a/Scripts/Bullet.cs b/Scripts/Bullet.cs index b0ae6853..5488a7b4 100644 --- a/Scripts/Bullet.cs +++ b/Scripts/Bullet.cs @@ -1,6 +1,8 @@ using Godot; using System; using System.Diagnostics; +using Cirno.Scripts.Components; +using Cirno.Scripts.Resources; public partial class Bullet : Area2D { @@ -14,15 +16,66 @@ public partial class Bullet : Area2D public BulletOwner Owner = BulletOwner.None; private Vector2 _direction = Vector2.Right; - - //public delegate void BulletHitEventHandler(Node Body); - //public event BulletHitEventHandler BulletHit; - - // Called when the node enters the scene tree for the first time. - public override void _Ready() + + private double _elapsedTime = 0f; + private BulletInfo _bulletInfo; + + public void Initialize(BulletInfo bulletInfo) { - //this.Connect("body_entered", new Callable(this, nameof(OnBodyEntered))); + _bulletInfo = bulletInfo; + + Position = bulletInfo.Position; + Owner = bulletInfo.Owner; } + + private void ApplyTimeModifiers() + { + foreach (var modifier in _bulletInfo.TimeModifiers) + { + if (modifier.Applied) + { + continue; + } + if (_elapsedTime >= modifier.TimeInSeconds) + { + switch (modifier.ModifierType) + { + case TimeModifierType.SpeedChange: + //_bulletInfo.Speed += modifier.Value; + Speed = modifier.Value; + break; + case TimeModifierType.RotationChange: + RotateBullet(modifier.Value); + //Rotation += Mathf.DegToRad(modifier.Value); + break; + case TimeModifierType.FacePlayer: + FacePlayer(); + break; + } + + modifier.Applied = true; + } + } + } + + private void RotateBullet(float degrees) + { + float radians = Mathf.DegToRad(degrees); + _direction = _direction.Rotated(radians).Normalized(); // Rotate direction + } + + + private void FacePlayer() + { + // TODO: cache player + var player = GetTree().GetFirstNodeInGroup("Player") as Node2D; + if (player != null) + { + _direction = (player.GlobalPosition - this.GlobalPosition).Normalized(); + //LookAt(player.GlobalPosition); + } + } + //private void OnBodyEntered(Node body) //{ @@ -45,6 +98,12 @@ public partial class Bullet : Area2D // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { + _elapsedTime += delta; + if (_bulletInfo != null) + { + ApplyTimeModifiers(); + } + this.Position += ((float)(Speed * delta) * _direction); } diff --git a/Scripts/Components/BulletSpawner.cs b/Scripts/Components/BulletSpawner.cs index 25592ca3..60a708c2 100644 --- a/Scripts/Components/BulletSpawner.cs +++ b/Scripts/Components/BulletSpawner.cs @@ -1,6 +1,8 @@ using System; +using System.Collections.Generic; using Cirno.Scripts.Resources; using Godot; +using Godot.Collections; namespace Cirno.Scripts.Components; @@ -20,9 +22,11 @@ public partial class BulletSpawner : Node2D { var bullet = this.CreateChildOf(_gameManager.BulletsContainer, bulletInfo.BulletScene ?? BulletScene, bulletInfo.Position); + bullet.Initialize(bulletInfo); + //var bullet = BulletScene.Instantiate(); - bullet.Position = bulletInfo.Position; - bullet.Owner = bulletInfo.Owner; + //bullet.Position = bulletInfo.Position; + //bullet.Owner = bulletInfo.Owner; //bullet.Speed = speed; float modifiedSpeed = bulletInfo.Modifier?.ModifySpeed(bulletInfo.Speed, i, bulletInfo.BulletCount) ?? bulletInfo.Speed; @@ -93,4 +97,5 @@ public class BulletInfo public float Spread { get; set; } public PackedScene BulletScene { get; set; } public IBulletModifier Modifier { get; set; } + public List TimeModifiers { get; set; } = new List(); } \ No newline at end of file diff --git a/Scripts/Resources/TimeModifier.cs b/Scripts/Resources/TimeModifier.cs new file mode 100644 index 00000000..a1bab8a0 --- /dev/null +++ b/Scripts/Resources/TimeModifier.cs @@ -0,0 +1,19 @@ +using Godot; + +namespace Cirno.Scripts.Resources; + +[GlobalClass] +public partial class TimeModifier : Resource +{ + [Export] public float TimeInSeconds = 1f; + [Export] public TimeModifierType ModifierType; + [Export] public float Value; + public bool Applied = false; +} + +public enum TimeModifierType +{ + SpeedChange, + RotationChange, + FacePlayer +} diff --git a/project.godot b/project.godot index 0adee7d3..f7199b33 100644 --- a/project.godot +++ b/project.godot @@ -21,7 +21,7 @@ Settings/temporaryLogLifetime=5.0 [application] config/name="Cirno" -run/main_scene="res://Scenes/test.tscn" +run/main_scene="res://Scenes/Maps/BossTestArena.tscn" config/features=PackedStringArray("4.3", "C#", "GL Compatibility") config/icon="res://icon.svg"