Example #1
0
    void Start()
    {
        Debug.Log("(LevelGeneration.Awake) is new map = " + isNewMap.ToString());

        objectsMap = new GameObject[mapSize, mapSize];

        GrounMap    = new GameObject("Level0");
        Map         = new GameObject("Level1");
        environment = new GameObject("environment");
        NPC         = new GameObject("NPC");

        if (!isNewMap)
        {
            DataGround data = SaveSystem.LoadGroundMap();
            mapSize             = data.mapSize;
            Tiles               = new Tile[mapSize, mapSize];
            TileToGameObjectMap = new Dictionary <Tile, GameObject>();
            objectsMap          = new GameObject[mapSize, mapSize];
            for (int i = 0; i < mapSize; i++)
            {
                for (int j = 0; j < mapSize; j++)
                {
                    Tiles[i, j].Elevation = data.Elevation[i, j];
                }
            }
        }
        else
        {
            InstantiateMap();
            GenerateIsland();
        }

        UpdateVisual();
        for (int i = 0; i < mapSize; i++)
        {
            for (int j = 0; j < mapSize; j++)
            {
                Vector3    tilePosition = new Vector3(i, -0.4f, j);
                GameObject tileGO       = (GameObject)Instantiate(TilePrefab, tilePosition, Quaternion.identity, GrounMap.transform);
                foreach (Transform c in tileGO.transform.GetComponentsInChildren <Transform>())
                {
                    c.gameObject.layer = 0;
                }
                tileGO.layer = 0;
                MeshRenderer mr = tileGO.GetComponentInChildren <MeshRenderer>();
                mr.material = MatDirtName;
            }
        }

        //TODO I spawning objects in coord "mapsize - i,mapsize - j" is that right? i dont remember what was in my mind when i wrote that, and i dont leaving enough coments xP
        //MapBaking.instance.BakeMap();
        if (isNewMap)
        {
            /// <summary>
            ///
            /// 0 - water
            /// 1 - sand
            /// 2 - grass
            /// 3 - forest
            /// 4 - rock
            ///
            /// </summary>
            for (int i = 0; i < mapSize; i++)
            {
                for (int j = 0; j < mapSize; j++)
                {
                    GameObject spawnedObject = null;
                    if (Tiles[i, j].Elevation > HeightRocks)
                    {
                        if (Random.Range(0, 100) < 15)
                        {
                            spawnedObject = GameObject.Instantiate(Resources.Load("LargeRock")) as GameObject;
                            spawnedObject.transform.position = new Vector3(mapSize - i, spawnedObject.transform.position.y, mapSize - j);
                            int angle = Random.Range(0, 359);
                            spawnedObject.transform.Rotate(new Vector3(0, angle, 0));
                            spawnedObject.transform.SetParent(environment.transform);
                        }
                        else if (Random.Range(0, 100) < 5)
                        {
                            spawnedObject = GameObject.Instantiate(Resources.Load("Mushroom")) as GameObject;
                            spawnedObject.transform.position = new Vector3(mapSize - i, spawnedObject.transform.position.y, mapSize - j);
                            int angle = Random.Range(0, 359);
                            spawnedObject.transform.Rotate(new Vector3(0, angle, 0));
                            spawnedObject.transform.SetParent(environment.transform);
                        }
                    }
                    else if (Tiles[i, j].Elevation > HeightForest)
                    {
                        if (Random.Range(0, 100) < 40)
                        {
                            spawnedObject = GameObject.Instantiate(Resources.Load("Tree")) as GameObject;
                            spawnedObject.transform.position = new Vector3(mapSize - i, spawnedObject.transform.position.y, mapSize - j);
                            int angle = Random.Range(-15, 15);
                            spawnedObject.transform.Rotate(new Vector3(0, angle, 0));
                            spawnedObject.transform.SetParent(environment.transform);
                        }
                        else if (Random.Range(0, 100) < 10)
                        {
                            spawnedObject = GameObject.Instantiate(Resources.Load("Brush")) as GameObject;
                            spawnedObject.transform.position = new Vector3(mapSize - i, spawnedObject.transform.position.y, mapSize - j);
                            int angle = Random.Range(0, 359);
                            spawnedObject.transform.Rotate(new Vector3(0, angle, 0));
                            spawnedObject.transform.SetParent(environment.transform);
                        }
                        else if (Random.Range(0, 100) < 3)
                        {
                            spawnedObject = GameObject.Instantiate(Resources.Load("Mushroom")) as GameObject;
                            spawnedObject.transform.position = new Vector3(mapSize - i, spawnedObject.transform.position.y, mapSize - j);
                            int angle = Random.Range(0, 359);
                            spawnedObject.transform.Rotate(new Vector3(0, angle, 0));
                            spawnedObject.transform.SetParent(environment.transform);
                        }
                    }
                    else if (Tiles[i, j].Elevation > HeightGrass)
                    {
                        if (Random.Range(0, 100) < 3)
                        {
                            spawnedObject = GameObject.Instantiate(Resources.Load("Brush")) as GameObject;
                            spawnedObject.transform.position = new Vector3(mapSize - i, spawnedObject.transform.position.y, mapSize - j);
                            int angle = Random.Range(0, 359);
                            spawnedObject.transform.Rotate(new Vector3(0, angle, 0));
                            spawnedObject.transform.SetParent(environment.transform);
                        }
                    }
                    if (Tiles[i, j].Elevation > HeightGrass)
                    {
                        if (spawnedObject == null)
                        {
                            if (Random.Range(0, 1000) < 7)
                            {
                                spawnedObject = GameObject.Instantiate(Resources.Load("Enemy")) as GameObject;
                                spawnedObject.transform.position = new Vector3(mapSize - i, spawnedObject.transform.position.y, mapSize - j);
                                int angle = Random.Range(0, 359);
                                spawnedObject.transform.Rotate(new Vector3(0, angle, 0));
                                spawnedObject.transform.SetParent(NPC.transform);
                            }
                        }
                    }
                    if (spawnedObject != null)
                    {
                        objectsMap[i, j] = spawnedObject;
                    }
                }
            }
            GameObject Boat      = GameObject.Instantiate(Resources.Load("Boat")) as GameObject;
            int        boatPosXZ = (int)(Mathf.Min(mapSize, mapSize) * BorderPrc);
            Boat.transform.position = new Vector3(boatPosXZ, Boat.transform.position.y, boatPosXZ);
            Boat.transform.SetParent(environment.transform);

            GameObject traderNPC   = GameObject.Instantiate(Resources.Load("TraderNPC")) as GameObject;
            int        traderPosXZ = (int)(Mathf.Min(mapSize, mapSize) * BorderPrc) + 4;
            traderNPC.transform.position = new Vector3(traderPosXZ - 1, traderNPC.transform.position.y, traderPosXZ);
            traderNPC.GetComponent <TraderNPC>().GenerateItems();
            traderNPC.transform.SetParent(NPC.transform);
        }
        else
        {
            DataStaticObjects dataEnviroment = SaveSystem.LoadEnvironment();
            for (int i = 0; i < dataEnviroment.length; i++)
            {
                GameObject spawnedObject = GameObject.Instantiate(Resources.Load(dataEnviroment.staticObjects[i])) as GameObject;
                spawnedObject.transform.position = new Vector3(dataEnviroment.position[i, 0], dataEnviroment.position[i, 1], dataEnviroment.position[i, 2]);
                spawnedObject.transform.rotation = Quaternion.Euler(dataEnviroment.rotation[i, 0], dataEnviroment.rotation[i, 1], dataEnviroment.rotation[i, 2]);
                spawnedObject.transform.SetParent(environment.transform);
            }
            DataNPC dataNPC = SaveSystem.LoadNPC();
            for (int i = 0; i < dataNPC.length; i++)
            {
                GameObject spawnedObject = GameObject.Instantiate(Resources.Load(dataNPC.NPC[i])) as GameObject;
                spawnedObject.transform.position = new Vector3(dataNPC.position[i, 0], dataNPC.position[i, 1], dataNPC.position[i, 2]);
                spawnedObject.transform.rotation = Quaternion.Euler(dataNPC.rotation[i, 0], dataNPC.rotation[i, 1], dataNPC.rotation[i, 2]);

                spawnedObject.GetComponent <CreachureStats>().Name          = dataNPC.Name[i];
                spawnedObject.GetComponent <CreachureStats>().maxHealth     = dataNPC.maxHp[i];
                spawnedObject.GetComponent <CreachureStats>().CurrentHealth = dataNPC.hp[i];
                spawnedObject.GetComponent <CreachureStats>().attackSpeed   = dataNPC.attackSpeed[i];
                spawnedObject.GetComponent <CreachureStats>().fameForKill   = dataNPC.fameForKill[i];
                spawnedObject.GetComponent <CreachureStats>().Damage        = dataNPC.damage[i];
                spawnedObject.GetComponent <CreachureStats>().Armor         = dataNPC.armor[i];


                if (dataNPC.hasInventory[i])
                {
                    Inventory inventory = spawnedObject.GetComponent <Inventory>();
                    if (inventory == null)
                    {
                        inventory = spawnedObject.AddComponent <Inventory>();
                    }
                    inventory.inventoryType = dataNPC.inventoryType[i];
                    inventory.space         = dataNPC.inventoryMaxSpace[i];
                    inventory.gold          = dataNPC.inventoryGold[i];
                    for (int j = 0; j < dataNPC.inventoryCurrentSpace[i]; j++)
                    {
                        inventory.Add(Resources.Load("Items\\" + dataNPC.inventoryItems[i, j]) as Items);
                    }
                }

                spawnedObject.transform.SetParent(NPC.transform);
            }
        }

        GameObject Player      = GameObject.Instantiate(Resources.Load("Player")) as GameObject;
        int        playerPosXZ = (int)(Mathf.Min(mapSize, mapSize) * BorderPrc) + 10;

        Player.transform.position = new Vector3(playerPosXZ, Player.transform.position.y, playerPosXZ);
        PlayerManager.instance.SetPlayer(Player);
        if (!isNewMap)
        {
            SaveManager.instance.LoadPlayer();
        }
        MapBaking.instance.BakeMap();
    }
Example #2
0
    public static DataStaticObjects LoadEnvironment()
    {
        DataStaticObjects data = LoadData(enviromentPath) as DataStaticObjects;

        return(data);
    }