private void Initialize() { if (_pixelGridSnapper == null) { _pixelGridSnapper = new PixelGridSnapper(_texelsPerUnit, _pixelsPerTexel); } _camera = FindObjectOfType <Camera>(); _soundManager = FindObjectOfType <SoundManager>(); _cursorController = FindObjectOfType <CursorController>(); _cutscenePlayer = FindObjectOfType <Cutscene>(); _uiManager = FindObjectOfType <UiManager>(); _levelGenerationController = FindObjectOfType <LevelGenerationController>(); _playerController = FindObjectOfType <PlayerController>(); _deadline = FindObjectOfType <DeadlineIdentifier>().gameObject; _camera.orthographicSize = (float)Screen.height / _texelsPerUnit / 2 / _pixelsPerTexel; _currentGameState = GameState.mainMenu; if (_shouldStartFromGameplay) { _shouldStartFromGameplay = false; _soundManager.StartNewGameWhithoutCutScene(); StartGameplay(); } else { Invoke("DeactivateObjectsBeforeStart", 0.1f); } }
protected virtual void Start() { _gameManager = FindObjectOfType <GameManager>(); _pixelGridSnapper = _gameManager.PixelGridSnapper; _pixelGridSnapper.SnapToTexelGrid(_childTransformToSnapToGrid, transform); }
protected virtual void PlaceObjectFromPool(Vector3 position, PixelGridSnapper gridSnapper) { PlaceObjectFromPool(position); ScrollingGameObject tempScrollingObject = _tempObject.GetComponent <ScrollingGameObject>(); if (!tempScrollingObject.ChildTransformToSnapToGrid.gameObject.activeSelf) { tempScrollingObject.SwitchVisibility(); } gridSnapper.SnapToTexelGrid(tempScrollingObject.ChildTransformToSnapToGrid, _tempObject.transform); }
void Start() { _gridSnapper = FindObjectOfType <GameManager>().PixelGridSnapper; _wallsGenerator = new ObjectsLyingOnPlatformsGenerator(_sizeOfWallsObjectPool, transform, _wallPrefab, (int)(_delayBeforeFirstWall / _delayToCheckBuildAndRemoveObjects), (int)(_minDelayBetweenWalls / _delayToCheckBuildAndRemoveObjects), (int)(_maxDelayBetweenWalls / _delayToCheckBuildAndRemoveObjects), true); _buffsOnPlatformsGenerator = new ObjectsLyingOnPlatformsGenerator(_sizeOfPickupBuffsOnPlatformsObjectPool, transform, _pickupBuffOnPlatformsPrefab, (int)(_delayBeforeFirstBuffOnPlatforms / _delayToCheckBuildAndRemoveObjects), (int)(_minDelayBetweenBuffOnPlatforms / _delayToCheckBuildAndRemoveObjects), (int)(_maxDelayBetweenBuffOnPlatforms / _delayToCheckBuildAndRemoveObjects), false); _debuffsOnPlatformsGenerator = new ObjectsLyingOnPlatformsGenerator(_sizeOfPickupDebuffsOnPlatformsObjectPool, transform, _pickupDebuffOnPlatformsPrefab, (int)(_delayBeforeFirstDebuffOnPlatforms / _delayToCheckBuildAndRemoveObjects), (int)(_minDelayBetweenDebuffOnPlatforms / _delayToCheckBuildAndRemoveObjects), (int)(_maxDelayBetweenDebuffOnPlatforms / _delayToCheckBuildAndRemoveObjects), false); _bugsInTheAirGenerator = new FloatingObjectsGenerator(_sizeOfBugsInTheAirObjectPool, transform, _BugInTheAirPrefab, (int)(_delayBeforeFirstBugInTheAir / _delayToCheckBuildAndRemoveObjects), (int)(_minDelayBetweenBugsInTheAir / _delayToCheckBuildAndRemoveObjects), (int)(_maxDelayBetweenBugsInTheAir / _delayToCheckBuildAndRemoveObjects)); _buffsInTheAirGenerator = new FloatingObjectsGenerator(_sizeOfBuffsInTheAirObjectPool, transform, _BuffInTheAirPrefab, (int)(_delayBeforeFirstBuffInTheAir / _delayToCheckBuildAndRemoveObjects), (int)(_minDelayBetweenBuffsInTheAir / _delayToCheckBuildAndRemoveObjects), (int)(_maxDelayBetweenBuffsInTheAir / _delayToCheckBuildAndRemoveObjects)); _farBackgroundObjectsGenerator = new BackgroundElementsGenerator(_sizeOfFarBackgroundObjectsObjectPool, transform, _farBackgroundObjectPrefab); _midBackgroundObjectsGenerator = new BackgroundElementsGenerator(_sizeOfMidBackgroundObjectsObjectPool, transform, _midBackgroundObjectPrefab); _nearBackgroundObjectsGenerator = new BackgroundElementsGenerator(_sizeOfNearBackgroundObjectsObjectPool, transform, _nearBackgroundObjectPrefab); _foregroundObjectsGenerator = new BackgroundElementsGenerator(_sizeOfForegroundObjectsObjectPool, transform, _foregroundObjectPrefab); }
void Awake() { #region создание и проверка синглтона if (Instance == null) { Instance = this; } else if (Instance != this) { Destroy(gameObject); } DontDestroyOnLoad(gameObject); #endregion _pixelGridSnapper = new PixelGridSnapper(_texelsPerUnit, _pixelsPerTexel); }
public void CheckAndTryCreateBatchOfObjects(int creatingBorderPositionX, float maxXDistanceToOriginPoint, float maxYDistanceFromPlatform, int minAmountOfObjectsInBatch, int maxAmountOfObjectsInBatch, Sprite[] sprites, PixelGridSnapper gridSnapper) { if (_activeObjects.Count == 0 || _activeObjects[_activeObjects.Count - 1].transform.position.x < creatingBorderPositionX) { Vector2 raycastSource = new Vector2(creatingBorderPositionX, 0); RaycastHit2D hit = Physics2D.Raycast(raycastSource, Vector2.down, RaycastDistance, LayerMaskForRaycasting); if (!hit) { hit = Physics2D.Raycast(raycastSource, Vector2.up); } if (hit && hit.collider.tag == "Platform") { int amountOfObjects = Random.Range(minAmountOfObjectsInBatch, maxAmountOfObjectsInBatch + 1); float xDistanceBetweenObjects = maxXDistanceToOriginPoint / amountOfObjects; for (int i = 0; i < amountOfObjects; i++) { PlaceObjectFromPool ( new Vector3( creatingBorderPositionX + (xDistanceBetweenObjects * i), hit.collider.transform.position.y - maxYDistanceFromPlatform + Random.Range(0, maxYDistanceFromPlatform * 2) + YPlus, _objectsPool.Peek().transform.position.z), sprites[Random.Range(0, sprites.Length)], gridSnapper ); if (_activeObjects.Count > 1) { SpriteRenderer previousObjectSpriteRenderer = _activeObjects[_activeObjects.Count - 2].GetComponentInChildren <SpriteRenderer>(); if (previousObjectSpriteRenderer.sortingOrder > MaxSortingOrder) { previousObjectSpriteRenderer.sortingOrder = 0; } _activeObjects[_activeObjects.Count - 1].GetComponentInChildren <SpriteRenderer>().sortingOrder = previousObjectSpriteRenderer.sortingOrder + 1; } } } } }
public void CheckAndTryCreateFloatingObject(int creatingBorderPositionX, float minHeightAbovePlatform, float maxHeightAbovePlatform, PixelGridSnapper gridSnapper) { if (CheckIsItTimeToPlaceObject()) { for (int i = 0; i < AttemptsToFindNearestPlatform; i++) { Vector2 raycastSource = new Vector2(creatingBorderPositionX - (i / 2), 0); RaycastHit2D hit = Physics2D.Raycast(raycastSource, Vector2.down, RaycastDistance, LayerMaskForRaycasting); if (!hit) { hit = Physics2D.Raycast(raycastSource, Vector2.up, RaycastDistance, LayerMaskForRaycasting); } if (hit && hit.collider.tag == "Platform") { //добавил немного рандома в положение по иксу PlaceObjectFromPool(new Vector3(creatingBorderPositionX + Random.Range(0, 2f), hit.collider.transform.position.y + Random.Range(minHeightAbovePlatform, maxHeightAbovePlatform), _objectsPool.Peek().transform.position.z), gridSnapper); break; } } } }
public PropsGenerator(int objectsPoolSize, Transform levelLayoutObject, GameObject objectPrefab, Vector3 firstObjectPosition, PixelGridSnapper gridSnapper) : base(objectsPoolSize, levelLayoutObject, objectPrefab) { PlaceFirstObject(firstObjectPosition, gridSnapper); }
protected virtual void PlaceObjectFromPool(Vector3 position, Sprite sprite, PixelGridSnapper gridSnapper) { PlaceObjectFromPool(position, gridSnapper); _tempObject.GetComponent <ScrollingGameObject>().ChildTransformToSnapToGrid.GetComponent <SpriteRenderer>().sprite = sprite; }
public virtual void PlaceFirstObject(Vector3 position, PixelGridSnapper gridSnapper) { PlaceObjectFromPool(position, gridSnapper); }
private void CreatePlatform(int platformLength, Vector3 startingPosition, Sprite leftEdge, Sprite[] middleSprites, Sprite rightEdge, PixelGridSnapper gridSnapper) { PlaceObjectFromPool(startingPosition, leftEdge, gridSnapper); for (int i = 0; i < platformLength - 2; i++) { PlaceObjectFromPool(new Vector3(startingPosition.x + ((i + 1) * _platformPartPrefabWidth), startingPosition.y, startingPosition.z), middleSprites[Random.Range(0, middleSprites.Length)], gridSnapper); } PlaceObjectFromPool(new Vector3(startingPosition.x + ((platformLength - 1) * _platformPartPrefabWidth), startingPosition.y, startingPosition.z), rightEdge, gridSnapper); PlatformWrappersGenerator.PlacePlatformFromPool((startingPosition + _activeObjects[_activeObjects.Count - 1].transform.position) / 2, new Vector2(_platformPartPrefabWidth * platformLength, PlatformWrappersGenerator.NextPlatformInPool.GetComponent <BoxCollider2D>().size.y)); }
/// <summary> /// Попытаться создать платформу. /// </summary> /// <param name="creatingBorderPositionX"></param> /// <param name="minPlatformLength"></param> /// <param name="maxPlatformLength"></param> /// <param name="minXDistanceBetweenPlatforms"></param> /// <param name="maxXDistanceBetweenPlatforms"></param> /// <param name="minYDistanceBetweenPlatforms"></param> /// <param name="maxYDistanceBetweenPlatforms"></param> /// <param name="leftEdge"></param> /// <param name="middleSprites"></param> /// <param name="rightEdge"></param> /// <param name="gridSnapper"></param> public void CheckAndTryCreatePlatform(int creatingBorderPositionX, int minPlatformLength, int maxPlatformLength, float minXDistanceBetweenPlatforms, float maxXDistanceBetweenPlatforms, float minYDistanceBetweenPlatforms, float maxYDistanceBetweenPlatforms, Sprite leftEdge, Sprite[] middleSprites, Sprite rightEdge, PixelGridSnapper gridSnapper) { Transform rightmostActivePlatformPart = _activeObjects[_activeObjects.Count - 1].transform; if (rightmostActivePlatformPart.position.x < creatingBorderPositionX) { CreatePlatform(Random.Range(minPlatformLength, maxPlatformLength + 1), new Vector3(rightmostActivePlatformPart.position.x + Random.Range(minXDistanceBetweenPlatforms, maxXDistanceBetweenPlatforms), rightmostActivePlatformPart.position.y + (Random.Range(minYDistanceBetweenPlatforms, maxYDistanceBetweenPlatforms) * Random.Range(-1, 2)), rightmostActivePlatformPart.transform.position.z), leftEdge, middleSprites, rightEdge, gridSnapper); } }
public PlatformGenerator(int platformPartsPoolSize, int platformWrappersPoolSize, Transform levelLayoutObject, GameObject platformPartPrefab, GameObject platformWrapperPrefab, int firstPlatformLength, float firstPlatformStartingPositionX, Sprite leftEdge, Sprite[] middleSprites, Sprite rightEdge, PixelGridSnapper gridSnapper) : base(platformPartsPoolSize, levelLayoutObject, platformPartPrefab) { _platformPartPrefabWidth = platformPartPrefab.GetComponentInChildren <SpriteRenderer>().sprite.bounds.size.x; PlatformWrappersGenerator = new InnerGeneratorForPlatformGenerator(platformWrappersPoolSize, levelLayoutObject, platformWrapperPrefab, platformPartPrefab.GetComponent <ScrollingGameObject>().ScrollSpeed); CreatePlatform(firstPlatformLength, new Vector3(levelLayoutObject.position.x + firstPlatformStartingPositionX, levelLayoutObject.position.y, levelLayoutObject.position.z), leftEdge, middleSprites, rightEdge, gridSnapper); }
/// <summary> /// Попробовать создать объект, лежащий на платформе. /// </summary> /// <param name="creatingBorderPositionX"></param> /// <param name="minXDistanceToNextObject"></param> /// <param name="maxXDistanceToNextObject"></param> /// <param name="gridSnapper"></param> public void CheckAndTryCreateObjectOnPlatform(int creatingBorderPositionX, float YDistanceToPlatform, PixelGridSnapper gridSnapper) { if (CheckIsItTimeToPlaceObject()) { for (int i = 0; i < AttemptsToPlaceObject; i++)//просто какой-то предел, чтобы в бесконечный цикл не попасть { Vector2 raycastSource = new Vector2(creatingBorderPositionX + (i / 2), 0); Collider2D hitCollider = Physics2D.Raycast(raycastSource, Vector2.down, RaycastDistance, LayerMaskForRaycasting).collider; if (hitCollider == null) { hitCollider = Physics2D.Raycast(raycastSource, Vector2.up, RaycastDistance, LayerMaskForRaycasting).collider; } if (hitCollider != null && hitCollider.tag == "Platform") { _objectsPool.Peek().GetComponent <ScrollingGameObject>().ScrollSpeed = hitCollider.gameObject.GetComponent <ScrollingGameObject>().ScrollSpeed; Vector3 spawnPosition = _placeObjectsOnlyAtPlatformCenter ? hitCollider.transform.position + Vector3.up * YDistanceToPlatform : new Vector3(Random.Range(creatingBorderPositionX + (i / 2), hitCollider.transform.position.x + (hitCollider.bounds.size.x / 2)), hitCollider.transform.position.y + YDistanceToPlatform, _objectsPool.Peek().transform.position.z); PlaceObjectFromPool(spawnPosition, gridSnapper); break; } } } }
protected virtual void CheckAndTryCreateObject(int creatingBorderPositionX, float minXDistanceToNextObject, float maxXDistanceToNextObject, float minYPosition, float maxYPosition, PixelGridSnapper gridSnapper) { Transform rightmostActiveObject = _activeObjects[_activeObjects.Count - 1].transform; if (rightmostActiveObject.position.x < creatingBorderPositionX) { PlaceObjectFromPool(new Vector3(rightmostActiveObject.position.x + Random.Range(minXDistanceToNextObject, maxXDistanceToNextObject), rightmostActiveObject.position.y + Random.Range(minYPosition, maxYPosition), _objectsPool.Peek().transform.position.z), gridSnapper); } }