首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >工法式联合电力线

工法式联合电力线
EN

Stack Overflow用户
提问于 2022-02-12 02:05:08
回答 1查看 158关注 0票数 0

在没有无限递归的情况下,如何制作电力线?我目前正在做一个游戏,你把建筑物和电线之间,把电力从煤炭工厂转移到建筑物需要电力。是否有一种算法或特定的方法来规划电力线,以便它们之间和从其他建筑物之间传递电力。主要的问题是,这样做没有能源积累和指数增长。考虑到可以有任意数量的电力线连接到任何其他数量,这是困难的。有什么建议或帮助吗?

EN

回答 1

Stack Overflow用户

发布于 2022-02-15 01:43:43

代码语言:javascript
运行
复制
private void GetTotalConnections(GameObject line, List<GameObject> checker) // HOLY SHIT IT WORKS HOLY SHIT IT WORKS HOLY SHIT IT WORKS
    {
        PowerLine temp = line.GetComponent<PowerLine>();
        
        if (checker.Contains(line))
        {
            if (CheckIfConnectionsContains(checker, line.GetComponent<PowerLine>()) == true)
            {
                return;
            }
        }

        for (int i = 0; i < temp.connectedLines.Count; i++)
        {
            if (checker.Contains(temp.connectedLines[i]) == false)
            {
                checker.Add(temp.connectedLines[i]);
                GetTotalConnections(temp.connectedLines[i], checker);
            }
            else // if its already in the list, skip this entry since we already checked that it still had some new connections
            {
                continue;
            }
        }
    }

    private bool CheckIfConnectionsContains(List<GameObject> list, PowerLine line)
    {
        int count = 0;
        for (int i = 0; i < line.connectedLines.Count; i++)
        {
            if (list.Contains(line.connectedLines[i]))
            {
                count++;
            }
        }

        if (count == line.connectedLines.Count) // if every connection is already in the list
        {
            return true;
        }

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

https://stackoverflow.com/questions/71088409

复制
相关文章

相似问题

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