首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >卡在环上c#

卡在环上c#
EN

Stack Overflow用户
提问于 2020-11-05 01:20:06
回答 1查看 69关注 0票数 0

我计算了一个单元和一个建筑物之间的距离,把距离放在一个struct[]中,并将最小的距离排序为最大。人工智能每次都要去最近的建筑,这是它的工作原理。现在我的问题是,我想添加一个检查并删除硬编码到索引0的最后一行代码。我希望AI去下一个索引,每次当前的索引耗尽资源。我该怎么做呢?每次AI选择最接近的建筑物来获取资源时,结构都会填充排序的距离。

它会是for循环中的if语句吗?还是另一个带if语句的for循环?或者,如果建筑物资源耗尽,是否更改结构中的索引?

代码语言:javascript
复制
    Destination2 = GameObject.FindGameObjectsWithTag("Storehouses");        // storehouse distance should be based off  production
    
    Vector3 position = transform.position;
    for (int i = 0; i < Destination2.Length; i++) // put in 10 storehouses, find the closest distance to the colonist
    {
        Debug.Log("1");
        buildingInformation[i].buildingID2 = Destination2[i];      // stores game objects 0,1
        

        Vector3 direction = (Destination2[i].transform.position - position);
        float distance = direction.sqrMagnitude;

        buildingInformation[i].buildingDistance2 = distance;
        Array.Sort<BuildingInformation>(buildingInformation, (x, y) => x.buildingDistance2.CompareTo(y.buildingDistance2));// smallest to largest
        
    }
    Supplier = buildingInformation[0].buildingID2;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-05 02:35:25

关于我如何处理它的一些Linqy伪代码:

代码语言:javascript
复制
destination = storehouses
              .OrderBy( storehouse.distance )
              .FirstOrDefault( storehouse.contains(item) )

尽管您可能最好只执行一次迭代:

代码语言:javascript
复制
SomeType destination
foreach storehouse in storehouses
    if ( storehouse.contains(item) )
        if ( destination is null || storehouse.distance < destination.distance )
            destination = storehouse
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64689887

复制
相关文章

相似问题

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