Exemple #1
0
    private Stack <Vector3> GeneratePath(Node current)
    {
        if (current.Position == goalPos) //If our current node is the goal, then we found a path
        {
            //Creates a stack to contain the final path
            Stack <Vector3> finalPath = new Stack <Vector3>();

            //Adds the nodes to the final path
            while (current != null)
            {
                //Debug.Log(current.Position);
                //DrawLine(current);

                //Adds the current node to the final path
                finalPath.Push(MyTilemap.CellToWorld(current.Position));
                //Find the parent of the node, this is actually retracing the whole path back to start
                //By doing so, we will end up with a complete path.
                current = current.Parent;
            }

            //Returns the complete path
            return(finalPath);
        }

        return(null);
    }
    public float GetBonusMalus(MyTilemap tilemap, Vector3 directionVector, Transform pt1, Transform pt2)
    {
        float bonusmalus = 1;

        Vector3 a = pt1.position;
        Vector3 b = pt2.position;

        //((yb-ya)(y)+(xb-xa)(x))
        float f = (b.x - a.x) * directionVector.x + (b.y - a.y) * directionVector.y;

        if (tilemap.HasMapLayer())
        {
            bonusmalus = tilemap.GetMapLayer().speedBonus;

            if (f <= 0)
            {
                bonusmalus = tilemap.GetMapLayer().flux.speedMalus;
                //Debug.Log("Le vecteur " + directionVector + " dans le flux de A(" + a + ") à B(" + b + ") est contre courant : " + f );
            }
            else
            {
                //Debug.Log("Le vecteur " + directionVector + " dans le flux de A(" + a + ") à B(" + b + ") est en courant : " + f);
            }
        }

        return(bonusmalus);
    }
Exemple #3
0
 public void RegistMap(EMap mapType, MyTilemap map)
 {
     if (tilemapDic.ContainsKey(mapType))
     {
         tilemapDic[mapType] = map;
     }
     else
     {
         tilemapDic.Add(mapType, map);
     }
 }
Exemple #4
0
    /// <summary>
    /// 算法指定的开始。
    /// </summary>
    /// <param name="start">开始坐标.</param>
    /// <param name="goal">目标坐标.</param>
    /// <returns></returns>
    public Stack <Vector3> Algorithm(Vector3 start, Vector3 goal)
    {
        startPos = MyTilemap.WorldToCell(start);
        goalPos  = MyTilemap.WorldToCell(goal);


        current = GetNode(startPos);

        //为我们稍后可能要查看的节点创建一个打开的列表
        openList = new HashSet <Node>();

        //为我们检查过的节点创建一个封闭列表
        closedList = new HashSet <Node>();

        foreach (KeyValuePair <Vector3Int, Node> node in allNodes)
        {
            node.Value.Parent = null;
        }

        allNodes.Clear();

        //将当前节点添加到打开列表(我们已经检查过)
        openList.Add(current);

        path = null;
        // pathTile = null;

        while (openList.Count > 0 && path == null)
        {
            List <Node> neighbours = FindNeighbours(current.Position);

            ExamineNeighbours(neighbours, current);

            UpdateCurrentTile(ref current);

            path = GeneratePath(current);

            //pathTile = GeneratePathTile(current);
        }

        if (path != null)
        {
            return(path);
        }

        //AstarDebug.Instance.Reset(allNodes);

        //AstarDebug.Instance.CreateTiles(openList, closedList, allNodes, startPos, goalPos, pathTile);
        return(null);
    }
Exemple #5
0
    private float getSpeedModifiers(GameObject go, Vector2 worldPos, Vector2 vectorToTarget)
    {
        float sm = 1f;

        //Get the speed modifer from map
        if (myMaps != null)
        {
            for (int i = myMaps.myTileMaps.Count - 1; i >= 0; i--)
            {
                if (hasTile(myMaps.myTileMaps[i], worldPos))
                {
                    MyTilemap tm = new MyTilemap(myMaps.myTileMaps[i], myMaps.myTileMaps[i].GetComponent <MapLayer>());

                    if (tm.HasMapLayer())
                    {
                        MapLayer layer = tm.GetMapLayer();

                        List <Transform> points = layer.flux.fluxPosis;

                        for (int j = 0; j < points.Count - 1; j++)
                        {
                            if (tm.IsBetween(worldPos, points[j], points[j + 1]))
                            {
                                sm *= tm.GetBonusMalus(tm, vectorToTarget, points[j], points[j + 1]);
                                break;
                            }
                        }
                    }

                    break;
                }
            }
        }

        Move mv = go.GetComponent <Move>();

        for (int i = mv.speedModifiers.Count - 1; i >= 0; i--)
        {
            mv.speedModifiers[i].decrement(Time.deltaTime);
            sm *= mv.speedModifiers[i].getModifier();

            if (!mv.speedModifiers[i].isValid())
            {
                mv.speedModifiers.RemoveAt(i);
            }
        }

        return(sm);
    }
    private float GetCost(MyTilemap tilemap, Vector3 directionVector, Transform pt1, Transform pt2)
    {
        float cost = Mathf.Infinity;

        //Vector3 vector = tilemap.GetTilemap().CellToWorld(directionVector);

        float sb = tilemap.GetBonusMalus(tilemap, directionVector, pt1, pt2);

        if (sb != 0)
        {
            cost = 1 / sb;
        }

        return(cost);
    }
Exemple #7
0
    public Stack <Vector3> Algorithm(Vector3 start, Vector3 goal)
    {
        startPos = MyTilemap.WorldToCell(start);
        goalPos  = MyTilemap.WorldToCell(goal);

        current = GetNode(startPos);

        //Creates an open list for nodes that we might want to look at later
        openList = new HashSet <Node>();

        //Creates a closed list for nodes that we have examined
        closedList = new HashSet <Node>();

        foreach (KeyValuePair <Vector3Int, Node> node in allNodes)
        {
            node.Value.Parent = null;
        }

        allNodes.Clear();

        //Adds the current node to the open list (we have examined it)
        openList.Add(current);

        path = null;

        while (openList.Count > 0 && path == null)
        {
            List <Node> neighbours = FindNeighbours(current.Position);

            ExamineNeighbours(neighbours, current);

            UpdateCurrentTile(ref current);

            path = GeneratePath(current);
        }

        if (path != null)
        {
            return(path);
        }


        return(null);
    }
    private float GetCost(MyTilemap tilemap, Vector3Int cell)
    {
        float cost = Mathf.Infinity;

        if (tilemap.HasMapLayer())
        {
            float sb = tilemap.GetMapLayer().speedBonus;
            if (sb != 0)
            {
                cost = 1 / sb;
            }
        }
        else
        {
            cost = 1f;
        }

        return(cost);
    }
Exemple #9
0
    /// <summary>
    /// 生成路径
    /// </summary>
    /// <param name="current">.</param>
    /// <returns></returns>
    private Stack <Vector3> GeneratePath(Node current)
    {
        //如果我们当前的节点是目标,那么我们找到了一条路径
        if (current.Position == goalPos)
        {
            //创建一个包含最终路径的堆栈
            Stack <Vector3> finalPath = new Stack <Vector3>();

            //将节点添加到最终路径
            while (current != null)
            {
                //将当前节点添加到最终路径
                finalPath.Push(MyTilemap.CellToWorld(current.Position));
                //找到节点的父节点,这实际上是回溯开始的整个路径
                //通过这样做,我们将以完整的路径结束。
                current = current.Parent;
            }

            //返回完整路径
            return(finalPath);
        }

        return(null);
    }
 public Astar(MyTilemap g)
 {
     this.tilemaps = new List <MyTilemap>();
     this.tilemaps.Add(g);
 }
Exemple #11
0
    public Stack <Vector3> Algorithm(Vector3 start, Vector3 goal)
    {
        startPos = MyTilemap.WorldToCell(start);
        goalPos  = MyTilemap.WorldToCell(goal);

        current = GetNode(startPos);

        //Creates an open list for nodes that we might want to look at later
        openList = new HashSet <Node>();

        //Creates a closed list for nodes that we have examined
        closedList = new HashSet <Node>();

        foreach (KeyValuePair <Vector3Int, Node> node in allNodes)
        {
            node.Value.Parent = null;
        }

        allNodes.Clear();

        //Adds the current node to the open list (we have examined it)
        openList.Add(current);

        path = null;

        while (openList.Count > 0 && path == null)
        {
            List <Node> neighbours = FindNeighbours(current.Position);

            ExamineNeighbours(neighbours, current);

            UpdateCurrentTile(ref current);

            path = GeneratePath(current);
        }



        if (path != null)
        {
            Stack <Vector3> ClearPath = new Stack <Vector3>();

            foreach (Vector3 position in path)//Paint Path
            {
                if (position != goalPos)
                {
                    //Debug.Log("Algorithm Vector3 position: "+position);

                    ClearPath.Push(position);

                    Vector3Int cellPosition = tilemap.WorldToCell(position);

                    tilemap.SetTile(cellPosition, tiles[0]);
                }
            }
            Debug.Log(ClearPath.Count);
            stepCount = ClearPath.Count - 1;

            while (ClearPath.Count != 0)
            {
                //Debug.Log("Algorithm Vector3Int ClearPath.Count: " + ClearPath.Count);
                //Debug.Log("Algorithm Vector3Int clearPo ClearPath.Peek(): " + ClearPath.Peek());
                Vector3 clearPos = ClearPath.Pop();

                StartCoroutine(ClearPathWait(clearPos));
            }

            return(path);
        }

        return(null);
    }