(c793f55af) Clamp velocities when pushing characters out of doorways, play only one damage sound when hit by a door (not once per each limb that's in the doorway), refactored the logic so to prevent creating new lists

This commit is contained in:
Joonas Rikkonen
2019-04-03 16:22:41 +03:00
parent f676e8c67e
commit c751c58495
2 changed files with 61 additions and 60 deletions
@@ -277,7 +277,7 @@ namespace Barotrauma.Items.Components
if (isClosing) if (isClosing)
{ {
PushCharactersAway(); if (OpenState < 0.9f) { PushCharactersAway(); }
} }
else else
{ {
@@ -374,7 +374,6 @@ namespace Barotrauma.Items.Components
return; return;
} }
//push characters out of the doorway when the door is closing/opening
Vector2 simPos = ConvertUnits.ToSimUnits(new Vector2(item.Rect.X, item.Rect.Y)); Vector2 simPos = ConvertUnits.ToSimUnits(new Vector2(item.Rect.X, item.Rect.Y));
Vector2 currSize = IsHorizontal ? Vector2 currSize = IsHorizontal ?
@@ -388,7 +387,7 @@ namespace Barotrauma.Items.Components
if (!c.Enabled) continue; if (!c.Enabled) continue;
if (!MathUtils.IsValid(c.SimPosition)) if (!MathUtils.IsValid(c.SimPosition))
{ {
DebugConsole.ThrowError("Failed to push a character out of a doorway - position of the character \"" + c.Name + "\" is not valid (" + c.SimPosition + ")"); if (GameSettings.VerboseLogging) { DebugConsole.ThrowError("Failed to push a character out of a doorway - position of the character \"" + c.Name + "\" is not valid (" + c.SimPosition + ")"); }
GameAnalyticsManager.AddErrorEventOnce("PushCharactersAway:CharacterPosInvalid", GameAnalyticsSDK.Net.EGAErrorSeverity.Error, GameAnalyticsManager.AddErrorEventOnce("PushCharactersAway:CharacterPosInvalid", GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
"Failed to push a character out of a doorway - position of the character \"" + c.Name + "\" is not valid (" + c.SimPosition + ")." + "Failed to push a character out of a doorway - position of the character \"" + c.Name + "\" is not valid (" + c.SimPosition + ")." +
" Removed: " + c.Removed + " Removed: " + c.Removed +
@@ -397,10 +396,22 @@ namespace Barotrauma.Items.Components
} }
int dir = IsHorizontal ? Math.Sign(c.SimPosition.Y - item.SimPosition.Y) : Math.Sign(c.SimPosition.X - item.SimPosition.X); int dir = IsHorizontal ? Math.Sign(c.SimPosition.Y - item.SimPosition.Y) : Math.Sign(c.SimPosition.X - item.SimPosition.X);
List<PhysicsBody> bodies = c.AnimController.Limbs.Select(l => l.body).ToList(); bool soundPlayed = false;
bodies.Add(c.AnimController.Collider); foreach (Limb limb in c.AnimController.Limbs)
{
if (PushBodyOutOfDoorway(c, limb.body, dir, simPos, simSize) && !soundPlayed)
{
#if CLIENT
SoundPlayer.PlayDamageSound("LimbBlunt", 1.0f, limb.body);
#endif
soundPlayed = true;
}
}
PushBodyOutOfDoorway(c, c.AnimController.Collider, dir, simPos, simSize);
}
}
foreach (PhysicsBody body in bodies) private bool PushBodyOutOfDoorway(Character c, PhysicsBody body, int dir, Vector2 doorRectSimPos, Vector2 doorRectSimSize)
{ {
float diff = 0.0f; float diff = 0.0f;
if (!MathUtils.IsValid(body.SimPosition)) if (!MathUtils.IsValid(body.SimPosition))
@@ -410,54 +421,48 @@ namespace Barotrauma.Items.Components
"Failed to push a character out of a doorway - position of the character \"" + c.Name + "\" is not valid (" + body.SimPosition + ")." + "Failed to push a character out of a doorway - position of the character \"" + c.Name + "\" is not valid (" + body.SimPosition + ")." +
" Removed: " + c.Removed + " Removed: " + c.Removed +
" Remoteplayer: " + c.IsRemotePlayer); " Remoteplayer: " + c.IsRemotePlayer);
continue; return false;
} }
if (IsHorizontal) if (IsHorizontal)
{ {
if (body.SimPosition.X < simPos.X || body.SimPosition.X > simPos.X + simSize.X) continue; if (body.SimPosition.X < doorRectSimPos.X || body.SimPosition.X > doorRectSimPos.X + doorRectSimSize.X) { return false; }
diff = body.SimPosition.Y - item.SimPosition.Y; diff = body.SimPosition.Y - item.SimPosition.Y;
} }
else else
{ {
if (body.SimPosition.Y > simPos.Y || body.SimPosition.Y < simPos.Y - simSize.Y) continue; if (body.SimPosition.Y > doorRectSimPos.Y || body.SimPosition.Y < doorRectSimPos.Y - doorRectSimSize.Y) { return false; }
diff = body.SimPosition.X - item.SimPosition.X; diff = body.SimPosition.X - item.SimPosition.X;
} }
//if the limb is at a different side of the door than the character (collider),
//immediately teleport it to the correct side
if (Math.Sign(diff) != dir) if (Math.Sign(diff) != dir)
{ {
#if CLIENT
SoundPlayer.PlayDamageSound("LimbBlunt", 1.0f, body);
#endif
if (IsHorizontal) if (IsHorizontal)
{ {
body.SetTransform(new Vector2(body.SimPosition.X, item.SimPosition.Y + dir * simSize.Y * 2.0f), body.Rotation); body.SetTransform(new Vector2(body.SimPosition.X, item.SimPosition.Y + dir * doorRectSimSize.Y * 2.0f), body.Rotation);
body.ApplyLinearImpulse(new Vector2(isOpen ? 0.0f : 1.0f, dir * 2.0f));
} }
else else
{ {
body.SetTransform(new Vector2(item.SimPosition.X + dir * simSize.X * 1.2f, body.SimPosition.Y), body.Rotation); body.SetTransform(new Vector2(item.SimPosition.X + dir * doorRectSimSize.X * 1.2f, body.SimPosition.Y), body.Rotation);
body.ApplyLinearImpulse(new Vector2(dir * 0.5f, isOpen ? 0.0f : -1.0f));
} }
} }
//apply an impulse to push the limb further from the door
if (IsHorizontal) if (IsHorizontal)
{ {
if (Math.Abs(body.SimPosition.Y - item.SimPosition.Y) > simSize.Y * 0.5f) continue; if (Math.Abs(body.SimPosition.Y - item.SimPosition.Y) > doorRectSimSize.Y * 0.5f) { return false; }
body.ApplyLinearImpulse(new Vector2(isOpen ? 0.0f : 1.0f, dir * 2.0f), maxVelocity: NetConfig.MaxPhysicsBodyVelocity);
body.ApplyLinearImpulse(new Vector2(isOpen ? 0.0f : 1.0f, dir * 0.5f));
} }
else else
{ {
if (Math.Abs(body.SimPosition.X - item.SimPosition.X) > simSize.X * 0.5f) continue; if (Math.Abs(body.SimPosition.X - item.SimPosition.X) > doorRectSimSize.X * 0.5f) { return false; }
body.ApplyLinearImpulse(new Vector2(dir * 2.0f, isOpen ? 0.0f : -1.0f), maxVelocity: NetConfig.MaxPhysicsBodyVelocity);
body.ApplyLinearImpulse(new Vector2(dir * 0.5f, isOpen ? 0.0f : -1.0f));
} }
c.SetStun(0.2f); c.SetStun(0.2f);
} return true;
}
} }
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item source, Character sender, float power = 0.0f, float signalStrength = 1.0f) public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item source, Character sender, float power = 0.0f, float signalStrength = 1.0f)
@@ -26,10 +26,6 @@ namespace Barotrauma.Items.Components
private float blinkTimer; private float blinkTimer;
private bool itemLoaded;
private float blinkTimer;
public PhysicsBody ParentBody; public PhysicsBody ParentBody;
[Editable(MinValueFloat = 0.0f, MaxValueFloat = 2048.0f), Serialize(100.0f, true)] [Editable(MinValueFloat = 0.0f, MaxValueFloat = 2048.0f), Serialize(100.0f, true)]