Esempio n. 1
0
 int GetStartCost(NodeOfGrid a, NodeOfGrid b)
 {
     if (a.x == b.x || a.y == b.y)
     {
         return(10);
     }
     else
     {
         return(14);
     }
 }
Esempio n. 2
0
    public List <NodeOfGrid> GetPath(NodeOfGrid start, NodeOfGrid end)
    {
        List <NodeOfGrid> temp = new List <NodeOfGrid>();
        NodeOfGrid        Nog  = end;

        while (Nog != null)
        {
            temp.Add(Nog);
            Debug.Log(Nog.StarCost);
            Nog = Nog.parent;
        }
        //temp.Add(Nog);
        temp.Reverse();
        return(temp);
    }
Esempio n. 3
0
    public NodeOfGrid GetNearestNode(Vector3 pos)
    {
        float             PersenX     = (pos.x - transform.position.x) / (GridInfo[MapSizeX - 1, MapSizeY - 1].pos.x - transform.position.x);
        float             PersenY     = (pos.z - transform.position.z) / (GridInfo[MapSizeX - 1, MapSizeY - 1].pos.z - transform.position.z);
        int               x           = Mathf.RoundToInt(PersenX * 100);
        int               y           = Mathf.RoundToInt(PersenY * 100);
        List <NodeOfGrid> dic         = GetAroundNOGWithSelf(x, y);
        float             min         = float.MaxValue;
        NodeOfGrid        NearestNode = null;

        foreach (NodeOfGrid node in dic)
        {
            if (Vector3.Distance(pos, node.pos) < min)
            {
                min         = Vector3.Distance(pos, node.pos);
                NearestNode = node;
            }
        }
        return(NearestNode);
    }
Esempio n. 4
0
 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hitinfo;
         if (Physics.Raycast(ray, out hitinfo, 1000, 1 << 9))
         {
             NodeOfGrid node = Rm.GetNearestNode(hitinfo.point);
             if (Rm.TargetPos == null)
             {
                 GameObject o = Instantiate(Rm.TargetPrefab);
                 o.transform.position = node.pos;
                 Rm.TargetPos         = o.transform;
             }
             else
             {
                 Rm.TargetPos.position = node.pos;
             }
             StartNode = Rm.GetNearestNode(Rm.StartPos.position);
             EndNode   = Rm.GetNearestNode(Rm.TargetPos.position);
         }
     }
 }
Esempio n. 5
0
    void MapInit()
    {
        for (int i = 0; i < MapSizeX; i++)
        {
            for (int j = 0; j < MapSizeY; j++)
            {
                GridInfo[i, j]     = new NodeOfGrid(false, i, j);
                GridInfo[i, j].pos = new Vector3(i + 0.5f, 0, j + 0.5f);
                int a = Random.Range(0, 100);
                if (a > 45)
                {
                    WallInfo[i, j] = null;
                    MapInfo.Add(new Vector2(i, j), 0);
                    continue;
                }

                GameObject o = Instantiate(Wall);
                o.transform.position = new Vector3(i + 0.5f, 0, j + 0.5f);
                o.transform.SetParent(transform);
                WallInfo[i, j] = o.transform;
                MapInfo.Add(new Vector2(i, j), 1);
            }
        }
    }
Esempio n. 6
0
 void Start()
 {
     StartNode = Rm.GetNearestNode(Rm.StartPos.position);
     EndNode   = Rm.GetNearestNode(Rm.StartPos.position);
     Op.Add(StartNode);
 }