using System.Linq; using UnityEngine; using UnityEngine.UI; using Random = UnityEngine.Random; namespace InterfaceOff.WorldScene { public class PlayerController : MonoBehaviour { private static PlayerController Instance; private PlayerFrameInfo[] Frames; [SerializeField] private WindowSpawner ActiveWindowSpawner; [SerializeField] private LineRenderer BulletTracerRenderer; [SerializeField] private Transform BulletTracerStart; [SerializeField] private Transform BulletTracerEnd; [field: SerializeField] public int FrameIndex { get; private set; } [SerializeField] private int[] DeathIndices; [SerializeField] private ExternalCamera ExternalCamera; [SerializeField] private Image FrontLayerImage; [SerializeField] private GameObject StillObject; [SerializeField] private GameObject MovingObject; [SerializeField] private AudioClip[] AudioClips; [SerializeField] private AudioSource PlayerAudioSource; [SerializeField] private HealthBar HealthTracker; public static void PlayRandomErrorSound() { if (DebugUtils.IsNull(Instance)) { return; } int audioIndex = Random.Range(0, Instance.AudioClips.Length); Instance.PlayerAudioSource.PlayOneShot(Instance.AudioClips[audioIndex]); Instance.PlayerAudioSource.pitch = Random.Range(0.9f, 1.1f); } private const int MAX_WINDOWS = 20; private bool PlayerAlive = true; private float LerpValue; private void Awake() { Instance = this; /* Loads the JSON */ Frames = JsonUtility.FromJson(PlayerFrameDump.DUMPED_STRING).FrameInfo; } private void SetAnimationState(bool active) { StillObject.SetActive(active); MovingObject.SetActive(!active); } private void FixedUpdate() { if (PlayerAlive) { /* Iterates the frame index */ FrameIndex = (FrameIndex + 1) % (Frames.Length - 2); if (DeathIndices.Contains(FrameIndex) && HealthTracker.Health <= 0) { ActiveWindowSpawner.StartEndSequence(); PlayerAlive = false; } } } private void Update() { if (PlayerAlive) { /* Updates the position according to the lerp */ LerpValue = (LerpValue + Time.deltaTime * 20f) % 1f; PlayerFrameInfo frameA = Frames[FrameIndex + 0]; PlayerFrameInfo frameB = Frames[FrameIndex + 1]; transform.position = Vector3.Lerp(frameA.Position, frameB.Position, LerpValue); Vector2 rotation = Vector2.Lerp(frameA.Rotation, frameB.Rotation, LerpValue); transform.rotation = Quaternion.Euler(rotation.x, rotation.y, 0); /* Updates the blocking state */ if (ActiveWindowSpawner.SpawnedWindowCount < MAX_WINDOWS) { SetAnimationState(!Frames[FrameIndex].ShootGun); } else { SetAnimationState(true); } } } private void LateUpdate() { FrontLayerImage.material.mainTexture = ExternalCamera.GetTexture(); } } }