private void Interact(Transform target, Vector3 position, Vector3 normal) { var interactable = FindInteractable(target); var held = hands.GetItemInHand(); InteractionReceiver heldInteractionReceiver = null; if (held != null) { heldInteractionReceiver = FindInteractable(held.transform); if (heldInteractionReceiver) { foreach (var kind in heldInteractionsOnClick) { heldInteractionReceiver.Trigger(new InteractionEvent(kind, hands.gameObject, gameObject) .WorldPosition(position).WorldNormal(normal).ForwardTo(interactable) .RunWhile(e => Input.GetButton("Click"))); } } } if (interactable) { foreach (var kind in interactionsOnClick) { interactable.Trigger(new InteractionEvent(kind, hands.gameObject, gameObject) .WorldPosition(position).WorldNormal(normal).WaitFor(heldInteractionReceiver)); } } }
public void SetPlayer(GameObject newPlayer) { _player = newPlayer; _interactionReceiver = _player.GetComponent <InteractionReceiver>(); _playerInteracting = _player.GetComponent <PlayerInteracting>(); }
public override IEnumerator Handle(InteractionEvent e) { if (e.sender == null) { Debug.LogWarning($"Tried to paint null on ({name})"); yield break; } Painter painter = e.sender.GetComponent <Painter>(); if (painter == null) { Debug.LogError($"Tried to paint with {e.sender} without a Painter component.)"); yield break; } PlayerInteractor playerInteractor = e.player.GetComponent <PlayerInteractor>(); if (playerInteractor == null) { Debug.LogError($"Received event from player {e.player} without a PlayerInteractor component."); yield break; } var playerHands = e.player.GetComponent <Hands>(); if (playerHands == null) { Debug.LogWarning($"Player painting with object ({e.sender.name}) does not have a hands component"); yield break; } NetworkInteractionSpawner networkInteractionSpawner = e.player.GetComponent <NetworkInteractionSpawner>(); if (networkInteractionSpawner == null) { Debug.LogError($"Received paint event from player {e.player} without a NetworkInteractionSpawner component."); yield break; } var inventoryUi = FindObjectOfType <UIInventory>(); while (e.runWhile.Invoke(e)) { float remainingSupply = painter.GetRemainingSupplyPercentage(); //Update paint location if (!playerInteractor.GetWorldData(out var hit)) { yield break; } //Stop painting if surface no longer applicable InteractionReceiver receiver = hit.transform.GetComponent <InteractionReceiver>(); if (receiver == null || !receiver.IsListeningForContinuous(this)) { yield break; } //Update remaining supply bar on painter if (inventoryUi) { inventoryUi.SetHeldItemSupply(remainingSupply); } //Decrease supply and spawn new decal. Set to be slightly above collider to avoid clipping. playerInteractor.CmdDecreaseSupplyOfItem(painter.gameObject); Vector3 position = hit.point + hit.normal * 0.01f; Quaternion rotation = Quaternion.FromToRotation(Vector3.back, hit.normal); networkInteractionSpawner.CmdSpawnPainterDecal(painter.Properties.name, position, rotation); if (remainingSupply <= 0) { playerHands.DestroyItemInHand(); yield break; } yield return(new WaitForSeconds(0.01f)); } }