コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        framecount++;

        //
        if ((framecount % 10 != 0 || (thingsSpawned.Count > maxToSpawnInBiome * 4)) && portal.activeInHierarchy)
        {
            return;
        }

        Random.seed = (int)(Time.deltaTime * 1000);

        //A Random Point in your Camera View Port
        Vector3 ranPt = new Vector3(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), backCam.nearClipPlane);
        //The World Point you see thru camera
        Vector3 startpt = backCam.ViewportToWorldPoint(ranPt);
        //The Direction vector from Camera to start point
        Vector3 dir = startpt - backCam.transform.position;
        Vector3 pos = new Vector3(), normal = new Vector3();

        if (vxe.RayCast(startpt, dir, 64, ref pos, ref normal))
        {
            //SPAWN PORTAL:*********************************************************************
            //If the portal is not active, try spawning one
            if (!portal.activeInHierarchy)
            {
                spawnPortal(pos, 0.6f, chunkFloorPos.y);
            }

            ///SPAWNING FOR ANIMALS*********************************************************************
            //Convert the voxel position into a Chunk World Position
            Vec3Int chunkcoord = vxe.ToGrid(pos) / vxe.chunk_size;

            //Loads up the BiomeMap through the grid size
            BIOMES mybiome = biome.biomeMap [chunkcoord.x, chunkcoord.z];

            //If there are too many spawns in this Biome, do not spawn more
            if (spawnCountInBoime [(int)mybiome] > maxToSpawnInBiome)
            {
                return;
            }

            //randomly chooses an animal to spawn
            int        animalIndex = Random.Range(0, spawnTable [mybiome].SpawnList.Length);
            GameObject spawnObject = spawnTable [mybiome].SpawnList [animalIndex].gameObject;

            Chunks chunk     = vxe.getChunkFromPt(pos);
            bool   isSurface = vxe.isChunkASurface(DIR.DIR_UP, chunk, .65f);

            if (spawnTable [mybiome].SpawnList [animalIndex].sticksToWalls || isSurface)               // Vector3.Dot (normal, Vector3.up) > 0.999f) {
            {
                GameObject newsphere = (GameObject)Instantiate(spawnObject, pos + normal * VoxelExtractionPointCloud.Instance.voxel_size * 0.5f, Quaternion.identity);
                newsphere.SetActive(true);
                //SimpleAI ai = newsphere.GetComponent<SimpleAI> ();
                thingsSpawned.Add(newsphere);
                //newsphere.GetComponent<GrowScript>().init(pos, normal, (Vector3.Dot (normal,Vector3.up) > 0.999f) );

                spawnCountInBoime [(int)mybiome]++;
            }
        }
    }
