예제 #1
0
        /// <summary>
        /// Clicks a tile. Returns whether a tile was present to be clicked on.
        /// </summary>
        /// <param name="tile"></param>
        /// <returns></returns>
        public bool ClickTile(TileBaseData tileBaseData)
        {
            var tile = GetTile(tileBaseData);

            if (tile == null)
            {
                return(false);
            }
            ModifyTileData(tile.BaseData, data =>
            {
                switch (data)
                {
                case TileRailFork fork:
                    if (!fork.Trains.Any())
                    {
                        return(new TileRailFork(fork.Trains, !fork.IsOn));
                    }
                    break;

                case TileDepot depot:
                    if (depot.Occupied)
                    {
                        TrainIdCounter++;
                        return(new TileDepot(depot.Trains.Add(new Train(1, TrainSpeed, false, TrainIdCounter - 1)), false));
                    }
                    break;
                }
                return(data);
            });


            return(true);
        }
예제 #2
0
 public Tile(TileBaseData baseData, ITileData data)
 {
     DebugEx.Assert(baseData != null);
     DebugEx.Assert(data != null);
     BaseData = baseData;
     Data     = data;
 }
예제 #3
0
        public bool ModifyTileData(TileBaseData tileBaseData, Func <ITileData, ITileData> modifyTileData)
        {
            var oldTile = GetTile(tileBaseData);

            if (Remove(tileBaseData))
            {
                var newTileData = modifyTileData(oldTile.Data);
                DebugEx.Assert(newTileData.GetType() == oldTile.Data.GetType());
                FastAddTile(new Tile(tileBaseData, newTileData));
                return(true);
            }
            return(false);
        }
예제 #4
0
        /// <summary>
        /// Removes a tile that exactly matches the one provided.
        /// </summary>
        public bool Remove(TileBaseData baseData)
        {
            var tile = GetTile(baseData);

            if (tile != null)
            {
                if (tile.Data is IRailTileData railTile && railTile.Trains.Any())
                {
                    var result = RailTiles.Remove(tile);
                    DebugEx.Assert(result, "Couldn't find train tile to remove.");
                }
                return(_superGrid.Remove(GridToSuperGrid(baseData.GridPosition), tile));
            }
            return(false);
        }
예제 #5
0
 /// <summary>
 /// Returns a tile with matching base data. Null is returned if no tile is found.
 /// </summary>
 /// <param name="tileBaseData"></param>
 /// <returns></returns>
 public Tile GetTile(TileBaseData tileBaseData) =>
 _superGrid.TryGetValue(GridToSuperGrid(tileBaseData.GridPosition), out IReadOnlyCollection <Tile> value)
         ? value.SingleOrDefault(item => item.BaseData == tileBaseData)
         : null;
예제 #6
0
 public static MemoryStream WriteTileBaseData(this MemoryStream stream, TileBaseData tile) =>
 stream
 .WriteInt(tile.TileTypeId)
 .WriteInt2(tile.GridPosition)
 .WriteInt(tile.Rotation);
 public ClickTileMessage(TileBaseData baseData)
 {
     BaseData = baseData;
 }
 public RemoveTileMessage(TileBaseData baseData)
 {
     BaseData = baseData;
 }
예제 #9
0
 public Tile With(TileBaseData baseData = null, ITileData data = null) =>
 new Tile(baseData ?? BaseData, data ?? Data);