Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- using UnityEngine.UI;
- using System.Linq;
- public class GameManager : MonoBehaviour
- {
- public List<GameObject> _currentCustomers;
- public List<GameObject> currentCustomers
- {
- get
- {
- if (_currentCustomers == null || _currentCustomers.Count == 0)
- {
- _currentCustomers = new List<GameObject>();
- }
- List<GameObject> temp = new List<GameObject>(_currentCustomers);
- foreach (GameObject customer in temp)
- {
- if (customer == null)
- {
- _currentCustomers.RemoveAt(temp.IndexOf(customer));
- }
- }
- return _currentCustomers;
- }
- set
- {
- _currentCustomers = value;
- }
- }
- public Transform customerStartPosition;
- public GameObject customerPrefab;
- [Header("UI references")]
- public GameObject startUI;
- public GameObject gameOverUI;
- public GameObject levelEndUI;
- public GameObject buttons;
- public TextMeshPro levelHeader;
- [Header("Timers")]
- // timers
- public float spawnTimer;
- public float waveTimer;
- public int currentSpawned = 0;
- public int currentCustomer;
- //public LevelData currentDay;
- public int currentWaveNum;
- public int currentLevelNum;
- public LevelData currentLevel;
- public bool playing = true;
- bool spawnedWave;
- public int spawnedInWave = 0;
- public float materialUsage = 0f;
- public List<LevelData> levelData;
- void Start()
- {
- levelData = UniversalData.levelData;
- levelData = levelData.OrderBy(x => float.Parse((float)x.name)).ToList();
- foreach (LevelData level in UniversalData.levelData)
- {
- foreach(Wave wave in level.waves)
- {
- wave.GenerateCustomers();
- }
- }
- //dayData = UniversalData.dayData;
- /*
- if (FileBasedPrefs.HasKey("BestScore"))
- {
- bestScore = FileBasedPrefs.GetFloat("BestScore");
- }
- gameOverUI.transform.Find("Panel").Find("BestScore").GetComponent<TextMeshPro>().text = "" + bestScore;
- */
- }
- public GameObject robberPrefab;
- public Transform robberSpawnPoint;
- public void SpawnRobber() {
- var robber = Instantiate(robberPrefab);
- robber.transform.position = robberSpawnPoint.position;
- }
- public void RegisterMaterialUsage()
- {
- materialUsage += 1f;
- }
- public void DisplayMenu()
- {
- DisableUI();
- startUI.SetActive(true);
- }
- void DisableUI()
- {
- levelEndUI.SetActive(false);
- gameOverUI.SetActive(false);
- startUI.SetActive(false);
- }
- public void RestartCurrentLevel()
- {
- DisableUI();
- SelectLevelAndStart(currentLevelNum);
- }
- public void NextLevel()
- {
- DisableUI();
- currentLevelNum++;
- SelectLevelAndStart(currentLevelNum);
- }
- // toppings and patience
- public void GameOver()
- {
- playing = false;
- DisableUI();
- gameOverUI.SetActive(true);
- buttons.SetActive(true);
- // make sure you show the player they died
- }
- void ClearWorkspace()
- {
- string[] tags =
- {
- "Dough",
- "Topping",
- "Pizza",
- "RollingPin",
- "Receiver",
- "PizzaBox",
- "Customer",
- "Meat",
- };
- foreach (string tag in tags)
- {
- GameObject[] gameObjects = GameObject.FindGameObjectsWithTag(tag);
- foreach (GameObject gameObj in gameObjects)
- {
- Destroy(gameObj);
- }
- }
- }
- public void StartLevel()
- {
- UniversalData.playerManager.ResetStats();
- // reset lines
- ClearWorkspace();
- UniversalData.lineManager.ClearLines();
- _currentCustomers = null;
- spawnTimer = 0f;
- //waveTimer = 1.5f;
- currentSpawned = 0;
- currentWaveNum = 0;
- currentCustomer = 0;
- spawnedInWave = 0;
- currentLevel = levelData[currentLevelNum];
- currentLevel.waves[0].GenerateCustomers();
- waveTimer = 0f;
- playing = true;
- }
- public void SelectLevelAndStart(int level)
- {
- cd = 2f;
- levelHeader.text = "Level: " + (level + 1);
- levelHeader.color = new Color(levelHeader.color.r, levelHeader.color.g, levelHeader.color.b, 1);
- currentLevelNum = level;
- StartLevel();
- }
- int CalculateStars(float revenue, float materialUsage)
- {
- int stars = 0;
- float materialCost = materialUsage;
- float gross = revenue - materialCost;
- if (gross > currentLevel.scoreThreeStar)
- {
- stars = 3;
- }
- else if(gross > currentLevel.scoreTwoStar)
- {
- stars = 2;
- }
- else if(gross > currentLevel.scoreOneStar)
- {
- stars = 1;
- }
- //levelData
- return stars;
- }
- void EndOfLevel()
- {
- DisableUI();
- int stars = CalculateStars(UniversalData.playerManager.money, materialUsage);
- levelEndUI.SetActive(true);
- float earnings = UniversalData.playerManager.money;
- float materialsCost = -materialUsage;
- float bonus = 0f;
- float score = earnings + materialsCost + bonus;
- float bestScore = 0f;
- if (FileBasedPrefs.HasKey("BestScore" + currentLevelNum))
- {
- bestScore = FileBasedPrefs.GetFloat("BestScore");
- if (score > bestScore)
- {
- FileBasedPrefs.SetFloat("BestScore" + currentLevelNum, score);
- bestScore = score;
- }
- }
- else if(score > bestScore)
- {
- bestScore = score;
- }
- if (UniversalData.playerManager.reputation == UniversalData.playerManager.maxRep)
- {
- bonus += earnings * 0.1f;
- bonus = Mathf.Round(bonus);
- }
- Transform panel = levelEndUI.transform.Find("Panel");
- panel.Find("Earnings").Find("Value").GetComponent<TextMeshProUGUI>().text = "+$" + earnings;
- panel.Find("Materials").Find("Value").GetComponent<TextMeshProUGUI>().text = "-$" + materialsCost;
- panel.Find("Bonus").Find("Value").GetComponent<TextMeshProUGUI>().text = "+$" + bonus;
- panel.Find("Final").GetComponent<TextMeshProUGUI>().text = "Final: " + score;
- panel.Find("Best").GetComponent<TextMeshProUGUI>().text = "Best: " + bestScore;
- panel.Find("Stars").Find("Background").GetComponent<Image>().fillAmount = (float)stars / 3f;
- buttons.SetActive(true);
- // display stars, next if got what is needed, next, and also ssaving best score, if unlocked saving that
- //endOfRoundUI.transform.Find("Panel").Find("InfoText").GetComponent<TextMeshPro>().text = "info";
- }
- public float cd = 0.5f;
- private void Update()
- {
- cd -= Time.deltaTime;
- if(cd <= 0f)
- {
- levelHeader.color = Color.Lerp(levelHeader.color, new Color(levelHeader.color.r, levelHeader.color.g, levelHeader.color.b, 0f), Time.deltaTime * 4f);
- }
- if (Input.GetKeyDown(KeyCode.R))
- {
- SpawnRobber();
- }
- if(Input.GetKeyDown(KeyCode.S))
- {
- StartLevel();
- }
- if(playing)
- {
- if(currentSpawned < currentLevel.GetCustomerCount())
- {
- if (spawnTimer > 0f)
- {
- spawnTimer -= Time.deltaTime;
- }
- if (waveTimer > 0f && spawnedInWave == 0)
- {
- waveTimer -= Time.deltaTime;
- }
- // if no wave timer cooldown
- if (waveTimer <= 0f)
- {
- if (spawnTimer <= 0f)
- {
- //print(currentDay.spawnCooldown);
- spawnTimer = currentLevel.waves[currentWaveNum].customerSpawnInterval;
- SpawnCustomer(currentLevel.waves[currentWaveNum].customers[currentCustomer]);
- // basaew
- currentSpawned++;
- spawnedInWave++;
- }
- // go to next wave, if spawned all the customers, and cooldown has finished
- if (spawnedInWave == currentLevel.waves[currentWaveNum].customers.Count && waveTimer <= 0f)
- {
- spawnedInWave = 0;
- spawnTimer = 0f;
- waveTimer = currentLevel.waves[currentWaveNum].timeToNextWave;
- currentWaveNum++;
- }
- }
- }
- // if spawned all the customers..
- else if(currentSpawned == currentLevel.GetCustomerCount() && currentCustomers.Count == 0)
- {
- EndOfLevel();
- playing = false;
- }
- }
- }
- void SpawnCustomer(CustomerData customerData)
- {
- var customer = Instantiate(customerPrefab);
- customer.transform.position = customerStartPosition.position;
- customer.GetComponent<Customer>().InitCustomerData(customerData);
- currentCustomers.Add(customer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement