Example #1
0
 protected void spawnRandomEnemy(SpawnSide side)
 {
     Enemy enemy = EnemiesPool.pool.getRandomEnemy();
     enemy.transform.position = getSpawnPosition(side);
     enemy.transform.rotation = Quaternion.identity;
     enemy.gameObject.SetActive (true);
 }
Example #2
0
        public Duck(float x, float y, int radius, SpawnSide side) : this(x, y, radius)
        {
            Random random = new Random();

            switch (side)
            {
            case SpawnSide.Top:
                _vel.X = random.Next(-1, 1);
                _vel.Y = random.Next(2, 7);
                break;

            case SpawnSide.Right:
                _vel.X = random.Next(-7, -2);
                _vel.Y = random.Next(-1, 1);
                break;

            case SpawnSide.Left:
                _vel.X = random.Next(2, 7);
                _vel.Y = random.Next(-1, 1);
                break;

            case SpawnSide.Bottom:
                _vel.X = random.Next(-1, 1);
                _vel.Y = random.Next(-7, -2);
                break;
            }
        }
 public void spawnEnemy(EnemyType type, SpawnSide side)
 {
     Enemy enemy = EnemiesPool.pool.getEnemy(type);
     enemy.transform.position = getSpawnPosition(side);
     enemy.transform.rotation = Quaternion.identity;
     enemy.gameObject.SetActive (true);
 }
Example #4
0
    private void CreateBigAsteroid()
    {
        GameObject asteroidToInstantiate = asteroidPrefabs[Random.Range(0, asteroidPrefabs.Length)];

        SpriteRenderer asteroidToInstantiateSR = asteroidToInstantiate.GetComponent <SpriteRenderer>();

        Vector2 bottomLeftScreenCorner = Camera.main.ViewportToWorldPoint(new Vector3(0, 0));
        Vector2 topRightScreenCorner   = Camera.main.ViewportToWorldPoint(new Vector3(1, 1));

        float randomYWithingScreenBoundaries = Random.Range(bottomLeftScreenCorner.y + asteroidToInstantiateSR.size.y, topRightScreenCorner.y - asteroidToInstantiateSR.size.y);
        float randomXWithingScreenBoundaries = Random.Range(bottomLeftScreenCorner.x + asteroidToInstantiateSR.size.x, topRightScreenCorner.x - asteroidToInstantiateSR.size.x);
        float yBeyondTopScreenBoundary       = topRightScreenCorner.y + asteroidToInstantiateSR.size.y;
        float xBeyondRightScreenBoundary     = topRightScreenCorner.x + asteroidToInstantiateSR.size.x;

        Vector2   asteroidSpawnPoint = Vector2.zero;
        SpawnSide randomSpawnSide    = (SpawnSide)Random.Range(0, System.Enum.GetValues(typeof(SpawnSide)).Length);

        if (randomSpawnSide == SpawnSide.right)
        {
            asteroidSpawnPoint = new Vector2(xBeyondRightScreenBoundary, randomYWithingScreenBoundaries);
        }
        else if (randomSpawnSide == SpawnSide.bottom)
        {
            asteroidSpawnPoint = new Vector2(randomXWithingScreenBoundaries, -yBeyondTopScreenBoundary);
        }
        else if (randomSpawnSide == SpawnSide.left)
        {
            asteroidSpawnPoint = new Vector2(-xBeyondRightScreenBoundary, randomYWithingScreenBoundaries);
        }
        else if (randomSpawnSide == SpawnSide.top)
        {
            asteroidSpawnPoint = new Vector2(randomXWithingScreenBoundaries, yBeyondTopScreenBoundary);
        }

        //Instantiating asteroid with random rotation
        GameObject go = Instantiate(asteroidToInstantiate, asteroidSpawnPoint, Quaternion.Euler(0, 0, Random.Range(0f, 360f)));

        //Setting asteroid as a child is an alternative for storing them in List or using Find function in future to delete all exisiting instances
        go.transform.SetParent(transform);
        Rigidbody2D asteroidRigidbody = go.GetComponent <Rigidbody2D>();

        if (asteroidRigidbody == null)
        {
            Debug.Log("asteroid's rigibody == null");
            Destroy(go);
            return;
        }

        go.GetComponent <AsteroidLogic>().AsteroidSize = AsteroidSize.Big;

        //Direction of applied force doesn't matter because 'ScreenCornerDetection' script will take care of keeping object in bounds anyway
        asteroidRigidbody.AddForce(go.transform.up * Random.Range(-1f, 1f) * GameManager.instance.AsteroidMaxSpeed, ForceMode2D.Impulse);
        asteroidRigidbody.AddTorque(Random.Range(-1f, 1f) * GameManager.instance.AsteroidMaxTurnSpeed, ForceMode2D.Impulse);
    }
