Ejemplo n.º 1
0
    /// <summary>
    /// 扇形监测:判断1:是否在圆形内,是否在范围内(角度)
    /// </summary>
    /// <param name="center">Center.</param>
    /// <param name="size">Size.</param>
    /// <param name="angle">Angle.</param>
    private void CheckFanHit(Vector2 center, float size, Vector2 dir, float fov)
    {
        Collider2D[] result   = new Collider2D[12];
        int          hitCount = Physics2D.OverlapCircleNonAlloc(center, size, result);

        for (int i = 0; i < hitCount; i++)
        {
            Vector2 resultDir = result [i].bounds.ClosestPoint(new Vector3(dir.x, dir.y, 0));
            if (Vector2.Angle(resultDir - center, dir) < fov * 0.5)
            {
                Damageable damageable = result[i].GetComponent <Damageable>();
                if (damageable)
                {
                    OnDamageableHit.Invoke(this, damageable);
                    damageable.TakeDamage(this, false);
                    if (disableDamageAfterHit)
                    {
                        DisableDamage();
                    }
                }
                else
                {
                    OnNonDamageableHit.Invoke(this);
                }
            }
        }
    }
Ejemplo n.º 2
0
    void FixedUpdate()
    {
        if (!m_CanDamage)
        {
            return;
        }

        Vector3 scale = m_DamagerTransform.lossyScale;

        Vector3 facingOffset = Vector3.Scale(offset, scale);

        if (offsetBasedOnSpriteFacing && spriteRenderer != null && spriteRenderer.armature.flipX != m_SpriteOriginallyFlipped)
        {
            facingOffset = new Vector2(-offset.x * scale.x, offset.y * scale.y);
        }

        Vector3 scaledSize = Vector3.Scale(size, scale);

        Vector3 boxCenter      = m_DamagerTransform.position + facingOffset;
        Vector3 boxHalfExtents = scaledSize * 0.5f;

        LayerMask ignoreLayers = ~hittableLayers;
        int       hitCount     = Physics.OverlapBoxNonAlloc(boxCenter, boxHalfExtents, m_AttackOverlapResults, Quaternion.Euler(0, 0, 0), hittableLayers);

        for (int i = 0; i < hitCount; i++)
        {
            // Self test
            if (m_AttackOverlapResults[i].gameObject == gameObject)
            {
                return;
            }

            // Team test
            StateController otherStateController = m_AttackOverlapResults[i].transform.GetComponent <StateController>();
            if (m_StateController != null && otherStateController != null &&
                (otherStateController.m_gameEnemyStats.m_teams.m_teamMask & m_StateController.m_gameEnemyStats.m_teams.m_teamMask) != 0)
            {
                continue;
            }

            m_LastHit = m_AttackOverlapResults[i];
            Damageable damageable = m_LastHit.GetComponent <Damageable>();

            if (damageable)
            {
                OnDamageableHit.Invoke(this, damageable);
                damageable.TakeDamage(this, ignoreInvincibility);
                if (disableDamageAfterHit)
                {
                    DisableDamage();
                }
            }
            else
            {
                OnNonDamageableHit.Invoke(this);
            }
        }
    }
        private void OnTriggerEnter2D(Collider2D other)
        {
            Enemy enemy = other.GetComponent <Enemy>();

            if (enemy != null)
            {
                OnDamageableHit?.Invoke(enemy);
            }
        }
Ejemplo n.º 4
0
        private void OnTriggerEnter2D(Collider2D other)
        {
            PlayableCharacter player = other.GetComponent <PlayableCharacter>();

            if (player != null)
            {
                OnDamageableHit?.Invoke(player);
            }
        }
Ejemplo n.º 5
0
    void FixedUpdate()
    {
        if (!m_CanDamage)
        {
            return;
        }

        Vector2 scale = m_DamagerTransform.lossyScale;

        Vector2 facingOffset = Vector2.Scale(offset, scale);

        if (offsetBasedOnSpriteFacing && spriteRenderer != null && spriteRenderer.armature.flipX != m_SpriteOriginallyFlipped)
        {
            facingOffset = new Vector2(-offset.x * scale.x, offset.y * scale.y);
        }

        Vector2 scaledSize = Vector2.Scale(size, scale);

        Vector2 pointA = (Vector2)m_DamagerTransform.position + facingOffset - scaledSize * 0.5f;
        Vector2 pointB = pointA + scaledSize;

        int hitCount = Physics2D.OverlapArea(pointA, pointB, m_AttackContactFilter, m_AttackOverlapResults);

        for (int i = 0; i < hitCount; i++)
        {
            m_LastHit = m_AttackOverlapResults[i];
            Damageable damageable = m_LastHit.GetComponent <Damageable>();

            if (damageable)
            {
                OnDamageableHit.Invoke(this, damageable);
                damageable.TakeDamage(this, ignoreInvincibility);
                if (disableDamageAfterHit)
                {
                    DisableDamage();
                }
            }
            else
            {
                OnNonDamageableHit.Invoke(this);
            }
        }
    }
Ejemplo n.º 6
0
    private void CheckBoxHit(Vector2 center, Vector2 size, float angle)
    {
        Collider2D[] result   = new Collider2D[12];
        int          hitCount = Physics2D.OverlapBoxNonAlloc(center, size, angle, result);

        for (int i = 0; i < hitCount; i++)
        {
            Damageable damageable = result[i].GetComponent <Damageable>();
            if (damageable)
            {
                OnDamageableHit.Invoke(this, damageable);
                damageable.TakeDamage(this, false);
                if (disableDamageAfterHit)
                {
                    DisableDamage();
                }
            }
            else
            {
                OnNonDamageableHit.Invoke(this);
            }
        }
    }