2023-04-30 00:04:18 -04:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2023-04-22 18:44:01 -04:00
|
|
|
using UnityEngine;
|
|
|
|
using RPGCreationKit.Player;
|
|
|
|
using RPGCreationKit.CellsSystem;
|
2023-04-30 00:04:18 -04:00
|
|
|
using TMPro;
|
2023-04-22 18:44:01 -04:00
|
|
|
|
|
|
|
namespace RPGCreationKit
|
|
|
|
{
|
|
|
|
public class LockpickingManager : MonoBehaviour
|
|
|
|
{
|
|
|
|
public static LockpickingManager instance;
|
|
|
|
private void Awake()
|
|
|
|
{
|
2023-04-30 00:04:18 -04:00
|
|
|
if (instance == null)
|
2023-04-22 18:44:01 -04:00
|
|
|
instance = this;
|
|
|
|
else
|
|
|
|
Debug.LogError("Anomaly detected with the singleton pattern of 'LockpickingManager', are you using multiple LockpickingManager?");
|
|
|
|
}
|
|
|
|
|
|
|
|
private Door curDoor;
|
2023-05-05 23:02:18 -04:00
|
|
|
private LootingPoint curLootingPoint;
|
2023-04-30 00:04:18 -04:00
|
|
|
|
2023-05-05 23:02:18 -04:00
|
|
|
[Space(8)]
|
2023-04-22 18:44:01 -04:00
|
|
|
|
2023-04-30 00:04:18 -04:00
|
|
|
[Header("\tLockpicking References")]
|
|
|
|
[SerializeField] private GameObject lockpickingUI;
|
2023-05-05 23:02:18 -04:00
|
|
|
[SerializeField] private Transform LockPickUI;
|
|
|
|
[SerializeField] private GameObject LockPickBack;
|
2023-04-30 00:04:18 -04:00
|
|
|
[SerializeField] public Item LockPick;
|
2023-05-05 23:02:18 -04:00
|
|
|
[SerializeField] private GameObject PadLock;
|
|
|
|
[SerializeField] private GameObject DoorLock;
|
2023-04-30 00:04:18 -04:00
|
|
|
[SerializeField] private TextMeshProUGUI PicksLeft;
|
2023-05-05 23:02:18 -04:00
|
|
|
[SerializeField] private SpinVert PadlockSV;
|
|
|
|
[SerializeField] private SpinVert DoorSV;
|
2023-04-30 00:04:18 -04:00
|
|
|
[SerializeField] private List<GameObject> ThingsToHide;
|
2023-04-22 18:44:01 -04:00
|
|
|
|
2023-05-05 23:02:18 -04:00
|
|
|
[Space(8)]
|
2023-04-22 18:44:01 -04:00
|
|
|
|
2023-04-30 00:04:18 -04:00
|
|
|
[Header("\tAudio")]
|
|
|
|
[SerializeField] private AudioSource audioSource;
|
2023-04-22 18:44:01 -04:00
|
|
|
|
2023-05-05 23:02:18 -04:00
|
|
|
[Space(8)]
|
2023-04-22 18:44:01 -04:00
|
|
|
|
2023-04-30 00:04:18 -04:00
|
|
|
[SerializeField] private AudioClip pickAttempt;
|
|
|
|
[SerializeField] private AudioClip pickBroke;
|
|
|
|
[SerializeField] private AudioClip pickSuccess;
|
|
|
|
[SerializeField] private AudioClip LockUnlocking;
|
2023-05-05 23:02:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
[Header("\tStarting Positions")]
|
|
|
|
[SerializeField] private Vector3 PickAngle;
|
|
|
|
[SerializeField] private Vector3 OrigPos;
|
|
|
|
[SerializeField] private float MinAngle = -70f;
|
|
|
|
[SerializeField] private float MaxAngle = 70f;
|
2023-04-22 18:44:01 -04:00
|
|
|
|
|
|
|
|
|
|
|
private bool isShaking = false;
|
2023-05-05 23:02:18 -04:00
|
|
|
private bool isBroken = false;
|
2023-04-22 18:44:01 -04:00
|
|
|
private float shakeTime = 0f;
|
|
|
|
private float angle = 0f;
|
2023-05-05 23:02:18 -04:00
|
|
|
private float angleX = 0f;
|
2023-04-22 18:44:01 -04:00
|
|
|
private float target = 0f;
|
|
|
|
private float acceptableRange = 0f;
|
|
|
|
private float direction = 0f;
|
2023-05-05 23:02:18 -04:00
|
|
|
private bool isDoor = false;
|
|
|
|
private SpinVert RealSV;
|
2023-04-22 18:44:01 -04:00
|
|
|
|
|
|
|
// Durability is Static, and Held between lockpicking sessions, so that they cant exit then return to lockpicking to reset durability
|
|
|
|
private static int durability = 0;
|
2023-05-05 23:02:18 -04:00
|
|
|
|
|
|
|
void Start() {
|
|
|
|
PickAngle = LockPickUI.transform.rotation.eulerAngles;
|
|
|
|
}
|
2023-04-22 18:44:01 -04:00
|
|
|
|
2023-05-05 23:02:18 -04:00
|
|
|
public void StartPicking(Door _door)
|
|
|
|
{
|
2023-04-22 18:44:01 -04:00
|
|
|
if (_door.lockLevel == DoorLockLevel.Impossible) return;
|
|
|
|
curDoor = _door;
|
2023-05-05 23:02:18 -04:00
|
|
|
isDoor = true;
|
|
|
|
RealSV = DoorSV;
|
2023-04-22 18:44:01 -04:00
|
|
|
switch (_door.lockLevel)
|
|
|
|
{
|
|
|
|
case DoorLockLevel.VeryEasy:
|
2023-05-05 23:02:18 -04:00
|
|
|
acceptableRange = 50f; //25
|
2023-04-22 18:44:01 -04:00
|
|
|
break;
|
|
|
|
case DoorLockLevel.Easy:
|
2023-05-05 23:02:18 -04:00
|
|
|
acceptableRange = 38f; //20
|
2023-04-22 18:44:01 -04:00
|
|
|
break;
|
|
|
|
case DoorLockLevel.Medium:
|
2023-05-05 23:02:18 -04:00
|
|
|
acceptableRange = 26f; //13
|
2023-04-22 18:44:01 -04:00
|
|
|
break;
|
|
|
|
case DoorLockLevel.Hard:
|
2023-05-05 23:02:18 -04:00
|
|
|
acceptableRange = 15f; //8
|
2023-04-22 18:44:01 -04:00
|
|
|
break;
|
|
|
|
case DoorLockLevel.VeryHard:
|
2023-05-05 23:02:18 -04:00
|
|
|
acceptableRange = 3f; //3
|
2023-04-22 18:44:01 -04:00
|
|
|
break;
|
|
|
|
}
|
2023-05-05 23:02:18 -04:00
|
|
|
|
|
|
|
Begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void StartPicking(LootingPoint _LootingPoint)
|
|
|
|
{
|
|
|
|
if (_LootingPoint.lockLevel == ContainerLockLevel.Impossible) return;
|
|
|
|
curLootingPoint = _LootingPoint;
|
|
|
|
isDoor = false;
|
|
|
|
RealSV = PadlockSV;
|
|
|
|
switch (_LootingPoint.lockLevel)
|
|
|
|
{
|
|
|
|
case ContainerLockLevel.VeryEasy:
|
|
|
|
acceptableRange = 50f; //25
|
|
|
|
break;
|
|
|
|
case ContainerLockLevel.Easy:
|
|
|
|
acceptableRange = 38f; //20
|
|
|
|
break;
|
|
|
|
case ContainerLockLevel.Medium:
|
|
|
|
acceptableRange = 26f; //13
|
|
|
|
break;
|
|
|
|
case ContainerLockLevel.Hard:
|
|
|
|
acceptableRange = 15f; //8
|
|
|
|
break;
|
|
|
|
case ContainerLockLevel.VeryHard:
|
|
|
|
acceptableRange = 3f; //3
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Begin() {
|
|
|
|
|
|
|
|
acceptableRange = (acceptableRange + EntityAttributes.PlayerAttributes.attributes.Dexterity);
|
|
|
|
Debug.Log("acceptableRange: "+acceptableRange);
|
|
|
|
|
|
|
|
PickAngle = LockPickUI.transform.rotation.eulerAngles;
|
|
|
|
OrigPos = PickAngle;
|
|
|
|
|
|
|
|
MinAngle = PickAngle.y - 70;
|
|
|
|
MaxAngle = PickAngle.y + 70;
|
|
|
|
|
|
|
|
|
|
|
|
RealSV.ResetRotation();
|
|
|
|
PicksLeft.text = Inventory.PlayerInventory.GetItemCount(LockPick).ToString();
|
|
|
|
if (!Inventory.PlayerInventory.HasItem(LockPick.ItemID))
|
|
|
|
return;
|
|
|
|
|
|
|
|
angle = PickAngle.y;
|
|
|
|
target = Random.Range(MinAngle, MaxAngle);
|
|
|
|
if (durability <= 0)
|
|
|
|
durability = 5; // 5 attempts per Lockpick
|
2023-04-22 18:44:01 -04:00
|
|
|
|
|
|
|
RckPlayer.instance.input.SwitchCurrentActionMap("LockpickingUI");
|
2023-05-05 23:02:18 -04:00
|
|
|
|
|
|
|
DoorLock.SetActive(isDoor);
|
|
|
|
PadLock.SetActive(!isDoor);
|
2023-04-22 18:44:01 -04:00
|
|
|
lockpickingUI.SetActive(true);
|
|
|
|
|
|
|
|
RckPlayer.instance.isPickingLock = true;
|
2023-04-30 00:04:18 -04:00
|
|
|
for (int i = 0; i < ThingsToHide.Count; i++)
|
|
|
|
ThingsToHide[i].SetActive(false);
|
2023-04-22 18:44:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FixedUpdate()
|
|
|
|
{
|
|
|
|
if (isShaking)
|
|
|
|
{
|
|
|
|
float distance = Mathf.Abs(angle - target);
|
2023-04-30 00:04:18 -04:00
|
|
|
|
2023-05-05 23:02:18 -04:00
|
|
|
PickAngle = LockPickUI.transform.rotation.eulerAngles;
|
|
|
|
PickAngle.x = Mathf.Clamp((Random.Range(-distance, distance) / 90f) * 10f, -10, 10);
|
|
|
|
PickAngle.y = angle;
|
|
|
|
LockPickUI.rotation = Quaternion.Euler(PickAngle);
|
2023-04-22 18:44:01 -04:00
|
|
|
shakeTime -= Time.fixedDeltaTime;
|
2023-05-05 23:02:18 -04:00
|
|
|
if (shakeTime <= 0) {
|
2023-04-22 18:44:01 -04:00
|
|
|
isShaking = false;
|
2023-05-05 23:02:18 -04:00
|
|
|
PickAngle = LockPickUI.transform.rotation.eulerAngles;
|
|
|
|
PickAngle.x = angleX;
|
|
|
|
LockPickUI.rotation = Quaternion.Euler(PickAngle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isBroken) {
|
|
|
|
//this is where I build an animation making it look like it is broke
|
|
|
|
shakeTime -= Time.fixedDeltaTime; // May as well use this as the breaking animation time
|
|
|
|
if (shakeTime <= 0) { // show it broken, then vanish for 1 second, and come back new. need to figure out how to reset rotation...
|
|
|
|
if (LockPickUI.gameObject.activeSelf) {
|
|
|
|
LockPickUI.gameObject.SetActive(false);
|
|
|
|
shakeTime = 1f;
|
|
|
|
|
|
|
|
Vector3 TempV = LockPickUI.transform.rotation.eulerAngles; // Reset Angle
|
|
|
|
//TempV.x = TempV.x - 90f;
|
|
|
|
LockPickBack.transform.rotation = Quaternion.Euler(TempV);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
isBroken = false;
|
|
|
|
//LockPickBack.SetActive(true);
|
|
|
|
LockPickUI.gameObject.SetActive(true);
|
|
|
|
|
|
|
|
LockPickUI.rotation = Quaternion.Euler(OrigPos);
|
|
|
|
angle = OrigPos.y;
|
|
|
|
|
|
|
|
durability = 5; // Resets the picks health
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-22 18:44:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
2023-04-30 00:04:18 -04:00
|
|
|
if (!RckPlayer.instance.isPickingLock)
|
|
|
|
return;
|
2023-04-22 18:44:01 -04:00
|
|
|
|
|
|
|
if (isShaking)
|
|
|
|
return;
|
|
|
|
|
2023-05-05 23:02:18 -04:00
|
|
|
if (isBroken)
|
|
|
|
return;
|
|
|
|
|
2023-04-22 18:44:01 -04:00
|
|
|
direction = -RckPlayer.instance.input.currentActionMap.FindAction("Move").ReadValue<Vector2>().x;
|
|
|
|
|
|
|
|
angle += direction * Time.deltaTime * 16.0f;
|
2023-05-05 23:02:18 -04:00
|
|
|
angle = Mathf.Clamp(angle, MinAngle, MaxAngle);
|
2023-04-30 00:04:18 -04:00
|
|
|
|
2023-05-05 23:02:18 -04:00
|
|
|
LockPickUI.rotation = Quaternion.Euler(PickAngle.x, angle, PickAngle.z);
|
2023-04-22 18:44:01 -04:00
|
|
|
|
|
|
|
if (RckPlayer.instance.input.currentActionMap.FindAction("Attempt").triggered)
|
|
|
|
{
|
|
|
|
Attempt();
|
|
|
|
}
|
|
|
|
else if (RckPlayer.instance.input.currentActionMap.FindAction("Close").triggered)
|
|
|
|
{
|
|
|
|
StopPicking();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Attempt()
|
|
|
|
{
|
|
|
|
if (!RckPlayer.instance.isPickingLock) return;
|
|
|
|
if (durability <= 0) return;
|
|
|
|
|
|
|
|
durability--;
|
|
|
|
|
|
|
|
// Check if successfull attempt
|
2023-05-05 23:02:18 -04:00
|
|
|
Debug.Log("Angle: "+angle+"; Min: "+(target - acceptableRange)+"; Max: "+(target + acceptableRange));
|
2023-04-22 18:44:01 -04:00
|
|
|
if(angle > target - acceptableRange && angle < target + acceptableRange)
|
|
|
|
{
|
2023-04-30 00:04:18 -04:00
|
|
|
// Success!
|
|
|
|
StartCoroutine(UnlockLock());
|
2023-04-22 18:44:01 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-30 00:04:18 -04:00
|
|
|
if (durability <= 0) // Breaks Pick. Lets make the pick in 2 pieces and animate a break
|
2023-04-22 18:44:01 -04:00
|
|
|
{
|
2023-04-30 00:04:18 -04:00
|
|
|
Inventory.PlayerInventory.RemoveItem(LockPick.ItemID, 1);
|
|
|
|
PicksLeft.text = Inventory.PlayerInventory.GetItemCount(LockPick).ToString();
|
2023-04-22 18:44:01 -04:00
|
|
|
if (audioSource != null)
|
|
|
|
audioSource.PlayOneShot(pickBroke);
|
2023-04-30 00:04:18 -04:00
|
|
|
if (Inventory.PlayerInventory.GetItemCount(LockPick) < 1)
|
|
|
|
StopPicking();
|
2023-05-05 23:02:18 -04:00
|
|
|
|
|
|
|
isBroken = true; // this makes Lateupdate fire, which fires off Attempt();
|
|
|
|
shakeTime = 2f; // Making this 0 so it just skips the shaking. We need an animation here instead (New Function)
|
|
|
|
angleX = PickAngle.x;
|
|
|
|
|
|
|
|
Vector3 TempV = LockPickBack.transform.rotation.eulerAngles; // Reset Angle
|
|
|
|
TempV.x = TempV.x + 45f;
|
|
|
|
LockPickBack.transform.rotation = Quaternion.Euler(TempV);
|
|
|
|
|
|
|
|
// LockPickBack.SetActive(false);
|
2023-04-22 18:44:01 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
isShaking = true;
|
|
|
|
shakeTime = 1f;
|
|
|
|
if(audioSource != null)
|
|
|
|
audioSource.PlayOneShot(pickAttempt);
|
|
|
|
}
|
|
|
|
}
|
2023-04-30 00:04:18 -04:00
|
|
|
|
|
|
|
public void CheatUnlock() {
|
|
|
|
StartCoroutine(UnlockLock());
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator UnlockLock() {
|
2023-05-05 23:02:18 -04:00
|
|
|
|
|
|
|
EntityAttributes.PlayerAttributes.attributes.DexterityPoints += 1;
|
|
|
|
int A = EntityAttributes.PlayerAttributes.attributes.DexterityPoints;
|
|
|
|
int B = EntityAttributes.PlayerAttributes.attributes.Dexterity;
|
|
|
|
while (A >= (B*3)) {
|
|
|
|
EntityAttributes.PlayerAttributes.attributes.Dexterity += 1;
|
|
|
|
EntityAttributes.PlayerAttributes.attributes.DexterityPoints = (A-(B*3));
|
|
|
|
A = EntityAttributes.PlayerAttributes.attributes.DexterityPoints;
|
|
|
|
B = EntityAttributes.PlayerAttributes.attributes.Dexterity;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDoor) {
|
|
|
|
curDoor.UnlockDoor();
|
|
|
|
} else {
|
|
|
|
curLootingPoint.UnlockContainer();
|
|
|
|
}
|
|
|
|
|
|
|
|
RealSV.StartSpinning();
|
2023-04-30 00:04:18 -04:00
|
|
|
if (audioSource != null)
|
|
|
|
audioSource.PlayOneShot(LockUnlocking);
|
|
|
|
yield return new WaitForSecondsRealtime(1.5f);
|
|
|
|
StopPicking();
|
|
|
|
}
|
2023-04-22 18:44:01 -04:00
|
|
|
|
|
|
|
public void StopPicking()
|
|
|
|
{
|
2023-04-30 00:04:18 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < ThingsToHide.Count; i++)
|
|
|
|
ThingsToHide[i].SetActive(true);
|
2023-04-22 18:44:01 -04:00
|
|
|
lockpickingUI.SetActive(false);
|
|
|
|
isShaking = false;
|
|
|
|
|
|
|
|
RckPlayer.instance.isPickingLock = false;
|
|
|
|
|
|
|
|
curDoor = null;
|
2023-05-05 23:02:18 -04:00
|
|
|
curLootingPoint = null;
|
|
|
|
|
2023-04-22 18:44:01 -04:00
|
|
|
RckPlayer.instance.input.SwitchCurrentActionMap("Player");
|
2023-05-05 23:02:18 -04:00
|
|
|
|
|
|
|
LockPickUI.rotation = Quaternion.Euler(OrigPos);
|
2023-04-22 18:44:01 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|