Fixed the door collision bug at docking ports

This commit is contained in:
Regalis
2016-08-16 20:21:33 +03:00
parent 26b6847bca
commit 7c8e866030
5 changed files with 113 additions and 54 deletions
+1 -1
View File
@@ -549,7 +549,7 @@ namespace Barotrauma
if (bodyShapeTexture == null) if (bodyShapeTexture == null)
{ {
switch (body.bodyShape) switch (body.BodyShape)
{ {
case PhysicsBody.Shape.Rectangle: case PhysicsBody.Shape.Rectangle:
bodyShapeTexture = GUI.CreateRectangle( bodyShapeTexture = GUI.CreateRectangle(
@@ -33,8 +33,12 @@ namespace Barotrauma.Items.Components
private Hull[] hulls; private Hull[] hulls;
private ushort?[] hullIds; private ushort?[] hullIds;
private Door door;
private Body[] bodies; private Body[] bodies;
private Body doorBody;
private Gap gap; private Gap gap;
private ushort? gapId; private ushort? gapId;
@@ -259,6 +263,21 @@ namespace Barotrauma.Items.Components
wire.Connect(recipient, false); wire.Connect(recipient, false);
} }
private void CreateDoorBody()
{
doorBody = BodyFactory.CreateRectangle(GameMain.World,
dockingTarget.door.Body.width,
dockingTarget.door.Body.height,
1.0f,
dockingTarget.door);
doorBody.CollisionCategories = Physics.CollisionWall;
doorBody.BodyType = BodyType.Static;
doorBody.SetTransform(
ConvertUnits.ToSimUnits(item.Position + (dockingTarget.door.Item.WorldPosition - item.WorldPosition)),
0.0f);
}
private void CreateHull() private void CreateHull()
{ {
var hullRects = new Rectangle[] { item.WorldRect, dockingTarget.item.WorldRect }; var hullRects = new Rectangle[] { item.WorldRect, dockingTarget.item.WorldRect };
@@ -266,6 +285,17 @@ namespace Barotrauma.Items.Components
hulls = new Hull[2]; hulls = new Hull[2];
bodies = new Body[4]; bodies = new Body[4];
if (dockingTarget.door != null)
{
CreateDoorBody();
}
if (door != null)
{
dockingTarget.CreateDoorBody();
}
if (IsHorizontal) if (IsHorizontal)
{ {
if (hullRects[0].Center.X > hullRects[1].Center.X) if (hullRects[0].Center.X > hullRects[1].Center.X)
@@ -304,8 +334,6 @@ namespace Barotrauma.Items.Components
gap.linkedTo.Add(hulls[1]); gap.linkedTo.Add(hulls[1]);
gap.linkedTo.Add(hulls[0]); gap.linkedTo.Add(hulls[0]);
} }
} }
else else
{ {
@@ -325,13 +353,6 @@ namespace Barotrauma.Items.Components
hulls[i].AddToGrid(subs[i]); hulls[i].AddToGrid(subs[i]);
if (hullIds[i] != null) hulls[i].ID = (ushort)hullIds[i]; if (hullIds[i] != null) hulls[i].ID = (ushort)hullIds[i];
//for (int j = 0; j < 2; j++)
//{
// bodies[i + j * 2] = BodyFactory.CreateEdge(GameMain.World,
// ConvertUnits.ToSimUnits(new Vector2(hullRects[i].X + hullRects[i].Width * j, hullRects[i].Y)),
// ConvertUnits.ToSimUnits(new Vector2(hullRects[i].X + hullRects[i].Width * j, hullRects[i].Y-hullRects[i].Height)));
//}
} }
gap = new Gap(new Rectangle(hullRects[0].X, hullRects[0].Y+2, hullRects[0].Width, 4), false, subs[0]); gap = new Gap(new Rectangle(hullRects[0].X, hullRects[0].Y+2, hullRects[0].Width, 4), false, subs[0]);
@@ -368,8 +389,6 @@ namespace Barotrauma.Items.Components
body.CollisionCategories = Physics.CollisionWall; body.CollisionCategories = Physics.CollisionWall;
} }
} }
public void Undock() public void Undock()
@@ -408,6 +427,12 @@ namespace Barotrauma.Items.Components
dockingTarget.Undock(); dockingTarget.Undock();
dockingTarget = null; dockingTarget = null;
if (doorBody != null)
{
GameMain.World.RemoveBody(doorBody);
doorBody = null;
}
var wire = item.GetComponent<Wire>(); var wire = item.GetComponent<Wire>();
if (wire != null) if (wire != null)
{ {
@@ -481,7 +506,6 @@ namespace Barotrauma.Items.Components
ConnectWireBetweenPorts(); ConnectWireBetweenPorts();
CreateJoint(true); CreateJoint(true);
if (!item.linkedTo.Any(e => e is Hull) && !dockingTarget.item.linkedTo.Any(e => e is Hull)) if (!item.linkedTo.Any(e => e is Hull) && !dockingTarget.item.linkedTo.Any(e => e is Hull))
@@ -495,6 +519,11 @@ namespace Barotrauma.Items.Components
} }
else else
{ {
if (dockingTarget.door != null && doorBody != null)
{
doorBody.Enabled = dockingTarget.door.Body.Enabled;
}
item.SendSignal(0, "1", "state_out"); item.SendSignal(0, "1", "state_out");
dockingState = MathHelper.Lerp(dockingState, 1.0f, deltaTime * 10.0f); dockingState = MathHelper.Lerp(dockingState, 1.0f, deltaTime * 10.0f);
@@ -564,11 +593,24 @@ namespace Barotrauma.Items.Components
public override void OnMapLoaded() public override void OnMapLoaded()
{ {
foreach (Item it in Item.ItemList)
{
if (it.Submarine != item.Submarine) continue;
var doorComponent = it.GetComponent<Door>();
if (doorComponent == null) continue;
if (Vector2.Distance(item.Position, doorComponent.Item.Position) < Submarine.GridSize.X)
{
this.door = doorComponent;
break;
}
}
if (!item.linkedTo.Any()) return; if (!item.linkedTo.Any()) return;
foreach (MapEntity entity in item.linkedTo) foreach (MapEntity entity in item.linkedTo)
{ {
var hull = entity as Hull; var hull = entity as Hull;
if (hull != null) if (hull != null)
{ {
@@ -651,9 +693,12 @@ namespace Barotrauma.Items.Components
hullIds[1] = message.ReadUInt16(); hullIds[1] = message.ReadUInt16();
gapId = message.ReadUInt16(); gapId = message.ReadUInt16();
if (hulls != null)
{
if (hulls[0] != null) hulls[0].ID = (ushort)hullIds[0]; if (hulls[0] != null) hulls[0].ID = (ushort)hullIds[0];
if (hulls[1] != null) hulls[1].ID = (ushort)hullIds[1]; if (hulls[1] != null) hulls[1].ID = (ushort)hullIds[1];
}
if (gap != null) gap.ID = (ushort)gapId; if (gap != null) gap.ID = (ushort)gapId;
+9 -3
View File
@@ -33,6 +33,11 @@ namespace Barotrauma.Items.Components
private float lastReceivedMessage; private float lastReceivedMessage;
public PhysicsBody Body
{
get { return body; }
}
private float stuck; private float stuck;
public float Stuck public float Stuck
{ {
@@ -145,13 +150,14 @@ namespace Barotrauma.Items.Components
(int)doorSprite.size.X, (int)doorSprite.size.X,
(int)doorSprite.size.Y); (int)doorSprite.size.Y);
body = new PhysicsBody(BodyFactory.CreateRectangle(GameMain.World, body = new PhysicsBody(
ConvertUnits.ToSimUnits(Math.Max(doorRect.Width, 1)), ConvertUnits.ToSimUnits(Math.Max(doorRect.Width, 1)),
ConvertUnits.ToSimUnits(Math.Max(doorRect.Height, 1)), ConvertUnits.ToSimUnits(Math.Max(doorRect.Height, 1)),
1.5f)); 0.0f,
1.5f);
body.CollisionCategories = Physics.CollisionWall;
body.UserData = item; body.UserData = item;
body.CollisionCategories = Physics.CollisionWall;
body.BodyType = BodyType.Static; body.BodyType = BodyType.Static;
body.SetTransform( body.SetTransform(
ConvertUnits.ToSimUnits(new Vector2(doorRect.Center.X, doorRect.Y - doorRect.Height / 2)), ConvertUnits.ToSimUnits(new Vector2(doorRect.Center.X, doorRect.Y - doorRect.Height / 2)),
+1 -1
View File
@@ -101,7 +101,7 @@ namespace Barotrauma.Items.Components
ropeList[i].CollisionCategories = Physics.CollisionItem; ropeList[i].CollisionCategories = Physics.CollisionItem;
ropeList[i].CollidesWith = Physics.CollisionWall; ropeList[i].CollidesWith = Physics.CollisionWall;
ropeBodies[i] = new PhysicsBody(ropeList[i]); //ropeBodies[i] = new PhysicsBody(ropeList[i]);
} }
List<RevoluteJoint> joints = PathManager.AttachBodiesWithRevoluteJoint(GameMain.World, ropeList, List<RevoluteJoint> joints = PathManager.AttachBodiesWithRevoluteJoint(GameMain.World, ropeList,
+37 -29
View File
@@ -42,8 +42,8 @@ namespace Barotrauma
private set; private set;
} }
public readonly Shape bodyShape; private Shape bodyShape;
public readonly float height, width, radius; public float height, width, radius;
private float density; private float density;
@@ -53,6 +53,11 @@ namespace Barotrauma
Vector2 offsetFromTargetPos; Vector2 offsetFromTargetPos;
public Shape BodyShape
{
get { return bodyShape; }
}
public Vector2 TargetPosition public Vector2 TargetPosition
{ {
get { return targetPosition; } get { return targetPosition; }
@@ -192,11 +197,9 @@ namespace Barotrauma
{ {
} }
public PhysicsBody(Body body) public PhysicsBody(float width, float height, float radius, float density)
{ {
this.body = body; CreateBody(width, height, radius, density);
density = 10.0f;
dir = 1.0f; dir = 1.0f;
@@ -213,29 +216,7 @@ namespace Barotrauma
density = ToolBox.GetAttributeFloat(element, "density", 10.0f); density = ToolBox.GetAttributeFloat(element, "density", 10.0f);
if (width != 0.0f && height != 0.0f) CreateBody(width, height, radius, density);
{
body = BodyFactory.CreateRectangle(GameMain.World, width, height, density);
bodyShape = Shape.Rectangle;
}
else if (radius != 0.0f && height != 0.0f)
{
body = BodyFactory.CreateCapsule(GameMain.World, height, radius, density);
bodyShape = Shape.Capsule;
}
else if (radius != 0.0f)
{
body = BodyFactory.CreateCircle(GameMain.World, radius, density);
bodyShape = Shape.Circle;
}
else
{
DebugConsole.ThrowError("Invalid body dimensions in " + element);
}
this.width = width;
this.height = height;
this.radius = radius;
dir = 1.0f; dir = 1.0f;
@@ -257,6 +238,33 @@ namespace Barotrauma
list.Add(this); list.Add(this);
} }
private void CreateBody(float width, float height, float radius, float density)
{
if (width != 0.0f && height != 0.0f)
{
body = BodyFactory.CreateRectangle(GameMain.World, width, height, density);
bodyShape = Shape.Rectangle;
}
else if (radius != 0.0f && height != 0.0f)
{
body = BodyFactory.CreateCapsule(GameMain.World, height, radius, density);
bodyShape = Shape.Capsule;
}
else if (radius != 0.0f)
{
body = BodyFactory.CreateCircle(GameMain.World, radius, density);
bodyShape = Shape.Circle;
}
else
{
DebugConsole.ThrowError("Invalid physics body dimensions (width: " + width + ", height: " + height + ", radius: " + radius + ")");
}
this.width = width;
this.height = height;
this.radius = radius;
}
public void ResetDynamics() public void ResetDynamics()
{ {
body.ResetDynamics(); body.ResetDynamics();