Hitbox visible

This commit is contained in:
Marco 2025-01-28 10:43:35 +01:00
commit 0ccc272ada
10 changed files with 141 additions and 17 deletions

View file

@ -44,6 +44,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
private GameManager _gameManager;
[Export] public Sprite2D HitboxSprite { get; set; }
private bool _isStrafing { get; set; }
[Signal]
@ -192,10 +194,30 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
}
// public override void _Draw()
// {
// if (_isStrafing)
// {
// HitboxSprite.Visible = true;
// }
// else
// {
// HitboxSprite.Visible = false;
// }
// base._Draw();
// }
public override void _PhysicsProcess(double delta)
{
_movementDirection = GetInput();
_isStrafing = Input.IsActionPressed("strafe");
// Toggle visibility of the hitbox sprite based on strafing
if (HitboxSprite != null)
{
HitboxSprite.Visible = _isStrafing;
}
_rightStickInput = GetRightStickInput();
// Update Facing Direction

View file

@ -23,6 +23,11 @@ public partial class Weapon : Node2D
[Export] public bool AutoReload = true;
[Export] public bool InfiniteAmmo = true;
[Export] public int BulletsPerShot = 1;
[Export] public float BulletsSpreadAngle = 0f;
public int Ammo { get; set; } = 0;
public int LoadedAmmo { get; private set; }
@ -80,23 +85,32 @@ public partial class Weapon : Node2D
}
// TODO: Shoot at muzzle position, need to provide a way to turn it, on a radius?
// TODO: Create not as child but as standalone
// if (BulletScene == null) return;
//
// var newInstance = BulletScene.Instantiate<Bullet>();
// _gameManager.BulletsContainer.CallDeferred("add_child", newInstance);
var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, BulletScene, _muzzle.GlobalPosition);
if (bullet == null)
{
GD.PrintErr("Bullet is null, not shooting");
return;
};
float halfSpread = BulletsSpreadAngle / 2f;
float spreadStep = BulletsPerShot > 1 ? BulletsSpreadAngle / (BulletsPerShot - 1) : 0;
bullet.SetDirection(ShootDirection);
bullet.Speed = BulletSpeed;
for (int i = 0; i < BulletsPerShot; i++)
{
// Calculate angle offset for this bullet
float spreadOffset = -halfSpread + (spreadStep * i);
// Rotate the ShootDirection by the spread angle
Vector2 spreadDirection = ShootDirection.Rotated(Mathf.DegToRad(spreadOffset));
var bullet = this.CreateChildOf<Bullet>(_gameManager.BulletsContainer, BulletScene, _muzzle.GlobalPosition);
if (bullet == null)
{
GD.PrintErr("Bullet is null, not shooting");
return;
};
//bullet.SetDirection(ShootDirection);
bullet.SetDirection(spreadDirection);
bullet.Speed = BulletSpeed;
}
LoadedAmmo -= 1;