Streamline SpawnOccupant management.
This commit is contained in:
@@ -62,6 +62,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
bool disableTeamChat;
|
bool disableTeamChat;
|
||||||
bool teamChat;
|
bool teamChat;
|
||||||
bool updateDiscordStatus = true;
|
bool updateDiscordStatus = true;
|
||||||
|
Dictionary<int, SpawnOccupant> spawnOccupants = new Dictionary<int, SpawnOccupant>();
|
||||||
|
|
||||||
readonly string chatLineSound = ChromeMetrics.Get<string>("ChatLineSound");
|
readonly string chatLineSound = ChromeMetrics.Get<string>("ChatLineSound");
|
||||||
|
|
||||||
@@ -118,6 +119,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
Game.LobbyInfoChanged += UpdateCurrentMap;
|
Game.LobbyInfoChanged += UpdateCurrentMap;
|
||||||
Game.LobbyInfoChanged += UpdatePlayerList;
|
Game.LobbyInfoChanged += UpdatePlayerList;
|
||||||
Game.LobbyInfoChanged += UpdateDiscordStatus;
|
Game.LobbyInfoChanged += UpdateDiscordStatus;
|
||||||
|
Game.LobbyInfoChanged += UpdateSpawnOccupants;
|
||||||
Game.BeforeGameStart += OnGameStart;
|
Game.BeforeGameStart += OnGameStart;
|
||||||
Game.ConnectionStateChanged += ConnectionStateChanged;
|
Game.ConnectionStateChanged += ConnectionStateChanged;
|
||||||
|
|
||||||
@@ -133,10 +135,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
"onMouseDown", (Action<MapPreviewWidget, MapPreview, MouseInput>)((preview, mapPreview, mi) =>
|
"onMouseDown", (Action<MapPreviewWidget, MapPreview, MouseInput>)((preview, mapPreview, mi) =>
|
||||||
LobbyUtils.SelectSpawnPoint(orderManager, preview, mapPreview, mi))
|
LobbyUtils.SelectSpawnPoint(orderManager, preview, mapPreview, mi))
|
||||||
},
|
},
|
||||||
{
|
{ "getSpawnOccupants", (Func<Dictionary<int, SpawnOccupant>>)(() => spawnOccupants) },
|
||||||
"getSpawnOccupants", (Func<MapPreview, Dictionary<CPos, SpawnOccupant>>)(mapPreview =>
|
|
||||||
LobbyUtils.GetSpawnOccupants(orderManager.LobbyInfo, mapPreview))
|
|
||||||
},
|
|
||||||
{ "showUnoccupiedSpawnpoints", true },
|
{ "showUnoccupiedSpawnpoints", true },
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -464,6 +463,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
Game.LobbyInfoChanged -= UpdateCurrentMap;
|
Game.LobbyInfoChanged -= UpdateCurrentMap;
|
||||||
Game.LobbyInfoChanged -= UpdatePlayerList;
|
Game.LobbyInfoChanged -= UpdatePlayerList;
|
||||||
Game.LobbyInfoChanged -= UpdateDiscordStatus;
|
Game.LobbyInfoChanged -= UpdateDiscordStatus;
|
||||||
|
Game.LobbyInfoChanged -= UpdateSpawnOccupants;
|
||||||
Game.BeforeGameStart -= OnGameStart;
|
Game.BeforeGameStart -= OnGameStart;
|
||||||
Game.ConnectionStateChanged -= ConnectionStateChanged;
|
Game.ConnectionStateChanged -= ConnectionStateChanged;
|
||||||
}
|
}
|
||||||
@@ -789,6 +789,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UpdateSpawnOccupants()
|
||||||
|
{
|
||||||
|
spawnOccupants = orderManager.LobbyInfo.Clients
|
||||||
|
.Where(c => c.SpawnPoint != 0)
|
||||||
|
.ToDictionary(c => c.SpawnPoint, c => new SpawnOccupant(c));
|
||||||
|
}
|
||||||
|
|
||||||
void OnGameStart()
|
void OnGameStart()
|
||||||
{
|
{
|
||||||
Ui.CloseWindow();
|
Ui.CloseWindow();
|
||||||
|
|||||||
@@ -227,22 +227,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
color.AttachPanel(colorChooser, onExit);
|
color.AttachPanel(colorChooser, onExit);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Dictionary<CPos, SpawnOccupant> GetSpawnOccupants(Session lobbyInfo, MapPreview preview)
|
|
||||||
{
|
|
||||||
var spawns = preview.SpawnPoints;
|
|
||||||
return lobbyInfo.Clients
|
|
||||||
.Where(c => (c.SpawnPoint - 1 >= 0) && (c.SpawnPoint - 1 < spawns.Length))
|
|
||||||
.ToDictionary(c => spawns[c.SpawnPoint - 1], c => new SpawnOccupant(c));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Dictionary<CPos, SpawnOccupant> GetSpawnOccupants(IEnumerable<GameInformation.Player> players, MapPreview preview)
|
|
||||||
{
|
|
||||||
var spawns = preview.SpawnPoints;
|
|
||||||
return players
|
|
||||||
.Where(c => (c.SpawnPoint - 1 >= 0) && (c.SpawnPoint - 1 < spawns.Length))
|
|
||||||
.ToDictionary(c => spawns[c.SpawnPoint - 1], c => new SpawnOccupant(c));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void SelectSpawnPoint(OrderManager orderManager, MapPreviewWidget mapPreview, MapPreview preview, MouseInput mi)
|
public static void SelectSpawnPoint(OrderManager orderManager, MapPreviewWidget mapPreview, MapPreview preview, MouseInput mi)
|
||||||
{
|
{
|
||||||
if (mi.Button != MouseButton.Left)
|
if (mi.Button != MouseButton.Left)
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
|
|
||||||
[ObjectCreator.UseCtor]
|
[ObjectCreator.UseCtor]
|
||||||
internal MapPreviewLogic(Widget widget, ModData modData, OrderManager orderManager, Func<MapPreview> getMap,
|
internal MapPreviewLogic(Widget widget, ModData modData, OrderManager orderManager, Func<MapPreview> getMap,
|
||||||
Action<MapPreviewWidget, MapPreview, MouseInput> onMouseDown, Func<MapPreview, Dictionary<CPos, SpawnOccupant>> getSpawnOccupants, bool showUnoccupiedSpawnpoints)
|
Action<MapPreviewWidget, MapPreview, MouseInput> onMouseDown, Func<Dictionary<int, SpawnOccupant>> getSpawnOccupants, bool showUnoccupiedSpawnpoints)
|
||||||
{
|
{
|
||||||
var mapRepository = modData.Manifest.Get<WebServices>().MapRepository;
|
var mapRepository = modData.Manifest.Get<WebServices>().MapRepository;
|
||||||
|
|
||||||
@@ -172,12 +172,12 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SetupWidgets(Widget parent, Func<MapPreview> getMap,
|
void SetupWidgets(Widget parent, Func<MapPreview> getMap,
|
||||||
Action<MapPreviewWidget, MapPreview, MouseInput> onMouseDown, Func<MapPreview, Dictionary<CPos, SpawnOccupant>> getSpawnOccupants, bool showUnoccupiedSpawnpoints)
|
Action<MapPreviewWidget, MapPreview, MouseInput> onMouseDown, Func<Dictionary<int, SpawnOccupant>> getSpawnOccupants, bool showUnoccupiedSpawnpoints)
|
||||||
{
|
{
|
||||||
var preview = parent.Get<MapPreviewWidget>("MAP_PREVIEW");
|
var preview = parent.Get<MapPreviewWidget>("MAP_PREVIEW");
|
||||||
preview.Preview = () => getMap();
|
preview.Preview = () => getMap();
|
||||||
preview.OnMouseDown = mi => onMouseDown(preview, getMap(), mi);
|
preview.OnMouseDown = mi => onMouseDown(preview, getMap(), mi);
|
||||||
preview.SpawnOccupants = () => getSpawnOccupants(getMap());
|
preview.SpawnOccupants = getSpawnOccupants;
|
||||||
preview.ShowUnoccupiedSpawnpoints = showUnoccupiedSpawnpoints;
|
preview.ShowUnoccupiedSpawnpoints = showUnoccupiedSpawnpoints;
|
||||||
|
|
||||||
var titleLabel = parent.GetOrNull<LabelWithTooltipWidget>("MAP_TITLE");
|
var titleLabel = parent.GetOrNull<LabelWithTooltipWidget>("MAP_TITLE");
|
||||||
|
|||||||
@@ -40,10 +40,17 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
tooltipContainer.BeforeRender = () =>
|
tooltipContainer.BeforeRender = () =>
|
||||||
{
|
{
|
||||||
showTooltip = true;
|
showTooltip = true;
|
||||||
var occupant = preview.SpawnOccupants().Values.FirstOrDefault(c => c.SpawnPoint == preview.TooltipSpawnIndex);
|
|
||||||
|
|
||||||
var teamWidth = 0;
|
var teamWidth = 0;
|
||||||
if (occupant == null)
|
if (preview.SpawnOccupants().TryGetValue(preview.TooltipSpawnIndex, out var occupant))
|
||||||
|
{
|
||||||
|
labelText = occupant.PlayerName;
|
||||||
|
playerFaction = occupant.Faction;
|
||||||
|
playerTeam = occupant.Team;
|
||||||
|
widget.Bounds.Height = playerTeam > 0 ? doubleHeight : singleHeight;
|
||||||
|
teamWidth = teamFont.Measure(team.GetText()).X;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
if (!showUnoccupiedSpawnpoints)
|
if (!showUnoccupiedSpawnpoints)
|
||||||
{
|
{
|
||||||
@@ -56,14 +63,6 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
playerTeam = 0;
|
playerTeam = 0;
|
||||||
widget.Bounds.Height = singleHeight;
|
widget.Bounds.Height = singleHeight;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
labelText = occupant.PlayerName;
|
|
||||||
playerFaction = occupant.Faction;
|
|
||||||
playerTeam = occupant.Team;
|
|
||||||
widget.Bounds.Height = playerTeam > 0 ? doubleHeight : singleHeight;
|
|
||||||
teamWidth = teamFont.Measure(team.GetText()).X;
|
|
||||||
}
|
|
||||||
|
|
||||||
label.Bounds.X = playerFaction != null ? flag.Bounds.Right + labelMargin : labelMargin;
|
label.Bounds.X = playerFaction != null ? flag.Bounds.Right + labelMargin : labelMargin;
|
||||||
label.Bounds.Width = ownerFont.Measure(labelText).X;
|
label.Bounds.Width = ownerFont.Measure(labelText).X;
|
||||||
|
|||||||
@@ -76,15 +76,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
mapPreviewRoot.IsVisible = () => selectedReplay != null;
|
mapPreviewRoot.IsVisible = () => selectedReplay != null;
|
||||||
panel.Get("REPLAY_INFO").IsVisible = () => selectedReplay != null;
|
panel.Get("REPLAY_INFO").IsVisible = () => selectedReplay != null;
|
||||||
|
|
||||||
|
var spawnOccupants = new CachedTransform<ReplayMetadata, Dictionary<int, SpawnOccupant>>(r =>
|
||||||
|
r.GameInfo.Players.ToDictionary(c => c.SpawnPoint, c => new SpawnOccupant(c)));
|
||||||
|
|
||||||
Ui.LoadWidget("MAP_PREVIEW", mapPreviewRoot, new WidgetArgs
|
Ui.LoadWidget("MAP_PREVIEW", mapPreviewRoot, new WidgetArgs
|
||||||
{
|
{
|
||||||
{ "orderManager", null },
|
{ "orderManager", null },
|
||||||
{ "getMap", (Func<MapPreview>)(() => map) },
|
{ "getMap", (Func<MapPreview>)(() => map) },
|
||||||
{ "onMouseDown", (Action<MapPreviewWidget, MapPreview, MouseInput>)((preview, mapPreview, mi) => { }) },
|
{ "onMouseDown", (Action<MapPreviewWidget, MapPreview, MouseInput>)((preview, mapPreview, mi) => { }) },
|
||||||
{
|
{ "getSpawnOccupants", (Func<Dictionary<int, SpawnOccupant>>)(() => spawnOccupants.Update(selectedReplay)) },
|
||||||
"getSpawnOccupants", (Func<MapPreview, Dictionary<CPos, SpawnOccupant>>)(mapPreview =>
|
|
||||||
LobbyUtils.GetSpawnOccupants(selectedReplay.GameInfo.Players, mapPreview))
|
|
||||||
},
|
|
||||||
{ "showUnoccupiedSpawnpoints", false },
|
{ "showUnoccupiedSpawnpoints", false },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -424,7 +424,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
var spawns = currentMap.SpawnPoints;
|
var spawns = currentMap.SpawnPoints;
|
||||||
var occupants = server.Clients
|
var occupants = server.Clients
|
||||||
.Where(c => (c.SpawnPoint - 1 >= 0) && (c.SpawnPoint - 1 < spawns.Length))
|
.Where(c => (c.SpawnPoint - 1 >= 0) && (c.SpawnPoint - 1 < spawns.Length))
|
||||||
.ToDictionary(c => spawns[c.SpawnPoint - 1], c => new SpawnOccupant(c, server.Mod != modData.Manifest.Id));
|
.ToDictionary(c => c.SpawnPoint, c => new SpawnOccupant(c, server.Mod != modData.Manifest.Id));
|
||||||
|
|
||||||
mapPreview.SpawnOccupants = () => occupants;
|
mapPreview.SpawnOccupants = () => occupants;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
readonly int2 spawnLabelOffset;
|
readonly int2 spawnLabelOffset;
|
||||||
|
|
||||||
public Func<MapPreview> Preview = () => null;
|
public Func<MapPreview> Preview = () => null;
|
||||||
public Func<Dictionary<CPos, SpawnOccupant>> SpawnOccupants = () => new Dictionary<CPos, SpawnOccupant>();
|
public Func<Dictionary<int, SpawnOccupant>> SpawnOccupants = () => new Dictionary<int, SpawnOccupant>();
|
||||||
public Action<MouseInput> OnMouseDown = _ => { };
|
public Action<MouseInput> OnMouseDown = _ => { };
|
||||||
public int TooltipSpawnIndex = -1;
|
public int TooltipSpawnIndex = -1;
|
||||||
public bool ShowUnoccupiedSpawnpoints = true;
|
public bool ShowUnoccupiedSpawnpoints = true;
|
||||||
@@ -182,19 +182,21 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
TooltipSpawnIndex = -1;
|
TooltipSpawnIndex = -1;
|
||||||
if (ShowSpawnPoints)
|
if (ShowSpawnPoints)
|
||||||
{
|
{
|
||||||
var colors = SpawnOccupants().ToDictionary(c => c.Key, c => c.Value.Color);
|
|
||||||
|
|
||||||
var spawnPoints = preview.SpawnPoints;
|
var spawnPoints = preview.SpawnPoints;
|
||||||
|
var occupants = SpawnOccupants();
|
||||||
var gridType = preview.GridType;
|
var gridType = preview.GridType;
|
||||||
foreach (var p in spawnPoints)
|
for (var i = 0; i < spawnPoints.Length; i++)
|
||||||
{
|
{
|
||||||
var owned = colors.ContainsKey(p);
|
var p = spawnPoints[i];
|
||||||
|
|
||||||
|
// Spawn numbers are 1 indexed with 0 meaning "random spawn".
|
||||||
|
var occupied = occupants.TryGetValue(i + 1, out var occupant);
|
||||||
var pos = ConvertToPreview(p, gridType);
|
var pos = ConvertToPreview(p, gridType);
|
||||||
var sprite = owned ? spawnClaimed : spawnUnclaimed;
|
var sprite = occupied ? spawnClaimed : spawnUnclaimed;
|
||||||
var offset = sprite.Size.XY.ToInt2() / 2;
|
var offset = sprite.Size.XY.ToInt2() / 2;
|
||||||
|
|
||||||
if (owned)
|
if (occupied)
|
||||||
WidgetUtils.FillEllipseWithColor(new Rectangle(pos.X - offset.X + 1, pos.Y - offset.Y + 1, (int)sprite.Size.X - 2, (int)sprite.Size.Y - 2), colors[p]);
|
WidgetUtils.FillEllipseWithColor(new Rectangle(pos.X - offset.X + 1, pos.Y - offset.Y + 1, (int)sprite.Size.X - 2, (int)sprite.Size.Y - 2), occupant.Color);
|
||||||
|
|
||||||
Game.Renderer.RgbaSpriteRenderer.DrawSprite(sprite, pos - offset);
|
Game.Renderer.RgbaSpriteRenderer.DrawSprite(sprite, pos - offset);
|
||||||
var number = Convert.ToChar('A' + spawnPoints.IndexOf(p)).ToString();
|
var number = Convert.ToChar('A' + spawnPoints.IndexOf(p)).ToString();
|
||||||
|
|||||||
Reference in New Issue
Block a user