Commit 3a5a4e15 by alsunj

refactor interactables

fixed no animation when interacting with a opened chest
fixed looking for interactables first and then looking for pickupables
fixed looking only for a single interactable/pickupable
parent ac0e80cf
...@@ -22,12 +22,9 @@ public class Chest : NetworkBehaviour, IInteractable ...@@ -22,12 +22,9 @@ public class Chest : NetworkBehaviour, IInteractable
[ServerRpc(RequireOwnership = false)] [ServerRpc(RequireOwnership = false)]
private void ChestFoundServerRpc() private void ChestFoundServerRpc()
{ {
if (!_chestFound.Value)
{
ChestFoundClientRpc(); ChestFoundClientRpc();
_chestFound.Value = true; _chestFound.Value = true;
} }
}
[ClientRpc] [ClientRpc]
private void ChestFoundClientRpc() private void ChestFoundClientRpc()
...@@ -38,8 +35,14 @@ public class Chest : NetworkBehaviour, IInteractable ...@@ -38,8 +35,14 @@ public class Chest : NetworkBehaviour, IInteractable
} }
public void Interact() public bool Interact()
{
if (_chestFound.Value)
{ {
return false;
}
ChestFoundServerRpc(); ChestFoundServerRpc();
return true;
} }
} }
\ No newline at end of file
...@@ -34,9 +34,10 @@ public class Door : NetworkBehaviour, IInteractable ...@@ -34,9 +34,10 @@ public class Door : NetworkBehaviour, IInteractable
.SetEase(Ease.OutBounce); .SetEase(Ease.OutBounce);
} }
public void Interact() public bool Interact()
{ {
RequestOpenDoorServerRpc(); RequestOpenDoorServerRpc();
return true;
} }
[ServerRpc(RequireOwnership = false)] [ServerRpc(RequireOwnership = false)]
......
...@@ -2,5 +2,5 @@ using UnityEngine; ...@@ -2,5 +2,5 @@ using UnityEngine;
public interface IInteractable public interface IInteractable
{ {
public void Interact(); public bool Interact();
} }
\ No newline at end of file
...@@ -334,41 +334,43 @@ public class PlayerController : NetworkBehaviour ...@@ -334,41 +334,43 @@ public class PlayerController : NetworkBehaviour
private void CheckForPickupableAndInteractableCollision() private void CheckForPickupableAndInteractableCollision()
{ {
CheckForInteractableCollision(); if (CheckForInteractableCollision()) return;
CheckForPickupables(); CheckForPickupables();
} }
private void CheckForInteractableCollision()
private bool CheckForInteractableCollision()
{ {
Collider[] hitColliders = Physics.OverlapSphere(transform.position, Collider closestCollider = FindClosestCollider(transform.position,
playerInteractionSettings.interactableRadius, playerInteractionSettings.interactableRadius,
playerInteractionSettings.interactableLayer); playerInteractionSettings.interactableLayer);
foreach (var hitCollider in hitColliders)
if (closestCollider != null)
{ {
switch (hitCollider.GetComponent<IInteractable>()) var interactable = closestCollider.GetComponent<IInteractable>();
if (interactable != null)
{
RotatePlayerTowardsTarget(closestCollider);
if (interactable.Interact())
{ {
case Chest chest:
RotatePlayerTowardsTarget(hitCollider);
_playerManager.playerEvents.PlayerInteract();
chest.Interact();
break;
case Door door:
RotatePlayerTowardsTarget(hitCollider);
_playerManager.playerEvents.PlayerInteract(); _playerManager.playerEvents.PlayerInteract();
door.Interact(); return true;
break;
} }
} }
} }
return false;
}
private void CheckForPickupables() private void CheckForPickupables()
{ {
Collider[] hitColliders = Physics.OverlapSphere(transform.position, Collider closestCollider = FindClosestCollider(transform.position,
playerInteractionSettings.interactableRadius, playerInteractionSettings.interactableRadius,
playerInteractionSettings.pickupableLayer); playerInteractionSettings.pickupableLayer);
foreach (var hitCollider in hitColliders)
if (closestCollider != null)
{ {
switch (hitCollider.GetComponent<Pickupable>()) switch (closestCollider.GetComponent<Pickupable>())
{ {
case Key key: case Key key:
PickupObject(key); PickupObject(key);
...@@ -382,7 +384,6 @@ public class PlayerController : NetworkBehaviour ...@@ -382,7 +384,6 @@ public class PlayerController : NetworkBehaviour
private void PickupObject(Pickupable pickupable) private void PickupObject(Pickupable pickupable)
{ {
// if (!isRightHandFull)
if (!_playerPlacements.IsRightHandFull()) if (!_playerPlacements.IsRightHandFull())
{ {
pickupable.RequestPickupObject( pickupable.RequestPickupObject(
...@@ -393,16 +394,6 @@ public class PlayerController : NetworkBehaviour ...@@ -393,16 +394,6 @@ public class PlayerController : NetworkBehaviour
pickupable.RequestPutDownObject( pickupable.RequestPutDownObject(
new NetworkObjectReference(gameObject.GetComponent<NetworkObject>())); new NetworkObjectReference(gameObject.GetComponent<NetworkObject>()));
} }
//TODO: boolean should be set to true only if the object was picked up
// isRightHandFull = !isRightHandFull;
// }
// else
// {
// pickupable.RequestPutDownObject(transform.position + transform.forward);
// isRightHandFull = !isRightHandFull;
// }
} }
private void CheckForWeapons() private void CheckForWeapons()
...@@ -412,25 +403,10 @@ public class PlayerController : NetworkBehaviour ...@@ -412,25 +403,10 @@ public class PlayerController : NetworkBehaviour
private void HitObject(float weaponHitDamage) private void HitObject(float weaponHitDamage)
{ {
Collider[] hitColliders = Physics.OverlapSphere(transform.position, Collider closestCollider = FindClosestCollider(transform.position,
playerInteractionSettings.interactableRadius, playerInteractionSettings.interactableRadius,
playerInteractionSettings.destructableLayer); playerInteractionSettings.destructableLayer);
if (hitColliders.Length > 0)
{
Collider closestCollider = null;
float closestDistance = float.MaxValue;
foreach (var hitCollider in hitColliders)
{
float distance = Vector3.Distance(transform.position, hitCollider.transform.position);
if (distance < closestDistance)
{
closestDistance = distance;
closestCollider = hitCollider;
}
}
if (closestCollider != null) if (closestCollider != null)
{ {
switch (closestCollider.GetComponent<IDestrucable>()) switch (closestCollider.GetComponent<IDestrucable>())
...@@ -442,13 +418,31 @@ public class PlayerController : NetworkBehaviour ...@@ -442,13 +418,31 @@ public class PlayerController : NetworkBehaviour
break; break;
} }
} }
}
else else
{ {
_playerManager.playerEvents.PlayerAttack(); _playerManager.playerEvents.PlayerAttack();
} }
} }
private Collider FindClosestCollider(Vector3 position, float radius, LayerMask layerMask)
{
Collider[] hitColliders = Physics.OverlapSphere(position, radius, layerMask);
Collider closestCollider = null;
float closestDistance = float.MaxValue;
foreach (var hitCollider in hitColliders)
{
float distance = Vector3.Distance(position, hitCollider.transform.position);
if (distance < closestDistance)
{
closestDistance = distance;
closestCollider = hitCollider;
}
}
return closestCollider;
}
private void RotatePlayerTowardsTarget(Collider hit) private void RotatePlayerTowardsTarget(Collider hit)
{ {
Vector3 direction = (hit.transform.position - transform.position).normalized; Vector3 direction = (hit.transform.position - transform.position).normalized;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment