示例#1
0
    public void LoadLevel(Vector2 lp)
    {
        print(levelPos);

        foreach (Transform child in wallParent)
        {
            Destroy(child.gameObject);
        }
        foreach (Transform child in groundParent)
        {
            Destroy(child.gameObject);
        }
        foreach (Transform child in holeParent)
        {
            Destroy(child.gameObject);
        }
        foreach (Transform child in stoolParent)
        {
            Destroy(child.gameObject);
        }
        doors = new List <GameObject>();
        foreach (Transform child in doorParent)
        {
            Destroy(child.gameObject);
        }

        Random.State oldState = Random.state;
        Random.InitState(Mathf.FloorToInt(hash21(lp) * 1000000.0f));

        bool[,] tiles  = new bool[levelRadiusW * 2, levelRadiusH * 2];
        bool[,] stools = new bool[levelRadiusW * 2, levelRadiusH * 2];

        int roomRule = Random.Range(0, 24);

        //Corners
        Instantiate(wallCorner, new Vector3(-levelRadiusW, -levelRadiusH + 1), Quaternion.Euler(0.0f, 0.0f, 90.0f), wallParent);
        Instantiate(wallCorner, new Vector3(levelRadiusW, 1 - levelRadiusH), Quaternion.Euler(0.0f, 0.0f, 180.0f), wallParent);
        Instantiate(wallCorner, new Vector3(levelRadiusW, 1 + levelRadiusH), Quaternion.Euler(0.0f, 0.0f, -90.0f), wallParent);
        Instantiate(wallCorner, new Vector3(-levelRadiusW, 1 + levelRadiusH), Quaternion.Euler(0.0f, 0.0f, 0.0f), wallParent);
        //Fill
        for (int x = 0; x < levelRadiusW * 2; x++)
        {
            //Bottom wall
            Instantiate(wallStraight, new Vector3(1 + x - levelRadiusW, -levelRadiusH), Quaternion.Euler(0.0f, 0.0f, 180.0f), wallParent);

            for (int y = 0; y < levelRadiusH * 2; y++)
            {
                //Setting tile existence
                tiles[x, y]  = tileRules[roomRule, y, x];
                stools[x, y] = stoolRules[roomRule, y, x];

                Instantiate(
                    tiles[x, y] ? groundTile : hole,
                    new Vector3(x - levelRadiusW, levelRadiusH + 1 - y),
                    Quaternion.identity,
                    tiles[x, y] ? groundParent : holeParent
                    );

                //Stools
                if (tiles[x, y] && stools[x, y])
                {
                    Instantiate(
                        stool,
                        new Vector3(x - levelRadiusW, levelRadiusH + 1 - y),
                        Quaternion.identity,
                        stoolParent
                        );
                }
            }
            //Top Wall
            Instantiate(wallStraight, new Vector3(x - levelRadiusW, 2 + levelRadiusH), Quaternion.Euler(0.0f, 0.0f, 0.0f), wallParent);
        }
        for (int y = 0; y < levelRadiusH * 2; y++)
        {
            //Left wall
            Instantiate(wallStraight, new Vector3(-1 - levelRadiusW, levelRadiusH - y), Quaternion.Euler(0.0f, 0.0f, 90.0f), wallParent);
            //Right Wall
            Instantiate(wallStraight, new Vector3(1 + levelRadiusW, levelRadiusH + 1 - y), Quaternion.Euler(0.0f, 0.0f, -90.0f), wallParent);
        }

        foreach (Transform child in groundParent)
        {
            child.gameObject.GetComponent <SpriteRenderer>().sortingOrder = -5 - levelRadiusH - (int)child.position.y;
        }

        if (hash21(levelPos + new Vector2(0.5f, 0.0f)) > 0.2f)
        {
            doors.Add(Instantiate(door, new Vector3(1 + levelRadiusW, 1.5f), Quaternion.Euler(0.0f, 0.0f, -90.0f), doorParent));
            doors[doors.Count - 1].GetComponent <doorControl>().setDirection(new Vector2(1.0f, 0.0f));
        }
        if (hash21(levelPos + new Vector2(-0.5f, 0.0f)) > 0.2f)
        {
            doors.Add(Instantiate(door, new Vector3(-1 - levelRadiusW, 0.5f), Quaternion.Euler(0.0f, 0.0f, 90.0f), doorParent));
            doors[doors.Count - 1].GetComponent <doorControl>().setDirection(new Vector2(-1.0f, 0.0f));
        }
        if (hash21(levelPos + new Vector2(0.0f, 0.5f)) > 0.2f)
        {
            doors.Add(Instantiate(door, new Vector3(-0.5f, 2 + levelRadiusH), Quaternion.Euler(0.0f, 0.0f, 0.0f), doorParent));
            doors[doors.Count - 1].GetComponent <doorControl>().setDirection(new Vector2(0.0f, 1.0f));
        }
        if (hash21(levelPos + new Vector2(0.0f, -0.5f)) > 0.2f)
        {
            doors.Add(Instantiate(door, new Vector3(0.5f, -levelRadiusH), Quaternion.Euler(0.0f, 0.0f, 180.0f), doorParent));
            doors[doors.Count - 1].GetComponent <doorControl>().setDirection(new Vector2(0.0f, -1.0f));
        }

        if (!vistedRooms.Contains(lp))
        {
            vistedRooms.Add(lp);
            int enemyNum    = 1;
            int enemyChance = 0;

            List <Transform> possiblePositions = new List <Transform>((Transform[])groundParent.GetComponentsInChildren <Transform>().Clone());
            foreach (Transform t1 in stoolParent.GetComponentsInChildren <Transform>())
            {
                foreach (Transform t2 in possiblePositions)
                {
                    if ((t1.position - t2.position).sqrMagnitude < 0.5f)
                    {
                        possiblePositions.Remove(t2);
                        break;
                    }
                }
            }

            while (enemyChance < 1)
            {
                int posInd = Random.Range(0, possiblePositions.Count);

                Vector3 prefabPos = possiblePositions[posInd].position + new Vector3(0.5f, -0.5f, 0.0f);

                possiblePositions.RemoveAt(posInd);

                GameObject temp = Instantiate(enemies[Random.Range(0, enemies.Count)], prefabPos, Quaternion.identity, enemyParent);
                if (temp.GetComponent <EnemyAI>() != null)
                {
                    temp.GetComponent <EnemyAI>().target   = player.transform;
                    temp.GetComponent <EnemyAI>().AstarObj = AStarObject.GetComponent <AstarPath>();
                }

                enemyChance = Random.Range(0, ++enemyNum);
            }
        }

        Random.state       = oldState;
        timeSinceLevelLoad = 0.0f;
    }