/// <summary> /// Helper method to select and give free counter for customer to use. /// </summary> /// <param name="counterIndex">out counter index chosen</param> /// <returns>Counter Game object if found else null</returns> public static GameObject selectRandomUsableCounter(out int counterIndex) { int rem = manager.customerCounters.Count; int currentIndex = 0; while (rem > 0) { currentIndex += UnityEngine.Random.Range(0, rem); int index = currentIndex % manager.customerCounters.Count; CustomerCounter counter = manager.customerCounters[index].GetComponentInChildren <CustomerCounter>(); if (counter.isCounterFree() && !manager.npcControllers[index].IsControllingPawn) { counterIndex = index; return(manager.customerCounters[index]); } rem--; } counterIndex = -1; return(null); }
private void Awake() { // Destroy in case more object exists as it is singleton. if (manager != null) { Destroy(gameObject); return; } HIGHSCORE_SAVE_PATH = Path.Combine(Application.persistentDataPath, "highscores.sav"); manager = this; trashCan.onItemTrashed += itemTrashed; // Creating all Player ,Player controller,Player input manager combination and setting necessary data for them at start of game. foreach (PlayerSpawnData psd in playersToSpawn) { BasicPawn pawn = ((GameObject)Instantiate(playerPrefab, psd.spawnTransform.position, Quaternion.identity)).GetComponent <BasicPawn>(); if (!pawn) { throw new Exception("Add proper Pawn Prefab in game manager"); } pawn.name = psd.playerName; pawn.GetComponent <SpriteRenderer>().color = psd.spriteColor; PlayerController controller = ((GameObject)Instantiate(playerControllerPrefab)).GetComponent <PlayerController>(); if (!controller) { throw new Exception("Add proper Pawn Controller Prefab in game manager"); } controller.name = psd.playerName + "Controller"; InputManager inputManager = new GameObject(controller.name + "InputManager").AddComponent <InputManager>(); playersList.Add(controller.GetID, controller); controller.InputPrefix = psd.playerInputPrefix; controller.playerName = psd.playerName; controller.controlPawn(pawn); controller.setupInputs(inputManager); controller.PlayerState.onScoreChanged += scoreChangedFor; psd.playerHud.PlayerInventory = controller.PlayerInventory; psd.playerHud.PlayerState = controller.PlayerState; psd.stove.userId = psd.dish.userId = controller.GetID; } // Creating all NPC controller needed,One for each counter. foreach (GameObject counterObj in customerCounters) { CustomerCounter counter = counterObj.GetComponentInChildren <CustomerCounter>(); GameObject go = Instantiate(npcControllerPrefab); go.name = counter.name + "NpcController"; BasicController controller = go.GetComponent <BasicController>(); npcControllers.Add(controller); counter.onCustomerLeaving += customerLeft; } resultScreen = gameEndCanvas.GetComponentInChildren <ResultDisplay>(); resultScreen.restartButton.onClick.AddListener(restartLevel); }