Files
OpenRA/OpenRA.Mods.Common/Traits/AppearsOnMapPreview.cs
Mustafa Alperen Seki 9fef2c01ec Add AppearsOnMapPreview
Allows preplaced actors to be rendered on map preview when saving the
map.

Also removes requirements for ResourceLayer in OpenRA.Game and moves it
to OpenRA.Mods.Common.
2019-04-04 19:37:19 +02:00

60 lines
2.1 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Render this actor when creating the minimap while saving the map.")]
public class AppearsOnMapPreviewInfo : TraitInfo<AppearsOnMapPreview>, IMapPreviewSignatureInfo, Requires<IOccupySpaceInfo>
{
[Desc("Use this color to render the actor, instead of owner player color.")]
public readonly Color Color = new Color();
[Desc("Use this terrain color to render the actor, instead of owner player color.",
"Overrides `Color` if both set.")]
public readonly string Terrain = null;
void IMapPreviewSignatureInfo.PopulateMapPreviewSignatureCells(Map map, ActorInfo ai, ActorReference s, List<Pair<MPos, Color>> destinationBuffer)
{
var tileSet = map.Rules.TileSet;
Color color;
if (!string.IsNullOrEmpty(Terrain))
{
color = tileSet[tileSet.GetTerrainIndex(Terrain)].Color;
}
else if (Color != new Color())
{
color = Color;
}
else
{
var owner = map.PlayerDefinitions.Where(p => s.InitDict.Get<OwnerInit>().PlayerName == p.Value.Nodes.First(k => k.Key == "Name").Value.Value).First();
var colorValue = owner.Value.Nodes.Where(n => n.Key == "Color");
var ownerColor = colorValue.Any() ? colorValue.First().Value.Value : "FFFFFF";
Color.TryParse(ownerColor, out color);
}
var ios = ai.TraitInfo<IOccupySpaceInfo>();
var cells = ios.OccupiedCells(ai, s.InitDict.Get<LocationInit>().Value(null));
foreach (var cell in cells)
destinationBuffer.Add(new Pair<MPos, Color>(cell.Key.ToMPos(map), color));
}
}
public class AppearsOnMapPreview { }
}