Example #1
0
    void OnCoin(SocketIOEvent socketIOEvent)
    {
        CoinsJSON  coinsJSON = CoinsJSON.CreateFromJSON(socketIOEvent.data.ToString());
        SpawnCoins es        = GetComponent <SpawnCoins>();

        es.SpawnsCoins(coinsJSON);
    }
    private void RandomizeChunk(ChunkType type)
    {
        chunkType = type;

        switch (chunkType)
        {
        case ChunkType.coinFull:

            SpawnCoins spawnCoins = gameObject.AddComponent <SpawnCoins>();
            spawnCoins.coin = coin;
            spawnCoins.Spawn();

            break;

        case ChunkType.enemies:

            int enemyAmount = Random.Range(1, maxEnemies);

            for (int i = 0; i < enemyAmount; i++)
            {
                GameObject enemyInstance = Instantiate(enemies[Random.Range(0, enemies.Length)]);

                Vector2 enemyPosition = new Vector2
                                            (transform.position.x - GetComponent <SpriteRenderer>().bounds.size.x / 2 + distanceFromEdge + (Random.Range(0, GetComponent <SpriteRenderer>().bounds.size.x - distanceFromEdge)),
                                            transform.position.y + GetComponent <SpriteRenderer>().bounds.size.y / 2 + enemyInstance.GetComponent <SpriteRenderer>().bounds.size.y / 2);

                enemyInstance.transform.position = enemyPosition;

                /*while()
                 * {
                 *
                 * }*/
            }

            break;

        case ChunkType.platforms:

            GameObject     platformInstance       = Instantiate(platform);
            SpriteRenderer platformSpriteRenderer = platformInstance.GetComponent <SpriteRenderer>();

            Vector2 platformSize = new Vector2(Random.Range(2, 6), platformInstance.GetComponent <SpriteRenderer>().size.y);

            platformSpriteRenderer.size = platformSize;
            platformInstance.GetComponent <BoxCollider2D>().size = platformSize;

            Vector2 platformPosition = new Vector2
                                           (transform.position.x - GetComponent <SpriteRenderer>().bounds.size.x / 2 + distanceFromEdge + (Random.Range(0, GetComponent <SpriteRenderer>().bounds.size.x - distanceFromEdge)),
                                           transform.position.y + GetComponent <SpriteRenderer>().bounds.size.y / 2 + platformSpriteRenderer.bounds.size.y / 2 + Random.Range(maxPlatformHeight, maxPlatformHeight + 1));

            platformInstance.transform.position = platformPosition;

            break;
        }

        chunkCount++;
    }
    void SpawnOne()
    {
        //not most efficient way, but i'll switch to pools if it starts lagging at this point
        if (platforms[currentPlatform] != null)
        {
            //reset not strictly necessary since we are not using pool
            //but it will be needed if switched to pool and also does platform children cleanup
            //platforms[currentPlatform].GetComponent<Platform>().Reset();
            Destroy(platforms[currentPlatform]);
        }
        int prefabStartIndex = previousPlatformId == 0 ? 1 : 0;                                                                              //should prevent from shortest platform appearing twice in a row
        int prefabId         = FirstPlatformSpawn() ? (platformPrefabs.Length - 1) : Random.Range(prefabStartIndex, platformPrefabs.Length); //always make first platform longest

        previousPlatformId = prefabId;

        platforms[currentPlatform] = (GameObject)Instantiate(platformPrefabs[prefabId], Vector2.zero, Quaternion.identity);
        SpawnCoins sc = platforms[currentPlatform].GetComponent <SpawnCoins>();

        Assert.IsNotNull(sc, "SpawnCoins component not found!");
        sc.SpawnCollectables(collectableSpawnProbability, gemSpawnProbability);

        float currentPlatformSize = PlatformSize(platforms[currentPlatform].transform);

        //update origin position
        if (!FirstPlatformSpawn())                                                    //dont update origin position for first platform
        {
            bool    closeToBottom = originPosition.y - verticalMax - 1f < lowerLimit; //added 1 just to be safe.
            float   above         = Random.value > 0.5f || closeToBottom ? 1f : -1f;  //go only up if went too low
            float   horizontalMin = previousPlatformSize * 0.5f + currentPlatformSize * 0.5f + horizontalGapMin;
            float   horizontalMax = previousPlatformSize * 0.5f + currentPlatformSize * 0.5f + horizontalGapMax;
            Vector2 offset        = new Vector2(Random.Range(horizontalMin, horizontalMax), Random.Range(verticalMin, verticalMax) * above);

            if (debugPlacePlatformsAdjacent && Debug.isDebugBuild)
            {
                offset.x = previousPlatformSize * 0.5f + currentPlatformSize * 0.5f;
                offset.y = 0.0f;
            }

            originPosition = originPosition + offset;

            //Debug.Log("Origin position: " + originPosition.ToString());
        }
        previousPlatformSize = currentPlatformSize;

        platforms[currentPlatform].transform.position = originPosition;

        currentPlatform = (currentPlatform + 1) % poolSize;
    }
Example #4
0
    //called before start
    void Awake()
    {
        pf = GetComponent <PlatformFall>();
        sc = GetComponent <SpawnCoins>();

        //find length of longest collider
        sizeX = -1f;
        List <BoxCollider2D> colliders2d = new List <BoxCollider2D>();

        GetComponents(colliders2d);
        foreach (BoxCollider2D c2d in colliders2d)
        {
            if (c2d.size.x > sizeX)
            {
                sizeX = c2d.size.x;
            }
        }
    }