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 DestroyOnCollision = true
Owner = 1 Owner = 1
DamageType = 0 DamageType = 0
Controllable = true
TimeModifiers = null TimeModifiers = null

View file

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

View file

@ -35,14 +35,14 @@ public partial class Bullet : Area2D
_gameManager = gameManager; _gameManager = gameManager;
_elapsedTime = 0f; _elapsedTime = 0f;
// Need to clone them here // Need to clone them here
// _modifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone()).ToList(); // _modifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone()).ToList();
// var clonedModifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone()); // var clonedModifiers = _bulletInfo.TimeModifiers.Select(x => x.MakeClone());
// _modifiers = clonedModifiers.ToList(); // _modifiers = clonedModifiers.ToList();
// Ugly hack to make instances unique // Ugly hack to make instances unique
_modifiers = _bulletInfo.TimeModifiers.Select(x => x.Wrap()).ToList(); _modifiers = _bulletInfo.TimeModifiers.Select(x => x.Wrap()).ToList();
} }
@ -151,9 +151,26 @@ public partial class Bullet : Area2D
ApplyTimeModifiers(delta); ApplyTimeModifiers(delta);
} }
if (BulletInfo.Controllabe)
{
ControlBullet(delta);
}
this.Position += ((float)(Speed * delta) * _direction); 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() private void _on_visible_on_screen_notifier_2d_screen_exited()
{ {
//Debug.WriteLine("Destroy bullet out of screen"); //Debug.WriteLine("Destroy bullet out of screen");

View file

@ -11,7 +11,7 @@ public partial class BulletSpawner : Node2D
{ {
[Export] public PackedScene BulletScene; [Export] public PackedScene BulletScene;
private GameManager _gameManager; private GameManager _gameManager;
public override void _Ready() public override void _Ready()
{ {
_gameManager = this.GetGameManager(); _gameManager = this.GetGameManager();
@ -21,16 +21,16 @@ public partial class BulletSpawner : Node2D
{ {
var bulletScene = bulletInfo.BulletScene ?? BulletScene; var bulletScene = bulletInfo.BulletScene ?? BulletScene;
Bullet bullet; Bullet bullet;
for (int i = 0; i < bulletInfo.BulletCount; i++) for (int i = 0; i < bulletInfo.BulletCount; i++)
{ {
if (bulletInfo.IsLaser) if (bulletInfo.IsLaser)
bullet = this.CreateChildOf<LaserBullet>(_gameManager.BulletsContainer, bulletScene, bulletInfo.Position); bullet = this.CreateChildOf<LaserBullet>(_gameManager.BulletsContainer, bulletScene, bulletInfo.Position);
else else
bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletScene, bulletInfo.Position); bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletScene, bulletInfo.Position);
// var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletInfo.BulletScene ?? BulletScene, bulletInfo.Position); // var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletInfo.BulletScene ?? BulletScene, bulletInfo.Position);
bullet.Initialize(bulletInfo, _gameManager); bullet.Initialize(bulletInfo, _gameManager);
float modifiedSpeed = bulletInfo.Modifier?.ModifySpeed(bulletInfo.Speed, i, bulletInfo.BulletCount) ?? bulletInfo.Speed; 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(); Vector2 baseDirection = bulletInfo.Direction == Vector2.Zero ? Vector2.Right : bulletInfo.Direction.Normalized();
float baseAngle = Mathf.Atan2(baseDirection.Y, baseDirection.X); float baseAngle = Mathf.Atan2(baseDirection.Y, baseDirection.X);
float offsetRadians = Mathf.DegToRad(bulletInfo.RotationOffset); 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 + offsetRadians + (spreadStep * i);
// float angle = baseAngle + Mathf.DegToRad(bulletInfo.RotationOffset + (bulletInfo.Spread / bulletInfo.BulletCount) * 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(angle), Mathf.Sin(angle));
bullet.SetDirection(bulletDirection); 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) 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++) for (int i = 0; i < count; i++)
{ {
var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletScene ?? BulletScene, position); var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletScene ?? BulletScene, position);
//var bullet = BulletScene.Instantiate<Bullet>(); //var bullet = BulletScene.Instantiate<Bullet>();
bullet.Position = position; bullet.Position = position;
//bullet.Speed = speed; //bullet.Speed = speed;
float modifiedSpeed = modifier?.ModifySpeed(speed, i, count) ?? speed; float modifiedSpeed = modifier?.ModifySpeed(speed, i, count) ?? speed;
bullet.Speed = modifiedSpeed; bullet.Speed = modifiedSpeed;
@ -74,7 +74,7 @@ public partial class BulletSpawner : Node2D
//GetParent().AddChild(bullet); //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) 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(); Vector2 direction = (target - position).Normalized();
@ -97,6 +97,7 @@ public class BulletInfo
public float RotationOffset { get; set; } public float RotationOffset { get; set; }
//public double Time { get; set; } //public double Time { get; set; }
public float Spread { get; set; } public float Spread { get; set; }
public bool Controllabe { get; set; } = false;
public PackedScene BulletScene { get; set; } public PackedScene BulletScene { get; set; }
public PackedScene DestructionParticlesScene { get; set; } public PackedScene DestructionParticlesScene { get; set; }
public IBulletModifier Modifier { 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 bool DestroyOnCollision = true;
[Export] public BulletOwner Owner = BulletOwner.None; [Export] public BulletOwner Owner = BulletOwner.None;
[Export] public DamageType DamageType = DamageType.Neutral; [Export] public DamageType DamageType = DamageType.Neutral;
[Export] public bool Controllable = false;
[Export] [Export]
public BulletCreationModifier Modifier; public BulletCreationModifier Modifier;
[Export] public Array<TimeModifier> TimeModifiers; [Export] public Array<TimeModifier> TimeModifiers;
public BulletInfo MakeBullet(Vector2 position, int count = 1, float spread = 0f, float rotationOffset = 0f) public BulletInfo MakeBullet(Vector2 position, int count = 1, float spread = 0f, float rotationOffset = 0f)
{ {
return new BulletInfo() return new BulletInfo()
@ -42,6 +43,7 @@ public partial class BulletResource : Resource
LifeTime = LifeTime, LifeTime = LifeTime,
DestroyOnCollision = DestroyOnCollision, DestroyOnCollision = DestroyOnCollision,
DestructionParticlesScene = DestructionParticlesScene, DestructionParticlesScene = DestructionParticlesScene,
Controllabe = Controllable,
TimeModifiers = TimeModifiers.Select(x => x).ToList() TimeModifiers = TimeModifiers.Select(x => x).ToList()
// TimeModifiers = TimeModifiers?.Where(mod => mod is TimeModifier).Cast<TimeModifier>().Select(m => new ModifierWrapper() // 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 public partial class MusicRoom : MenuBase
{ {
[Export] [Export]
public Array<MusicResource> Tracks { get; private set; } = new(); public Array<MusicResource> Tracks { get; private set; } = new();
[Export] [Export]
public ItemList TracksContainer { get; private set; } public ItemList TracksContainer { get; private set; }
[Export] [Export]
public Texture2D Icon { get; private set; } public Texture2D Icon { get; private set; }
[Export] [Export]
public Label DescriptionLabel { get; private set; } public Label DescriptionLabel { get; private set; }
private Dictionary<long, AudioNameVisualizer> _tracks = new(); private Dictionary<long, AudioNameVisualizer> _tracks = new();
public override void _Ready() public override void _Ready()
{ {
DescriptionLabel.Text = string.Empty; DescriptionLabel.Text = string.Empty;
TracksContainer.Clear(); TracksContainer.Clear();
foreach (var track in Tracks) foreach (var track in Tracks)
{ {
var index = TracksContainer.AddItem($"{track.TrackName} ({track.AuthorName})", Icon); var index = TracksContainer.AddItem($"{track.TrackName} ({track.AuthorName})", Icon);
var visualizer = new AudioNameVisualizer(); var visualizer = new AudioNameVisualizer();
visualizer.MusicData = track; visualizer.MusicData = track;
TracksContainer.CallDeferred("add_child", visualizer); TracksContainer.CallDeferred("add_child", visualizer);
_tracks.Add((long)index, visualizer); _tracks.Add((long)index, visualizer);
} }
TracksContainer.ItemSelected += (long selectedItem) => TracksContainer.ItemSelected += (long selectedItem) =>
{ {
var vis = _tracks[selectedItem]; var vis = _tracks[selectedItem];
if (vis.MusicData is null) { return; } if (vis.MusicData is null) { return; }
StopAllTracks(); StopAllTracks();
vis.PlayWithName(); vis.PlayWithName();
UpdateDescription(vis.MusicData.Description); UpdateDescription(vis.MusicData.Description);
}; };
} }
private void StopAllTracks() private void StopAllTracks()
{ {
foreach (var track in _tracks) foreach (var track in _tracks)
{ {
track.Value.Stop(); track.Value.Stop();
} }
} }
private void UpdateDescription(string name) private void UpdateDescription(string name)
{ {
DescriptionLabel.Text = name; DescriptionLabel.Text = name;
} }
} }