コード例 #2
0
ファイル: ItemSpawner.cs プロジェクト: wertkh32/GardenMR
    IEnumerator SpawnItems()
    {
        yield return(new WaitForSeconds(5.0f));

        Vector3 coords = Vector3.zero, norm = Vector3.zero;

        bool hitsomething = false;

        while (!hitsomething)
        {
            hitsomething = vxe.RayCast(camera.transform.position, Vector3.down, 64, ref coords, ref norm, 1.0f);
            yield return(null);
        }

        Random.seed = System.DateTime.Now.Millisecond;
        floorChunkY = vxe.getChunkCoords(coords).y;
        Vec3Int prevcc  = new Vec3Int(vxe.num_chunks_x / 2, vxe.num_voxels_y / 2, vxe.num_chunks_z / 2);
        Vector2 prevdir = Vector2.zero;

        for (int s = 0; s < stages.Length; s++)
        {
            currentStage = s;
            ItemInfo[] items = stages[s].items;
            nextStage = false;

            for (int i = 0; i < items.Length; i++)
            {
                int biomeIndex = (int)items[i].biome;
                IndexStack <Vec3Int> occupiedChunks =
                    items[i].biome == BIOMES.none ? vxe.occupiedChunks : BiomeScript.Instance.biomeOccupiedChunks[biomeIndex];

                bool spawned  = false;
                int  maxdist  = 3;
                int  attempts = 0;
                while (!spawned)
                {
                    int chunkx;
                    int chunkz;

                    while (true)
                    {
                        int count  = occupiedChunks.getCount();
                        int period = Random.Range(0, count);

                        Vec3Int randomCC = occupiedChunks.peek(period);
                        chunkx = randomCC.x;
                        chunkz = randomCC.z;


                        bool isFarEnough = true;
                        int  start       = Mathf.Max(0, prevpositions.getCount() - 3);

                        for (int k = start; k < prevpositions.getCount(); k++)
                        {
                            Vec3Int pcc  = prevpositions.peek(k);
                            int     dist = (chunkx - pcc.x) * (chunkx - pcc.x) + (chunkz - pcc.z) * (chunkz - pcc.z);
                            Debug.Log(dist);
                            if (dist < maxdist * maxdist)
                            {
                                isFarEnough = false;
                                break;
                            }
                        }


                        if (SpawnCount == 0 || isFarEnough)
                        {
                            break;
                        }

                        attempts++;

                        if (attempts % 20 == 0)
                        {
                            if (maxdist > 1)
                            {
                                maxdist--;
                            }
                        }

                        yield return(null);
                    }



                    Chunks chunk = null;

                    for (int k = floorChunkY + range; k >= floorChunkY; k--)
                    {
                        chunk = vxe.grid.voxelGrid [chunkx, k, chunkz];
                        Chunks chunkup   = vxe.grid.voxelGrid [chunkx, k + 1, chunkz];
                        bool   isthereUp = (chunkup != null && chunkup.voxel_count > 3);

                        if (!isthereUp && chunk != null && chunk.voxel_count > 60 && vxe.isChunkASurface(DIR.DIR_UP, chunk, 0.5f))
                        {
                            Vector3 chunkBaseCoords = new Vector3(chunkx, k, chunkz) * vxe.chunk_size;



                            for (int ox = 0; ox < vxe.chunk_size; ox++)
                            {
                                int x = (ox + vxe.chunk_size / 2 - 1) % vxe.chunk_size;
                                for (int oz = 0; oz < vxe.chunk_size; oz++)
                                {
                                    int z = (oz + vxe.chunk_size / 2 - 1) % vxe.chunk_size;
                                    for (int y = vxe.chunk_size - 1; y >= 0; y--)
                                    {
                                        Voxel vx = chunk.getVoxel(new Vec3Int(x, y, z));

                                        if (vx.isOccupied() && vxe.voxelHasSurface(vx, VF.VX_TOP_SHOWN))
                                        {
                                            Vector3 voxelCoords = vxe.FromGridUnTrunc(chunkBaseCoords + new Vector3(x, y, z));
                                            if (voxelCoords.y < coords.y + items [i].minSpawnHeightOffFloor * vxe.voxel_size ||
                                                voxelCoords.y > coords.y + items [i].maxSpawnHeightOffFloor * vxe.voxel_size)
                                            {
                                                continue;
                                            }

                                            GameObject newItem = (GameObject)Instantiate(items [i].item, voxelCoords + new Vector3(0, vxe.voxel_size, 0), Quaternion.identity);
                                            newItem.SetActive(true);


                                            newItem.GetComponent <VoxelParent>().chunkCoords = new Vec3Int(chunkx, k, chunkz);

                                            prevpositions.push(new Vec3Int(chunkx, 0, chunkz));

                                            SpawnCount++;

                                            spawned = true;
                                            Debug.Log("spawned!");
                                            canSpawn = false;
                                            goto imout;
                                        }
                                        yield return(null);
                                    }
                                }
                            }
                        }
                    }

imout:

                    if (spawned && !stages[s].allAtOnce)
                    {
                        while (!canSpawn)
                        {
                            yield return(new WaitForSeconds(1.0f));
                        }
                    }
                    else
                    {
                        yield return(null);
                    }
                }
            }

            while (stages[s].stageWait && !nextStage)
            {
                yield return(new WaitForSeconds(1.0f));
            }
        }
    }