/// <summary> Sets the <see cref="GridItem" /> value associated with the cell </summary>
            /// <param name="value"> The value to set the grid item to. </param>
            /// <param name="changeType"> (Optional) The type of the change that was made. </param>
            public void Set(GridItem value, GridItemPropertyChange changeType = GridItemPropertyChange.All)
            {
                var oldValue = *_item;

                *_item = value;

                FireChanged(oldValue, value, changeType);
            }
            private void FireChanged(GridItem oldValue, GridItem newValue, GridItemPropertyChange changeType)
            {
                var innerCoordinate = GetCoordinate(_cellIndex);

                _chunk.OnGridItemChanged(_chunk,
                                         new GridCoordinate(_chunk.Position, innerCoordinate),
                                         oldValue,
                                         newValue,
                                         changeType);
            }
Exemple #3
0
        private void HandleDataChanged(
            SliceUnitData <UnityWorldData> oldData,
            ref SliceUnitData <UnityWorldData> newData,
            GridItemPropertyChange changeType
            )
        {
            // TODO should we do something?
            if (changeType == GridItemPropertyChange.HealthChange)
            {
                newData.Data = oldData.Data;
                return;
            }

            if (oldData.Data?.Structure != null)
            {
                UnityExtensions.Destroy(oldData.Data.Structure);
            }

            if (oldData.Data?.Tile != null)
            {
                UnityExtensions.Destroy(oldData.Data.Tile.gameObject);
            }

            var item = newData.GridItem;

            var worldPosition = newData.Position.ToVector3();

            var tileDescriptor = TileLookup.FindTileOrNull(item.TileType);
            var tileBehavior   = tileDescriptor.Construct(
                new PositionAndRotation(worldPosition, PossibleRotations[(byte)item.TileRotation]),
                WorldGrid,
                newData.Position
                );

            GameObject placeableInstance = null;

            if (item.StructureType != 0)
            {
                var associatedStructure = TileLookup.FindStructureOrNull(item.StructureType);
                if (associatedStructure == null)
                {
                    Debug.Log($"No object type of «{item.StructureType}» found while building world");
                }
                else
                {
                    placeableInstance = associatedStructure.CreateInstanceInWorld(this, newData.Position);
                }
            }

            newData.Data = new UnityWorldData
            {
                Tile      = tileBehavior,
                Structure = placeableInstance
            };
        }
            private void UpdateItem <T>(UpdateGridItemPropertyCallback <T> mutateCallback,
                                        GridItemPropertyChange changeType,
                                        T value)
            {
                var oldValue = *_item;

                mutateCallback.Invoke(_item, value);
                var newValue = *_item;

                FireChanged(oldValue, newValue, changeType);
            }
        /// <summary> Updates the data at the specified index inside the grid. </summary>
        /// <param name="index"> Zero-based index of the. </param>
        /// <param name="data"> The grid data to store. </param>
        /// <param name="position"> The coordinate of the original GridItem where the data was retrieved
        ///   from. </param>
        /// <param name="chunk"></param>
        /// <param name="changeType"></param>
        private void UpdateData(Array2DIndex index, GridItem data, GridCoordinate position, Chunk chunk, GridItemPropertyChange changeType)
        {
            var newData  = new SliceUnitData <T>(chunk, position, data, default(T));
            var rawIndex = _visibleGridItems.CalculateRawArrayIndex(index);

            ref var slot = ref _visibleGridItems.Data[rawIndex];
        /// <summary>
        ///  Checks the array index/position to verify that it has the correct position information and if
        ///  it does not, re-reads the data from the world and updates the data.
        /// </summary>
        private void Invalidate(Array2DIndex index, GridCoordinate coordinate, bool bypassSamePositionCheck, GridItemPropertyChange changeType)
        {
            var existingData = _visibleGridItems[index];

            if (!bypassSamePositionCheck && existingData.Position == coordinate)
            {
                return;
            }

            var subCoordinate = coordinate.AsSubCoordinates();

            if (!_worldGrid.IsValid(subCoordinate.ChunkCoordinate))
            {
                return;
            }

            var(chunk, data) = _worldGrid.GetChunkAndCellData(subCoordinate);

            UpdateData(index, data, coordinate, chunk, changeType);
        }