private SinglyLinkedList <Point> FindPaths(Point start, Point target, GameMap map) { var queue = new Queue <Point>(); var visited = new HashSet <Point>(); var ways = new Dictionary <Point, SinglyLinkedList <Point> >(); queue.Enqueue(start); visited.Add(start); ways[start] = new SinglyLinkedList <Point>(start); while (queue.Count > 0) { var point = queue.Dequeue(); if (map.IsCollide(this, point) || ways[point].Length > 20) { continue; } foreach (var vector in vectors) { var nextPoint = new Point(point.X + vector.X, point.Y + vector.Y); if (visited.Contains(nextPoint)) { continue; } queue.Enqueue(nextPoint); visited.Add(nextPoint); ways[nextPoint] = new SinglyLinkedList <Point>(nextPoint, ways[point]); } } if (ways.ContainsKey(target)) { return(ways[target]); } return(null); }
public void Move(int dirX, int dirY, GameMap map) { collided = map.IsCollide(this, new Point(PosX + dirX, PosY + dirY)); if (!collided) { PosY += dirY; PosX += dirX; } }
public void MoveTo(Point target, GameMap map) { if (target == new Point(PosX, PosY)) { map.Lose = true; } var pathToTarget = FindPaths(new Point(PosX, PosY), target, map)?.Reverse().ToList(); if (pathToTarget != null) { var pathInDir = ParseToDirection(pathToTarget).FirstOrDefault(); if (pathInDir.X < 0) { flip = true; } else if (pathInDir.X > 0) { flip = false; } if (pathInDir.X != 0 || pathInDir.Y != 0) { SetAnimationConfiguration("Run"); } else { SetAnimationConfiguration("State"); } PosX += pathInDir.X; PosY += pathInDir.Y; } else { if (pause != 0) { SetAnimationConfiguration("State"); pause--; } else { PosX += baseDir; lenghtWay += baseDir; flip = baseDir < 0; SetAnimationConfiguration("Run"); if (map.IsCollide(this, new Point(PosX + baseDir, PosY)) || Math.Abs(lenghtWay) >= 100) { pause = 10; baseDir *= -1; } } } }
public void Act(GameMap map) { if (!map.IsCollide(this, new Point(PosX + DirX * SpeedValue, PosY + DirY * SpeedValue))) { if (capturedFurniture != null) { capturedFurniture.Move(DirX * SpeedValue, DirY * SpeedValue, map); } if (capturedFurniture != null && !capturedFurniture.CheckCollide() || capturedFurniture == null) { PosX += DirX * SpeedValue; PosY += DirY * SpeedValue; } } }