campfire/Assets/Scripts/ItemGrabber.cs

188 lines
6.9 KiB
C#
Raw Permalink Normal View History

2020-04-18 22:52:37 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2020-04-18 22:52:37 +02:00
public class ItemGrabber : MonoBehaviour {
public Transform CameraTransform;
public Transform HandTransform;
2020-04-20 01:20:02 +02:00
public Transform TorchHandTransform;
public CanvasGroup PrimaryIndicator;
public Text PrimaryText;
public CanvasGroup SecondaryIndicator;
public Text SecondaryText;
2020-04-18 22:52:37 +02:00
public LayerMask ItemLayer;
2020-04-21 01:55:24 +02:00
public LayerMask CasetteLayer;
2020-04-19 16:42:42 +02:00
public LayerMask DroppingRaycastMask;
2020-04-18 22:52:37 +02:00
public float Distance;
2020-04-20 01:20:02 +02:00
[Header("Prefabs")]
public GameObject TorchPrefab;
2020-04-18 22:52:37 +02:00
[Header("Runtime values")]
2020-04-20 01:39:38 +02:00
public List<Item> GrabbedItems;
2020-04-20 01:20:02 +02:00
public Torch Torch;
public GameObject NextTorch;
2020-04-18 22:52:37 +02:00
2020-04-21 01:55:24 +02:00
private CasettePickup PreviousCasette = null;
private CasettePickup Casette = null;
private float LastCasetteHitTime = -1;
2020-04-20 22:50:17 +02:00
private Item LookedAtItem = null;
private float LastItemHitTime = -1;
2020-04-20 01:20:02 +02:00
private void Start() {
PrimaryIndicator.alpha = 0;
SecondaryIndicator.alpha = 0;
2020-04-18 22:52:37 +02:00
}
private void Update() {
2020-04-20 01:20:02 +02:00
Campfire Campfire = null;
2020-04-18 22:52:37 +02:00
RaycastHit Hit;
Vector3 From = CameraTransform.position;
Vector3 Direction = CameraTransform.forward;
Item ThrowableItem = GetThrowableItem();
Item TorchableItem = GetTorchableItem();
bool CanIgniteTorch = Torch != null && TorchableItem != null;
2020-04-20 01:39:38 +02:00
if (Physics.Raycast(From, Direction, out Hit, Distance, ItemLayer) &&
Hit.collider.attachedRigidbody != null) {
2020-04-20 22:50:17 +02:00
LookedAtItem = Hit.collider.attachedRigidbody.GetComponent<Item>();
LastItemHitTime = Time.time;
} else if (Time.time - LastItemHitTime > 0.1f) {
LookedAtItem = null;
2020-04-20 01:39:38 +02:00
}
2020-04-21 01:55:24 +02:00
if (Physics.Raycast(From, Direction, out Hit, Distance, CasetteLayer) &&
Hit.collider.attachedRigidbody != null) {
Casette = Hit.collider.attachedRigidbody.GetComponent<CasettePickup>();
LastCasetteHitTime = Time.time;
} else if (Time.time - LastCasetteHitTime > 0.1f) {
Casette = null;
}
2020-04-20 22:50:17 +02:00
if (Physics.Raycast(From, Direction, out Hit, Distance * 2f, 1 << LayerMask.NameToLayer("Campfire")) &&
Hit.collider.attachedRigidbody != null) {
Campfire = Hit.collider.attachedRigidbody.GetComponent<Campfire>();
CanIgniteTorch |= Campfire != null && TorchableItem != null;
2020-04-20 01:39:38 +02:00
}
bool JustPickedUpCasette = false;
2020-04-21 01:55:24 +02:00
if (Casette != null && Input.GetButtonDown("Grab")) {
Casette.Play(PreviousCasette);
PreviousCasette = Casette;
Casette = null;
JustPickedUpCasette = true;
2020-04-21 01:55:24 +02:00
} else if (Campfire == null && LookedAtItem != null && Input.GetButtonDown("Grab")) {
2020-04-20 22:50:17 +02:00
LookedAtItem.PickUp(HandTransform);
GrabbedItems.Add(LookedAtItem);
}
2020-04-20 01:20:02 +02:00
2020-04-20 22:50:17 +02:00
if (GrabbedItems.Count > 0) {
if (CanIgniteTorch && Input.GetButtonDown("Ignite")) {
if (Torch == null) {
NextTorch = TorchableItem.gameObject;
GrabbedItems.Remove(TorchableItem);
EquipNewTorch();
} else {
// Starts the pass
NextTorch = TorchableItem.gameObject;
// Passing the torch will cause the current one to extinguish,
// and then the new one will be equipped
Torch.PassTorch(TorchableItem.IgnitePoint);
}
2020-04-20 01:39:38 +02:00
}
if (Input.GetButtonDown("Grab") && (LookedAtItem == null || Campfire != null) && ThrowableItem != null && !JustPickedUpCasette) {
2020-04-20 01:20:02 +02:00
Vector3 DropPosition;
if (Campfire != null) {
DropPosition = Campfire.transform.position + Vector3.up * 0.7f;
} else {
if (Physics.Raycast(From, Direction, out Hit, Distance * 2f, DroppingRaycastMask)) {
DropPosition = Hit.point + Hit.normal * 0.1f;
} else {
DropPosition = From + Direction * Distance;
}
}
ThrowableItem.Drop(DropPosition);
GrabbedItems.Remove(ThrowableItem);
2020-04-18 22:52:37 +02:00
}
}
if (Torch != null && !Torch.Burning) {
// Move torch back into the right hand
int NextTorchIndex = -1;
for (int I = 0; I < GrabbedItems.Count; I++) {
if (GrabbedItems[I].gameObject == NextTorch) {
NextTorchIndex = I;
continue;
}
}
// Previous torch's item component
Item TorchItem = Torch.GetComponent<Item>();
TorchItem.PickUp(HandTransform);
if (NextTorchIndex != -1) {
GrabbedItems[NextTorchIndex] = TorchItem;
} else {
GrabbedItems.Add(TorchItem);
}
// Equip new torch if there is one queued
Torch = null;
if (NextTorch != null) {
EquipNewTorch();
}
}
2020-04-23 13:30:36 +02:00
float PrimaryIndicatorTargetAlpha = 1;
2020-04-21 01:55:24 +02:00
if (Casette != null) {
PrimaryText.text = Casette.DisplayPrompt;
} else if (Campfire != null && ThrowableItem != null) {
2020-04-20 20:46:48 +02:00
PrimaryText.text = $"Burn {ThrowableItem.Quality.DisplayName}";
2020-04-20 22:50:17 +02:00
} else if (LookedAtItem != null && Campfire == null) {
PrimaryText.text = $"Take {LookedAtItem.Quality.DisplayName}";
} else if (ThrowableItem != null) {
PrimaryText.text = $"Throw {ThrowableItem.Quality.DisplayName}";
2020-04-23 13:30:36 +02:00
} else {
PrimaryIndicatorTargetAlpha = 0;
2020-04-18 22:52:37 +02:00
}
2020-04-23 13:30:36 +02:00
PrimaryIndicator.alpha = Mathf.Lerp(PrimaryIndicator.alpha, PrimaryIndicatorTargetAlpha, 10f * Time.deltaTime);
2020-04-20 20:46:48 +02:00
if (CanIgniteTorch) {
SecondaryText.text = $"Ignite {TorchableItem.Quality.DisplayName}";
}
2020-04-23 13:30:36 +02:00
SecondaryIndicator.alpha = Mathf.Lerp(SecondaryIndicator.alpha, CanIgniteTorch ? 1 : 0, 10f * Time.deltaTime);
2020-04-18 22:52:37 +02:00
}
private Item GetTorchableItem() {
Item ChosenItem = null;
foreach (Item I in GrabbedItems) {
Torch CurrentTorch = I.GetComponent<Torch>();
if (I.Quality.Ignitable && (CurrentTorch == null || CurrentTorch.Burning)) {
ChosenItem = I;
continue;
}
}
return ChosenItem;
}
private Item GetThrowableItem() {
Item ChosenItem = null;
foreach (Item I in GrabbedItems) {
if (I.gameObject != NextTorch) {
ChosenItem = I;
continue;
}
}
return ChosenItem;
}
private void EquipNewTorch() {
GameObject TorchObj = Instantiate(TorchPrefab, NextTorch.transform.position, NextTorch.transform.rotation, TorchHandTransform);
Torch = TorchObj.GetComponent<Torch>();
Destroy(NextTorch);
NextTorch = null;
}
2020-04-18 22:52:37 +02:00
}