Gamemanager in bullet

This commit is contained in:
MaddoScientisto 2025-02-13 21:55:14 +01:00
commit 1ee8f085e8
6 changed files with 92 additions and 13 deletions

View file

@ -27,10 +27,14 @@ public partial class Bullet : Area2D
private List<ModifierWrapper> _modifiers = new();
public void Initialize(BulletInfo bulletInfo)
private GameManager _gameManager;
public void Initialize(BulletInfo bulletInfo, GameManager gameManager)
{
_bulletInfo = bulletInfo;
_gameManager = gameManager;
// Ugly hack to make instances unique
_modifiers = _bulletInfo.TimeModifiers.Select(x => new ModifierWrapper()
{
@ -79,14 +83,11 @@ public partial class Bullet : Area2D
//Rotation = radians;
}
private void FacePlayer()
{
// TODO: cache player
var player = GetTree().GetFirstNodeInGroup("Player") as Node2D;
if (player != null)
if (_gameManager.Player != null)
{
_direction = (player.GlobalPosition - this.GlobalPosition).Normalized();
_direction = (_gameManager.Player.GlobalPosition - this.GlobalPosition).Normalized();
//LookAt(player.GlobalPosition);
}
}

View file

@ -22,7 +22,7 @@ public partial class BulletSpawner : Node2D
{
var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, bulletInfo.BulletScene ?? BulletScene, bulletInfo.Position);
bullet.Initialize(bulletInfo);
bullet.Initialize(bulletInfo, _gameManager);
float modifiedSpeed = bulletInfo.Modifier?.ModifySpeed(bulletInfo.Speed, i, bulletInfo.BulletCount) ?? bulletInfo.Speed;
bullet.Speed = modifiedSpeed;
@ -37,7 +37,6 @@ public partial class BulletSpawner : Node2D
Vector2 bulletDirection = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
bullet.SetDirection(bulletDirection);
}
}

View file

@ -64,7 +64,8 @@ public static class Tools
public static GameManager GetGameManager(this Node2D node)
{
return node.GetNode<GameManager>("/root/GameScene");
return node.GetTree().Root.GetNode<GameManager>("GameScene");
// node.GetNode<GameManager>("/root/GameScene");
}
public static AlarmManager GetAlarmManager(this Node2D node)

View file

@ -107,7 +107,7 @@ public partial class Weapon : Node2D
return;
};
bullet.Initialize(WeaponData.MakeBullet(_muzzle.GlobalPosition));
bullet.Initialize(WeaponData.MakeBullet(_muzzle.GlobalPosition), _gameManager);
//bullet.SetDirection(ShootDirection);
bullet.SetDirection(spreadDirection);