//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);
        }
Пример #2
0
 private bool CanBePlacedInContainer(EquipmentContainer container)
 {
     foreach (PlacingMethod placingMethod in placingMethods)
     {
         if (container.IsPlacingMethodSupported(placingMethod))
         {
             return(true);
         }
     }
     return(false);
 }
Пример #3
0
 private void SetCommonPlacingMethods(EquipmentContainer container)
 {
     commonPlacingMethodsWithContainer.Clear();
     foreach (PlacingMethod placingMethod in placingMethods)
     {
         if (container.IsPlacingMethodSupported(placingMethod))
         {
             commonPlacingMethodsWithContainer.Add(placingMethod);
         }
     }
 }
Пример #4
0
 public void Initialize(EquipmentContainer container)
 {
     currentContainer = container;
     currentContainer.OccupySpace(spaceOccupier);
     SetCommonPlacingMethods(container);
     if (Utility.OverlapsOtherEquipment(spaceOccupier, transform.position, transform.rotation, container))
     {
         boundingBoxRenderer.SetWrongMaterial();
     }
     else
     {
         boundingBoxRenderer.SetRightMaterial();
     }
 }
Пример #5
0
        //returns true if moved successfully
        public bool Move(Vector3 position, EquipmentContainer container)
        {
            if (isPlaced)
            {
                return(false);
            }

            Vector3 newPosition = position + new Vector3(0, spaceOccupier.Volume.y * 0.5f, 0);

            newPosition = Utility.KeepPositionInsideContainer(spaceOccupier, newPosition, container);

            if (Utility.OverlapsOtherEquipment(spaceOccupier, newPosition, transform.rotation, container))
            {
                return(false);
            }

            if (container.Capacity.y < spaceOccupier.Volume.y)
            {
                return(false);
            }

            if (container != currentContainer)
            {
                if (!CanBePlacedInContainer(container))
                {
                    return(false);
                }

                SetCommonPlacingMethods(container);
                currentContainer.UnoccupySpace(spaceOccupier);
                currentContainer = container;
                currentContainer.OccupySpace(spaceOccupier);
                transform.rotation = container.transform.rotation;
            }
            boundingBoxRenderer.SetRightMaterial();
            transform.position = newPosition;
            return(true);
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }