Controllers don't have to be wired directly to an item for camera focus to work (= railguns and cameras can be used over wifi). Closes #535

This commit is contained in:
Joonas Rikkonen
2018-07-31 15:03:20 +03:00
parent d81ee1a27e
commit 82103cf394
5 changed files with 42 additions and 21 deletions
@@ -6,9 +6,7 @@ namespace Barotrauma.Items.Components
{ {
public override void DrawHUD(SpriteBatch spriteBatch, Character character) public override void DrawHUD(SpriteBatch spriteBatch, Character character)
{ {
var focusTarget = GetFocusTarget(); if (focusTarget != null && character.ViewTarget == focusTarget)
if (focusTarget == null) return;
if (character.ViewTarget == focusTarget)
{ {
foreach (ItemComponent ic in focusTarget.components) foreach (ItemComponent ic in focusTarget.components)
{ {
@@ -33,6 +33,9 @@ namespace Barotrauma.Items.Components
private Character character; private Character character;
private Item focusTarget;
private float targetRotation;
public Vector2 UserPos public Vector2 UserPos
{ {
get { return userPos; } get { return userPos; }
@@ -174,7 +177,7 @@ namespace Barotrauma.Items.Components
return true; return true;
} }
public override bool SecondaryUse(float deltaTime, Character character = null) public override bool SecondaryUse(float deltaTime, Character character = null)
{ {
if (this.character != character) if (this.character != character)
@@ -190,7 +193,7 @@ namespace Barotrauma.Items.Components
} }
if (character == null) return false; if (character == null) return false;
Entity focusTarget = GetFocusTarget(); focusTarget = GetFocusTarget();
if (focusTarget == null) if (focusTarget == null)
{ {
Vector2 centerPos = new Vector2(item.WorldRect.Center.X, item.WorldRect.Center.Y); Vector2 centerPos = new Vector2(item.WorldRect.Center.X, item.WorldRect.Center.Y);
@@ -198,9 +201,7 @@ namespace Barotrauma.Items.Components
Vector2 offset = character.CursorWorldPosition - centerPos; Vector2 offset = character.CursorWorldPosition - centerPos;
offset.Y = -offset.Y; offset.Y = -offset.Y;
float targetRotation = MathUtils.WrapAngleTwoPi(MathUtils.VectorToAngle(offset)); targetRotation = MathUtils.WrapAngleTwoPi(MathUtils.VectorToAngle(offset));
item.SendSignal(0, targetRotation.ToString(), "position_out", character);
return false; return false;
} }
@@ -211,7 +212,7 @@ namespace Barotrauma.Items.Components
Lights.LightManager.ViewTarget = focusTarget; Lights.LightManager.ViewTarget = focusTarget;
cam.TargetPos = focusTarget.WorldPosition; cam.TargetPos = focusTarget.WorldPosition;
cam.OffsetAmount = MathHelper.Lerp(cam.OffsetAmount, (focusTarget as Item).Prefab.OffsetOnSelected, deltaTime*10.0f); cam.OffsetAmount = MathHelper.Lerp(cam.OffsetAmount, (focusTarget as Item).Prefab.OffsetOnSelected, deltaTime * 10.0f);
} }
#endif #endif
@@ -232,9 +233,7 @@ namespace Barotrauma.Items.Components
Vector2 offset = character.CursorWorldPosition - centerPos; Vector2 offset = character.CursorWorldPosition - centerPos;
offset.Y = -offset.Y; offset.Y = -offset.Y;
float targetRotation = MathUtils.WrapAngleTwoPi(MathUtils.VectorToAngle(offset)); targetRotation = MathUtils.WrapAngleTwoPi(MathUtils.VectorToAngle(offset));
item.SendSignal(0, targetRotation.ToString(), "position_out", character);
} }
return true; return true;
@@ -242,17 +241,16 @@ namespace Barotrauma.Items.Components
private Item GetFocusTarget() private Item GetFocusTarget()
{ {
foreach (Connection c in item.Connections) item.SendSignal(0, targetRotation.ToString(), "position_out", character);
{
if (c.Name != "position_out") continue;
foreach (Connection c2 in c.Recipients) for (int i = item.LastSentSignalRecipients.Count - 1; i >= 0; i--)
{
if (item.LastSentSignalRecipients[i].Prefab.FocusOnSelected)
{ {
if (c2 == null || c2.Item == null || !c2.Item.Prefab.FocusOnSelected) continue; return item.LastSentSignalRecipients[i];
return c2.Item;
} }
} }
return null; return null;
} }
@@ -160,6 +160,11 @@ namespace Barotrauma.Items.Components
if (recipient == null) continue; if (recipient == null) continue;
if (recipient.item == this.item || recipient.item == source) continue; if (recipient.item == this.item || recipient.item == source) continue;
if (source != null && !source.LastSentSignalRecipients.Contains(recipient.item))
{
source.LastSentSignalRecipients.Add(recipient.item);
}
foreach (ItemComponent ic in recipient.item.components) foreach (ItemComponent ic in recipient.item.components)
{ {
ic.ReceiveSignal(stepsTaken, signal, recipient, item, sender, power); ic.ReceiveSignal(stepsTaken, signal, recipient, item, sender, power);
@@ -95,6 +95,16 @@ namespace Barotrauma.Items.Components
foreach (WifiComponent wifiComp in receivers) foreach (WifiComponent wifiComp in receivers)
{ {
wifiComp.item.SendSignal(stepsTaken, signal, "signal_out", sender); wifiComp.item.SendSignal(stepsTaken, signal, "signal_out", sender);
if (source != null)
{
foreach (Item receiverItem in wifiComp.item.LastSentSignalRecipients)
{
if (!source.LastSentSignalRecipients.Contains(receiverItem))
{
source.LastSentSignalRecipients.Add(receiverItem);
}
}
}
} }
break; break;
} }
@@ -268,7 +268,16 @@ namespace Barotrauma
return IsInWater(); return IsInWater();
} }
} }
/// <summary>
/// A list of items the last signal sent by this item went through
/// </summary>
public List<Item> LastSentSignalRecipients
{
get;
private set;
} = new List<Item>();
public ItemPrefab Prefab public ItemPrefab Prefab
{ {
get { return prefab; } get { return prefab; }
@@ -1011,9 +1020,10 @@ namespace Barotrauma
} }
} }
} }
public void SendSignal(int stepsTaken, string signal, string connectionName, Character sender, float power = 0.0f) public void SendSignal(int stepsTaken, string signal, string connectionName, Character sender, float power = 0.0f)
{ {
LastSentSignalRecipients.Clear();
if (connections == null) return; if (connections == null) return;
stepsTaken++; stepsTaken++;