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.
This commit is contained in:
Mustafa Alperen Seki
2018-10-25 21:15:56 +03:00
committed by reaperrr
parent ea4f24d0b7
commit 9fef2c01ec
8 changed files with 109 additions and 15 deletions

View File

@@ -11,7 +11,7 @@
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
using OpenRA.Mods.Common.Traits;
namespace OpenRA.Mods.Common.Widgets
{

View File

@@ -280,6 +280,7 @@
<Compile Include="Traits\Air\FlyAwayOnIdle.cs" />
<Compile Include="Traits\Air\FallsToEarth.cs" />
<Compile Include="Traits\Air\ReturnOnIdle.cs" />
<Compile Include="Traits\AppearsOnMapPreview.cs" />
<Compile Include="Traits\Armament.cs" />
<Compile Include="Traits\Armor.cs" />
<Compile Include="Traits\AttackMove.cs" />
@@ -565,6 +566,7 @@
<Compile Include="Traits\World\Locomotor.cs" />
<Compile Include="Traits\World\JumpjetLocomotor.cs" />
<Compile Include="Traits\World\PaletteFromEmbeddedSpritePalette.cs" />
<Compile Include="Traits\World\ResourceType.cs" />
<Compile Include="Traits\World\SubterraneanLocomotor.cs" />
<Compile Include="Traits\World\LoadWidgetAtGameStart.cs" />
<Compile Include="Traits\World\MPStartLocations.cs" />

View File

@@ -0,0 +1,59 @@
#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 { }
}

View File

@@ -0,0 +1,111 @@
#region Copyright & License Information
/*
* Copyright 2007-2019 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 OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class ResourceTypeInfo : ITraitInfo, IMapPreviewSignatureInfo
{
[Desc("Sequence image that holds the different variants.")]
public readonly string Image = "resources";
[FieldLoader.Require]
[SequenceReference("Image")]
[Desc("Randomly chosen image sequences.")]
public readonly string[] Sequences = { };
[PaletteReference]
[Desc("Palette used for rendering the resource sprites.")]
public readonly string Palette = TileSet.TerrainPaletteInternalName;
[Desc("Resource index used in the binary map data.")]
public readonly int ResourceType = 1;
[Desc("Credit value of a single resource unit.")]
public readonly int ValuePerUnit = 0;
[Desc("Maximum number of resource units allowed in a single cell.")]
public readonly int MaxDensity = 10;
[FieldLoader.Require]
[Desc("Resource identifier used by other traits.")]
public readonly string Type = null;
[FieldLoader.Require]
[Desc("Resource name used by tooltips.")]
public readonly string Name = null;
[FieldLoader.Require]
[Desc("Terrain type used to determine unit movement and minimap colors.")]
public readonly string TerrainType = null;
[Desc("Terrain types that this resource can spawn on.")]
public readonly HashSet<string> AllowedTerrainTypes = new HashSet<string>();
[Desc("Allow resource to spawn under Mobile actors.")]
public readonly bool AllowUnderActors = false;
[Desc("Allow resource to spawn under Buildings.")]
public readonly bool AllowUnderBuildings = false;
[Desc("Allow resource to spawn on ramp tiles.")]
public readonly bool AllowOnRamps = false;
[Desc("Harvester content pip color.")]
public PipType PipColor = PipType.Yellow;
void IMapPreviewSignatureInfo.PopulateMapPreviewSignatureCells(Map map, ActorInfo ai, ActorReference s, List<Pair<MPos, Color>> destinationBuffer)
{
var tileSet = map.Rules.TileSet;
var color = tileSet[tileSet.GetTerrainIndex(TerrainType)].Color;
for (var i = 0; i < map.MapSize.X; i++)
{
for (var j = 0; j < map.MapSize.Y; j++)
{
var cell = new MPos(i, j);
if (map.Resources[cell].Type == ResourceType)
destinationBuffer.Add(new Pair<MPos, Color>(cell, color));
}
}
}
public object Create(ActorInitializer init) { return new ResourceType(this, init.World); }
}
public class ResourceType : IWorldLoaded
{
public readonly ResourceTypeInfo Info;
public PaletteReference Palette { get; private set; }
public readonly Dictionary<string, Sprite[]> Variants;
public ResourceType(ResourceTypeInfo info, World world)
{
Info = info;
Variants = new Dictionary<string, Sprite[]>();
foreach (var v in info.Sequences)
{
var seq = world.Map.Rules.Sequences.GetSequence(Info.Image, v);
var sprites = Exts.MakeArray(seq.Length, x => seq.GetSprite(x));
Variants.Add(v, sprites);
}
}
public void WorldLoaded(World w, WorldRenderer wr)
{
Palette = wr.Palette(Info.Palette);
}
}
}

View File

@@ -11,7 +11,7 @@
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic