Example #1
0
    private void DoUpdateMemory(Vector2 position, Node targetPosition, int localRangeX, int localRangeY, List <Vector2> platformList)
    {
        UpdateMapCacheSize(localRangeX, localRangeY);
        aiPosition = position;
        offSetX    = -1 * DivByBoxFactor(aiPosition.x) + localRangeX;
        offSetY    = -1 * DivByBoxFactor(aiPosition.y) + localRangeY;
        nodeCache.SwapLists();
        AllNodesIntern.Clear();
        allAreas.Clear();
        targetNode = null;

        AnalyseEnvironment(localRangeX, localRangeY, platformList);

        if (targetPosition != null)
        {
            int tmpX = targetPosition.X + offSetX;
            int tmpY = targetPosition.Y + offSetY;
            if (tmpY - 1 > 0 && tmpY - 1 < mapCache.GetLength(0) && tmpX >= 0 && tmpX < mapCache.GetLength(1) && mapCache[tmpY - 1, tmpX].Type.Equals(SquareType.PLATFORM))
            {
                AllNodesIntern.Add(targetPosition);
                targetNode = targetPosition;
            }
            else if (tmpY - 2 > 0 && tmpY - 2 < mapCache.GetLength(0) && tmpX >= 0 && tmpX < mapCache.GetLength(1) && mapCache[tmpY - 2, tmpX].Type.Equals(SquareType.PLATFORM))
            {
                targetPosition.Y -= 1;
                AllNodesIntern.Add(targetPosition);
                targetNode = targetPosition;
            }
        }

        DetectAreas();
        CalculateTransitions();
        nodeCache.SwapLists();
    }
Example #2
0
    private void DetectAreas()
    {
        for (int x = 0; x < mapCache.GetLength(1); x++)
        {
            for (int y = 0; y < mapCache.GetLength(0); y++)
            {
                if (mapCache[y, x].Type == SquareType.PLATFORM)
                {
                    for (int i = y + charachterHight; i < mapCache.GetLength(0); i++)
                    {
                        if (mapCache[i, x] != null && mapCache[i, x].Type != SquareType.AIR)
                        {
                            mapCache[y, x].RangeToNextAbove = i - y;
                            break;
                        }
                    }
                    if (x > 0)
                    {
                        if (mapCache[y, x - 1].Type == SquareType.PLATFORM && mapCache[y, x - 1].RangeToNextAbove == mapCache[y, x].RangeToNextAbove) //Prüfe, ob der Vorgenänger von der selben "ebene" überdeckt wird
                        {
                            if (maxAreaWidth <= 0 || mapCache[y, x - 1].Area.Width < maxAreaWidth)
                            {
                                mapCache[y, x].Area         = mapCache[y, x - 1].Area; //Erweitere bei Wahr den Bereich des schon vorhandenen Knotens
                                mapCache[y, x].Area.XRight += 1;
                            }
                        }
                    }
                    if (mapCache[y, x].Area == null)
                    {
                        int x1 = x - offSetX;
                        int x2 = x - offSetX;
                        int y1 = y + 1 - offSetY;

                        AddArea(x1, x2, y1);
                        mapCache[y, x].Area = allAreas.Last.Value;
                    }
                }
            }
        }
        //Füge die Knoten ein, da nun alle Bereiche erkannt wurden.
        foreach (PlatformArea area in allAreas)
        {
            Node toAddLeft = new AStarNode(area.XLeft, area.YBottom);
            area.LeftNode = toAddLeft;
            AllNodesIntern.Add(toAddLeft);
            if (area.XLeft != area.XRight)
            {
                Node toAddRight = new AStarNode(area.XRight, area.YBottom);
                area.RightNode = toAddRight;
                AllNodesIntern.Add(toAddRight);
            }
        }

        //Füge einen Knoten ein, der die Aktuelle Position der AI darstellt, sofern ein solcher Knoten noch nicht vorhanden ist
        int xAi = DivByBoxFactor(aiPosition.x);
        int yAi = DivByBoxFactor(aiPosition.y) - 1;

        if (mapCache[yAi + offSetY, xAi + offSetX].Type != SquareType.AIR)
        {
            yAi += 1;
        }
        Node aiNode = new AStarNode(xAi, yAi);

        if (!AllNodesIntern.Contains(aiNode))
        {
            AllNodesIntern.Add(aiNode);
            actualAINode = aiNode;
        }
        else
        {
            actualAINode = AllNodesIntern[AllNodesIntern.IndexOf(aiNode)];
        }
    }