Example #1
0
        void FixedUpdate()
        {
            if (!m_CanDamage)
            {
                return;
            }

            Vector2 scale = m_DamagerTransform.lossyScale;

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

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

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

            //点A,B分别是攻击范围指示框的左下和右上点
            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);
                }
            }
        }
Example #2
0
        public void Die(Damager damager, Damageable damageable)
        {
            Vector2 throwVector   = new Vector2(0, 2.0f);
            Vector2 damagerToThis = damager.transform.position - transform.position;

            throwVector.x = Mathf.Sign(damagerToThis.x) * -4.0f;
            SetMoveVector(throwVector);

            m_Animator.SetTrigger(m_HashDeathPara);

            dieAudio.PlayRandomSound();

            m_Dead             = true;
            m_Collider.enabled = false;

            CameraShaker.Shake(0.15f, 0.3f);

            GameController.EnemySlayed();
        }
        public void Hit(Damager damager, Damageable damageable)
        {
            if (damageable.CurrentHealth <= 0)
            {
                return;
            }

            m_Animator.SetTrigger(m_HashHitPara);

            Vector2 throwVector   = new Vector2(0, 3.0f);
            Vector2 damagerToThis = damager.transform.position - transform.position;

            throwVector.x = Mathf.Sign(damagerToThis.x) * -2.0f;
            m_MoveVector  = throwVector;

            if (m_FlickeringCoroutine != null)
            {
                StopCoroutine(m_FlickeringCoroutine);
                m_SpriteRenderer.color = m_OriginalColor;
            }

            m_FlickeringCoroutine = StartCoroutine(Flicker(damageable));
            CameraShaker.Shake(0.15f, 0.3f);
        }
 public void OnHitDamageable(Damager origin, Damageable damageable)
 {
     FindSurface(origin.LastHit);
 }
Example #5
0
 /* AdaScript - I wanted to do this differently
  * public Damageable representedDamageable;
  * public GameObject healthIconPrefab;
  *
  * protected Animator[] m_HealthIconAnimators;
  *
  * protected readonly int m_HashActivePara = Animator.StringToHash ("Active");
  * protected readonly int m_HashInactiveState = Animator.StringToHash ("Inactive");
  * protected const float k_HeartIconAnchorWidth = 0.041f;
  *
  * IEnumerator Start ()
  * {
  *  if(representedDamageable == null)
  *      yield break;
  *
  *  yield return null;
  *
  *  m_HealthIconAnimators = new Animator[representedDamageable.startingHealth];
  *
  *  for (int i = 0; i < representedDamageable.startingHealth; i++)
  *  {
  *      GameObject healthIcon = Instantiate (healthIconPrefab);
  *      healthIcon.transform.SetParent (transform);
  *      RectTransform healthIconRect = healthIcon.transform as RectTransform;
  *      healthIconRect.anchoredPosition = Vector2.zero;
  *      healthIconRect.sizeDelta = Vector2.zero;
  *      healthIconRect.anchorMin += new Vector2(k_HeartIconAnchorWidth, 0f) * i;
  *      healthIconRect.anchorMax += new Vector2(k_HeartIconAnchorWidth, 0f) * i;
  *      m_HealthIconAnimators[i] = healthIcon.GetComponent<Animator> ();
  *
  *      if (representedDamageable.CurrentHealth < i + 1)
  *      {
  *          m_HealthIconAnimators[i].Play (m_HashInactiveState);
  *          m_HealthIconAnimators[i].SetBool (m_HashActivePara, false);
  *      }
  *  }
  * }
  *
  * public void ChangeHitPointUI (Damageable damageable)
  * {
  *  if(m_HealthIconAnimators == null)
  *      return;
  *
  *  for (int i = 0; i < m_HealthIconAnimators.Length; i++)
  *  {
  *      m_HealthIconAnimators[i].SetBool(m_HashActivePara, damageable.CurrentHealth >= i + 1);
  *  }
  * }*/
 public static void ChangeHitPointUI(Damageable damageable)
 {
     healthBarSlider.GetComponent <Slider>().value = damageable.CurrentHealth;
     healthBarFill.GetComponent <Text>().text      = damageable.CurrentHealth + " / " + damageable.startingHealth;
 }