Files
Inter-Face-Off/Assets/Scripts/EnemyController.cs
2026-01-29 11:52:12 +00:00

98 lines
3.0 KiB
C#

using System.Collections;
using UnityEngine;
using Random = UnityEngine.Random;
namespace InterfaceOff.WorldScene
{
public class EnemyController : MonoBehaviour
{
[SerializeField] private PlayerController Player;
[SerializeField] private int DeathIndex = 500;
[SerializeField] private Rigidbody Body;
[SerializeField] private GameObject RendererObject;
[SerializeField] private GameObject ExplosionPrefab;
[SerializeField] private LineRenderer BulletTracer;
[SerializeField] private GameObject GunStart;
[SerializeField] private GameObject Head;
private Quaternion StartRotation;
private Vector3 StartPosition;
private bool StartedShooting;
private bool WasAliveLastFrame;
private bool Alive = true;
private void Start()
{
StartPosition = transform.position;
StartRotation = transform.rotation;
Body.Sleep();
}
private void Update()
{
/* Updates the death state */
WasAliveLastFrame = Alive;
Alive = DeathIndex >= Player.FrameIndex;
/* Checks if it is about to die or kill */
if (DeathIndex == (Player.FrameIndex + 10) && !StartedShooting)
{
StartCoroutine(routine: ShootyShooty());
StartedShooting = true;
}
/* Checks if it just died */
if (!Alive && WasAliveLastFrame)
{
Body.WakeUp();
Body.AddForce(Random.insideUnitSphere * 250);
StartCoroutine(routine: Suicide());
}
if (Alive)
{
transform.position = StartPosition;
transform.rotation = StartRotation;
Body.velocity = Vector3.zero;
RendererObject.SetActive(true);
Body.Sleep();
}
/* Check for resurrection */
if (Alive && !WasAliveLastFrame)
{
StartedShooting = false;
}
}
private IEnumerator ShootyShooty()
{
BulletTracer.enabled = true;
for (int i = 0; i < 10; i++)
{
BulletTracer.SetPosition(0, Head.transform.position);
BulletTracer.SetPosition(1, Player.transform.position);
BulletTracer.SetPosition(2, GunStart.transform.position);
yield return new WaitForFixedUpdate();
}
BulletTracer.enabled = false;
}
private IEnumerator Suicide()
{
yield return new WaitForSeconds(1f);
RendererObject.SetActive(false);
GameObject explosionGoBoomBoom = Instantiate(ExplosionPrefab, transform);
yield return new WaitForSeconds(1f);
Destroy(explosionGoBoomBoom);
}
}
}