Ejemplo n.º 1
0
        /// <summary>
        /// Get a MapState POCO which represents this Map and can be easily serialized
        /// Use Restore with the MapState to get back a full Map
        /// </summary>
        /// <returns>MapState POCO (Plain Old C# Object) which represents this Map and can be easily serialized</returns>
        public MapState Save()
        {
            var mapState = new MapState
            {
                Width  = Width,
                Height = Height,
                Cells  = new MapState.CellProperties[Width * Height]
            };

            foreach (ICell cell in GetAllCells())
            {
                MapState.CellProperties cellProperties = MapState.CellProperties.None;
                if (cell.IsInFov)
                {
                    cellProperties |= MapState.CellProperties.Visible;
                }
                if (cell.IsTransparent)
                {
                    cellProperties |= MapState.CellProperties.Transparent;
                }
                if (cell.IsWalkable)
                {
                    cellProperties |= MapState.CellProperties.Walkable;
                }
                mapState.Cells[(cell.Y * Width) + cell.X] = cellProperties;
            }
            return(mapState);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Restore the state of this Map from the specified MapState
        /// </summary>
        /// <param name="state">Mapstate POCO (Plain Old C# Object) which represents this Map and can be easily serialized and deserialized</param>
        /// <exception cref="ArgumentNullException">Thrown on null map state</exception>
        public void Restore(MapState state)
        {
            if (state == null)
            {
                throw new ArgumentNullException("state", "Map state cannot be null");
            }

            var inFov = new HashSet <int>();

            Initialize(state.Width, state.Height);
            foreach (ICell cell in GetAllCells())
            {
                MapState.CellProperties cellProperties = state.Cells[cell.Y * Width + cell.X];
                if (cellProperties.HasFlag(MapState.CellProperties.Visible))
                {
                    inFov.Add(IndexFor(cell.X, cell.Y));
                }
                _isTransparent[cell.X, cell.Y] = cellProperties.HasFlag(MapState.CellProperties.Transparent);
                _isWalkable[cell.X, cell.Y]    = cellProperties.HasFlag(MapState.CellProperties.Walkable);
            }

            _fieldOfView = new FieldOfView(this, inFov);
        }