Projectile Rotation

This commit is contained in:
Marco 2025-02-13 18:25:55 +01:00
commit 56ac07367b
12 changed files with 188 additions and 27 deletions

View file

@ -5,6 +5,7 @@ using Cirno.Scripts.Components;
using Cirno.Scripts.Resources;
using Godot;
using Godot.Collections;
using Array = System.Array;
namespace Cirno.Scripts.AttackPatterns;
@ -23,7 +24,7 @@ public partial class SpiralPattern : AttackPattern
[Export] private DamageType _damageType = DamageType.Neutral;
[Export] private float _bulletDamage = 1f;
[Export] private BulletCreationModifier _modifier;
[Export] private Array<Resource> _timeModifiers;
[Export] private Array<TimeModifier> _timeModifiers;
[Export] private bool _targetPlayer = false;
@ -50,7 +51,6 @@ public partial class SpiralPattern : AttackPattern
float angleOffset = _rotationOffset + (float)(rotationSpeed * timer);
Vector2 direction = Vector2.Right;
;
if (_targetPlayer && Boss.GameManager.PlayerPosition.HasValue)
{
@ -70,7 +70,11 @@ public partial class SpiralPattern : AttackPattern
BulletScene = BulletScene,
RotationOffset = angleOffset,
Modifier = _modifier,
TimeModifiers = _timeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().ToList() ?? new List<TimeModifier>()
TimeModifiers = ((_timeModifiers?.Where(mod => mod != null)) ?? Array.Empty<TimeModifier>()).Select(m => new ModifierWrapper()
{
TimeModifier = m,
Applied = false
}).ToList()
});
// spawner.SpawnSpiralPattern(Boss.GlobalPosition, bulletSpeed, owner, bulletCount, rotationSpeed, timer, spread, BulletScene);

View file

@ -1,6 +1,8 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Cirno.Scripts;
using Cirno.Scripts.Components;
using Cirno.Scripts.Resources;
@ -22,32 +24,41 @@ public partial class Bullet : Area2D
private BulletInfo _bulletInfo;
public BulletInfo BulletInfo => _bulletInfo;
private List<ModifierWrapper> _modifiers = new();
public void Initialize(BulletInfo bulletInfo)
{
_bulletInfo = bulletInfo;
// Ugly hack to make instances unique
_modifiers = _bulletInfo.TimeModifiers.Select(x => new ModifierWrapper()
{
TimeModifier = x.TimeModifier,
Applied = x.Applied
}).ToList();
//Position = bulletInfo.Position;
}
private void ApplyTimeModifiers()
{
foreach (var modifier in _bulletInfo.TimeModifiers)
foreach (var modifier in _modifiers)
{
if (modifier.Applied)
{
continue;
}
if (_elapsedTime >= modifier.TimeInSeconds)
if (_elapsedTime >= modifier.TimeModifier.TimeInSeconds)
{
switch (modifier.ModifierType)
GD.Print("Applied time modifier");
switch (modifier.TimeModifier.ModifierType)
{
case TimeModifierType.SpeedChange:
//_bulletInfo.Speed += modifier.Value;
Speed = modifier.Value;
Speed = modifier.TimeModifier.Value;
break;
case TimeModifierType.RotationChange:
RotateBullet(modifier.Value);
RotateBullet(modifier.TimeModifier.Value);
//Rotation += Mathf.DegToRad(modifier.Value);
break;
case TimeModifierType.FacePlayer:
@ -64,6 +75,8 @@ public partial class Bullet : Area2D
{
float radians = Mathf.DegToRad(degrees);
_direction = _direction.Rotated(radians).Normalized(); // Rotate direction
SetRotation(Rotation + radians);
//Rotation = radians;
}
@ -93,7 +106,9 @@ public partial class Bullet : Area2D
var normalized = direction.Normalized();
_direction = normalized;
SetRotation(Mathf.Atan2(normalized.Y,normalized.X) + Mathf.Pi / 2);
//Debug.WriteLine($"Bullet Shot at direction {direction.X} {direction.Y}");
}

View file

@ -23,24 +23,22 @@ public partial class BulletSpawner : Node2D
var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletInfo.BulletScene ?? BulletScene, bulletInfo.Position);
bullet.Initialize(bulletInfo);
//var bullet = BulletScene.Instantiate<Bullet>();
//bullet.Position = bulletInfo.Position;
//bullet.Owner = bulletInfo.Owner;
//bullet.Speed = speed;
float modifiedSpeed = bulletInfo.Modifier?.ModifySpeed(bulletInfo.Speed, i, bulletInfo.BulletCount) ?? bulletInfo.Speed;
bullet.Speed = modifiedSpeed;
Vector2 baseDirection = bulletInfo.Direction == Vector2.Zero ? Vector2.Right : bulletInfo.Direction.Normalized();
float baseAngle = Mathf.Atan2(baseDirection.Y, baseDirection.X);
//float angle = angleOffset + (360 / count) * i;
float angle = baseAngle + Mathf.DegToRad(bulletInfo.RotationOffset + (bulletInfo.Spread / bulletInfo.BulletCount) * i);
float offsetRadians = Mathf.DegToRad(bulletInfo.RotationOffset);
float spreadStep = Mathf.DegToRad(bulletInfo.Spread) / (bulletInfo.BulletCount - 1); // Ensure proper spread spacing
float angle = baseAngle + offsetRadians + (spreadStep * i);
// float angle = baseAngle + Mathf.DegToRad(bulletInfo.RotationOffset + (bulletInfo.Spread / bulletInfo.BulletCount) * 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);
}
}
@ -93,5 +91,5 @@ public class BulletInfo
public PackedScene BulletScene { get; set; }
public PackedScene DestructionParticlesScene { get; set; }
public IBulletModifier Modifier { get; set; }
public List<TimeModifier> TimeModifiers { get; set; } = new List<TimeModifier>();
public List<ModifierWrapper> TimeModifiers { get; set; } = new List<ModifierWrapper>();
}

View file

@ -39,8 +39,18 @@ public partial class BulletResource : Resource
Modifier = Modifier,
LifeTime = LifeTime,
DestructionParticlesScene = DestructionParticlesScene,
TimeModifiers = TimeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().ToList() ??
new List<TimeModifier>()
TimeModifiers = TimeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().Select(m => new ModifierWrapper()
{
TimeModifier = m,
Applied = false
}).ToList()
// TimeModifiers = TimeModifiers?.Select(x => new ModifierWrapper()
// {
// Applied = false,
// TimeModifier = x
// })
// TimeModifiers = TimeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().ToList() ??
// new List<TimeModifier>()
};
}
}

View file

@ -8,7 +8,12 @@ public partial class TimeModifier : Resource
[Export] public float TimeInSeconds = 1f;
[Export] public TimeModifierType ModifierType;
[Export] public float Value;
public bool Applied = false;
}
public class ModifierWrapper
{
public TimeModifier TimeModifier { get; set; }
public bool Applied { get; set; } = false;
}
public enum TimeModifierType