First project upload
This commit is contained in:
167
Assets/New Lowpoly Models/Scripts/Vault.cs
Normal file
167
Assets/New Lowpoly Models/Scripts/Vault.cs
Normal file
@@ -0,0 +1,167 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Vault : MonoBehaviour
|
||||
{
|
||||
[Header("Vault Value")]
|
||||
[SerializeField] private int vaultCash = 1000;
|
||||
|
||||
[Header("Interaction")]
|
||||
[SerializeField] private Transform interactionPoint;
|
||||
|
||||
private SimpleTeamAI reservedBy;
|
||||
private bool isBeingOpened;
|
||||
|
||||
private PlayerControllerNewRohan currentPlayer;
|
||||
private CashManager currentCashManager;
|
||||
private bool playerInside;
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (isBeingOpened)
|
||||
return;
|
||||
|
||||
PlayerControllerNewRohan player =
|
||||
other.GetComponent<PlayerControllerNewRohan>();
|
||||
|
||||
if (player == null)
|
||||
return;
|
||||
|
||||
currentPlayer = player;
|
||||
currentCashManager = other.GetComponent<CashManager>();
|
||||
|
||||
playerInside = true;
|
||||
|
||||
MobileInteractButton.Instance?.Show();
|
||||
InteractionManager.CurrentInteraction = OpenVaultForPlayer;
|
||||
|
||||
if (!isBeingOpened)
|
||||
{
|
||||
UI_Manager.Instance?.ShowInteractionPrompt("Press F To Open Vault");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (other.GetComponent<PlayerControllerNewRohan>() == currentPlayer)
|
||||
{
|
||||
playerInside = false;
|
||||
|
||||
currentPlayer?.CancelVaultOpening();
|
||||
|
||||
currentPlayer = null;
|
||||
currentCashManager = null;
|
||||
|
||||
UI_Manager.Instance?.HideActionTimer();
|
||||
MobileInteractButton.Instance?.Hide();
|
||||
InteractionManager.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!playerInside)
|
||||
return;
|
||||
|
||||
|
||||
//if (Input.GetKeyDown(KeyCode.F))
|
||||
//{
|
||||
// OpenVaultForPlayer();
|
||||
//}
|
||||
}
|
||||
|
||||
private void OpenVaultForPlayer()
|
||||
{
|
||||
//if (reservedBy != null)
|
||||
// return;
|
||||
|
||||
if (currentPlayer == null ||
|
||||
currentCashManager == null ||
|
||||
isBeingOpened)
|
||||
return;
|
||||
|
||||
UI_Manager.Instance?.HideActionTimer();
|
||||
|
||||
isBeingOpened = true;
|
||||
|
||||
CharacterController cc =
|
||||
currentPlayer.GetComponent<CharacterController>();
|
||||
|
||||
if (cc != null)
|
||||
cc.enabled = false;
|
||||
|
||||
currentPlayer.transform.position =
|
||||
interactionPoint.position;
|
||||
|
||||
if (cc != null)
|
||||
cc.enabled = true;
|
||||
|
||||
Vector3 lookDir =
|
||||
transform.position - currentPlayer.transform.position;
|
||||
|
||||
lookDir.y = 0;
|
||||
|
||||
if (lookDir.sqrMagnitude > 0.01f)
|
||||
currentPlayer.transform.rotation =
|
||||
Quaternion.LookRotation(lookDir);
|
||||
|
||||
currentPlayer.PlayVaultOpen(() =>
|
||||
{
|
||||
currentCashManager.AddCash(vaultCash);
|
||||
|
||||
VaultSpawner.Instance?.NotifyVaultDestroyed();
|
||||
|
||||
Debug.Log("VAULT OPENED: $" + vaultCash);
|
||||
|
||||
Destroy(gameObject);
|
||||
});
|
||||
}
|
||||
|
||||
public Transform InteractionPoint => interactionPoint;
|
||||
|
||||
public int GetVaultCash()
|
||||
{
|
||||
return vaultCash;
|
||||
}
|
||||
|
||||
public bool IsReserved()
|
||||
{
|
||||
return reservedBy != null || playerInside;
|
||||
}
|
||||
|
||||
public bool TryReserve(SimpleTeamAI ai)
|
||||
{
|
||||
if (isBeingOpened)
|
||||
return false;
|
||||
|
||||
if (reservedBy != null && reservedBy != ai)
|
||||
return false;
|
||||
|
||||
reservedBy = ai;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool IsReservedBy(SimpleTeamAI ai)
|
||||
{
|
||||
return reservedBy == ai;
|
||||
}
|
||||
|
||||
public void MarkBeingOpened(SimpleTeamAI ai)
|
||||
{
|
||||
if (reservedBy == ai)
|
||||
isBeingOpened = true;
|
||||
}
|
||||
|
||||
public void Release(SimpleTeamAI ai)
|
||||
{
|
||||
if (reservedBy == ai)
|
||||
{
|
||||
reservedBy = null;
|
||||
isBeingOpened = false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsPlayerInside()
|
||||
{
|
||||
return playerInside;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user