✨ Speed up round when no body alive
This commit is contained in:
parent
33c6762ed3
commit
dd27528fed
@ -4,152 +4,172 @@ namespace AceField.Scripts.Logic;
|
|||||||
|
|
||||||
public partial class World : Node2D
|
public partial class World : Node2D
|
||||||
{
|
{
|
||||||
[Export] public Scoreboard Scoreboard;
|
[Export] public Scoreboard Scoreboard;
|
||||||
[Export] public PackedScene PlayerScene;
|
[Export] public PackedScene PlayerScene;
|
||||||
|
|
||||||
[Export] public int RoundCount = 1;
|
[Export] public int RoundCount = 1;
|
||||||
[Export] public int RoundTotalCount = 20;
|
[Export] public int RoundTotalCount = 20;
|
||||||
[Export] public double RoundDuration = 60;
|
[Export] public double RoundDuration = 60;
|
||||||
[Export] public double RoundProgress;
|
[Export] public double RoundProgress;
|
||||||
[Export] public double RoundTimeLeft;
|
[Export] public double RoundTimeLeft;
|
||||||
|
|
||||||
private Timer _roundTimer;
|
private Timer _roundTimer;
|
||||||
|
|
||||||
public void StartGame(string currentPlayerName = null)
|
public void StartGame(string currentPlayerName = null)
|
||||||
{
|
{
|
||||||
if (!Multiplayer.IsServer())
|
if (!Multiplayer.IsServer())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_roundTimer = new Timer();
|
_roundTimer = new Timer();
|
||||||
_roundTimer.WaitTime = RoundDuration;
|
_roundTimer.WaitTime = RoundDuration;
|
||||||
_roundTimer.Autostart = true;
|
_roundTimer.Autostart = true;
|
||||||
_roundTimer.Timeout += NewRound;
|
_roundTimer.Timeout += NewRound;
|
||||||
_roundTimer.OneShot = true;
|
_roundTimer.OneShot = true;
|
||||||
AddChild(_roundTimer);
|
GetParent().AddChild(_roundTimer);
|
||||||
_roundTimer.Start();
|
_roundTimer.Start(RoundDuration);
|
||||||
|
|
||||||
// Add players into the game
|
// Add players into the game
|
||||||
PutPlayers(currentPlayerName);
|
PutPlayers(currentPlayerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
if (!Multiplayer.IsServer())
|
if (!Multiplayer.IsServer())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Handling player connect / disconnect after this client connected
|
// Handling player connect / disconnect after this client connected
|
||||||
Multiplayer.PeerDisconnected += RemovePlayer_Adaptor;
|
Multiplayer.PeerDisconnected += RemovePlayer_Adaptor;
|
||||||
Multiplayer.PeerConnected += AddPlayer_Adaptor;
|
Multiplayer.PeerConnected += AddPlayer_Adaptor;
|
||||||
|
|
||||||
// Handling player connected before this client
|
// Handling player connected before this client
|
||||||
foreach (var id in Multiplayer.GetPeers())
|
foreach (var id in Multiplayer.GetPeers())
|
||||||
Scoreboard.AddPlayer(id);
|
Scoreboard.AddPlayer(id);
|
||||||
|
|
||||||
// Add this client as a player if client isn't a dedicated server
|
// Add this client as a player if client isn't a dedicated server
|
||||||
if (!OS.HasFeature("dedicated_server"))
|
if (!OS.HasFeature("dedicated_server"))
|
||||||
Scoreboard.AddPlayer(1);
|
Scoreboard.AddPlayer(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _ExitTree()
|
public override void _ExitTree()
|
||||||
{
|
{
|
||||||
if (!Multiplayer.IsServer())
|
if (!Multiplayer.IsServer())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Multiplayer.PeerDisconnected -= RemovePlayer_Adaptor;
|
Multiplayer.PeerDisconnected -= RemovePlayer_Adaptor;
|
||||||
Multiplayer.PeerConnected -= AddPlayer_Adaptor;
|
Multiplayer.PeerConnected -= AddPlayer_Adaptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _Process(double delta)
|
public override void _Process(double delta)
|
||||||
{
|
{
|
||||||
if (Multiplayer.IsServer())
|
if (Multiplayer.IsServer())
|
||||||
{
|
{
|
||||||
RoundProgress = _roundTimer.TimeLeft / _roundTimer.WaitTime;
|
RoundProgress = _roundTimer.TimeLeft / _roundTimer.WaitTime;
|
||||||
RoundTimeLeft = _roundTimer.TimeLeft;
|
RoundTimeLeft = _roundTimer.TimeLeft;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string BuildPlayerName(int id)
|
private static string BuildPlayerName(int id)
|
||||||
=> $"Player@{id}";
|
=> $"Player@{id}";
|
||||||
|
|
||||||
private void AddPlayer_Adaptor(long id)
|
private void AddPlayer_Adaptor(long id)
|
||||||
=> Scoreboard.AddPlayer((int)id);
|
=> Scoreboard.AddPlayer((int)id);
|
||||||
|
|
||||||
private void RemovePlayer_Adaptor(long id)
|
private void RemovePlayer_Adaptor(long id)
|
||||||
=> Scoreboard.RemovePlayer((int)id);
|
=> Scoreboard.RemovePlayer((int)id);
|
||||||
|
|
||||||
private void SpawnPlayer(int id, string name = null)
|
private void SpawnPlayer(int id, string name = null)
|
||||||
{
|
{
|
||||||
var player = PlayerScene.Instantiate<Player>();
|
var player = PlayerScene.Instantiate<Player>();
|
||||||
player.PlayerId = id;
|
player.PlayerId = id;
|
||||||
player.Position = new Vector2(GD.RandRange(-100, 100), GD.RandRange(-100, 100));
|
player.Position = new Vector2(GD.RandRange(-100, 100), GD.RandRange(-100, 100));
|
||||||
player.Name = BuildPlayerName(id);
|
player.Name = BuildPlayerName(id);
|
||||||
player.PlayerDied += (killerId) =>
|
player.PlayerDied += (killerId) =>
|
||||||
{
|
{
|
||||||
if (killerId == 0) return;
|
if (killerId == 0) return;
|
||||||
Scoreboard.AddScore(killerId);
|
Scoreboard.AddScore(killerId);
|
||||||
};
|
if (CountPlayer() <= 2)
|
||||||
|
_roundTimer.Start(3);
|
||||||
|
};
|
||||||
|
|
||||||
AddChild(player, true);
|
AddChild(player, true);
|
||||||
player.PlayerName = name;
|
player.PlayerName = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void PutPlayers(string currentPlayerName = null)
|
private void PutPlayers(string currentPlayerName = null)
|
||||||
{
|
{
|
||||||
// Spawn clients
|
// Spawn clients
|
||||||
foreach (var id in Multiplayer.GetPeers())
|
foreach (var id in Multiplayer.GetPeers())
|
||||||
SpawnPlayer(id, Scoreboard.Players[id]?.Name);
|
SpawnPlayer(id, Scoreboard.Players[id]?.Name);
|
||||||
|
|
||||||
// Spawn host
|
// Spawn host
|
||||||
if (!OS.HasFeature("dedicated_server"))
|
if (!OS.HasFeature("dedicated_server"))
|
||||||
SpawnPlayer(1, currentPlayerName ?? Scoreboard.Players[1]?.Name);
|
SpawnPlayer(1, currentPlayerName ?? Scoreboard.Players[1]?.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void NewRound()
|
private void NewRound()
|
||||||
{
|
{
|
||||||
if (RoundCount >= RoundTotalCount)
|
if (RoundCount >= RoundTotalCount)
|
||||||
{
|
{
|
||||||
Rpc(nameof(EndGame));
|
Rpc(nameof(EndGame));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
RoundCount++;
|
|
||||||
foreach (var child in GetChildren())
|
|
||||||
{
|
|
||||||
if (child is not Timer)
|
|
||||||
child.QueueFree();
|
|
||||||
}
|
|
||||||
|
|
||||||
PutPlayers();
|
RoundCount++;
|
||||||
_roundTimer.Start();
|
Rpc(nameof(CleanWorld));
|
||||||
}
|
|
||||||
|
|
||||||
public Player GetCurrentPlayer()
|
PutPlayers();
|
||||||
{
|
_roundTimer.Start(RoundDuration);
|
||||||
foreach (var child in GetChildren())
|
}
|
||||||
{
|
|
||||||
if (child is Player { IsCurrentPlayer: true } player)
|
|
||||||
return player;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
public Player GetCurrentPlayer()
|
||||||
}
|
{
|
||||||
|
foreach (var child in GetChildren())
|
||||||
|
{
|
||||||
|
if (child is Player { IsCurrentPlayer: true } player)
|
||||||
|
return player;
|
||||||
|
}
|
||||||
|
|
||||||
public T GetTileByPosition<T>(Vector2 position) where T : Node2D
|
return null;
|
||||||
{
|
}
|
||||||
foreach (var item in GetChildren())
|
|
||||||
{
|
|
||||||
if (item is T tile && tile.Position == position)
|
|
||||||
return tile;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
public int CountPlayer()
|
||||||
}
|
{
|
||||||
|
var count = 0;
|
||||||
|
foreach (var child in GetChildren())
|
||||||
|
{
|
||||||
|
if (child is Player)
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
|
return count;
|
||||||
private void EndGame()
|
}
|
||||||
{
|
|
||||||
GetParent().GetNode<Control>("OverlayLayer/Hud").Hide();
|
public T GetTileByPosition<T>(Vector2 position) where T : Node2D
|
||||||
GetParent().GetNode<Control>("OverlayLayer/GameOverScreen").Show();
|
{
|
||||||
GetTree().Paused = true;
|
foreach (var item in GetChildren())
|
||||||
}
|
{
|
||||||
}
|
if (item is T tile && tile.Position == position)
|
||||||
|
return tile;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
|
||||||
|
private void CleanWorld()
|
||||||
|
{
|
||||||
|
foreach (var child in GetChildren())
|
||||||
|
{
|
||||||
|
child.QueueFree();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
|
||||||
|
private void EndGame()
|
||||||
|
{
|
||||||
|
GetParent().GetNode<Control>("OverlayLayer/Hud").Hide();
|
||||||
|
GetParent().GetNode<Control>("OverlayLayer/GameOverScreen").Show();
|
||||||
|
GetTree().Paused = true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user