✨ Player name
This commit is contained in:
@ -30,13 +30,13 @@ public partial class Launcher : Node
|
||||
}
|
||||
}
|
||||
|
||||
public void StartAsSingle()
|
||||
public void StartAsSingle(string currentPlayerName = null)
|
||||
{
|
||||
GameUnfreeze();
|
||||
World.StartGame();
|
||||
World.StartGame(currentPlayerName);
|
||||
}
|
||||
|
||||
public bool StartAsServer(int port)
|
||||
public bool StartAsServer(int port, string currentPlayerName = null)
|
||||
{
|
||||
var peer = new ENetMultiplayerPeer();
|
||||
peer.CreateServer(port);
|
||||
@ -51,12 +51,12 @@ public partial class Launcher : Node
|
||||
|
||||
Multiplayer.MultiplayerPeer = peer;
|
||||
GameUnfreeze();
|
||||
World.StartGame();
|
||||
World.StartGame(currentPlayerName);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool StartAsClient(string addr, int port)
|
||||
public bool StartAsClient(string addr, int port, string currentPlayerName = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(addr)) return false;
|
||||
|
||||
@ -74,7 +74,7 @@ public partial class Launcher : Node
|
||||
|
||||
Multiplayer.MultiplayerPeer = peer;
|
||||
GameUnfreeze();
|
||||
World.StartGame();
|
||||
World.StartGame(currentPlayerName);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ public partial class World : Node2D
|
||||
{
|
||||
[Export] public PackedScene PlayerScene;
|
||||
|
||||
public void StartGame()
|
||||
public void StartGame(string currentPlayerName = null)
|
||||
{
|
||||
if (!Multiplayer.IsServer())
|
||||
return;
|
||||
@ -21,7 +21,7 @@ public partial class World : Node2D
|
||||
|
||||
// Add this client as a player if client isn't a dedicated server
|
||||
if (!OS.HasFeature("dedicated_server"))
|
||||
AddPlayer(1);
|
||||
AddPlayer(1, currentPlayerName);
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
@ -44,13 +44,14 @@ public partial class World : Node2D
|
||||
private void RemovePlayer_Adaptor(long id)
|
||||
=> RemovePlayer((int)id);
|
||||
|
||||
private void AddPlayer(int id)
|
||||
private void AddPlayer(int id, string name = null)
|
||||
{
|
||||
var player = PlayerScene.Instantiate<AceField.Scripts.Player>();
|
||||
player.SetPlayerId(id);
|
||||
var player = PlayerScene.Instantiate<Player>();
|
||||
player.PlayerId = id;
|
||||
var position = Vector2.FromAngle(GD.Randf() * 2 * Mathf.Pi);
|
||||
player.Position = new Vector2(position.X * 5f * GD.Randf(), position.Y * 5f * GD.Randf());
|
||||
player.Name = BuildPlayerName(id);
|
||||
player.PlayerName = name;
|
||||
|
||||
AddChild(player, true);
|
||||
}
|
||||
@ -63,4 +64,17 @@ public partial class World : Node2D
|
||||
|
||||
GetNode(name).QueueFree();
|
||||
}
|
||||
|
||||
public Player GetCurrentPlayer()
|
||||
{
|
||||
foreach(var child in GetChildren())
|
||||
{
|
||||
if (child is Player { IsCurrentPlayer: true } player)
|
||||
{
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -6,132 +6,130 @@ 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 double AmmoAmount = 30;
|
||||
[Export] public double MaxAmmoAmount = 30;
|
||||
|
||||
[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 string PlayerName;
|
||||
[Export] public string PlayerName;
|
||||
|
||||
[Export]
|
||||
public int PlayerId
|
||||
{
|
||||
get => _currentPlayerId;
|
||||
set
|
||||
{
|
||||
_currentPlayerId = value;
|
||||
PlayerInput.SetMultiplayerAuthority(value);
|
||||
}
|
||||
}
|
||||
[Export]
|
||||
public int PlayerId
|
||||
{
|
||||
get => _currentPlayerId;
|
||||
set
|
||||
{
|
||||
_currentPlayerId = value;
|
||||
PlayerInput.SetMultiplayerAuthority(value);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetPlayerId(int id)
|
||||
{
|
||||
PlayerId = id;
|
||||
PlayerInput.SetMultiplayerAuthority(id);
|
||||
}
|
||||
public bool IsCurrentPlayer => _currentPlayerId == Multiplayer.GetUniqueId();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Health = MaxHealth;
|
||||
ActionPoints = MaxActionPoints;
|
||||
public override void _Ready()
|
||||
{
|
||||
Health = MaxHealth;
|
||||
ActionPoints = MaxActionPoints;
|
||||
|
||||
if (PlayerId == Multiplayer.GetUniqueId())
|
||||
PlayerCamera.Enabled = true;
|
||||
if (PlayerId == Multiplayer.GetUniqueId())
|
||||
PlayerCamera.Enabled = true;
|
||||
|
||||
PlayerName = $"Player#{PlayerId}"; // TODO Remove mock data
|
||||
GetNode<Label>("HUD/NameTag").Text = PlayerName;
|
||||
PlayerName ??= $"Player#{PlayerId}";
|
||||
GetNode<Label>("Overlay/NameTag").Text = PlayerName;
|
||||
|
||||
GetNode<ProgressBar>("HUD/HealthBar").Value = Health / MaxHealth * 100;
|
||||
}
|
||||
GetNode<ProgressBar>("Overlay/HealthBar").Value = Health / MaxHealth * 100;
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (PlayerInput.IsShooting)
|
||||
{
|
||||
Rpc(nameof(Shoot));
|
||||
PlayerInput.IsShooting = false;
|
||||
}
|
||||
}
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (PlayerInput.IsShooting)
|
||||
{
|
||||
Rpc(nameof(Shoot));
|
||||
PlayerInput.IsShooting = false;
|
||||
}
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
var input = PlayerInput.MovementDirection;
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
var input = PlayerInput.MovementDirection;
|
||||
|
||||
if (input != Vector2.Zero)
|
||||
{
|
||||
input = input.Normalized();
|
||||
Velocity = Velocity.MoveToward(input * MaxSpeed, Acceleration * (float)delta);
|
||||
if (input != Vector2.Zero)
|
||||
{
|
||||
input = input.Normalized();
|
||||
Velocity = Velocity.MoveToward(input * MaxSpeed, Acceleration * (float)delta);
|
||||
|
||||
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 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();
|
||||
}
|
||||
var dashCountdown = GetNode<Timer>("DashCountdown");
|
||||
if (PlayerInput.IsDashing && dashCountdown.IsStopped())
|
||||
{
|
||||
PlayerInput.IsDashing = false;
|
||||
Velocity *= PlayerDashAcceleration;
|
||||
dashCountdown.Start();
|
||||
}
|
||||
|
||||
Position += Velocity * (float)delta;
|
||||
MoveAndSlide();
|
||||
}
|
||||
Position += Velocity * (float)delta;
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
public void TakeDamage(double damage)
|
||||
{
|
||||
Rpc(nameof(GotDamage), damage);
|
||||
}
|
||||
public void TakeDamage(double damage)
|
||||
{
|
||||
Rpc(nameof(GotDamage), damage);
|
||||
}
|
||||
|
||||
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
|
||||
private void GotDamage(double damage)
|
||||
{
|
||||
var protection = GetNode<Timer>("ProtectionCountdown");
|
||||
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
|
||||
private void GotDamage(double damage)
|
||||
{
|
||||
var protection = GetNode<Timer>("ProtectionCountdown");
|
||||
|
||||
if (protection.IsStopped())
|
||||
{
|
||||
Health -= damage;
|
||||
if (protection.IsStopped())
|
||||
{
|
||||
Health -= damage;
|
||||
|
||||
var tween = CreateTween();
|
||||
var bar = GetNode<ProgressBar>("HUD/HealthBar");
|
||||
tween.TweenProperty(bar, "value", Health / MaxHealth * 100, 0.3);
|
||||
|
||||
protection.Start();
|
||||
}
|
||||
var tween = CreateTween();
|
||||
var bar = GetNode<ProgressBar>("Overlay/HealthBar");
|
||||
tween.TweenProperty(bar, "value", Health / MaxHealth * 100, 0.3);
|
||||
|
||||
var shakableCamera = GetNode<CameraShake>("Camera2D");
|
||||
shakableCamera.AddTrauma(0.5f);
|
||||
}
|
||||
protection.Start();
|
||||
}
|
||||
|
||||
[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;
|
||||
var shakableCamera = GetNode<CameraShake>("Camera2D");
|
||||
shakableCamera.AddTrauma(0.5f);
|
||||
}
|
||||
|
||||
GetParent().AddChild(projectile);
|
||||
}
|
||||
}
|
||||
[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);
|
||||
}
|
||||
}
|
||||
|
@ -2,16 +2,16 @@ using Godot;
|
||||
|
||||
namespace AceField.Scripts.UI;
|
||||
|
||||
public partial class StartScreen : Control
|
||||
public partial class LaunchScreen : Control
|
||||
{
|
||||
[Export] public int DefaultServerPort = 4343;
|
||||
[Export] public string DefaultServerAddr = "127.0.0.1";
|
||||
|
||||
[Export] public Launcher Launcher;
|
||||
|
||||
[Export] public LineEdit PlayerNameInput;
|
||||
[Export] public LineEdit ServerPortInput;
|
||||
[Export] public LineEdit ServerAddrInput;
|
||||
[Export] public Button StartAsSingleButton;
|
||||
[Export] public Button StartAsServerButton;
|
||||
[Export] public Button StartAsClientButton;
|
||||
|
||||
@ -42,11 +42,6 @@ public partial class StartScreen : Control
|
||||
ServerPortInput.Text = DefaultServerPort.ToString();
|
||||
ServerAddrInput.Text = DefaultServerAddr;
|
||||
|
||||
StartAsSingleButton.Pressed += () =>
|
||||
{
|
||||
Launcher.StartAsSingle();
|
||||
Hide();
|
||||
};
|
||||
StartAsServerButton.Pressed += () =>
|
||||
{
|
||||
if (!DoValidation())
|
||||
@ -56,8 +51,9 @@ public partial class StartScreen : Control
|
||||
}
|
||||
|
||||
var port = ServerPortInput.Text;
|
||||
var result = Launcher.StartAsServer(int.Parse(port));
|
||||
|
||||
var name = string.IsNullOrEmpty(PlayerNameInput.Text) ? null : PlayerNameInput.Text;
|
||||
var result = Launcher.StartAsServer(int.Parse(port), currentPlayerName: name);
|
||||
|
||||
if (result)
|
||||
Hide();
|
||||
};
|
||||
@ -71,7 +67,8 @@ public partial class StartScreen : Control
|
||||
|
||||
var addr = ServerAddrInput.Text;
|
||||
var port = ServerPortInput.Text;
|
||||
var result = Launcher.StartAsClient(addr, int.Parse(port));
|
||||
var name = string.IsNullOrEmpty(PlayerNameInput.Text) ? null : PlayerNameInput.Text;
|
||||
var result = Launcher.StartAsClient(addr, int.Parse(port), currentPlayerName: name);
|
||||
|
||||
if (result)
|
||||
Hide();
|
Reference in New Issue
Block a user