private RunnerItem ItemFactory(Type inItemType, LevelGroup.eLevelGroupID levelGroupID) { RunnerItem spawnedItem = null; if (inItemType == typeof(HazardItem)) { List <HazardItem> hazardItems = levelHazardItems[levelGroupID]; HazardItem randomHazard = hazardItems[Random.Range(0, hazardItems.Count)]; spawnedItem = (HazardItem)Instantiate(randomHazard); } else if (inItemType == typeof(CoinItem)) { CoinItem randomCoin = CoinItems[Random.Range(0, CoinItems.Count)]; spawnedItem = (CoinItem)Instantiate(randomCoin); } else if (inItemType == typeof(RunnerItem)) { RunnerItem randomItem = ItemPrefabs[Random.Range(0, ItemPrefabs.Count)]; spawnedItem = (RunnerItem)Instantiate(randomItem); } else { Debug.LogError("No spawn logic set for item type " + inItemType); } spawnedItem.transform.parent = runnerItemsParent.transform; return(spawnedItem); }
/* * CurrentLevel is optional for CoinItem and RunnerItem. Required for Hazard Item because * hazard item could be different for different levels */ public RunnerItem GetRandomItemOfType(Type inItemType, LevelGroup.eLevelGroupID currentLevelGroup) { if (inItemType == typeof(HazardItem)) { if (mHazardItemPool.ContainsKey(currentLevelGroup) && mHazardItemPool[currentLevelGroup].Count > 0) { RunnerItem existingHazardItem = mHazardItemPool[currentLevelGroup].Dequeue(); existingHazardItem.gameObject.SetActive(true); return(existingHazardItem); } else { //Create new Hazard Item RunnerItem newItem = ItemFactory(inItemType, currentLevelGroup); return(newItem); } } else { if (mItemPool.ContainsKey(inItemType) && mItemPool[inItemType].Count > 0) { RunnerItem existingPoolItem = mItemPool[inItemType].Dequeue(); existingPoolItem.gameObject.SetActive(true); return(existingPoolItem); } else { // Create a new item RunnerItem newItem = ItemFactory(inItemType, currentLevelGroup); return(newItem); } } }
//TO DO: need to fix caching bug public void StoreOrDisposeItem(RunnerItem inItem, LevelGroup.eLevelGroupID levelGroupID) { // Disable it. If its queued, then it will 'disapaear' off the map. If its deleted well who cares! if (inItem != null) { inItem.gameObject.SetActive(false); Destroy(inItem.gameObject); } // Type itemType = inItem.GetType(); // //HazardItem pool needs to be handled differently from the other items // if(itemType == typeof(HazardItem)){ // // //Create cache queue // // if(!mHazardItemPool.ContainsKey(levelGroupID)) // // mHazardItemPool.Add(levelGroupID, new Queue<HazardItem>()); // // //Add to cache if cache size allows // // if(mHazardItemPool[levelGroupID].Count < ItemPoolMaxSize) // // mHazardItemPool[levelGroupID].Enqueue((HazardItem)inItem); // // else // GameObject.Destroy(inItem.gameObject); // }else{ // //Create cache queue // if (!mItemPool.ContainsKey(itemType)) // mItemPool.Add(itemType, new Queue<RunnerItem>()); // //Add to cache if cache size allows // if (mItemPool[itemType].Count < ItemPoolMaxSize) // mItemPool[itemType].Enqueue(inItem); // else // GameObject.Destroy(inItem.gameObject); // } }
/// <summary> /// Transitions to random new level group. /// </summary> private void TransitionToRandomNewLevelGroup() { // Cut to a new, different level that has the proper level ID. // Update our level switches mNumLevelSwitches++; // Pull out all levels that are 'legal' to switch to List <LevelGroup> potentialLevels = new List <LevelGroup>(); foreach (LevelGroup levelGroup in LevelGroups) { if (levelGroup != mCurrentLevelGroup && // Don't switch to the current group levelGroup.levelGroupDifficulty <= mNumLevelSwitches) // Dont switch to a group that is a higher 'level' then us { potentialLevels.Add(levelGroup); } } if (potentialLevels.Count > 0) { // Choose a random level int randomIndex = Random.Range(0, potentialLevels.Count); LevelGroup newLevelGroup = potentialLevels[randomIndex]; LevelGroup.eLevelGroupID currentGroupID = mCurrentLevelGroup.LevelGroupID; LevelGroup.eLevelGroupID newGroupID = newLevelGroup.LevelGroupID; // Transition! mCurrentLevelGroup = newLevelGroup; Debug.Log("Transitioning to level " + newLevelGroup.LevelGroupID); // Now that we succesfully transitioned, determine if there is a level transition component. foreach (LevelTransitionComponent currentTransition in LevelTransitionGroups) { if (currentTransition.FromGroupID == currentGroupID && currentTransition.ToGroupID == newGroupID) { // Push this component nowww PushAndInstantiateRandomComponent(currentTransition); } } } else { Debug.LogError("No other levels to switch to. Perhaps there are no other level groups, or we havent reached a new " + "level number yet? Current number transitions: " + mNumLevelSwitches); } }