steering bullets

This commit is contained in:
Maddo 2025-02-27 18:48:13 +01:00
commit 6125565d8c
6 changed files with 89 additions and 67 deletions

View file

@ -13,4 +13,5 @@ LifeTime = 10.0
DestroyOnCollision = true
Owner = 1
DamageType = 0
Controllable = true
TimeModifiers = null

View file

@ -461,6 +461,7 @@ LifeTime = 20.0
DestroyOnCollision = true
Owner = 2
DamageType = 0
Controllable = false
Modifier = SubResource("Resource_ksslq")
TimeModifiers = Array[Object]([])
metadata/_custom_type_script = "uid://dslyrfcej3g2n"

View file

@ -35,14 +35,14 @@ public partial class Bullet : Area2D
_gameManager = gameManager;
_elapsedTime = 0f;
// Need to clone them here
// _modifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone()).ToList();
// var clonedModifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone());
// _modifiers = clonedModifiers.ToList();
// Ugly hack to make instances unique
_modifiers = _bulletInfo.TimeModifiers.Select(x => x.Wrap()).ToList();
}
@ -151,9 +151,26 @@ public partial class Bullet : Area2D
ApplyTimeModifiers(delta);
}
if (BulletInfo.Controllabe)
{
ControlBullet(delta);
}
this.Position += ((float)(Speed * delta) * _direction);
}
private void ControlBullet(double delta)
{
var axis = Input.GetAxis("left", "right");
if (axis != 0)
{
float rotationSpeed = 180f; // Degrees per second
RotateBullet(axis * rotationSpeed * (float)delta);
}
}
private void _on_visible_on_screen_notifier_2d_screen_exited()
{
//Debug.WriteLine("Destroy bullet out of screen");

View file

@ -11,7 +11,7 @@ public partial class BulletSpawner : Node2D
{
[Export] public PackedScene BulletScene;
private GameManager _gameManager;
public override void _Ready()
{
_gameManager = this.GetGameManager();
@ -21,16 +21,16 @@ public partial class BulletSpawner : Node2D
{
var bulletScene = bulletInfo.BulletScene ?? BulletScene;
Bullet bullet;
for (int i = 0; i < bulletInfo.BulletCount; i++)
{
if (bulletInfo.IsLaser)
bullet = this.CreateChildOf<LaserBullet>(_gameManager.BulletsContainer, bulletScene, bulletInfo.Position);
else
bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletScene, bulletInfo.Position);
// var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletInfo.BulletScene ?? BulletScene, bulletInfo.Position);
bullet.Initialize(bulletInfo, _gameManager);
float modifiedSpeed = bulletInfo.Modifier?.ModifySpeed(bulletInfo.Speed, i, bulletInfo.BulletCount) ?? bulletInfo.Speed;
@ -38,28 +38,28 @@ public partial class BulletSpawner : Node2D
Vector2 baseDirection = bulletInfo.Direction == Vector2.Zero ? Vector2.Right : bulletInfo.Direction.Normalized();
float baseAngle = Mathf.Atan2(baseDirection.Y, baseDirection.X);
float offsetRadians = Mathf.DegToRad(bulletInfo.RotationOffset);
float spreadStep = Mathf.DegToRad(bulletInfo.Spread) / Mathf.Max(1,bulletInfo.BulletCount - 1); // Ensure proper spread spacing, also add 1 if 0
float spreadStep = Mathf.DegToRad(bulletInfo.Spread) / Mathf.Max(1, bulletInfo.BulletCount - 1); // Ensure proper spread spacing, also add 1 if 0
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));
bullet.SetDirection(bulletDirection);
}
}
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.Speed = speed;
float modifiedSpeed = modifier?.ModifySpeed(speed, i, count) ?? speed;
bullet.Speed = modifiedSpeed;
@ -74,7 +74,7 @@ public partial class BulletSpawner : Node2D
//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();
@ -97,6 +97,7 @@ public class BulletInfo
public float RotationOffset { get; set; }
//public double Time { get; set; }
public float Spread { get; set; }
public bool Controllabe { get; set; } = false;
public PackedScene BulletScene { get; set; }
public PackedScene DestructionParticlesScene { get; set; }
public IBulletModifier Modifier { get; set; }

View file

@ -19,11 +19,12 @@ public partial class BulletResource : Resource
[Export] public bool DestroyOnCollision = true;
[Export] public BulletOwner Owner = BulletOwner.None;
[Export] public DamageType DamageType = DamageType.Neutral;
[Export] public bool Controllable = false;
[Export]
public BulletCreationModifier Modifier;
[Export] public Array<TimeModifier> TimeModifiers;
public BulletInfo MakeBullet(Vector2 position, int count = 1, float spread = 0f, float rotationOffset = 0f)
{
return new BulletInfo()
@ -42,6 +43,7 @@ public partial class BulletResource : Resource
LifeTime = LifeTime,
DestroyOnCollision = DestroyOnCollision,
DestructionParticlesScene = DestructionParticlesScene,
Controllabe = Controllable,
TimeModifiers = TimeModifiers.Select(x => x).ToList()
// TimeModifiers = TimeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().Select(m => new ModifierWrapper()
// {

View file

@ -5,56 +5,56 @@ using Godot.Collections;
public partial class MusicRoom : MenuBase
{
[Export]
public Array<MusicResource> Tracks { get; private set; } = new();
[Export]
public Array<MusicResource> Tracks { get; private set; } = new();
[Export]
public ItemList TracksContainer { get; private set; }
[Export]
public ItemList TracksContainer { get; private set; }
[Export]
public Texture2D Icon { get; private set; }
[Export]
public Texture2D Icon { get; private set; }
[Export]
public Label DescriptionLabel { get; private set; }
[Export]
public Label DescriptionLabel { get; private set; }
private Dictionary<long, AudioNameVisualizer> _tracks = new();
private Dictionary<long, AudioNameVisualizer> _tracks = new();
public override void _Ready()
{
DescriptionLabel.Text = string.Empty;
TracksContainer.Clear();
foreach (var track in Tracks)
{
var index = TracksContainer.AddItem($"{track.TrackName} ({track.AuthorName})", Icon);
public override void _Ready()
{
DescriptionLabel.Text = string.Empty;
TracksContainer.Clear();
foreach (var track in Tracks)
{
var index = TracksContainer.AddItem($"{track.TrackName} ({track.AuthorName})", Icon);
var visualizer = new AudioNameVisualizer();
visualizer.MusicData = track;
TracksContainer.CallDeferred("add_child", visualizer);
var visualizer = new AudioNameVisualizer();
visualizer.MusicData = track;
TracksContainer.CallDeferred("add_child", visualizer);
_tracks.Add((long)index, visualizer);
_tracks.Add((long)index, visualizer);
}
}
TracksContainer.ItemSelected += (long selectedItem) =>
{
var vis = _tracks[selectedItem];
if (vis.MusicData is null) { return; }
StopAllTracks();
vis.PlayWithName();
UpdateDescription(vis.MusicData.Description);
};
}
TracksContainer.ItemSelected += (long selectedItem) =>
{
var vis = _tracks[selectedItem];
if (vis.MusicData is null) { return; }
StopAllTracks();
vis.PlayWithName();
UpdateDescription(vis.MusicData.Description);
};
}
private void StopAllTracks()
{
foreach (var track in _tracks)
{
track.Value.Stop();
}
}
private void StopAllTracks()
{
foreach (var track in _tracks)
{
track.Value.Stop();
}
}
private void UpdateDescription(string name)
{
DescriptionLabel.Text = name;
}
private void UpdateDescription(string name)
{
DescriptionLabel.Text = name;
}
}