Files
OpenRA/OpenRA.Game/Map/CellLayer.cs
RoosterDragon 9cd55df584 Ensure editorconfig naming styles align with StyleCop SA13XX style rules.
Aligns the naming conventions defined in editorconfig (dotnet_naming_style, dotnet_naming_symbols, dotnet_naming_rule) which are reported under the IDE1006 rule with the existing StyleCop rules from the SA13XX range.

This ensures the two rulesets agree when rejecting and accepting naming conventions within the IDE, with a few edges cases where only one ruleset can enforce the convention. IDE1006 allows use to specify a naming convention for type parameters, const locals and protected readonly fields which SA13XX cannot enforce. Some StyleCop SA13XX rules such as SA1309 'Field names should not begin with underscore' are not possible to enforce with the naming rules of IDE1006.

Therefore we enable the IDE1006 as a build time warning to enforce conventions and extend them. We disable SA13XX rules that can now be covered by IDE1006 to avoid double-reporting but leave the remaining SA13XX rules that cover additional cases enabled.

We also re-enable the SA1311 rule convention but enforce it via IDE1006, requiring some violations to be fixed or duplication of existing suppressions. Most violations fixes are trivial renames with the following exception. In ActorInitializer.cs, we prefer to make the fields private instead. ValueActorInit provides a publicly accessible property for access and OwnerInit provides a publicly accessible method. Health.cs is adjusted to access the property base instead when overriding. The reflection calls must be adjusted to target the base class specifically, as searching for a private field from the derived class will fail to locate it on the base class.

Unused suppressions were removed.
2022-02-07 19:14:45 +01:00

147 lines
3.7 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2021 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;
using OpenRA.Primitives;
namespace OpenRA
{
// Represents a layer of "something" that covers the map
public sealed class CellLayer<T> : CellLayerBase<T>
{
public event Action<CPos> CellEntryChanged = null;
public CellLayer(Map map)
: base(map) { }
public CellLayer(MapGridType gridType, Size size)
: base(gridType, size) { }
public override void CopyValuesFrom(CellLayerBase<T> anotherLayer)
{
if (CellEntryChanged != null)
throw new InvalidOperationException(
$"Cannot copy values when there are listeners attached to the {nameof(CellEntryChanged)} event.");
base.CopyValuesFrom(anotherLayer);
}
public override void Clear()
{
if (CellEntryChanged != null)
throw new InvalidOperationException(
$"Cannot clear values when there are listeners attached to the {nameof(CellEntryChanged)} event.");
base.Clear();
}
public override void Clear(T clearValue)
{
if (CellEntryChanged != null)
throw new InvalidOperationException(
$"Cannot clear values when there are listeners attached to the {nameof(CellEntryChanged)} event.");
base.Clear(clearValue);
}
// Resolve an array index from cell coordinates
int Index(CPos cell)
{
// PERF: Inline CPos.ToMPos to avoid MPos allocation
var x = cell.X;
var y = cell.Y;
if (GridType == MapGridType.Rectangular)
return y * Size.Width + x;
var u = (x - y) / 2;
var v = x + y;
return v * Size.Width + u;
}
// Resolve an array index from map coordinates
int Index(MPos uv)
{
return uv.V * Size.Width + uv.U;
}
/// <summary>Gets or sets the <see cref="CellLayer"/> using cell coordinates</summary>
public T this[CPos cell]
{
get => Entries[Index(cell)];
set
{
Entries[Index(cell)] = value;
CellEntryChanged?.Invoke(cell);
}
}
/// <summary>Gets or sets the layer contents using raw map coordinates (not CPos!)</summary>
public T this[MPos uv]
{
get => Entries[Index(uv)];
set
{
Entries[Index(uv)] = value;
CellEntryChanged?.Invoke(uv.ToCPos(GridType));
}
}
public bool Contains(CPos cell)
{
// .ToMPos() returns the same result if the X and Y coordinates
// are switched. X < Y is invalid in the RectangularIsometric coordinate system,
// so we pre-filter these to avoid returning the wrong result
if (GridType == MapGridType.RectangularIsometric && cell.X < cell.Y)
return false;
return Contains(cell.ToMPos(GridType));
}
public bool Contains(MPos uv)
{
return Bounds.Contains(uv.U, uv.V);
}
public CPos Clamp(CPos uv)
{
return Clamp(uv.ToMPos(GridType)).ToCPos(GridType);
}
public MPos Clamp(MPos uv)
{
return uv.Clamp(new Rectangle(0, 0, Size.Width - 1, Size.Height - 1));
}
}
// Helper functions
public static class CellLayer
{
/// <summary>Create a new layer by resizing another layer. New cells are filled with defaultValue.</summary>
public static CellLayer<T> Resize<T>(CellLayer<T> layer, Size newSize, T defaultValue)
{
var result = new CellLayer<T>(layer.GridType, newSize);
var width = Math.Min(layer.Size.Width, newSize.Width);
var height = Math.Min(layer.Size.Height, newSize.Height);
result.Clear(defaultValue);
for (var j = 0; j < height; j++)
for (var i = 0; i < width; i++)
result[new MPos(i, j)] = layer[new MPos(i, j)];
return result;
}
}
}