Example #5
0
    protected Vector3 getSpawnPosition(SpawnSide side)
    {
        float x = 0;
        float y = 0;

        if (side == SpawnSide.ANY)
            side = (SpawnSide)Random.Range (1, SpawnSide.GetNames (typeof(SpawnSide)).Length);

        switch (side) {
            case SpawnSide.TOP:
                y = map.YMovementRange + 1; x = Random.Range(-map.XMovementRange - 1, map.XMovementRange + 1); break;
            case SpawnSide.BOT:
                y = -map.YMovementRange - 1; x = Random.Range(-map.XMovementRange - 1, map.XMovementRange + 1); break;
            case SpawnSide.RIGHT:
                x = map.XMovementRange + 1; y = Random.Range(-map.YMovementRange - 1, map.YMovementRange + 1); break;
            case SpawnSide.LEFT:
                x = -map.XMovementRange - 1; y = Random.Range(-map.YMovementRange - 1, map.YMovementRange + 1); break;
        }
        return new Vector3(x, y, 0);
    }
Example #6
0
        public void SpawnElement(GameObject objectSpawned, float normPosition, SpawnSide spawnSide)
        {
            switch (spawnSide)
            {
            case SpawnSide.Top:
                objectSpawned.transform.position = new Vector3(Mathf.Lerp(leftBound, rightBound, normPosition), topBound, 0);
                break;

            case SpawnSide.Bot:
                objectSpawned.transform.position = new Vector3(Mathf.Lerp(leftBound, rightBound, normPosition), botBound, 0);
                break;

            case SpawnSide.Right:
                objectSpawned.transform.position = new Vector3(rightBound, Mathf.Lerp(botBound, topBound, normPosition), 0);
                break;

            case SpawnSide.Left:
                objectSpawned.transform.position = new Vector3(leftBound, Mathf.Lerp(botBound, topBound, normPosition), 0);
                break;
            }
        }
Example #7
0
    /// <summary>
    /// Called when the script is loaded or a value is changed in the
    /// inspector (Called in the editor only).
    /// </summary>
    //void OnValidate()
    //{
    //    if (!mapSprite)
    //    {
    //        mapSprite = mapTransform.GetComponent<SpriteRenderer>().sprite;
    //    }

    //    spawnSize = new Vector2(
    //            mapSprite.rect.width / mapSprite.pixelsPerUnit * mapTransform.lossyScale.x,
    //            mapSprite.rect.height / mapSprite.pixelsPerUnit * mapTransform.lossyScale.y
    //        ) + Vector2.one * extraDistance;
    //}

    private Vector2 PickSpawnLocation()
    {
        // Vector3 spawnSize = mapTransform.lossyScale + Vector3.one * extraDistance;
        Vector3 mapPosition = mapTransform.position;

        //choose which side of the rectangle to spawn
        SpawnSide side = (SpawnSide)Random.Range(0, 4);

        //position vector declarations
        float positionX = 0;
        float positionY = 0;

        switch (side)
        {
        case SpawnSide.Bottom:
        case SpawnSide.Top:

            //randomly choose an x-coordinate value
            positionX = Random.Range(mapPosition.x - spawnSize.x / 2, mapPosition.x + spawnSize.x / 2);

            //depending on whether it is the top or bottom, set the y-coordinate value as needed
            positionY = (side == SpawnSide.Bottom) ? mapPosition.y - spawnSize.y / 2 : mapPosition.y + spawnSize.y / 2;

            break;

        case SpawnSide.Left:
        case SpawnSide.Right:

            //depending on whether it is left or right, set the x-coordinate as needed
            positionX = (side == SpawnSide.Left) ? mapPosition.x - spawnSize.x / 2 : mapPosition.x + spawnSize.x / 2;

            //randomly choose an y-coordinate value
            positionY = Random.Range(mapPosition.y - spawnSize.y / 2, mapPosition.y + spawnSize.y / 2);

            break;
        }

        return(new Vector2(positionX, positionY));
    }
