Beispiel #1
0
 public TileType(string codeName, Int2 gridSize, string toolboxIconSprite, TileCategory category, ITileTypeData data)
 {
     DebugEx.Assert(Sprite.IsValidElmFunctionName(codeName));
     CodeName          = codeName;
     GridSize          = gridSize;
     ToolboxIconSprite = toolboxIconSprite;
     Category          = category;
     Data = data;
 }
Beispiel #2
0
        public void AssertPath(ITileTypeData data)
        {
            var margin = 0.01;

            bool ValueClose(double value0, double value1) => Math.Abs(value0 - value1) < margin;

            bool OnTileEdge(Double2 point) =>
            (ValueClose(point.X, 0) || ValueClose(point.X, GridSize.X)) &&
            (ValueClose(point.Y, 0) || ValueClose(point.Y, GridSize.Y));

            bool OnGridEdge(Double2 point) =>
            (ValueClose(point.X % 1, 0) && ValueClose(point.Y % 1, 0.5)) ||
            (ValueClose(point.Y % 1, 0) && ValueClose(point.X % 1, 0.5));

            bool PathValid(Double2 start, Double2 end) => OnTileEdge(start) && OnGridEdge(start) && OnTileEdge(end) && OnGridEdge(end);

            switch (data)
            {
            case Rail rail:
                DebugEx.Assert(
                    PathValid(rail.Path.Func(0), rail.Path.Func(1)),
                    "Path must start and end on different grid edges that are also on the edge of the tile.");
                break;

            case RailFork rail:
                var pathOnStart  = rail.PathOn.Func(0);
                var pathOffStart = rail.PathOn.Func(0);
                DebugEx.Assert(
                    PathValid(pathOnStart, rail.PathOn.Func(1)),
                    "On path must start and end on different grid edges that are also on the edge of the tile.");
                DebugEx.Assert(
                    PathValid(pathOffStart, rail.PathOff.Func(1)),
                    "Off path must start and end on different grid edges that are also on the edge of the tile.");
                DebugEx.Assert(
                    !ValueClose(pathOnStart.X, pathOffStart.X) || !ValueClose(pathOnStart.Y, pathOffStart.Y),
                    "On and off path must start at the same place.");
                break;

            case Depot depot:
                var pathStart = depot.Path.Func(0);
                DebugEx.Assert(
                    OnGridEdge(pathStart) && OnTileEdge(pathStart) && !OnTileEdge(depot.Path.Func(1)),
                    "Path should start at the edge of the tile and end inside it");
                break;

            default:
                throw new NotImplementedException();
            }
        }