You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using SiegeSong;
namespace SiegeSong
{
public class Selector : MonoBehaviour
{
public WorldObject Target;
public InventoryManager Inventory;
public Text TightSelectorText;
public Image TightSelectorIcon;
public Text LooseSelectorText;
public Image LooseSelectorIcon;
public GameObject LooseSelector;
public CameraManager CameraManager;
public Sprite DefaultIcon;
public bool InteractInputNewState;
private Color dangerColor = Color.red;
private Color defaultColor = Color.white;
private float tightSelectorMaxDistance = 2.0f;
private bool interacting;
void Start()
{
}
void Update()
{
var cameraObject = CameraManager.ActiveCamera;
if (Physics.Raycast(cameraObject.transform.position, cameraObject.transform.forward, out RaycastHit raycastHit))
{
if (Vector3.Distance(cameraObject.transform.position, raycastHit.point) < tightSelectorMaxDistance)
{
var gameWorldObject = raycastHit.transform.gameObject.GetComponent<WorldObject>();
if (gameWorldObject == null)
gameWorldObject = raycastHit.transform.gameObject.GetComponentInChildren<WorldObject>();
if (gameWorldObject == null && raycastHit.transform.parent != null)
gameWorldObject = raycastHit.transform.parent.gameObject.GetComponent<WorldObject>();
if (gameWorldObject == null && raycastHit.transform.parent != null)
gameWorldObject = raycastHit.transform.parent.gameObject.GetComponent<WorldObject>();
if (gameWorldObject == null && raycastHit.transform.parent != null && raycastHit.transform.parent.parent != null)
gameWorldObject = raycastHit.transform.parent.parent.gameObject.GetComponent<WorldObject>();
if (gameWorldObject == null && raycastHit.transform.parent != null && raycastHit.transform.parent.parent != null && raycastHit.transform.parent.parent != null)
gameWorldObject = raycastHit.transform.parent.parent.parent.gameObject.GetComponent<WorldObject>();
if (gameWorldObject != null && gameWorldObject.enabled)
{
Target = gameWorldObject;
TightSelectorText.text = $"{gameWorldObject.Interaction} {gameWorldObject.Name}";
TightSelectorIcon.sprite = gameWorldObject.Icon;
if (gameWorldObject.Danger)
{
TightSelectorText.color = dangerColor;
}
else
{
TightSelectorText.color = defaultColor;
}
if (InteractInputNewState)
{
if (!interacting)
{
interacting = true;
gameWorldObject.Activate(Inventory);
}
}
else
{
interacting = false;
}
}
else
{
Target = null;
TightSelectorText.text = $"";
TightSelectorIcon.sprite = DefaultIcon;
}
}
else
{
Target = null;
TightSelectorText.text = $"";
TightSelectorIcon.sprite = DefaultIcon;
}
}
else
{
Target = null;
TightSelectorText.text = $"";
TightSelectorIcon.sprite = DefaultIcon;
}
}
}
}