Example #1
0
        /// <summary>
        /// Tries to add the block, but skips the CanAddBlock test.
        /// </summary>
        private bool AddBlock(BlockPosition position, BlockInfo info, byte rotation)
        {
            if (info is SingleBlockInfo single)
            {
                _blocks.Add(position, BlockFactory.MakeSinglePlaced(transform, single, rotation, position));
            }
            else
            {
                PlacedMultiBlockParent parent = BlockFactory.MakeMultiPlaced(transform, (MultiBlockInfo)info,
                                                                             rotation, position, out PlacedMultiBlockPart[] parts);
                if (parent == null)
                {
                    return(false);
                }

                _blocks.Add(position, parent);
                foreach (PlacedMultiBlockPart part in parts)
                {
                    _blocks.Add(part.Position, part);
                }
            }

            RealBlockCount++;
            if (info.Type == BlockType.Mainframe)
            {
                _mainframePosition = position;
            }
            else if (SystemFactory.IsAnySystem(info.Type))
            {
                if (SystemFactory.IsActiveSystem(info.Type))
                {
                    _activeSystemPresent = true;
                }
                else
                {
                    WeaponSystem.Type weaponType = SystemFactory.GetWeaponType(info.Type);
                    if (weaponType != WeaponSystem.Type.None)
                    {
                        _weaponType = weaponType;                         //may or may not be first time set
                        _weaponCount++;
                    }
                }
            }
            return(true);
        }
Example #2
0
        public static PlacedMultiBlockParent MakeMultiPlaced(Transform parent, MultiBlockInfo info, byte rotation, BlockPosition position,
                                                             out PlacedMultiBlockPart[] parts)
        {
            if (!info.GetRotatedPositions(position, rotation, out KeyValuePair <BlockPosition, BlockSides>[] partPositions))
            {
                parts = null;
                return(null);
            }

            GameObject             block     = InstantiatePrefab(parent, info, rotation, position);
            PlacedMultiBlockParent component = block.AddComponent <PlacedMultiBlockParent>();

            // ReSharper disable once CoVariantArrayConversion
            InitializeMulti(component, info, rotation, position, partPositions, count => new PlacedMultiBlockPart[count],
                            pair => new PlacedMultiBlockPart(pair.Value, pair.Key), out IMultiBlockPart[] tempParts);
            parts = (PlacedMultiBlockPart[])tempParts;
            return(component);
        }
Example #3
0
        private void RemoveBlock(IPlacedBlock block)
        {
            RealBlockCount--;
            if (block.Position.Equals(_mainframePosition))
            {
                _mainframePosition = null;
            }
            else if (SystemFactory.IsActiveSystem(block.Type))
            {
                _activeSystemPresent = false;
            }
            else if (SystemFactory.GetWeaponType(block.Type) != WeaponSystem.Type.None)
            {
                if (--_weaponCount == 0)
                {
                    _weaponType = WeaponSystem.Type.None;
                }
            }

            PlacedSingleBlock single = block as PlacedSingleBlock;

            if (single != null)
            {
                Destroy(single.gameObject);
                _blocks.Remove(single.Position);
                return;
            }

            PlacedMultiBlockParent parent = block as PlacedMultiBlockParent;

            if (parent == null)
            {
                parent = ((PlacedMultiBlockPart)block).Parent;
            }

            Destroy(parent.gameObject);
            Assert.IsTrue(_blocks.Remove(parent.Position), "The parent of the multi block is not present.");
            foreach (PlacedMultiBlockPart part in parent.Parts)
            {
                Assert.IsTrue(_blocks.Remove(part.Position), "A part of the multi block is not present.");
            }
        }