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

96 lines
2.2 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
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;
public class ArtefactControl : MonoBehaviour
{
private bool _show;
protected bool _canInteract;
2021-03-03 17:45:09 +01:00
private GUIStyle guiS = new GUIStyle();
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 TMP_Text _text;
// TODO: template system
private void OnEnable()
{
_artefacts.RegisterArtefact(_data);
}
protected virtual void Start()
{
_artefactID = data.artefactID;
_show = data.show;
_canInteract = data.canInteract;
EventHandler.current.onArtefactTriggerEnter += NearArtefact;
EventHandler.current.onArtefactTriggerExit += AwayArtefact;
EventHandler.current.onArtefactPickUp += PickUp;
2021-03-03 17:45:09 +01:00
guiS.fontSize = 22;
guiS.normal.textColor = Color.white;
guiS.alignment = TextAnchor.MiddleCenter;
}
private void LateUpdate()
{
_text.enabled = _show;
}
private void NearArtefact(int id)
{
if (id == this._artefactID)
{
_show = true;
this._canInteract = true;
}
}
private void AwayArtefact(int id)
{
if (id == this._artefactID)
{
_show = false;
_canInteract = 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);
}
}
}
}