105 lines
3.2 KiB
C#
105 lines
3.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class World : MonoBehaviour
|
|
{
|
|
public static World Instance { get; private set; }
|
|
|
|
//number of tiles in the game world
|
|
public const int WORLD_SIZE_X = 2000;
|
|
public const int WORLD_SIZE_Y = 2000;
|
|
|
|
private WorldObject[] objects;
|
|
|
|
private float lastCullTime;
|
|
private const float CULL_TIME = 0.5f;
|
|
|
|
private TMP_Text objectInteractMessage;
|
|
|
|
void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
objects = FindObjectsOfType<WorldObject>();
|
|
objectInteractMessage = UIManager.Instance.objectInteractMessage;
|
|
|
|
for (int i = 0; i < objects.Length; i++)
|
|
{
|
|
if (objects[i].destroyOnStart)
|
|
objects[i].gameObject.SetActive(false);
|
|
}
|
|
lastCullTime = Time.time;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void LateUpdate()
|
|
{
|
|
//ground items have presidence over object interaction.
|
|
if (GroundItemManager.InteractingWithItem)
|
|
{
|
|
objectInteractMessage.text = "";
|
|
return;
|
|
}
|
|
|
|
if (Time.time - lastCullTime > CULL_TIME)
|
|
{
|
|
if (Player.Instance != null)
|
|
{
|
|
bool interactableInRange = false;
|
|
for (int i = 0; i < objects.Length; i++)
|
|
{
|
|
float distance = Vector3.Distance(objects[i].transform.position, Player.Instance.transform.position);
|
|
if (distance > Config.RENDER_DISTANCE && !objects[i].largeObject)
|
|
{
|
|
objects[i].gameObject.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
if (!objects[i].destroyOnStart)
|
|
objects[i].gameObject.SetActive(true);
|
|
|
|
}
|
|
|
|
if (objects[i].interactiable && !interactableInRange)
|
|
{
|
|
if (distance < objects[i].interactDistance)
|
|
{
|
|
interactableInRange = true;
|
|
Player.Instance.interactableWorldObject = objects[i].gameObject;
|
|
|
|
if (objectInteractMessage != null)
|
|
{
|
|
objectInteractMessage.text = InputMessageHandler.Instance.Interpolate($"{objects[i].interactMessage}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Player.Instance.interactableWorldObject = null;
|
|
if (objectInteractMessage != null)
|
|
{
|
|
objectInteractMessage.text = "";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
lastCullTime = Time.time;
|
|
}
|
|
}
|
|
|
|
public float getHeight(Vector3 position)
|
|
{
|
|
Terrain terrain = Misc.GetClosestCurrentTerrain(position);
|
|
float height = terrain.SampleHeight(position);
|
|
|
|
return height;
|
|
}
|
|
|
|
|
|
}
|