✨ Name & Health bar
This commit is contained in:
		@@ -50,9 +50,4 @@ public partial class CameraShake : Camera2D
 | 
			
		||||
    {
 | 
			
		||||
        _trauma = Mathf.Min(_trauma + amount, 1.0f);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public void SetTrauma(float amount)
 | 
			
		||||
    {
 | 
			
		||||
        _trauma = Mathf.Min(amount, 1.0f);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -6,113 +6,124 @@ namespace AceField.Scripts;
 | 
			
		||||
 | 
			
		||||
public partial class Player : CharacterBody2D
 | 
			
		||||
{
 | 
			
		||||
	private int _currentPlayerId = 1;
 | 
			
		||||
    private int _currentPlayerId = 1;
 | 
			
		||||
 | 
			
		||||
	[Export] public float MaxSpeed = 400f;
 | 
			
		||||
	[Export] public float Acceleration = 500f;
 | 
			
		||||
	[Export] public float Deceleration = 500f;
 | 
			
		||||
	[Export] public float RotationSpeed = 5f;
 | 
			
		||||
    [Export] public float MaxSpeed = 400f;
 | 
			
		||||
    [Export] public float Acceleration = 500f;
 | 
			
		||||
    [Export] public float Deceleration = 500f;
 | 
			
		||||
    [Export] public float RotationSpeed = 5f;
 | 
			
		||||
 | 
			
		||||
	[Export] public int Reach = 5;
 | 
			
		||||
    [Export] public int Reach = 5;
 | 
			
		||||
 | 
			
		||||
	[Export] public double Health = 100;
 | 
			
		||||
	[Export] public double MaxHealth = 100;
 | 
			
		||||
	[Export] public double ActionPoints = 20;
 | 
			
		||||
	[Export] public double MaxActionPoints = 20;
 | 
			
		||||
    [Export] public double Health = 100;
 | 
			
		||||
    [Export] public double MaxHealth = 100;
 | 
			
		||||
    [Export] public double ActionPoints = 20;
 | 
			
		||||
    [Export] public double MaxActionPoints = 20;
 | 
			
		||||
 | 
			
		||||
	[Export] public Camera2D PlayerCamera;
 | 
			
		||||
	[Export] public PlayerInput PlayerInput;
 | 
			
		||||
    [Export] public Camera2D PlayerCamera;
 | 
			
		||||
    [Export] public PlayerInput PlayerInput;
 | 
			
		||||
 | 
			
		||||
	[Export] public float PlayerDashAcceleration = 2f;
 | 
			
		||||
    [Export] public float PlayerDashAcceleration = 2f;
 | 
			
		||||
 | 
			
		||||
	[Export] public PackedScene BulletScene;
 | 
			
		||||
    [Export] public PackedScene BulletScene;
 | 
			
		||||
 | 
			
		||||
	[Export]
 | 
			
		||||
	public int PlayerId
 | 
			
		||||
	{
 | 
			
		||||
		get => _currentPlayerId;
 | 
			
		||||
		set
 | 
			
		||||
		{
 | 
			
		||||
			_currentPlayerId = value;
 | 
			
		||||
			PlayerInput.SetMultiplayerAuthority(value);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
    [Export] public string PlayerName;
 | 
			
		||||
 | 
			
		||||
	public void SetPlayerId(int id)
 | 
			
		||||
	{
 | 
			
		||||
		PlayerId = id;
 | 
			
		||||
		PlayerInput.SetMultiplayerAuthority(id);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public override void _Ready()
 | 
			
		||||
	{
 | 
			
		||||
		Health = MaxHealth;
 | 
			
		||||
		ActionPoints = MaxActionPoints;
 | 
			
		||||
		
 | 
			
		||||
		if (PlayerId == Multiplayer.GetUniqueId())
 | 
			
		||||
			PlayerCamera.Enabled = true;
 | 
			
		||||
	}
 | 
			
		||||
    [Export]
 | 
			
		||||
    public int PlayerId
 | 
			
		||||
    {
 | 
			
		||||
        get => _currentPlayerId;
 | 
			
		||||
        set
 | 
			
		||||
        {
 | 
			
		||||
            _currentPlayerId = value;
 | 
			
		||||
            PlayerInput.SetMultiplayerAuthority(value);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
	public override void _Process(double delta)
 | 
			
		||||
	{
 | 
			
		||||
		if (PlayerInput.IsShooting)
 | 
			
		||||
		{
 | 
			
		||||
			Rpc(nameof(Shoot));
 | 
			
		||||
			PlayerInput.IsShooting = false;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
    public void SetPlayerId(int id)
 | 
			
		||||
    {
 | 
			
		||||
        PlayerId = id;
 | 
			
		||||
        PlayerInput.SetMultiplayerAuthority(id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
	public override void _PhysicsProcess(double delta)
 | 
			
		||||
	{
 | 
			
		||||
		var input = PlayerInput.MovementDirection;
 | 
			
		||||
    public override void _Ready()
 | 
			
		||||
    {
 | 
			
		||||
        Health = MaxHealth;
 | 
			
		||||
        ActionPoints = MaxActionPoints;
 | 
			
		||||
 | 
			
		||||
		if (input != Vector2.Zero)
 | 
			
		||||
		{
 | 
			
		||||
			input = input.Normalized();
 | 
			
		||||
			Velocity = Velocity.MoveToward(input * MaxSpeed, Acceleration * (float)delta);
 | 
			
		||||
        if (PlayerId == Multiplayer.GetUniqueId())
 | 
			
		||||
            PlayerCamera.Enabled = true;
 | 
			
		||||
 | 
			
		||||
			var finalRotation = input.Angle() + Mathf.Pi / 2;
 | 
			
		||||
			Rotation = Mathf.LerpAngle(Rotation, finalRotation, RotationSpeed * (float)delta);
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
 | 
			
		||||
		}
 | 
			
		||||
        PlayerName = $"Player#{PlayerId}"; // TODO Remove mock data
 | 
			
		||||
        GetNode<Label>("HUD/NameTag").Text = PlayerName;
 | 
			
		||||
 | 
			
		||||
		var dashCountdown = GetNode<Timer>("DashCountdown");
 | 
			
		||||
		if (PlayerInput.IsDashing && dashCountdown.IsStopped())
 | 
			
		||||
		{
 | 
			
		||||
			PlayerInput.IsDashing = false;
 | 
			
		||||
			Velocity *= PlayerDashAcceleration;
 | 
			
		||||
			dashCountdown.Start();
 | 
			
		||||
		}
 | 
			
		||||
        GetNode<ProgressBar>("HUD/HealthBar").Value = Health / MaxHealth * 100;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Position += Velocity * (float)delta;
 | 
			
		||||
		MoveAndSlide();
 | 
			
		||||
	}
 | 
			
		||||
    public override void _Process(double delta)
 | 
			
		||||
    {
 | 
			
		||||
        if (PlayerInput.IsShooting)
 | 
			
		||||
        {
 | 
			
		||||
            Rpc(nameof(Shoot));
 | 
			
		||||
            PlayerInput.IsShooting = false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
	public void TakeDamage(double damage)
 | 
			
		||||
	{
 | 
			
		||||
		Rpc(nameof(GotDamage), damage);
 | 
			
		||||
	}
 | 
			
		||||
    public override void _PhysicsProcess(double delta)
 | 
			
		||||
    {
 | 
			
		||||
        var input = PlayerInput.MovementDirection;
 | 
			
		||||
 | 
			
		||||
	[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
 | 
			
		||||
	private void GotDamage(double damage)
 | 
			
		||||
	{
 | 
			
		||||
		Health -= damage;
 | 
			
		||||
		
 | 
			
		||||
		var shakableCamera = GetNode<CameraShake>("Camera2D");
 | 
			
		||||
		shakableCamera.AddTrauma(0.5f);
 | 
			
		||||
	}
 | 
			
		||||
        if (input != Vector2.Zero)
 | 
			
		||||
        {
 | 
			
		||||
            input = input.Normalized();
 | 
			
		||||
            Velocity = Velocity.MoveToward(input * MaxSpeed, Acceleration * (float)delta);
 | 
			
		||||
 | 
			
		||||
	[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
 | 
			
		||||
	private void Shoot()
 | 
			
		||||
	{
 | 
			
		||||
		var marker = GetNode<Marker2D>("Muzzle");
 | 
			
		||||
		var projectile = BulletScene.Instantiate<Bullet>();
 | 
			
		||||
		projectile.Transform = marker.GlobalTransform;
 | 
			
		||||
		projectile.PlayerId = PlayerId;
 | 
			
		||||
		
 | 
			
		||||
		GetParent().AddChild(projectile);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
            var centre = GetNode<Node2D>("RotationCentre");
 | 
			
		||||
            var finalRotation = input.Angle() + Mathf.Pi / 2;
 | 
			
		||||
            centre.Rotation = Mathf.LerpAngle(centre.Rotation, finalRotation, RotationSpeed * (float)delta);
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            Velocity = Velocity.MoveToward(Vector2.Zero, Deceleration * (float)delta);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        var dashCountdown = GetNode<Timer>("DashCountdown");
 | 
			
		||||
        if (PlayerInput.IsDashing && dashCountdown.IsStopped())
 | 
			
		||||
        {
 | 
			
		||||
            PlayerInput.IsDashing = false;
 | 
			
		||||
            Velocity *= PlayerDashAcceleration;
 | 
			
		||||
            dashCountdown.Start();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Position += Velocity * (float)delta;
 | 
			
		||||
        MoveAndSlide();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void TakeDamage(double damage)
 | 
			
		||||
    {
 | 
			
		||||
        Rpc(nameof(GotDamage), damage);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
 | 
			
		||||
    private void GotDamage(double damage)
 | 
			
		||||
    {
 | 
			
		||||
        Health -= damage;
 | 
			
		||||
 | 
			
		||||
        var bar = GetNode<ProgressBar>("HUD/HealthBar");
 | 
			
		||||
        bar.Value = Health / MaxHealth * 100;
 | 
			
		||||
 | 
			
		||||
        var shakableCamera = GetNode<CameraShake>("Camera2D");
 | 
			
		||||
        shakableCamera.AddTrauma(0.5f);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    [Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
 | 
			
		||||
    private void Shoot()
 | 
			
		||||
    {
 | 
			
		||||
        var marker = GetNode<Marker2D>("RotationCentre/Muzzle");
 | 
			
		||||
        var projectile = BulletScene.Instantiate<Bullet>();
 | 
			
		||||
        projectile.Transform = marker.GlobalTransform;
 | 
			
		||||
        projectile.PlayerId = PlayerId;
 | 
			
		||||
 | 
			
		||||
        GetParent().AddChild(projectile);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user