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

113 lines
2.4 KiB

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Variables;
public class MonsterController : MonoBehaviour, IResettable
{
[SerializeField, Header("References")]
Reference<float> m_light;
[SerializeField]
private GameObject[] m_possibleFrames;
private Transform m_player;
public GameObject m_defaultObject;
[SerializeField]
private float m_distance = 3;
[SerializeField]
private Vector3 m_offset = Vector3.zero;
private bool m_hasChanged = false;
private void OnEnable()
{
m_light.OnValueChanged += OnLightChange;
}
private void OnDisable()
{
m_light.OnValueChanged -= OnLightChange;
}
private void Start()
{
m_player = GameObject.FindGameObjectWithTag("Player").transform;
foreach (Transform child in transform)
{
if (child.gameObject.activeInHierarchy)
m_defaultObject = child.gameObject;
}
}
private void ChangeRandomMonster()
{
GameObject[] inactiveFrames = m_possibleFrames.Where(p => !p.activeInHierarchy).ToArray();
GameObject nextFrame = inactiveFrames[Random.Range(0, inactiveFrames.Length)];
foreach(Transform child in transform)
{
child.gameObject.SetActive(child.gameObject == nextFrame);
}
}
private void ResetMonster()
{
foreach (Transform child in transform)
{
child.gameObject.SetActive(child.gameObject == m_defaultObject);
}
}
private void OnLightChange(float value)
{
float distanceToPlayer = Vector3.Distance(m_player.position, transform.position + m_offset);
if (value <= 0.1 && distanceToPlayer < m_distance && !m_hasChanged)
{
Debug.Log(distanceToPlayer);
ChangeRandomMonster();
m_hasChanged = true;
}
else if (value <= 0)
{
ResetMonster();
}
if (value > 0.5)
{
m_hasChanged = false;
}
}
public void OnLevelLoad()
{
}
public IEnumerator OnResetStart(float time)
{
yield break;
}
public void OnResetEnd()
{
ResetMonster();
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position + m_offset, m_distance);
}
}