private void CheckForDamage() { if (damageCollider != null) { var other = damageCollider.Collide(X, Y, Global.Tags.Attack); if (other != null) { var damage = other.Entity.GetComponent <Damage>(); if (damage != null && damage.side == Damage.Side.enemy) { lastDamage = damage; damage.hitSomething = true; if (state.CurrentState == States.Hurt) { state.ChangeState(States.Dead); } else { state.ChangeState(States.Hurt); } } } } if (Stats.instance.thirst < 0) { state.ChangeState(States.Dead); Ui.instance.ShowMessage("The thirst takes you"); } }
private void CheckCollision(CircleCollider collider) { var hit = collider.Collide(X, Y, (int)Tags.PLAYERATTACK); if (hit != null) { bool isHurt = true; // Make sure it didn't go through the front side if (Hitbox.Collide(X, Y, hit.Entity) != null) { var hitboxPos = new Vector2(Hitbox.X, Hitbox.Y); var colliderPos = new Vector2(collider.X, collider.Y); var entityPos = new Vector2(hit.X, hit.Y); var toHitbox = entityPos - hitboxPos; var angleToHitbox = Math.Atan2(toHitbox.Y, toHitbox.X); var hypt = (float)Math.Sin(angleToHitbox) * toHitbox.Y; var hitboxPenetration = hypt - hit.HalfWidth; var colliderPenetration = (collider.Radius + hit.HalfWidth) - (colliderPos - entityPos).Length; isHurt = (colliderPenetration > hitboxPenetration); } // Apply damage if okay if (isHurt) { var e = hit.Entity as Projectile; if (e.damage > 1) { ApplyDamage(e.damage); if (health > 0) { Game.Coroutine.Start(SwimAttack()); } } e.HitEnemy(); } } }