コード例 #1
0
ファイル: BaseActor.cs プロジェクト: ouroborose/LD41
    protected void UpdateDetection(float radius, float range)
    {
        if (m_pickUpCandidate != null)
        {
            m_pickUpCandidate.SetColor(BaseBuildingPart.BROKEN_COLOR);
        }
        m_pickUpCandidate  = null;
        m_placementTile    = null;
        m_placementAllowed = false;

        m_buildingPartsInRange.Clear();
        m_actorsInRange.Clear();

        s_sharedRay       = new Ray(transform.position + Vector3.up * 0.7f, transform.forward);
        s_sharedHitsCount = Physics.SphereCastNonAlloc(s_sharedRay, radius, s_sharedHits, range);

        float bestPartValue = float.MaxValue;

        for (int i = 0; i < s_sharedHitsCount; ++i)
        {
            RaycastHit hit = s_sharedHits[i];
            BaseObject obj = hit.collider.GetComponentInParent <BaseObject>();
            if (obj == null || obj == this)
            {
                continue;
            }

            BaseBuildingPart part = obj as BaseBuildingPart;
            if (part != null && !part.m_isBeingCarried)
            {
                m_buildingPartsInRange.Add(new KeyValuePair <RaycastHit, BaseBuildingPart>(hit, part));
                if (m_heldPart == null && part.m_isBroken)
                {
                    if (hit.distance < bestPartValue)
                    {
                        bestPartValue     = hit.distance;
                        m_pickUpCandidate = part;
                    }
                }
                continue;
            }

            BaseActor actor = obj as BaseActor;
            if (actor != null)
            {
                m_actorsInRange.Add(new KeyValuePair <RaycastHit, BaseActor>(hit, actor));
            }
        }

        if (m_pickUpCandidate != null)
        {
            m_pickUpCandidate.SetColor(Color.green);
        }

        // update placement indicator
        if (m_heldPart != null)
        {
            m_placementTile = LevelGenerator.Instance.GetClosestTile(transform.position + transform.forward);

            if (m_placementTile != null)
            {
                BaseBuildingPart topPart = m_placementTile.GetTopPart();
                if (topPart == null)
                {
                    m_placementAllowed = true;
                    m_placementIndicator.transform.position = m_placementTile.transform.position;
                }
                else
                {
                    m_placementAllowed = topPart.m_type != BaseBuildingPart.BuildingPartType.Roof && topPart.transform.position.y <= transform.position.y + 1.5f;
                    m_placementIndicator.transform.position = topPart.transform.position + Vector3.up;
                }
            }
        }

        m_placementIndicator.gameObject.SetActive(m_placementTile != null);
        if (m_placementIndicator.gameObject.activeSelf)
        {
            m_placementIndicator.SetColor(m_placementAllowed ? PLACEMENT_ALLOWED_COLOR : PLACEMENT_NOT_ALLOWED_COLOR);
        }
    }