Example #8
0
    // Returns a random location on the plane edge
    private Vector3 GenerateStartLocation()
    {
        float randomValue = Random.value;
        float xPos;
        float zPos;

        if (randomValue < 0.25f)
        {
            // Spawn on top
            spawnSide = SpawnSide.Top;
            xPos      = Random.Range(planeLeftEdge + spawnOffset, planeRightEdge - spawnOffset);
            zPos      = planeTopEdge;
        }
        else if (randomValue < 0.5f)
        {
            // Spawn to the right
            spawnSide = SpawnSide.Right;
            xPos      = planeRightEdge;
            zPos      = Random.Range(planeBottomEdge + spawnOffset, planeTopEdge - spawnOffset);
        }
        else if (randomValue < 0.75f)
        {
            // Spawn on bottom
            spawnSide = SpawnSide.Bottom;
            xPos      = Random.Range(planeLeftEdge + spawnOffset, planeRightEdge - spawnOffset);
            zPos      = planeBottomEdge;
        }
        else
        {
            // Spawn to the left
            spawnSide = SpawnSide.Left;
            xPos      = planeLeftEdge;
            zPos      = Random.Range(planeBottomEdge + spawnOffset, planeTopEdge - spawnOffset);
        }

        return(new Vector3(xPos, flightHeight, zPos));
    }
Example #9
0
    /// <summary>
    /// Call this to start spawning buildings. This sets up the buidling spawning values and starts the Coroutine
    /// </summary>
    /// <param name="side">The side to spawn buildings on</param>
    /// <param name="frequency">How long to wait before spawning the next building.</param>
    public void setBuildings(char side, float frequency)
    {
        switch (side)
        {
        case 'L':
            sideToSpawnBuildings = SpawnSide.Left;
            break;

        case 'R':
            sideToSpawnBuildings = SpawnSide.Right;
            break;

        case 'B':
            sideToSpawnBuildings = SpawnSide.Both;
            break;
        }
        this.frequency = frequency;

        if (!droppingBuildings)
        {
            droppingBuildings = true;
            StartCoroutine(SpawnBuildings());
        }
    }
Example #10
0
    public void setFoliage(char side, float frequency)
    {
        switch (side)
        {
        case 'L':
            sideToSpawnFoliage = SpawnSide.Left;
            break;

        case 'R':
            sideToSpawnFoliage = SpawnSide.Right;
            break;

        case 'B':
            sideToSpawnFoliage = SpawnSide.Both;
            break;
        }
        this.frequency = frequency;

        if (!droppingTrees)
        {
            droppingTrees = true;
            StartCoroutine(SpawnFoliage());
        }
    }
Example #11
0
        public void Initialize(Texture2D obstacleTexture, Player player, SpawnSide side)
        {
            viewport = curGame.GraphicsDevice.Viewport;

            texture = obstacleTexture;
            this.player = player;

            //Initially set the object to active
            Active = true;

            //Set starting positions of the object
            leftPosition = new Vector2(0, player.Position.Y - player.marginVertical - player.verticalOffset);
            rightPosition = new Vector2(viewport.Width - texture.Width, player.Position.Y - player.marginVertical - player.verticalOffset);

            if (side == SpawnSide.Left)
            {
                flip = SpriteEffects.None;
                Position = leftPosition;
            }
            else if (side == SpawnSide.Right)
            {
                flip = SpriteEffects.FlipHorizontally;
                Position = rightPosition;
            }

            initialYPosition = player.Position.Y;
        }