using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Photon.Pun; public class UI : MonoBehaviourPun { #region declerations //Car related public GameObject[] cars; public int[] carPrice; Transform carSpawnOffset; private GameObject spawnedCar; bool inCar = false; bool isHeli = false; //Teleport related public Transform[] locations; public int[] teleportPrice; public Transform player; //settings related string chatBlocked = "ChatBlocked"; // scripts PlayerProperties playerProperties; //dance related public int[] dancePrice; public IdleState Idle = new IdleState(); public WalkState Walk = new WalkState(); public RunState Run = new RunState(); public CrouchState Crouch = new CrouchState(); public Dance dance1 = new Dance(); public Dance1 dance2 = new Dance1(); public Dance2 dance3 = new Dance2(); public Dance3 dance4 = new Dance3(); public Dance4 dance5 = new Dance4(); public Dance5 dance6 = new Dance5(); public Dance6 dance7 = new Dance6(); private object[] dances; private MovementBaseState lastDance; //adrelated int adIndex = 0; string coins = "coins"; public GameObject moreCoin; public GameObject adSuccess; public GameObject adFail; //skin related public GameObject[] skins; public int[] skinPrice; public RuntimeAnimatorController playerController; private GameObject currentSkin; private PlayerManager playerManager; //ui elements public GameObject getOutButton; public GameObject getInButton; public GameObject carMenu; public GameObject danceMenu; public GameObject teleportMenu; public GameObject skinMenu; public GameObject chatCanvas; public GameObject bottomBar; GameObject currentCar; public GameObject chatBox; public GameObject settingsMenu; public Color red; public Color def; GameObject breakButton; //helicopter elements public GameObject helicopterMenu; public GameObject helicopter; //timers private WaitForSeconds fiveMinutes = new WaitForSeconds(10f); private WaitForSeconds thirtyMinutes = new WaitForSeconds(1800f); int fiveMinReward = 500; int thirtyMinReward = 3000; public GameObject fiveMinCanva; public GameObject thirtyMinCanva; #endregion //respawn related public Transform respawnPoint; // Start is called before the first frame update public void ManualStart(GameObject p) { currentSkin = p; player = p.GetComponent(); playerProperties = player.GetComponent(); playerManager = p.GetComponent(); carSpawnOffset = player.transform.Find("carspawn"); CloseMenus(); AddCoins(500000); dances = new object[] {dance1,dance2,dance3,dance4,dance5,dance6,dance7 }; StartCoroutine("FiveMinReward"); StartCoroutine("ThirtyMinReward"); chatBox.SetActive(PlayerPrefs.GetInt(chatBlocked) == 0); breakButton = GameObject.Find("UI").transform.Find("HUD/break").gameObject; } // Update is called once per frame void Update() { if (player != null) { UIUpdate(); FallCatcher(); breakButton.SetActive(playerManager.InCar && playerManager.CurrentCar.tag != "Heli"); } } #region UI properties void UIUpdate() { getOutButton.SetActive(playerProperties.InCar); getInButton.SetActive(playerProperties.CollidedCar != null && GetInPermission()); transform.Find("BottomBar/ChatButton").gameObject.GetComponent().color = PlayerPrefs.GetInt(chatBlocked) == 1 ? red : def; transform.Find("BottomBar/CarsButton").gameObject.GetComponent().color = playerProperties.InCar ? red : def; transform.Find("BottomBar/HelicopterButton").gameObject.GetComponent().color = playerProperties.InCar ? red : def; transform.Find("BottomBar/DancesButton").gameObject.GetComponent().color = playerProperties.InCar ? red : def; transform.Find("BottomBar/SkinsButton").gameObject.GetComponent().color = playerProperties.InCar ? red : def; } private void FallCatcher() { float respawnBelowHere = -30f; if (playerManager.InCar && playerManager.CurrentCar.GetComponent().position.y < respawnBelowHere) playerManager.CurrentCar.GetComponent().position = respawnPoint.position; if (!playerManager.InCar && currentSkin.GetComponent().position.y < respawnBelowHere) { currentSkin.GetComponent().enabled = false; currentSkin.GetComponent().position = respawnPoint.position; currentSkin.GetComponent().enabled = true; } } private IEnumerator FiveMinReward() { while (true) { yield return fiveMinutes; AddCoins(fiveMinReward); StartCoroutine("ToggleVisibilityWithDelay", fiveMinCanva); } } private IEnumerator ThirtyMinReward() { while (true) { yield return thirtyMinutes; AddCoins(thirtyMinReward); StartCoroutine("ToggleVisibilityWithDelay", fiveMinCanva); } } private IEnumerator ToggleVisibilityWithDelay(GameObject targetObject) { targetObject.SetActive(true); float fadeInTime = 0.5f; float visibleDuration = 1f; float fadeOutTime = fadeInTime; // Make the object fully visible SetObjectVisibility(targetObject, 1f); yield return new WaitForSeconds(fadeInTime); yield return new WaitForSeconds(visibleDuration); // Make the object fully invisible SetObjectVisibility(targetObject, 0f); yield return new WaitForSeconds(fadeOutTime); targetObject.SetActive(false); } private void SetObjectVisibility(GameObject targetObject, float visibility) { Renderer renderer = targetObject.GetComponent(); if (renderer != null) { Color currentColor = renderer.material.color; currentColor.a = visibility; renderer.material.color = currentColor; } } public void SpawnCar(int car) { carMenu.SetActive(false); if (Coins()< carPrice[car]) { carMenu.SetActive(false); moreCoin.SetActive(true); } else { AddCoins(carPrice[car] * -1); if (spawnedCar != null) PhotonNetwork.Destroy(spawnedCar); Vector3 spawnPoint = new Vector3 (carSpawnOffset.transform.position.x, carSpawnOffset.transform.position.y, carSpawnOffset.transform.position.z); spawnedCar =PhotonNetwork.Instantiate(cars[car].name, spawnPoint, Quaternion.identity); spawnedCar.GetComponent().enabled = false; spawnedCar.GetComponent().HasOwner = true; spawnedCar.GetComponent().OwnerID = playerProperties.ID; spawnedCar.transform.position = spawnPoint; } } public void HelicopterSpawn() { helicopterMenu.SetActive(false); if (Coins()< 30000) { helicopterMenu.SetActive(false); moreCoin.SetActive(true); } else { AddCoins(30000 * -1); if (spawnedCar != null) PhotonNetwork.Destroy(spawnedCar); Vector3 spawnPoint = new Vector3 (carSpawnOffset.transform.position.x, carSpawnOffset.transform.position.y, carSpawnOffset.transform.position.z); spawnedCar =PhotonNetwork.Instantiate(helicopter.name, spawnPoint, Quaternion.identity); spawnedCar.GetComponent().enabled = false; spawnedCar.GetComponent().HasOwner = true; spawnedCar.GetComponent().OwnerID = playerProperties.ID; spawnedCar.transform.position = spawnPoint; } } public bool GetInPermission() { GameObject carC = playerManager.collidedCar; CarProperties carProperties = carC.GetComponent(); if (!carProperties.BeingDriven) { if (carProperties.HasOwner && playerProperties.ID == carProperties.OwnerID) return true; else if (!carProperties.HasOwner) return true; else return false; } else { return false; } } private void AddCoins(int i) { if(playerManager.photonView.IsMine) PlayerPrefs.SetInt(coins, PlayerPrefs.GetInt(coins) + i); } private int Coins() { return PlayerPrefs.GetInt(coins); } private void ShowAd() { GameObject.Find("Ads").GetComponent().ShowRewardedAd(adIndex); if (adIndex == 2) adIndex = 0; else adIndex++; Debug.Log("adIndex: " + adIndex); } void CloseMenus() { carMenu.SetActive(false); danceMenu.SetActive(false); teleportMenu.SetActive(false); chatCanvas.SetActive(false); skinMenu.SetActive(false); helicopterMenu.SetActive(false); moreCoin.SetActive(false); adFail.SetActive(false); adSuccess.SetActive(false); settingsMenu.SetActive(false); fiveMinCanva.SetActive(false); thirtyMinCanva.SetActive(false); } void PriceUpdater(GameObject openMenu, int amount, int[] prices) { for (int i = 0; i < amount; i++) { Transform priceTransform = openMenu.transform.Find("ViewPort/CarContent/Car" + i.ToString() + "/PriceBox/priceText"); priceTransform.gameObject.GetComponent().text = prices[i].ToString(); } } #endregion #region UI mapping public void CarMenu() { if (playerProperties.InCar) { //TODO send message about them having to leave the car return; } bool what = !carMenu.activeInHierarchy; CloseMenus(); carMenu.SetActive(what); if (carMenu.activeInHierarchy) PriceUpdater(carMenu, 8, carPrice); } public void DanceMenu() { if (playerProperties.InCar) { //TODO send message about them having to leave the car return; } bool what = !danceMenu.activeInHierarchy; CloseMenus(); danceMenu.SetActive(what); if (danceMenu.activeInHierarchy) PriceUpdater(danceMenu, 7, dancePrice); } public void TeleportMenu() { bool what = !teleportMenu.activeInHierarchy; CloseMenus(); teleportMenu.SetActive(what); if (teleportMenu.activeInHierarchy) PriceUpdater(teleportMenu, 3, teleportPrice); } public void HelicopterMenu() { if (playerProperties.InCar) { //TODO send message about them having to leave the car return; } bool what = !helicopterMenu.activeInHierarchy; CloseMenus(); helicopterMenu.SetActive(what); } public void SkinMenu() { if (playerProperties.InCar) { //TODO send message about them having to leave the car return; } bool what = !skinMenu.activeInHierarchy; CloseMenus(); skinMenu.SetActive(what); if (skinMenu.activeInHierarchy) PriceUpdater(skinMenu, 7, skinPrice); } public void SettingsMenu() { bool what = !settingsMenu.activeInHierarchy; CloseMenus(); settingsMenu.SetActive(what); Transform chatSettingsButton = gameObject.transform.Find("HUD/Settings/ChatSettingsButton"); bool chatBlock = PlayerPrefs.GetInt(chatBlocked) == 0 ? false : true; gameObject.transform.Find("HUD/Settings/ChatSettingsButton/disableMessages").gameObject.SetActive(!chatBlock); gameObject.transform.Find("HUD/Settings/ChatSettingsButton/enableMessages").gameObject.SetActive(chatBlock); } public void ChatSettingButton() { PlayerPrefs.SetInt(chatBlocked, PlayerPrefs.GetInt(chatBlocked) == 1 ? 0 : 1); bool chatBlock = PlayerPrefs.GetInt(chatBlocked) == 0 ? false : true; gameObject.transform.Find("HUD/Settings/ChatSettingsButton/disableMessages").gameObject.SetActive(!chatBlock); gameObject.transform.Find("HUD/Settings/ChatSettingsButton/enableMessages").gameObject.SetActive(chatBlock); } public void HideButton() { RectTransform upordownimage = transform.Find("HideButton").transform.Find("Image").GetComponent(); Vector3 upordown = upordownimage.eulerAngles; upordown.x = upordown.x == 180.0f ? 0.0f : 180.0f; bottomBar.SetActive(!bottomBar.activeInHierarchy); CloseMenus(); upordownimage.transform.eulerAngles = upordown; } public void HideChatButton() { RectTransform upordownimage = transform.Find("HUD/HideMessages").transform.Find("Image").GetComponent(); Vector3 upordown = upordownimage.eulerAngles; chatBox.SetActive(!chatBox.activeInHierarchy); upordown.z = chatBox.activeInHierarchy ? 90.0f : -90.0f; upordownimage.transform.eulerAngles = upordown; } public void MessageCanvas() { if (PlayerPrefs.GetInt(chatBlocked) == 1) return; bool what = !chatCanvas.activeInHierarchy; CloseMenus(); chatCanvas.SetActive(what); } public void DanceButtons(int i) { if (Coins() < dancePrice[i]) { danceMenu.SetActive(false); moreCoin.SetActive(true); } else { AddCoins(dancePrice[i] * -1); if (lastDance != null) lastDance.ExitState(currentSkin.GetComponent(), Idle); currentSkin.GetComponent().SwitchState(dances[i] as MovementBaseState); Debug.Log(dances[i]); lastDance = dances[i] as MovementBaseState; } } public void TeleportButtons(int i) { if (Coins() < teleportPrice[i]) { teleportMenu.SetActive(false); moreCoin.SetActive(true); } else { AddCoins(teleportPrice[i] * -1); Debug.Log("Teleport Button Clicked"); float offset = 10; Vector3 shuffle = new Vector3(Random.Range(locations[i].position.x - offset, locations[i].position.x + offset), locations[i].position.y, Random.Range(locations[i].position.z - offset, locations[i].position.z + offset)); if (currentSkin.GetComponent().InCar) currentSkin.GetComponent().CurrentCar.GetComponent().transform.position = shuffle; else { Debug.Log("teleport else called"); player.gameObject.GetComponent().enabled = false; player.gameObject.transform.position = shuffle; player.gameObject.GetComponent().enabled = true; } } } public void SkinChange(int i) { //bunun yerini de�i�tir if (currentSkin.GetComponent().InCar) { return; } if (Coins() < skinPrice[i]) { skinMenu.SetActive(false); moreCoin.SetActive(true); } else { AddCoins(skinPrice[i] * -1); Transform tempPos = player; PhotonNetwork.Destroy(player.gameObject); GameObject newSkin = GameObject.Find("Player Spawner").GetComponent().SpawnPlayer(tempPos, skins[i]); playerProperties = newSkin.GetComponent(); player = newSkin.GetComponent(); currentSkin = newSkin; playerProperties.ID = newSkin.GetComponent().ViewID.ToString(); if (spawnedCar != null) spawnedCar.GetComponent().OwnerID = playerProperties.ID; } } public void AdForCoinButton() { ShowAd(); moreCoin.SetActive(false); } public void CoinPlusButton() { moreCoin.SetActive(true); } public void AdError() { adFail.SetActive(true); } public void AdSuccess() { adSuccess.SetActive(true); } public void AdReportExit() { adSuccess.SetActive(false); adFail.SetActive(false); } #endregion }