Playing whack-a-mole with the interaction logic again:

- Fixed server keeping clients focused on entities until they report focusing on something else. This allowed clients to interact with the entity they previously focused on, even if they weren't anywhere near anymore.
- Checking if clients are allowed to focus on an entity before letting them do so (!!!).
- Server doesn't highlight entities when a client focuses on them.
- Characters can interact with a wire if they've selected either of the items it's connected to.
This commit is contained in:
Joonas Rikkonen
2017-07-26 21:03:21 +03:00
parent 520e1df1a5
commit 61b90464dd
3 changed files with 49 additions and 14 deletions
@@ -9,11 +9,7 @@ namespace Barotrauma.Items.Components
public override void UpdateHUD(Character character)
{
if (character != Character.Controlled || character != user) return;
if (Screen.Selected != GameMain.EditMapScreen &&
character.IsKeyHit(InputType.Select) &&
character.SelectedConstruction == this.item) character.SelectedConstruction = null;
if (HighlightedWire != null)
{
HighlightedWire.Item.IsHighlighted = true;
@@ -978,6 +978,16 @@ namespace Barotrauma
return true;
}
public bool CanInteractWith(Character c, float maxDist = 200.0f)
{
if (c == this || !c.enabled || c.info == null || !c.IsHumanoid || !c.CanBeSelected) return false;
maxDist = ConvertUnits.ToSimUnits(maxDist);
if (Vector2.DistanceSquared(SimPosition, c.SimPosition) > maxDist * maxDist) return false;
return true;
}
public bool CanInteractWith(Item item)
{
float distanceToItem;
@@ -995,6 +1005,14 @@ namespace Barotrauma
return CanAccessInventory(item.ParentInventory);
}
Wire wire = item.GetComponent<Wire>();
if (wire != null)
{
//wires are interactable if the character has selected either of the items the wire is connected to
if (wire.Connections[0]?.Item != null && selectedConstruction == wire.Connections[0].Item) return true;
if (wire.Connections[1]?.Item != null && selectedConstruction == wire.Connections[1].Item) return true;
}
if (item.InteractDistance == 0.0f && !item.Prefab.Triggers.Any()) return false;
Pickable pickableComponent = item.GetComponent<Pickable>();
@@ -1126,7 +1144,7 @@ namespace Barotrauma
foreach (Character c in CharacterList)
{
if (c == this || !c.enabled || c.info == null || !c.IsHumanoid || !c.CanBeSelected) continue;
if (!CanInteractWith(c)) continue;
float dist = Vector2.DistanceSquared(mouseSimPos, c.SimPosition);
if (dist < maxDist*maxDist && (closestCharacter == null || dist < closestDist))
@@ -1212,7 +1230,8 @@ namespace Barotrauma
focusedCharacter = null;
return;
}
if ((!isLocalPlayer && IsKeyHit(InputType.Select) && GameMain.Server == null) || (isLocalPlayer && (findFocusedTimer <= 0.0f || Screen.Selected == GameMain.EditMapScreen)))
if ((!isLocalPlayer && IsKeyHit(InputType.Select) && GameMain.Server == null) ||
(isLocalPlayer && (findFocusedTimer <= 0.0f || Screen.Selected == GameMain.EditMapScreen)))
{
focusedCharacter = FindCharacterAtPosition(mouseSimPos);
focusedItem = FindItemAtPosition(mouseSimPos, AnimController.InWater ? 0.5f : 0.25f);
@@ -1223,6 +1242,10 @@ namespace Barotrauma
{
focusedCharacter = null;
}
else
{
focusedItem = null;
}
}
findFocusedTimer = 0.05f;
}
@@ -1241,7 +1264,10 @@ namespace Barotrauma
}
else if (focusedItem != null)
{
focusedItem.IsHighlighted = true;
if (Controlled == this)
{
focusedItem.IsHighlighted = true;
}
focusedItem.TryInteract(this);
}
else if (IsKeyHit(InputType.Select) && selectedConstruction != null)
@@ -144,18 +144,31 @@ namespace Barotrauma
cursorPosition = (ViewTarget == null ? AnimController.AimSourcePos : ViewTarget.Position)
+ new Vector2((float)Math.Cos(aimAngle), (float)Math.Sin(aimAngle)) * 60.0f;
var closestEntity = Entity.FindEntityByID(memInput[memInput.Count - 1].interact);
//reset focus when attempting to use/select something
if (memInput[memInput.Count - 1].states.HasFlag(InputNetFlags.Use) ||
memInput[memInput.Count - 1].states.HasFlag(InputNetFlags.Select))
{
focusedItem = null;
focusedCharacter = null;
}
var closestEntity = FindEntityByID(memInput[memInput.Count - 1].interact);
if (closestEntity is Item)
{
focusedItem = (Item)closestEntity;
focusedCharacter = null;
if (CanInteractWith((Item)closestEntity))
{
focusedItem = (Item)closestEntity;
focusedCharacter = null;
}
}
else if (closestEntity is Character)
{
focusedCharacter = (Character)closestEntity;
focusedItem = null;
if (CanInteractWith((Character)closestEntity))
{
focusedCharacter = (Character)closestEntity;
focusedItem = null;
}
}
memInput.RemoveAt(memInput.Count - 1);
TransformCursorPos();