Ejemplo n.º 1
0
        public static LG_WeakResourceContainer GetParentResourceContainer(Vector3 position)
        {
            var resourceContainers = GetComponentsInParentAtPosition <LG_WeakResourceContainer>(position)
                                     .Distinct(UnhollowerExtensions.InstanceIDComparer)
                                     .Cast <LG_WeakResourceContainer>()
                                     .ToArray();

            if (resourceContainers.Length == 0)
            {
                return(null);
            }

            if (resourceContainers.Length > 1)
            {
                QoLFixPlugin.LogError($"{nameof(ItemInLevel)} is inside of multiple resource containers!?");
            }

            return(resourceContainers[0]);
        }
Ejemplo n.º 2
0
        public static bool GetComponentInSight <T>(
            Vector3 origin,
            Vector3 direction,
            out T comp,
            out Vector3 hitPos,
            float maxDistance,
            int layerMask,
            Func <T, bool> predicate = null,
            bool debug = false)
        {
            if (!Physics.Raycast(origin, direction, out var hitInfo, maxDistance, layerMask, QueryTriggerInteraction.Ignore))
            {
                comp   = default;
                hitPos = default;
                return(false);
            }

            comp   = hitInfo.collider.gameObject.GetComponentInChildren <T>();
            hitPos = hitInfo.point;

            var resourceContainer = hitInfo.collider.GetComponentInParent <LG_WeakResourceContainer>();

            // Ignore placeholder hits unless we're specifically looking for
            // a placeholder.
            if (typeof(DropResourcesPatch.StorageSlotPlaceholder) != typeof(T))
            {
                var placeholder = hitInfo.collider.gameObject.GetComponent <DropResourcesPatch.StorageSlotPlaceholder>();
                if (resourceContainer != null && placeholder != null)
                {
                    comp = resourceContainer.m_graphics.Cast <Component>().GetComponentInChildren <T>();
                }
            }

            if (resourceContainer?.m_intOpen.enabled == false)
            {
                var storageChildren = resourceContainer.m_storageComp.gameObject
                                      .GetChildren()
                                      .Where(x => x.GetComponentInChildren <T>() != null)
                                      .ToHashSet();

                if (debug)
                {
                    foreach (var child in storageChildren)
                    {
                        QoLFixPlugin.LogDebug($"<{nameof(GetComponentInSight)}> StorageChild: {child.name}");
                    }
                }

                var hits      = Physics.RaycastAll(origin, direction, maxDistance, layerMask, QueryTriggerInteraction.Ignore);
                T   childComp = default;
                foreach (var hit in hits)
                {
                    if (!storageChildren.Contains(hit.collider.gameObject, UnhollowerExtensions.InstanceIDComparer))
                    {
                        continue;
                    }
                    childComp = hit.collider.gameObject.GetComponentInChildren <T>();
                    if (childComp == null)
                    {
                        continue;
                    }
                    if (predicate != null && !predicate(childComp))
                    {
                        continue;
                    }

                    if (debug)
                    {
                        QoLFixPlugin.LogDebug($"<{nameof(GetComponentInSight)}> Selecting storage child as target");
                    }

                    hitPos = hit.point;
                    break;
                }

                if (childComp != null)
                {
                    comp = childComp;
                }
            }

            return(comp != null);
        }