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
        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.");
            }
        }
Example #3
0
        /// <summary>
        /// Determines whether the block can be added checking whether a block is already
        /// present at the position and whether this new block can connect to another block.
        /// </summary>
        public bool CanAddBlock(BlockPosition position, BlockInfo info, byte rotation)
        {
            if (info.Type == BlockType.Mainframe)
            {
                if (_mainframePosition != null)
                {
                    return(false);
                }
            }
            else if (SystemFactory.IsAnySystem(info.Type))
            {
                if (SystemFactory.IsActiveSystem(info.Type))
                {
                    if (_activeSystemPresent)
                    {
                        return(false);
                    }
                }
                else
                {
                    WeaponSystem.Type weaponType = SystemFactory.GetWeaponType(info.Type);
                    if (weaponType != WeaponSystem.Type.None && weaponType != _weaponType && _weaponType != WeaponSystem.Type.None)
                    {
                        return(false);
                    }
                }
            }

            if (info is SingleBlockInfo single)
            {
                return(!_blocks.ContainsKey(position) &&
                       CanConnect(position, Rotation.RotateSides(single.ConnectSides, rotation)));
            }
            else
            {
                return(((MultiBlockInfo)info).GetRotatedPositions(position, rotation,
                                                                  out KeyValuePair <BlockPosition, BlockSides>[] positions) &&
                       !positions.Any(pair => _blocks.ContainsKey(pair.Key)) &&
                       positions.Any(pair => CanConnect(pair.Key, pair.Value)));
            }
        }