public void Initialize() { LevelEventBus.SubscribeEvent(LevelEventType.FAIL, () => { _finalHighway = null; _levelIndex = 0; _levelManager.DeleteWholeLevels(); }); LevelEventBus.SubscribeEvent(LevelEventType.STARTED, () => { GenerateLevels(3); _carBase.SetCarPosition(_levelManager.GetHighwayOfLevel(0, 0).transform); }); LevelEventBus.SubscribeEvent(LevelEventType.LEVEL_UP, () => { Timer.Instance.TimerWait(5f, () => GenerateLevels(1)); }); }
private void GenerateLevels(int levelCount) { var highwayDirections = new List <HighwayDirection> { HighwayDirection.UP, HighwayDirection.LEFT, HighwayDirection.RIGHT }; var straightDirection = highwayDirections.GetRandomElementFromList(); var cornerDirection = straightDirection; HighwayBase straightHighway = null; HighwayBase corner = null; for (int j = 0; j < levelCount; j++) { for (int i = 0; i < GameConfig.LEVEL_LENGTH; i++) { if (straightDirection == cornerDirection) { straightDirection = HighwayDirection.UP; } if (_finalHighway == null) { straightHighway = GenerateHighway <StraightHighway>(straightDirection, corner, false); } else { straightHighway = _finalHighway; } if (straightDirection == HighwayDirection.UP) { cornerDirection = highwayDirections.GetRandomElementFromList(HighwayDirection.UP); corner = GenerateHighway <CornerHighway>(cornerDirection, straightHighway); straightDirection = cornerDirection == HighwayDirection.LEFT ? HighwayDirection.RIGHT : HighwayDirection.LEFT; straightHighway = GenerateHighway <StraightHighway>(straightDirection, corner, false); // Random U Corner Generation int rnd = Random.Range(0, 10); if (rnd > GameConfig.U_CORNER_PROBABILITY) { cornerDirection = straightDirection; straightDirection = cornerDirection == HighwayDirection.LEFT ? HighwayDirection.RIGHT : HighwayDirection.LEFT; corner = GenerateHighway <UCornerHighway>(cornerDirection, straightHighway, false); _finalHighway = null; continue; } } _finalHighway = null; cornerDirection = straightDirection; corner = GenerateHighway <CornerHighway>(cornerDirection, straightHighway); } if (straightDirection == cornerDirection) { straightDirection = HighwayDirection.UP; } _finalHighway = GenerateHighway <FinalHighway>(straightDirection, corner, false); _levelIndex++; } }