public void CraftItem() { // return if the window isn't enabled if (!enabled) { return; } // craft the item at the current index CraftingRecipe recipe = _recipes [_index]; if ((_inventory.resources.scrap < recipe.ScrapCost || _inventory.resources.energy < recipe.EnergyCost || _inventory.resources.wire < recipe.WireCost) && !DebugEnabled) { _ui.ShowAlert("Not enough resources to craft", "", 1f); return; } // craft Scraps if (recipe.PrefabName.Equals("Prefabs/Scrap")) { _inventory.resources.scrap += 1; _inventory.resources.scrap -= recipe.ScrapCost; _inventory.resources.energy -= recipe.EnergyCost; _inventory.resources.wire -= recipe.WireCost; string alertMessage = recipe.CraftedItemName + " successfully converted"; _ui.ShowAlert(alertMessage, "", 1f); return; } else if (recipe.PrefabName.Equals("Prefabs/Energy")) { _inventory.resources.energy += 1; _inventory.resources.scrap -= recipe.ScrapCost; _inventory.resources.energy -= recipe.EnergyCost; _inventory.resources.wire -= recipe.WireCost; string alertMessage = recipe.CraftedItemName + " successfully converted"; _ui.ShowAlert(alertMessage, "", 1f); return; } else if (recipe.PrefabName.Equals("Prefabs/Wire")) { _inventory.resources.wire += 1; _inventory.resources.scrap -= recipe.ScrapCost; _inventory.resources.energy -= recipe.EnergyCost; _inventory.resources.wire -= recipe.WireCost; string alertMessage = recipe.CraftedItemName + " successfully converted"; _ui.ShowAlert(alertMessage, "", 1f); return; } Item newItem = Resources.Load(recipe.PrefabName, typeof(Item)) as Item; // see if the item exists if (newItem == null) { Debug.Log(recipe.PrefabName + " is not the name of an Item prefab"); _ui.ShowAlert("Invalid item", "", 1f); return; } // see if it was added to the inventory if (_inventory.AddItem(newItem, 1)) { _inventory.resources.scrap -= recipe.ScrapCost; _inventory.resources.energy -= recipe.EnergyCost; _inventory.resources.wire -= recipe.WireCost; string alertMessage = recipe.CraftedItemName + " successfully crafted"; _ui.ShowAlert(alertMessage, "", 1f); } else { _ui.ShowAlert("Item already owned", "", 1f); } }
// Update is called once per frame void Update() { switch (currentState) { case GameState.START: if (!_hasShownAlert) { _ui.ShowAlert("WAVE " + wave.ToString(), "Press Enter", (wave == 1?float.PositiveInfinity:5)); _hasShownAlert = true; } if (Input.GetKeyDown(KeyCode.Return)) { timer = 0.0f; spawnTimer = 0.0f; currentState = GameState.DEFEND; // Enable the player playerObj.GetComponent <PlayerMovement>().enabled = true; waveTimer = (defendDuration / waveInfo.numberOfWaves); Debug.Log("Game State: " + currentState.ToString()); } break; case GameState.DEFEND: // Update the time timer += Time.deltaTime; spawnTimer += Time.deltaTime; _hasShownAlert = false; // If the player dies if (playerMngr.currentHealth <= 0) { currentState = GameState.END; ClearMapEntities(); Debug.Log("Game State: " + currentState.ToString()); } // Split wave spawn rate to = duration of wave / number of waves if (spawnTimer > waveTimer && timer <= defendDuration) { SpawnNextWave(); spawnTimer = 0.0f; Debug.Log("Spawning Wave..."); } enemiesRemaining = GameObject.FindGameObjectsWithTag("Enemy").Length; // End of Defend State if (timer > defendDuration && enemiesRemaining <= 0) { timer = 0.0f; ClearMapEntities(); wave++; currentState = GameState.REST; if (wave % 3 == 0) { waveInfo.waveSize += 1; } if (wave % 5 == 0) { waveInfo.numberOfWaves += 1; } Debug.Log("Game State: " + currentState.ToString()); } break; case GameState.REST: timer += Time.deltaTime; // End of Rest State if (timer > restDuration) { timer = 0.0f; currentState = GameState.DEFEND; waveTimer = (defendDuration / waveInfo.numberOfWaves); Debug.Log("Game State: " + currentState.ToString()); } break; case GameState.END: if (!_hasShownAlert) { _ui.ShowAlert("GAME OVER", "Press Enter", float.PositiveInfinity, true); _hasShownAlert = true; } if (Input.GetKeyDown(KeyCode.Return)) { SceneManager.LoadScene(SceneManager.GetActiveScene().name); } break; } }