public void Retreat(LocatableObject obj) { animationState = ShipAnimationState.None; path = null; Location target = new Location(Position.X, Position.Y); for (int i = 0; i < speed; i++) { if (Position.X > obj.Position.X) { target.X++; } if (Position.X <= obj.Position.X) { target.X--; } if (Position.Y > obj.Position.Y) { target.Y++; } if (Position.Y <= obj.Position.Y) { target.Y--; } if (target.X < 0) { target.X = 0; } if (target.Y < 0) { target.Y = 0; } if (target.X > space.Width) { target.X = space.Width; } if (target.Y > space.Height) { target.Y = space.Height; } MoveTo(target); } }
public virtual void MoveTo(Location target) { animationState = ShipAnimationState.None; if (path == null || path.Count == 0) { path = BFS(Position, target); } else { if (path.Count > 0 && space[path.Peek().X, path.Peek().Y] == null) { space[path.Peek().X, path.Peek().Y] = this; path.Pop(); } else { path = null; } } }