//do chopping coroutine, used to freeze player for duration of GameConstants.ChopTime IEnumerator DoChopping(CuttingBoardBehaviour cuttingBoard) { yield return(new WaitForSecondsRealtime(GameConstants.ChopTime)); cuttingBoard.CreateSaladFromIngredient(); isChopping = false; cuttingBoard.working = false; }
//Handle the actual interaction void TryInteract(PlayerConstants.InteractMode mode, GameObject nearObj) { //Modes are drop and pickup switch (mode) { case PlayerConstants.InteractMode.Drop: //the player has something to drop? if (heldObjects.Count > 0) { switch (nearObj.tag) { //player is dropping object onto plate case GameConstants.PlateTag: PlateBehaviour plate = nearObj.GetComponent <PlateBehaviour>(); //plate has no object stored if (plate.heldObject.name == "") { var scoreObject = heldObjects.Dequeue(); if (scoreObject is Vegetable vegetable) { plate.heldObject = new Vegetable(vegetable); } else { plate.heldObject = new Salad((Salad)scoreObject); } didInteractThisFrame = true; } break; //player is dropping object onto customer case GameConstants.CustomerTag: CustomerBehaviour customer = nearObj.GetComponent <CustomerBehaviour>(); //customer is active and ready for food if (customer.canAcceptFood) { //dequeue object, getting ready to put it in front of customer var activeObject = heldObjects.Dequeue(); if (activeObject is Salad salad1) { if (customer.submittedFood == null) { customer.submittedFood = salad1; customer.submittingPlayer = this; } //this shouldn't happen, but if there is a salad there already, put the salad we attempt to put there back else { ScoreObject tempObject = null; if (heldObjects.Count > 0) { tempObject = heldObjects.Dequeue(); } heldObjects.Enqueue(activeObject); if (tempObject != null) { heldObjects.Enqueue(tempObject); } } } //we can't give customers raw ingredients, put the thing back if it is not a salad else { ScoreObject tempObject = null; if (heldObjects.Count > 0) { tempObject = heldObjects.Dequeue(); } heldObjects.Enqueue(activeObject); if (tempObject != null) { heldObjects.Enqueue(tempObject); } } } didInteractThisFrame = true; break; //player is dropping object onto cutting board case GameConstants.CuttingBoardTag: CuttingBoardBehaviour cuttingBoard = nearObj.GetComponent <CuttingBoardBehaviour>(); //the cutting board is not busy, so objects can be dropped on it if (!cuttingBoard.working) { var activeObject = heldObjects.Dequeue(); if (activeObject is Salad salad1) { if (cuttingBoard.activeSalad == null) { cuttingBoard.activeSalad = new Salad(salad1); didInteractThisFrame = true; } //there is already a salad there, so we shouldn't place one. Restore queue to previous state else { ScoreObject tempObject = null; if (heldObjects.Count > 0) { tempObject = heldObjects.Dequeue(); } heldObjects.Enqueue(activeObject); if (tempObject != null) { heldObjects.Enqueue(tempObject); } } } else if (activeObject is Vegetable vegetable) { //cutting board has no ingredients, and is ready to start chopping. Do so. if (cuttingBoard.ingredient.name == "") { cuttingBoard.ingredient = new Vegetable(vegetable); ProgressBarBehaviour progressBar = nearObj.GetComponentInChildren <ProgressBarBehaviour>(); progressBar.StartProgressBar(GameConstants.ChopTime); isChopping = true; cuttingBoard.working = true; StartCoroutine("DoChopping", cuttingBoard); didInteractThisFrame = true; } //this shouldn't happen, but just in case restore queue to previous state else { ScoreObject tempObject = null; if (heldObjects.Count > 0) { tempObject = heldObjects.Dequeue(); } heldObjects.Enqueue(activeObject); if (tempObject != null) { heldObjects.Enqueue(tempObject); } } } } break; //player is dropping object onto trash case GameConstants.TrashTag: default: //get scene controller var mainController = FindObjectOfType <MainSceneController>(); var droppedObject = heldObjects.Dequeue(); //deduct points based on what player is throwing away if (droppedObject is Salad salad) { switch (playerNumber) { case 1: mainController.player1Score -= salad.GetIngredientCount() * GameConstants.ScorePerIngredient; break; case 2: mainController.player2Score -= salad.GetIngredientCount() * GameConstants.ScorePerIngredient; break; } } else if (droppedObject is Vegetable) { switch (playerNumber) { case 1: mainController.player1Score -= GameConstants.ScorePerIngredient; break; case 2: mainController.player2Score -= GameConstants.ScorePerIngredient; break; } } break; } } break; //picking up food case PlayerConstants.InteractMode.PickUp: switch (nearObj.tag) { //player is picking up from vegetable dispenser case GameConstants.DispenserTag: VegetableDispenserBehaviour vegetableDispenser = nearObj.GetComponent <VegetableDispenserBehaviour>(); //player has room for another item? if (heldObjects.Count < 2) { heldObjects.Enqueue(new Vegetable(vegetableDispenser.vegetableType)); } didInteractThisFrame = true; break; //player is picking up from plate case GameConstants.PlateTag: PlateBehaviour plate = nearObj.GetComponent <PlateBehaviour>(); //plate has item and player can hold it? if (plate.heldObject.name != "" && heldObjects.Count < 2) { if (plate.heldObject is Vegetable vegetable) { heldObjects.Enqueue(new Vegetable(vegetable)); } else if (plate.heldObject is Salad salad) { heldObjects.Enqueue(new Salad(salad)); } plate.heldObject = new ScoreObject(); } didInteractThisFrame = true; break; case GameConstants.CuttingBoardTag: CuttingBoardBehaviour cuttingBoard = nearObj.GetComponent <CuttingBoardBehaviour>(); //cutting board is not busy? if (!cuttingBoard.working) { //cutting board has item to pick up and player can hold it? if (cuttingBoard.activeSalad != null && heldObjects.Count < 2) { heldObjects.Enqueue(cuttingBoard.activeSalad); cuttingBoard.activeSalad = null; didInteractThisFrame = true; } } break; } break; } StringBuilder stringBuilder = new StringBuilder(); foreach (var vegetable in heldObjects) { stringBuilder.Append(string.Format("{0}\n", vegetable.name)); } //set player text to the names of the items held text.text = stringBuilder.ToString(); }