首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >形成统一的随机平台

形成统一的随机平台
EN

Stack Overflow用户
提问于 2018-06-03 13:44:38
回答 1查看 754关注 0票数 1

所以,在我想要制作一个像极受欢迎的zigzag游戏一样的游戏的时候,我被困在随机生成的平台上。平台按X、-X、Z、-Z方向随机生成。我已经编写了我的代码,它在那里生成平台。我可能会采取一种很长的方法(如果有的话)

代码语言:javascript
复制
                void Start () 
                {
                    lastPos = platform.transform.position;
                    size = platform.transform.localScale.x;

                    InvokeRepeating("SpawnXZ",1f,0.2f);
                }

                void SpawnX()
                {   
                    Vector3 pos = lastPos;
                    pos.x += size;
                    lastPos = pos;
                    Instantiate(platform, pos, Quaternion.identity);    
                }

                void SpawnZ()
                {
                    Vector3 pos = lastPos;
                    pos.z += size;
                    lastPos = pos;
                    Instantiate(platform, pos, Quaternion.identity); 
                }

                void SpawnNegX()
                {
                    Vector3 pos = lastPos;
                    pos.x -= size;
                    lastPos = pos;
                    Instantiate(platform, pos, Quaternion.identity);  
                }

                void SpawnNegZ()
                {
                    Vector3 pos = lastPos;
                    pos.z -= size;
                    lastPos = pos;
                    Instantiate(platform, pos, Quaternion.identity);
                }

                void SpawnXZ()
                {

                        int rand = Random.Range(0, 6);
                        if (rand < 3)
                        {
                                SpawnX();
                        }
                        else if(rand >= 3)
                        {
                                SpawnZ();
                        }

                        if(--counter == 0) { CancelInvoke("SpawnXZ"); };


                    if(counter == 0)
                    {   
                        counter = 25;
                        int r = Random.Range(0,2);
                        if(r == 0)
                        {   
                            InvokeRepeating("SpawnNegXZ",0f,0.2f);
                        }
                        else
                        {       
                            InvokeRepeating("SpawnXNegZ",0f,0.2f);
                        }
                    }

                }

                void SpawnNegXZ()
                {
                    int rand = Random.Range(0, 6);
                        if (rand < 3)
                        {   
                                SpawnNegX();
                        }
                        else if(rand >= 3)
                        {   
                                SpawnZ();
                        }

                    if(--counter == 0) { CancelInvoke("SpawnNegXZ"); };


                    if(counter == 0)
                    {   
                        counter = 25;
                        int r = Random.Range(0,2);
                        if(r == 0)
                        {   
                            InvokeRepeating("SpawnXZ",0f,0.2f);
                        }
                        else
                        {   
                            InvokeRepeating("SpawnNegXNegZ",0f,0.2f);
                        }
                    }
                }

                void SpawnXNegZ()
                {
                        int rand = Random.Range(0, 6);
                        if (rand < 3)
                        {   
                                SpawnX();
                        }
                        else if(rand >= 3)
                        {   
                                SpawnNegZ();
                        }       


                    if(--counter == 0) { CancelInvoke("SpawnXNegZ"); };


                    if(counter == 0)
                    {   
                        counter = 25;
                        int r = Random.Range(0,2);
                        if(r == 0)
                        {   
                            InvokeRepeating("SpawnXZ",0f,0.2f);
                        }
                        else
                        {   
                            InvokeRepeating("SpawnNegXNegZ",0f,0.2f);
                        }
                    }
                }

                void SpawnNegXNegZ()
                {

                        int rand = Random.Range(0, 6);
                        if (rand < 3)
                        {
                                SpawnNegX();    
                        }
                        else if(rand >= 3)
                        {   
                                SpawnNegZ();
                        }

                    if(--counter == 0) { CancelInvoke("SpawnNegXNegZ"); };


                    if(counter == 0)
                    {   
                        counter = 25;
                        int r = Random.Range(0,2);
                        if(r == 0)
                        {   
                            InvokeRepeating("SpawnNegXZ",0f,0.2f);
                        }
                        else
                        {   
                            InvokeRepeating("SpawnXNegZ",0f,0.2f);
                        }
                    }
                }

我打过xz,-xz,x -z和-x -z。我首先调用X和Z方向的平台产卵,然后切换到-XZ或x -Z等等。但主要有两个问题。

PS :那些小黑方块只不过是钻石(忽略它们)。

它们要么形成一个2X2块,要么相互重叠。

我该怎么避免这些?或者有一种更简单的方法来生成我所缺少的平台。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-03 15:39:35

您需要一个数组来指示占用了哪些瓷砖:

代码语言:javascript
复制
bool[,] tiles = new bool[N,N];

或者一本字典

代码语言:javascript
复制
Dictionary<XZ, bool> tiles = new Dictionary<XZ, bool>();
public struct XZ { public int X; public int Z; }

每当生成瓷砖时,请检查该值,以确定是否有可能生成:

代码语言:javascript
复制
           void SpawnXZ()
           {
                int x = (int) (lastpos.x / size);
                int z = (int) (lastpos.z / size);

                int rand = Random.Range(0, 6);
                if (rand < 3 && !tiles[x + 1, z])
                {
                    SpawnX();
                    tiles[x + 1, z] = true;
                }
                else if(rand >= 3 && && !tiles[x, z + 1])
                {
                    SpawnZ();
                    tiles[x, z + 1] = true;
                }
                else
                {
                     ...
                }

请注意,这段代码将不能工作,它只是进一步开发它的起点。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50667051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档