revival/game/Assets/Scripts/Artefacts/ArtefactControl.cs

87 lines
2.1 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using FMOD.Studio;
using Google.Apis.Http;
using UnityEngine;
2021-02-19 21:38:45 +01:00
using Ktyl.Util;
using TMPro;
2021-02-23 14:05:42 +01:00
using UnityEngine.Events;
using UnityEngine.InputSystem;
public class ArtefactControl : MonoBehaviour
{
protected bool _canInteract;
2021-03-02 17:33:31 +01:00
public Artefact data => _data;
[SerializeField] private Artefact _data;
public int artefactID => _artefactID;
private int _artefactID;
2021-02-19 21:38:45 +01:00
[SerializeField] private SerialInt _nearbyArtefactID;
2021-02-23 14:05:42 +01:00
[SerializeField] private ArtefactSystem _artefacts;
[SerializeField] private ArtefactInteractUI _ui;
private void OnEnable()
{
_artefacts.RegisterArtefact(_data);
SetNearby(false);
}
private void SetNearby(bool playerNearby)
{
_canInteract = playerNearby;
_ui.gameObject.SetActive(playerNearby);
_nearbyArtefactID.Value = playerNearby ? artefactID : -1;
}
protected virtual void Start()
{
_artefactID = data.artefactID;
_canInteract = data.canInteract;
EventHandler.current.onArtefactPickUp += PickUp;
}
private void OnTriggerEnter(Collider other)
{
if (!other.gameObject.TryGetComponent(out PlayerInput playerInput))
{
Debug.LogError("collided with not the player ????", this);
return;
}
SetNearby(true);
_ui.Input = playerInput;
}
private void OnTriggerExit(Collider other)
{
SetNearby(false);
}
protected virtual void PickUp(int id)
{
if (id == this._artefactID)
{
if (_canInteract == true)
{
2021-02-23 14:05:42 +01:00
// artefact system informs other systems about found artefact
_artefacts.FindArtefact(data);
}
2021-02-23 14:05:42 +01:00
if (this.gameObject != null)
{
data.Pickup();
2021-03-02 19:20:23 +01:00
_nearbyArtefactID.Value = -1;
_canInteract = false;
2021-03-01 19:09:21 +01:00
EventHandler.current.ArtefactUI();
Destroy(this.gameObject);
}
}
}
}