Refactor player setup.

Tweak how players in a world are created. Change the collection to be an array to more strongly imply it will not change during a game.
This commit is contained in:
RoosterDragon
2015-11-27 15:00:29 +00:00
parent 3e8df55bcb
commit 23a38a08f7
3 changed files with 26 additions and 12 deletions

View File

@@ -38,9 +38,14 @@ namespace OpenRA
public readonly MersenneTwister SharedRandom; public readonly MersenneTwister SharedRandom;
public readonly List<Player> Players = new List<Player>(); public Player[] Players = new Player[0];
public void SetPlayers(IEnumerable<Player> players, Player localPlayer)
{
Players = players.ToArray();
SetLocalPlayer(localPlayer);
}
public void AddPlayer(Player p) { Players.Add(p); }
public Player LocalPlayer { get; private set; } public Player LocalPlayer { get; private set; }
public event Action GameOver = () => { }; public event Action GameOver = () => { };
@@ -82,12 +87,18 @@ namespace OpenRA
get { return LobbyInfo.GlobalSettings.AllowCheats || LobbyInfo.IsSinglePlayer; } get { return LobbyInfo.GlobalSettings.AllowCheats || LobbyInfo.IsSinglePlayer; }
} }
public void SetLocalPlayer(string pr) void SetLocalPlayer(Player localPlayer)
{ {
if (localPlayer == null)
return;
if (!Players.Contains(localPlayer))
throw new ArgumentException("The local player must be one of the players in the world.", "localPlayer");
if (IsReplay) if (IsReplay)
return; return;
LocalPlayer = Players.FirstOrDefault(p => p.InternalName == pr); LocalPlayer = localPlayer;
RenderPlayer = LocalPlayer; RenderPlayer = LocalPlayer;
} }

View File

@@ -8,6 +8,7 @@
*/ */
#endregion #endregion
using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Network; using OpenRA.Network;
using OpenRA.Traits; using OpenRA.Traits;
@@ -22,16 +23,19 @@ namespace OpenRA.Mods.Common.Traits
public void CreatePlayers(World w) public void CreatePlayers(World w)
{ {
var players = new MapPlayers(w.Map.PlayerDefinitions).Players; var players = new MapPlayers(w.Map.PlayerDefinitions).Players;
var worldPlayers = new List<Player>();
// Create the unplayable map players -- neutral, shellmap, scripted, etc. // Create the unplayable map players -- neutral, shellmap, scripted, etc.
foreach (var kv in players.Where(p => !p.Value.Playable)) foreach (var kv in players.Where(p => !p.Value.Playable))
{ {
var player = new Player(w, null, kv.Value); var player = new Player(w, null, kv.Value);
w.AddPlayer(player); worldPlayers.Add(player);
if (kv.Value.OwnsWorld) if (kv.Value.OwnsWorld)
w.WorldActor.Owner = player; w.WorldActor.Owner = player;
} }
Player localPlayer = null;
// Create the regular playable players. // Create the regular playable players.
foreach (var kv in w.LobbyInfo.Slots) foreach (var kv in w.LobbyInfo.Slots)
{ {
@@ -40,22 +44,24 @@ namespace OpenRA.Mods.Common.Traits
continue; continue;
var player = new Player(w, client, players[kv.Value.PlayerReference]); var player = new Player(w, client, players[kv.Value.PlayerReference]);
w.AddPlayer(player); worldPlayers.Add(player);
if (client.Index == Game.LocalClientId) if (client.Index == Game.LocalClientId)
w.SetLocalPlayer(player.InternalName); localPlayer = player;
} }
// Create a player that is allied with everyone for shared observer shroud. // Create a player that is allied with everyone for shared observer shroud.
w.AddPlayer(new Player(w, null, new PlayerReference worldPlayers.Add(new Player(w, null, new PlayerReference
{ {
Name = "Everyone", Name = "Everyone",
NonCombatant = true, NonCombatant = true,
Spectating = true, Spectating = true,
Faction = "Random", Faction = "Random",
Allies = w.Players.Where(p => !p.NonCombatant && p.Playable).Select(p => p.InternalName).ToArray() Allies = worldPlayers.Where(p => !p.NonCombatant && p.Playable).Select(p => p.InternalName).ToArray()
})); }));
w.SetPlayers(worldPlayers, localPlayer);
foreach (var p in w.Players) foreach (var p in w.Players)
foreach (var q in w.Players) foreach (var q in w.Players)
if (!p.Stances.ContainsKey(q)) if (!p.Stances.ContainsKey(q))

View File

@@ -164,10 +164,7 @@ namespace OpenRA.Mods.Common.Traits
var index = int.Parse(name.Substring(5)); var index = int.Parse(name.Substring(5));
if (index >= newCount) if (index >= newCount)
{
Players.Players.Remove(name); Players.Players.Remove(name);
worldRenderer.World.Players.RemoveAll(pp => pp.InternalName == name);
}
} }
for (var index = 0; index < newCount; index++) for (var index = 0; index < newCount; index++)