campfire/Assets/Scripts/ItemGrabber.cs

90 lines
3.5 KiB
C#
Raw 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-19 16:42:42 +02:00
public LayerMask DroppingRaycastMask;
2020-04-18 22:52:37 +02:00
public float Distance;
public float ThrowVelocity;
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;
2020-04-18 22:52:37 +02:00
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() {
Item Item = null;
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;
2020-04-20 01:39:38 +02:00
if (Physics.Raycast(From, Direction, out Hit, Distance, ItemLayer) &&
Hit.collider.attachedRigidbody != null) {
Item = Hit.collider.attachedRigidbody.GetComponent<Item>();
}
if (Item != null && Input.GetButtonDown("Grab")) {
Item.PickUp(HandTransform, new Vector3(Random.value - 0.5f, Random.value - 0.5f, Random.value - 0.5f) * 0.1f);
GrabbedItems.Add(Item);
}
if (GrabbedItems.Count > 0) {
2020-04-20 01:20:02 +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>();
}
if (Campfire != null && Torch == null && Input.GetButtonDown("Ignite")) {
2020-04-20 01:39:38 +02:00
Item GrabbedItem = GrabbedItems[0];
GrabbedItems.RemoveAt(0);
2020-04-20 01:20:02 +02:00
GameObject TorchObj = Instantiate(TorchPrefab, GrabbedItem.transform.position, GrabbedItem.transform.rotation, TorchHandTransform);
Torch = TorchObj.GetComponent<Torch>();
Destroy(GrabbedItem.gameObject);
2020-04-20 01:39:38 +02:00
}
if (Input.GetButtonDown("Grab") && Item == null) {
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;
}
}
2020-04-20 01:39:38 +02:00
GrabbedItems[0].Drop(DropPosition);
GrabbedItems.RemoveAt(0);
2020-04-18 22:52:37 +02:00
}
}
2020-04-20 01:20:02 +02:00
PrimaryIndicator.alpha = Mathf.Lerp(PrimaryIndicator.alpha, Item != null || Campfire != null ? 1 : 0, 10f * Time.deltaTime);
2020-04-20 01:39:38 +02:00
if (Campfire != null && GrabbedItems.Count > 0) {
PrimaryText.text = $"Burn {GrabbedItems[0].name.ToLower()}";
2020-04-20 01:20:02 +02:00
} else if (Item != null) {
PrimaryText.text = $"Take {Item.name.ToLower()}";
2020-04-18 22:52:37 +02:00
}
2020-04-20 01:39:38 +02:00
SecondaryIndicator.alpha = Mathf.Lerp(SecondaryIndicator.alpha, Campfire != null && GrabbedItems.Count > 0 && Torch == null ? 1 : 0, 10f * Time.deltaTime);
if (GrabbedItems.Count > 0) {
SecondaryText.text = $"Ignite {GrabbedItems[0].name.ToLower()}";
}
2020-04-18 22:52:37 +02:00
}
}