using UnityEngine; using UnityEngine.Events; using System; using System.Collections.Generic; using System.Collections; using TMPro; using Oculus.Platform; using Oculus.Platform.Models; using Platform = Oculus.Platform; using Random = UnityEngine.Random; using NaughtyAttributes; using System.Text; public class BoxUI : MonoBehaviour { public bool active; public List uiButtons; public List uiSteppers; public List stepRanges; [Serializable] public class Stepper { public int[] steps; public int defaultIndex; } [Header("References")] public Main main; public Transform uiCursor; public GameObject tape, menuMain, menuPause, menuGameOver, menuConfig; public AudioSource audioSource; public AudioClip sfxHover, sfxClick, sfxBack; public TextMeshPro usernameText, leaderboardText, scoreText, trackText; [Header("Runtime")] public int cursorIndex; Color trackColor; void Awake() { if (main.sideQuest) { leaderboardText.transform.parent.gameObject.SetActive(false); usernameText.transform.parent.gameObject.SetActive(false); } else { if (!UnityEngine.Application.isEditor) { Core.AsyncInitialize(); Entitlements.IsUserEntitledToApplication().OnComplete(AreThey); } } } void AreThey(Message msg) { if (msg.IsError) { Debug.Log("Not Verified"); UnityEngine.Application.Quit(); } else { Debug.Log("Verified"); Platform.Users.GetLoggedInUser().OnComplete(GetUsername); } } string username = "anon"; void GetUsername(Message user) { username = user.Data.OculusID; UpdateLabel(); } public void UpdateLabel() { int highscore = PlayerPrefs.GetInt("highscore", 0); usernameText.text = username + "\n" + highscore.ToString("000") + " Highscore Ave"; if (username != "anon") { if (!main.sideQuest) { Leaderboards.WriteEntry("AnyBPM", (uint)highscore); leaderboardText.text = "loading..."; StartCoroutine(GetEntriesDelay()); } } } IEnumerator GetEntriesDelay() { yield return new WaitForSeconds(3); Leaderboards.GetEntries("AnyBPM", 3, LeaderboardFilterType.None, LeaderboardStartAt.Top).OnComplete(UpdateLeaderboard); } void UpdateLeaderboard(Message msg) { string text = ""; if (!msg.IsError) { foreach (LeaderboardEntry entry in msg.Data) { text += entry.Score.ToString("000") + " "; text += entry.User.OculusID + "\n"; } } leaderboardText.text = text; } void Start() { Rig.OnReady += RigReady; Main.OnGameStart += GameStart; Game.OnCrash += Crash; Screen.SetResolution(1280, 720, false); UpdateLabel(); // PlayerPref Defaults int playCount = PlayerPrefs.GetInt("PlayCount", 0); if (playCount == 0) { for (int i = 0; i < uiSteppers.Count; i++) { PlayerPrefs.SetInt(uiSteppers[i].name, stepRanges[i].defaultIndex); } } playCount++; PlayerPrefs.SetInt("PlayCount", playCount); Debug.Log("Play Count: " + playCount); Color backup = main.graphXData.snakeMat.GetColor("_Backup"); Color customColor = new Color( PlayerPrefs.GetFloat("rValue", backup.r), PlayerPrefs.GetFloat("gValue", backup.g), PlayerPrefs.GetFloat("bValue", backup.b) ); main.graphXData.snakeMat.SetColor("_Base", customColor); ApplyPref(); TiltAll(); } void ApplyPref() { for (int i = 0; i < uiSteppers.Count; i++) { int index = PlayerPrefs.GetInt(uiSteppers[i].name); int value = stepRanges[i].steps[index]; uiSteppers[i].GetComponentInChildren().text = "" + value; switch (i) { // CONFIG MENU case 0: if (index > 1 && PlayerPrefs.GetInt("BoxFills", 0) == 0) { index = 0; PlayerPrefs.SetInt(uiSteppers[i].name, index); value = stepRanges[i].steps[index]; uiSteppers[i].GetComponentInChildren().text = "" + value; } main.bpm = value; main.soundData.musicTrack = main.soundData.musicTracks[index]; break; case 1: QualitySettings.antiAliasing = value; break; case 2: main.soundData.musicVolume = (float)value / 5f; break; case 3: main.soundData.sfxVolume = (float)value / 5f; break; } } } void RigReady() { trackText.text = main.soundData.musicLoop.name; trackColor.a = 0.5f; } void GameStart(Main main) { trackText.text = main.soundData.musicTrack.name; trackColor.a = 0.5f; } void Crash(Vector3 dir) { if (!main.aiMode) { string endText; int oldHighscore = PlayerPrefs.GetInt("highscore", 0); int score = main.snake.Count + main.core.Count; float total = ( (1f + main.design.bounds.x * 2f) * (1f + main.design.bounds.y * 2f) * (1f + main.design.bounds.z * 2f) ) / 100f; Debug.Log(total); if (score >= oldHighscore) { endText = (score / total).ToString("00.0") + "%\n^-^"; PlayerPrefs.SetInt("highscore", score); UpdateLabel(); } else { endText = (score / total).ToString("00.0") + "%\n-_-"; // / " + oldHighscore.ToString("000") + "\n-_-"; } // Use for unlocks and tracking uiButtons[7].gameObject.SetActive(PlayerPrefs.GetInt("BoxFills", 0) > 0); active = true; menuGameOver.SetActive(true); menuGameOver.GetComponentInChildren().text = endText; } } bool backDown; int oldScore = 0; void Update() { backDown = OVRInput.GetDown(OVRInput.Button.Two, main.rig.remote); if (main.snake.Count != oldScore) { scoreText.text = "" + main.snake.Count.ToString("000") + ""; oldScore = main.snake.Count; } scoreText.transform.forward = main.rig.head.forward; scoreText.gameObject.SetActive(!main.aiMode); trackColor.a -= Time.deltaTime / 9; trackText.color = trackColor; } [Header("Editor")] public float tiltDegrees = 4; [Button] public void TiltAll() { Tilt(uiButtons); Tilt(uiSteppers); } void Tilt(List transforms) { foreach (Transform t in transforms) { t.rotation = Quaternion.Euler(0, t.eulerAngles.y, Random.Range(-tiltDegrees, tiltDegrees)); } } Vector2 touchPos; void LateUpdate() { tape.SetActive(active); uiCursor.gameObject.SetActive(active); if (active) { int firstActive = -1; int lastActive = -1; for (int i = 0; i < uiButtons.Count; i++) { if (uiButtons[i].gameObject.activeInHierarchy) { if (firstActive < i) { if (lastActive == -1) { firstActive = i; } lastActive = i; } } } for (int i = 0; i < uiSteppers.Count; i++) { if (uiSteppers[i].gameObject.activeInHierarchy) { lastActive++; } } // SWIPE SYSTEM FOR OCULUS GO AND GEAR VR if (OVRInput.Get(OVRInput.Touch.PrimaryTouchpad, main.rig.remote)) { touchPos = OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad, main.rig.remote); } if (OVRInput.GetUp(OVRInput.Touch.PrimaryTouchpad, main.rig.remote)) { // check direction if (touchPos.x < 0) { if (cursorIndex > 0) { cursorIndex--; } else { cursorIndex = lastActive - firstActive; // check } } else { cursorIndex++; } audioSource.PlayOneShot(sfxHover); } if (OVRInput.GetDown(OVRInput.Button.PrimaryThumbstickLeft, main.rig.remote)) { if (cursorIndex > 0) { cursorIndex--; } else { cursorIndex = lastActive - firstActive; // check } audioSource.PlayOneShot(sfxHover); } if (OVRInput.GetDown(OVRInput.Button.PrimaryThumbstickRight, main.rig.remote)) { cursorIndex++; audioSource.PlayOneShot(sfxHover); } if (firstActive + cursorIndex > lastActive) { cursorIndex = 0; } if (firstActive + cursorIndex < uiButtons.Count) { uiCursor.position = uiButtons[firstActive + cursorIndex].position; } else { uiCursor.position = uiSteppers[firstActive + cursorIndex - uiButtons.Count].position; } // uiCursor.position = hit.point * 3; uiCursor.localPosition = new Vector3( Mathf.Clamp(uiCursor.localPosition.x, -4.4f, 4.4f), Mathf.Clamp(uiCursor.localPosition.y, -4.4f, 4.4f), -0.2f ); int buttonIndex = -1; for (int i = 0; i < uiButtons.Count; i++) { uiButtons[i].localScale = Vector3.one; if (uiButtons[i].gameObject.activeInHierarchy && uiButtons[i].localPosition.x + 0.5f > uiCursor.localPosition.x && uiButtons[i].localPosition.x - 0.5f < uiCursor.localPosition.x && uiButtons[i].localPosition.y + 0.5f > uiCursor.localPosition.y && uiButtons[i].localPosition.y - 0.5f < uiCursor.localPosition.y) { buttonIndex = i; } } if (buttonIndex > -1) { uiButtons[buttonIndex].localScale = Vector3.one * 1.3f; if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger, main.rig.remote)) { audioSource.PlayOneShot(sfxClick); switch (buttonIndex) { // MENU MAIN case 0: // play main.aiMode = false; menuMain.SetActive(false); active = false; main.restart = 1; break; case 1: // config menuConfig.SetActive(true); menuMain.SetActive(false); break; case 2: // quit UnityEngine.Application.Quit(); Debug.Log("Quit"); break; // MENU PAUSE case 3: // RESUME main.playing = true; menuPause.SetActive(false); active = false; break; case 4: // RESTART menuPause.SetActive(false); active = false; main.restart = 1; break; case 5: // HOME menuPause.SetActive(false); menuMain.SetActive(true); main.aiMode = true; main.restart = 1; break; // MENU GAMEOVER case 6: // RESTART menuGameOver.SetActive(false); active = false; main.restart = 1; break; case 7: // COLOR Color randomColor = new Color(Random.value, Random.value, Random.value); main.graphXData.snakeMat.SetColor("_Base", randomColor); PlayerPrefs.SetFloat("rValue", randomColor.r); PlayerPrefs.SetFloat("gValue", randomColor.r); PlayerPrefs.SetFloat("bValue", randomColor.r); break; case 8: // HOME menuGameOver.SetActive(false); menuMain.SetActive(true); main.aiMode = true; main.restart = 1; break; // MENU CONFIG case 9: // HOME menuConfig.SetActive(false); menuMain.SetActive(true); break; default: break; } } } int stepperIndex = -1; for (int i = 0; i < uiSteppers.Count; i++) { uiSteppers[i].localScale = Vector3.one; if (uiSteppers[i].gameObject.activeInHierarchy && uiSteppers[i].localPosition.x + 0.5f > uiCursor.localPosition.x && uiSteppers[i].localPosition.x - 0.5f < uiCursor.localPosition.x && uiSteppers[i].localPosition.y + 0.5f > uiCursor.localPosition.y && uiSteppers[i].localPosition.y - 0.5f < uiCursor.localPosition.y) { stepperIndex = i; } } if (stepperIndex > -1) { uiSteppers[stepperIndex].localScale = Vector3.one * 1.2f; if (OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger, main.rig.remote)) { audioSource.PlayOneShot(sfxClick); int step = PlayerPrefs.GetInt(uiSteppers[stepperIndex].name); if (step < stepRanges[stepperIndex].steps.Length - 1) { step++; } else { step = 0; } PlayerPrefs.SetInt(uiSteppers[stepperIndex].name, step); ApplyPref(); } } if (backDown) { if (menuConfig.activeSelf) { menuConfig.SetActive(false); menuMain.SetActive(true); } else if (menuPause.activeSelf) { main.playing = true; menuPause.SetActive(false); active = false; } else if (menuGameOver.activeSelf) { menuGameOver.SetActive(false); active = false; main.restart = 1; } } } else { if (backDown) { main.playing = false; menuPause.SetActive(true); active = true; } } if (backDown) { audioSource.PlayOneShot(sfxBack); } } }