//returns true if spawned successfully
        private bool SpawnObject(SpaceOccupier newObject)
        {
            int     containerIndex = 0;
            Vector3 spawnPosition  = spawnPoint.position;

            while (containerIndex < containers.Length)
            {
                EquipmentContainer container = containers[containerIndex];
                if (container.Capacity.y >= newObject.Volume.y)
                {
                    spawnPosition.y = container.transform.position.y + newObject.Volume.y * 0.5f;
                    newObject.transform.position = spawnPosition;
                    Equipment equipment = newObject.GetComponent <Equipment>();
                    equipment.Initialize(container);
                    onEquipmentSpawned?.Invoke(equipment.gameObject);
                    return(true);
//                    break;
                }
                containerIndex++;
                if (containerIndex >= containers.Length)
                {
                    Destroy(newObject.gameObject);
                    return(false);
                }
            }
            return(true);
        }
 public void UnoccupySpace(SpaceOccupier occupier)
 {
     containedEquipment.Remove(occupier);
     if (HeightOccupied == occupier.Volume.y)
     {
         ResetHeight();
     }
 }
 public void OccupySpace(SpaceOccupier occupier)
 {
     if (occupier.Volume.y > HeightOccupied)
     {
         HeightOccupied = occupier.Volume.y;
     }
     containedEquipment.Add(occupier);
     occupier.transform.parent = transform;
 }
 private void Awake()
 {
     spaceOccupier       = GetComponent <SpaceOccupier>();
     boundingBoxRenderer = GetComponent <BoundingBoxRenderer>();
     if (currentContainer)
     {
         currentContainer.OccupySpace(spaceOccupier);
     }
 }
        public void PlaceInIronBox(Equipment equipment)
        {
            SpaceOccupier ironBox = Instantiate(ironBoxPrefab);

            equipment.GetComponent <Collider>().enabled = false;
            equipment.GetComponent <BoundingBoxRenderer>().Hide();
            ironBox.transform.position = equipment.transform.position;
            equipment.transform.parent = ironBox.transform;
            ironBox.transform.rotation = equipment.transform.rotation;
            ironBox.transform.position = Utility.KeepPositionInsideContainer(ironBox, equipment.transform.position, equipment.currentContainer);
            equipment.currentContainer.OccupySpace(ironBox);
            ironBox.GetComponent <DeleteableGameObject>().onDelete.AddListener(OnIronBoxDeleted);
        }
        public void SpawnFuelTank()
        {
            SpaceOccupier newFuelTank = Instantiate <SpaceOccupier>(fuelTankPrefab);

            newFuelTank.GetComponent <DeleteableGameObject>().onDelete.AddListener(OnFuelTankDeleted);
            if (SpawnObject(newFuelTank))
            {
                onFuelTankSpawned?.Invoke();
            }
            else
            {
                onSpawnFailed?.Invoke();
            }
        }
        public void SpawnBoltCutter()
        {
            SpaceOccupier newBoltCutter = Instantiate <SpaceOccupier>(boltCutterPrefab);

            newBoltCutter.GetComponent <DeleteableGameObject>().onDelete.AddListener(OnBoltCutterDeleted);
            if (SpawnObject(newBoltCutter))
            {
                onBoltCutterSpawned?.Invoke();
            }
            else
            {
                onSpawnFailed?.Invoke();
            }
        }
        public static bool OverlapsOtherEquipment(SpaceOccupier spaceOccupier, Vector3 newPosition, Quaternion newRotation, EquipmentContainer container)
        {
            Collider[] colliders = Physics.OverlapBox(newPosition, spaceOccupier.Volume * 0.5f, newRotation, DragHandler.singleton.equipmentLayer);

            bool overlaps = true;

            if (colliders.Length == 1)
            {
                overlaps = colliders[0].gameObject != spaceOccupier.gameObject;
            }

            if (colliders.Length == 0)
            {
                overlaps = false;
            }

            return(overlaps);
        }
 private void OnUnselectedObject(GameObject unselectedGameObject)
 {
     target = null;
 }
        public static Vector3 KeepPositionInsideContainer(SpaceOccupier spaceOccupier, Vector3 newPosition, EquipmentContainer container)
        {
            Vector3 clampedPosition = newPosition;
            Vector3 containerCenter = Vector3.zero;

            if (container.containerType == EquipmentContainer.ContainerType.shelf)
            {
                containerCenter = container.transform.position + Vector3.up * container.Capacity.y;
            }
            else
            {
                containerCenter = container.transform.position + container.transform.up * container.Capacity.z;
            }

            Bounds containerBounds = new Bounds(containerCenter, container.Capacity);

            Vector3 halfVolume = spaceOccupier.BoundingBox.extents;

            Vector3 maxEquipmentBound = newPosition + halfVolume;
            Vector3 minEquipmentBound = newPosition - halfVolume;

            if (containerBounds.max.x < maxEquipmentBound.x)
            {
                clampedPosition.x = containerBounds.max.x - halfVolume.x;
            }

            if (containerBounds.min.x > minEquipmentBound.x)
            {
                clampedPosition.x = containerBounds.min.x + halfVolume.x;
            }

            switch (container.containerType)
            {
            case EquipmentContainer.ContainerType.shelf:
            {
                if (containerBounds.max.z < maxEquipmentBound.z)
                {
                    clampedPosition.z = containerBounds.max.z - halfVolume.z;
                }

                if (containerBounds.min.z > minEquipmentBound.z)
                {
                    clampedPosition.z = containerBounds.min.z + halfVolume.z;
                }
                break;
            }

            case EquipmentContainer.ContainerType.backWall:
            {
                if (containerBounds.max.y < maxEquipmentBound.y)
                {
                    clampedPosition.y = containerBounds.max.y - halfVolume.y;
                }

                if (containerBounds.min.y > minEquipmentBound.y)
                {
                    clampedPosition.y = containerBounds.min.y + halfVolume.y;
                }
                break;
            }
            }
            return(clampedPosition);
        }