public void UpdateAttack(float deltaTime, Vector2 attackPosition, IDamageable damageTarget) { float dist = ConvertUnits.ToDisplayUnits(Vector2.Distance(SimPosition, attackPosition)); AttackTimer += deltaTime; body.ApplyTorque(Mass * character.AnimController.Dir * attack.Torque); if (dist < attack.Range * 0.5f) { if (AttackTimer >= attack.Duration && damageTarget != null) { attack.DoDamage(character, damageTarget, WorldPosition, 1.0f, (soundTimer <= 0.0f)); soundTimer = Limb.SoundInterval; } } Vector2 diff = attackPosition - SimPosition; if (diff.LengthSquared() > 0.00001f) { Vector2 pos = pullJoint == null ? body.SimPosition : pullJoint.WorldAnchorA; body.ApplyLinearImpulse(Mass * attack.Force * Vector2.Normalize(attackPosition - SimPosition), pos); } }
public void UpdateAttack(float deltaTime, Vector2 attackPosition, IDamageable damageTarget) { float dist = ConvertUnits.ToDisplayUnits(Vector2.Distance(SimPosition, attackPosition)); AttackTimer += deltaTime; body.ApplyTorque(Mass * character.AnimController.Dir * attack.Torque); if (dist < attack.DamageRange) { if (AttackTimer >= attack.Duration && damageTarget != null) { attack.DoDamage(character, damageTarget, WorldPosition, 1.0f, (SoundTimer <= 0.0f)); SoundTimer = SoundInterval; } } Vector2 diff = attackPosition - SimPosition; if (diff.LengthSquared() < 0.00001f) { return; } if (attack.ApplyForceOnLimbs != null) { foreach (int limbIndex in attack.ApplyForceOnLimbs) { if (limbIndex < 0 || limbIndex >= character.AnimController.Limbs.Length) { continue; } Limb limb = character.AnimController.Limbs[limbIndex]; Vector2 forcePos = limb.pullJoint == null ? limb.body.SimPosition : limb.pullJoint.WorldAnchorA; limb.body.ApplyLinearImpulse( limb.Mass * attack.Force * Vector2.Normalize(attackPosition - SimPosition), forcePos); } } else { Vector2 forcePos = pullJoint == null ? body.SimPosition : pullJoint.WorldAnchorA; body.ApplyLinearImpulse(Mass * attack.Force * Vector2.Normalize(attackPosition - SimPosition), forcePos); } }
public void UpdateAttack(float deltaTime, Vector2 attackPosition, IDamageable damageTarget) { float dist = ConvertUnits.ToDisplayUnits(Vector2.Distance(SimPosition, attackPosition)); AttackTimer += deltaTime; body.ApplyTorque(Mass * character.AnimController.Dir * attack.Torque); bool wasHit = false; if (damageTarget != null) { switch (attack.HitDetectionType) { case HitDetection.Distance: wasHit = dist < attack.DamageRange; break; case HitDetection.Contact: List <Body> targetBodies = new List <Body>(); if (damageTarget is Character) { Character targetCharacter = (Character)damageTarget; foreach (Limb limb in targetCharacter.AnimController.Limbs) { if (!limb.IsSevered && limb.body?.FarseerBody != null) { targetBodies.Add(limb.body.FarseerBody); } } } else if (damageTarget is Structure) { Structure targetStructure = (Structure)damageTarget; if (character.Submarine == null && targetStructure.Submarine != null) { targetBodies.Add(targetStructure.Submarine.PhysicsBody.FarseerBody); } else { targetBodies.AddRange(targetStructure.Bodies); } } else if (damageTarget is Item) { Item targetItem = damageTarget as Item; if (targetItem.body?.FarseerBody != null) { targetBodies.Add(targetItem.body.FarseerBody); } } if (targetBodies != null) { ContactEdge contactEdge = body.FarseerBody.ContactList; while (contactEdge != null) { if (contactEdge.Contact != null && contactEdge.Contact.IsTouching && targetBodies.Any(b => b == contactEdge.Contact.FixtureA?.Body || b == contactEdge.Contact.FixtureB?.Body)) { wasHit = true; break; } contactEdge = contactEdge.Next; } } break; } } if (wasHit) { if (AttackTimer >= attack.Duration && damageTarget != null) { attack.DoDamage(character, damageTarget, WorldPosition, 1.0f, (SoundTimer <= 0.0f)); SoundTimer = SoundInterval; } } Vector2 diff = attackPosition - SimPosition; if (diff.LengthSquared() < 0.00001f) { return; } if (attack.ApplyForceOnLimbs != null) { foreach (int limbIndex in attack.ApplyForceOnLimbs) { if (limbIndex < 0 || limbIndex >= character.AnimController.Limbs.Length) { continue; } Limb limb = character.AnimController.Limbs[limbIndex]; Vector2 forcePos = limb.pullJoint == null ? limb.body.SimPosition : limb.pullJoint.WorldAnchorA; limb.body.ApplyLinearImpulse( limb.Mass * attack.Force * Vector2.Normalize(attackPosition - SimPosition), forcePos); } } else { Vector2 forcePos = pullJoint == null ? body.SimPosition : pullJoint.WorldAnchorA; body.ApplyLinearImpulse(Mass * attack.Force * Vector2.Normalize(attackPosition - SimPosition), forcePos); } }