示例#1
0
    /// <summary>
    /// Generates the chunk at xy chunk position.
    /// </summary>
    /// <returns>The chunk.</returns>
    /// <param name="xPos">X position.</param>
    /// <param name="yPos">Y position.</param>
    public void generateChunk(int xPos, int yPos)
    {
        if (loadedChunks == null)
        {
            Debug.LogWarning("lost chunks");
            return;
        }
        // If the chunk is alreaady loaded on screen return
        if (loadedChunks.ContainsKey(xPos + " " + yPos))
        {
            loadedChunks[xPos + " " + yPos] = null;
            return;// loadedChunks [xPos + " " + yPos];
        }


        // Create chunk
        Chunk chunkMap = new Chunk();

        GameObject chunkLoc = new GameObject("Chunk: " + xPos + " " + yPos);

        chunkLoc.transform.SetPositionAndRotation(new Vector3(xPos * Chunk.SIZE, 0, yPos * Chunk.SIZE), Quaternion.identity);

        for (int y = 0; y < Chunk.SIZE; y++)
        {
            for (int x = 0; x < Chunk.SIZE; x++)
            {
                // Check if this tile is edited already
                if (!addTerrain(x, y, xPos, yPos, chunkMap, terrainMap))
                {
                    Debug.Log("Terrain was not correctly added to tile " + x + " " + y);
                }


                //Generate resources
                addResource(x, y, xPos, yPos, chunkMap, resourceMap);
            }
        }
        loadedChunks[xPos + " " + yPos] = chunkMap;
        GameObject tempTile;
        GameObject tempResource;

        for (int y = 0; y < Chunk.SIZE; y++)
        {
            for (int x = 0; x < Chunk.SIZE; x++)
            {
                Position worldPos = new Position(xPos * Chunk.SIZE + x, yPos * Chunk.SIZE + y);
                string   key      = worldPos.xCoord + " " + worldPos.yCoord;
                tempTile = Instantiate(getObject(terrainMap[key]), new Vector3(worldPos.xCoord, 0, worldPos.yCoord), Quaternion.identity);

                if (terrainMap[key] == terrain.PLOT)
                {
                    resourceMap[key] = resource.BUILDSIGN;
                }

                if (terrainMap[key] == terrain.CAMPSITE)
                {
                    tempTile.name = "TownCenter";
                }
                else
                {
                    tempTile.transform.SetParent(chunkLoc.transform);
                }

                chunkMap.addTileAt(tempTile, x, y, 0);
                if (terrainMap[key] == terrain.CAMPSITE)
                {
                    Instantiate(getObject(terrain.PLOT), new Vector3(worldPos.xCoord, 0, worldPos.yCoord), Quaternion.identity);
                }
                if (resourceMap.ContainsKey(key))
                {
                    if (resourceMap[key] == resource.NONE)
                    {
                    }
                    else if (resourceMap[key] == resource.CAVE)
                    {
                        Instantiate(Cave, new Vector3(worldPos.xCoord, 0, worldPos.yCoord), Quaternion.identity);
                    }
                    else if (resourceMap[key] == resource.STONE)
                    {
                        int temp = Random.Range(1, 5);
                        for (int i = 0; i < temp; i++)
                        {
                            int xtemp = Random.Range(-5, 5);
                            int ytemp = Random.Range(-5, 5);
                            if (Random.Range(0, 1000) < rareResourceRate)
                            {
                                tempResource = Instantiate(getResourceObject(resource.DIAMOND), new Vector3(worldPos.xCoord + ((float)xtemp / 10), 0, worldPos.yCoord + ((float)ytemp / 10)), getResourceObject(resourceMap[key]).transform.rotation);
                            }
                            else if (Random.Range(0, 100) < rareResourceRate)
                            {
                                tempResource = Instantiate(getResourceObject(resource.COAL), new Vector3(worldPos.xCoord + ((float)xtemp / 10), 0, worldPos.yCoord + ((float)ytemp / 10)), getResourceObject(resourceMap[key]).transform.rotation);
                            }
                            else
                            {
                                tempResource = Instantiate(getResourceObject(resourceMap[key]), new Vector3(worldPos.xCoord + ((float)xtemp / 10), 0, worldPos.yCoord + ((float)ytemp / 10)), getResourceObject(resourceMap[key]).transform.rotation);
                            }

                            tempResource.transform.SetParent(chunkLoc.transform);
                        }
                    }
                    else if (resourceMap[key] == resource.IRON)
                    {
                        int temp = Random.Range(1, 3);
                        for (int i = 0; i < temp; i++)
                        {
                            int xtemp = Random.Range(-5, 5);
                            int ytemp = Random.Range(-5, 5);
                            tempResource = Instantiate(getResourceObject(resourceMap[key]), new Vector3(worldPos.xCoord + ((float)xtemp / 10), 0, worldPos.yCoord + ((float)ytemp / 10)), getResourceObject(resourceMap[key]).transform.rotation);

                            tempResource.transform.SetParent(chunkLoc.transform);
                        }
                    }
                    else if (resourceMap[key] == resource.TREE)
                    {
                        int temp = Random.Range(1, 5);
                        for (int i = 0; i < temp; i++)
                        {
                            int xtemp = Random.Range(-5, 5);
                            int ytemp = Random.Range(-5, 5);
                            tempResource = Instantiate(getResourceObject(resourceMap[key]), new Vector3(worldPos.xCoord + ((float)xtemp / 10), 0, worldPos.yCoord + ((float)ytemp / 10)), getResourceObject(resourceMap[key]).transform.rotation);

                            tempResource.transform.SetParent(chunkLoc.transform);
                        }
                    }
                    else if (resourceMap[key] == resource.BERRIES)
                    {
                        int temp = Random.Range(1, 5);
                        for (int i = 0; i < temp; i++)
                        {
                            int xtemp = Random.Range(-5, 5);
                            int ytemp = Random.Range(-5, 5);
                            tempResource = Instantiate(getResourceObject(resourceMap[key]), new Vector3(worldPos.xCoord + ((float)xtemp / 10), 0, worldPos.yCoord + ((float)ytemp / 10)), getResourceObject(resourceMap[key]).transform.rotation);

                            tempResource.transform.SetParent(chunkLoc.transform);
                        }
                    }
                    else
                    {
                        tempResource = Instantiate(getResourceObject(resourceMap[key]), new Vector3(worldPos.xCoord, 0, worldPos.yCoord), getResourceObject(resourceMap[key]).transform.rotation);

                        if (resourceMap[key] == resource.NPC)
                        {
                            tempResource.GetComponent <QuestNPC>().addGoal(tempResource.GetComponent <Skills>().getIntRank());
                            tempResource.name = NameGen.create();
                            tempResource.GetComponentInChildren <TextMesh>().text = tempResource.name;
                        }
                        else
                        {
                            tempResource.transform.SetParent(chunkLoc.transform);
                        }
                        chunkMap.addTileAt(tempTile, x, y, 1);
                    }
                }
            }
        }
    }