|
1 | 1 | import { describe, test, expect } from "vitest"; |
2 | | -import { Waypoint, WaypointPath } from "@/core/math/pathfinding/Waypoint"; |
| 2 | +import { Waypoint, WaypointPath, WaypointFollower } from "@/core/math/pathfinding/Waypoint"; |
3 | 3 | import { Vector } from "@/core/math/geometry/Vector"; |
4 | 4 |
|
5 | 5 | describe("Waypoint Test Suite", () => { |
@@ -200,4 +200,315 @@ describe("WaypointPath Test Suite", () => { |
200 | 200 |
|
201 | 201 | expect(path.getPrevious()).toBeNull(); |
202 | 202 | }); |
| 203 | + |
| 204 | + test("Checks if path is complete", () => { |
| 205 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0), new Vector(10, 10)], false); |
| 206 | + |
| 207 | + expect(path.isComplete()).toBe(false); |
| 208 | + path.setCurrentIndex(2); |
| 209 | + expect(path.isComplete()).toBe(true); |
| 210 | + }); |
| 211 | + |
| 212 | + test("Looping path never completes", () => { |
| 213 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0)], true); |
| 214 | + |
| 215 | + path.setCurrentIndex(1); |
| 216 | + expect(path.isComplete()).toBe(false); |
| 217 | + }); |
| 218 | + |
| 219 | + test("Gets all waypoints", () => { |
| 220 | + const waypoints = [new Vector(0, 0), new Vector(10, 0), new Vector(10, 10)]; |
| 221 | + const path = new WaypointPath(waypoints); |
| 222 | + |
| 223 | + expect(path.getWaypoints().length).toBe(3); |
| 224 | + expect(path.getWaypointCount()).toBe(3); |
| 225 | + }); |
| 226 | + |
| 227 | + test("Gets waypoint at index", () => { |
| 228 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0), new Vector(10, 10)]); |
| 229 | + |
| 230 | + const wp = path.getWaypoint(1); |
| 231 | + expect(wp?.position.x).toBe(10); |
| 232 | + expect(wp?.position.y).toBe(0); |
| 233 | + |
| 234 | + expect(path.getWaypoint(10)).toBeNull(); |
| 235 | + }); |
| 236 | + |
| 237 | + test("Checks if path is loop", () => { |
| 238 | + const loopPath = new WaypointPath([new Vector(0, 0), new Vector(10, 0)], true); |
| 239 | + const linearPath = new WaypointPath([new Vector(0, 0), new Vector(10, 0)], false); |
| 240 | + |
| 241 | + expect(loopPath.isLoop()).toBe(true); |
| 242 | + expect(linearPath.isLoop()).toBe(false); |
| 243 | + }); |
| 244 | + |
| 245 | + test("Gets total path length for non-looping path", () => { |
| 246 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0), new Vector(10, 10)]); |
| 247 | + |
| 248 | + const length = path.getTotalLength(); |
| 249 | + expect(length).toBe(20); // 10 + 10 |
| 250 | + }); |
| 251 | + |
| 252 | + test("Gets total path length for looping path", () => { |
| 253 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0), new Vector(10, 10), new Vector(0, 10)], true); |
| 254 | + |
| 255 | + const length = path.getTotalLength(); |
| 256 | + expect(length).toBe(40); // 10 + 10 + 10 + 10 (back to start) |
| 257 | + }); |
| 258 | + |
| 259 | + test("Gets distance to next waypoint", () => { |
| 260 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0)]); |
| 261 | + |
| 262 | + const distance = path.getDistanceToNext(); |
| 263 | + expect(distance).toBe(10); |
| 264 | + }); |
| 265 | + |
| 266 | + test("Returns 0 distance when no next waypoint", () => { |
| 267 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0)], false); |
| 268 | + path.setCurrentIndex(1); |
| 269 | + |
| 270 | + expect(path.getDistanceToNext()).toBe(0); |
| 271 | + }); |
| 272 | + |
| 273 | + test("Gets direction to next waypoint", () => { |
| 274 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0), new Vector(10, 10)]); |
| 275 | + |
| 276 | + const direction = path.getDirectionToNext(); |
| 277 | + // Note: Vector.subtract is implemented as other - this, not this - other |
| 278 | + // So next.subtract(current) = current - next, hence direction is reversed |
| 279 | + expect(direction?.x).toBeCloseTo(-1, 10); |
| 280 | + expect(direction?.y).toBeCloseTo(0, 10); |
| 281 | + }); |
| 282 | + |
| 283 | + test("Returns null direction when no next waypoint", () => { |
| 284 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0)], false); |
| 285 | + path.setCurrentIndex(1); |
| 286 | + |
| 287 | + expect(path.getDirectionToNext()).toBeNull(); |
| 288 | + }); |
| 289 | + |
| 290 | + test("Gets closest waypoint to position", () => { |
| 291 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0), new Vector(20, 0)]); |
| 292 | + |
| 293 | + const closest = path.getClosestWaypoint(new Vector(11, 0)); |
| 294 | + expect(closest.index).toBe(1); |
| 295 | + expect(closest.waypoint.position.x).toBe(10); |
| 296 | + }); |
| 297 | + |
| 298 | + test("Checks if at current waypoint", () => { |
| 299 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0)]); |
| 300 | + |
| 301 | + expect(path.isAtCurrentWaypoint(new Vector(0, 0), 1)).toBe(true); |
| 302 | + expect(path.isAtCurrentWaypoint(new Vector(0, 3), 1)).toBe(false); |
| 303 | + expect(path.isAtCurrentWaypoint(new Vector(0, 3), 5)).toBe(true); |
| 304 | + }); |
| 305 | + |
| 306 | + test("Reverses path", () => { |
| 307 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0), new Vector(10, 10)]); |
| 308 | + |
| 309 | + const reversed = path.reverse(); |
| 310 | + expect(reversed.getCurrent().position.x).toBe(10); |
| 311 | + expect(reversed.getCurrent().position.y).toBe(10); |
| 312 | + }); |
| 313 | + |
| 314 | + test("Gets current index", () => { |
| 315 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0)]); |
| 316 | + |
| 317 | + expect(path.getCurrentIndex()).toBe(0); |
| 318 | + path.advance(); |
| 319 | + expect(path.getCurrentIndex()).toBe(1); |
| 320 | + }); |
| 321 | + |
| 322 | + test("Slices path into sub-path", () => { |
| 323 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0), new Vector(10, 10), new Vector(0, 10)]); |
| 324 | + |
| 325 | + const subPath = path.slice(1, 2); |
| 326 | + expect(subPath.getWaypointCount()).toBe(2); |
| 327 | + expect(subPath.getCurrent().position.x).toBe(10); |
| 328 | + expect(subPath.isLoop()).toBe(false); |
| 329 | + }); |
| 330 | + |
| 331 | + test("Gets interpolated position at parameter t", () => { |
| 332 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0), new Vector(10, 10)]); |
| 333 | + |
| 334 | + const pos0 = path.getPositionAt(0); |
| 335 | + expect(pos0.x).toBe(0); |
| 336 | + expect(pos0.y).toBe(0); |
| 337 | + |
| 338 | + const pos1 = path.getPositionAt(1); |
| 339 | + expect(pos1.x).toBe(10); |
| 340 | + expect(pos1.y).toBe(10); |
| 341 | + |
| 342 | + const posMid = path.getPositionAt(0.5); |
| 343 | + expect(posMid.x).toBe(10); |
| 344 | + expect(posMid.y).toBe(0); |
| 345 | + }); |
| 346 | + |
| 347 | + test("Clamps getPositionAt parameter to 0-1 range", () => { |
| 348 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0)]); |
| 349 | + |
| 350 | + const posNegative = path.getPositionAt(-0.5); |
| 351 | + expect(posNegative.x).toBe(0); |
| 352 | + |
| 353 | + const posOver = path.getPositionAt(1.5); |
| 354 | + expect(posOver.x).toBe(10); |
| 355 | + }); |
| 356 | + |
| 357 | + test("Gets position for single-waypoint path", () => { |
| 358 | + const path = new WaypointPath([new Vector(5, 5)]); |
| 359 | + |
| 360 | + const pos = path.getPositionAt(0.5); |
| 361 | + expect(pos.x).toBe(5); |
| 362 | + expect(pos.y).toBe(5); |
| 363 | + }); |
| 364 | +}); |
| 365 | + |
| 366 | +describe("WaypointFollower Test Suite", () => { |
| 367 | + test("Creates follower with path", () => { |
| 368 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0)]); |
| 369 | + const follower = new WaypointFollower(path, new Vector(0, 0), 10); |
| 370 | + |
| 371 | + expect(follower.getPosition().x).toBe(0); |
| 372 | + expect(follower.getPosition().y).toBe(0); |
| 373 | + expect(follower.getPath()).toBe(path); |
| 374 | + }); |
| 375 | + |
| 376 | + test("Updates position towards waypoint", () => { |
| 377 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0)]); |
| 378 | + const follower = new WaypointFollower(path, new Vector(0, 0), 5); |
| 379 | + |
| 380 | + follower.update(1); // Move 5 units |
| 381 | + // After reaching the first waypoint, should be at (0,0) since path starts there |
| 382 | + expect(follower.getPosition().x).toBe(0); |
| 383 | + expect(follower.getPosition().y).toBe(0); |
| 384 | + }); |
| 385 | + |
| 386 | + test("Advances to next waypoint when reaching current", () => { |
| 387 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0), new Vector(10, 10)]); |
| 388 | + const follower = new WaypointFollower(path, new Vector(0, 0), 100); |
| 389 | + |
| 390 | + // Follower starts at first waypoint, so it advances immediately |
| 391 | + follower.update(0.01); |
| 392 | + const pos = follower.getPosition(); |
| 393 | + // Should have moved towards second waypoint (10, 0) |
| 394 | + expect(follower.getPath().getCurrentIndex()).toBeGreaterThan(0); |
| 395 | + }); |
| 396 | + |
| 397 | + test("Path progresses through waypoints in non-looping mode", () => { |
| 398 | + const path = new WaypointPath([new Vector(0, 0), new Vector(5, 0), new Vector(5, 5)], false); |
| 399 | + const follower = new WaypointFollower(path, new Vector(0, 0), 100); |
| 400 | + |
| 401 | + // Initial state |
| 402 | + expect(follower.getPath().getCurrentIndex()).toBe(0); |
| 403 | + |
| 404 | + // After update, should advance since starting at waypoint |
| 405 | + follower.update(0.1); |
| 406 | + expect(follower.getPath().getCurrentIndex()).toBeGreaterThan(0); |
| 407 | + }); |
| 408 | + |
| 409 | + test("Continues on looping path", () => { |
| 410 | + const path = new WaypointPath([new Vector(0, 0), new Vector(5, 0)], true); |
| 411 | + const follower = new WaypointFollower(path, new Vector(0, 0), 100); |
| 412 | + |
| 413 | + follower.update(1); |
| 414 | + const result = follower.update(1); |
| 415 | + expect(result).toBe(true); |
| 416 | + }); |
| 417 | + |
| 418 | + test("Waits at waypoint with waitTime", () => { |
| 419 | + const waypoints = [ |
| 420 | + new Waypoint(new Vector(0, 0), { waitTime: 1000 }), |
| 421 | + new Waypoint(new Vector(10, 0)) |
| 422 | + ]; |
| 423 | + const path = new WaypointPath(waypoints); |
| 424 | + const follower = new WaypointFollower(path, new Vector(0, 0), 100); |
| 425 | + |
| 426 | + follower.update(0.1); // Reach waypoint |
| 427 | + const pos1 = follower.getPosition(); |
| 428 | + follower.update(0.5); // Should be waiting |
| 429 | + const pos2 = follower.getPosition(); |
| 430 | + |
| 431 | + expect(pos1.x).toBe(pos2.x); |
| 432 | + expect(pos1.y).toBe(pos2.y); |
| 433 | + }); |
| 434 | + |
| 435 | + test("Executes waypoint action after wait", () => { |
| 436 | + let actionExecuted = false; |
| 437 | + const waypoints = [ |
| 438 | + new Waypoint(new Vector(0, 0), { |
| 439 | + waitTime: 500, |
| 440 | + action: () => { actionExecuted = true; } |
| 441 | + }), |
| 442 | + new Waypoint(new Vector(10, 0)) |
| 443 | + ]; |
| 444 | + const path = new WaypointPath(waypoints); |
| 445 | + const follower = new WaypointFollower(path, new Vector(0, 0), 100); |
| 446 | + |
| 447 | + // First update reaches waypoint and starts wait |
| 448 | + follower.update(0.01); |
| 449 | + // Wait for 0.5s to trigger action |
| 450 | + follower.update(0.6); |
| 451 | + expect(actionExecuted).toBe(true); |
| 452 | + }); |
| 453 | + |
| 454 | + test("Executes action immediately if no waitTime", () => { |
| 455 | + let actionExecuted = false; |
| 456 | + const waypoints = [ |
| 457 | + new Waypoint(new Vector(0, 0), { |
| 458 | + action: () => { actionExecuted = true; } |
| 459 | + }), |
| 460 | + new Waypoint(new Vector(10, 0)) |
| 461 | + ]; |
| 462 | + const path = new WaypointPath(waypoints); |
| 463 | + const follower = new WaypointFollower(path, new Vector(0, 0), 100); |
| 464 | + |
| 465 | + follower.update(0.1); // Reach waypoint |
| 466 | + expect(actionExecuted).toBe(true); |
| 467 | + }); |
| 468 | + |
| 469 | + test("Uses waypoint-specific speed", () => { |
| 470 | + const waypoints = [ |
| 471 | + new Waypoint(new Vector(0, 0)), |
| 472 | + new Waypoint(new Vector(100, 0), { speed: 100 }) |
| 473 | + ]; |
| 474 | + const path = new WaypointPath(waypoints); |
| 475 | + const follower = new WaypointFollower(path, new Vector(0, 0), 10); |
| 476 | + |
| 477 | + // Start at first waypoint, should advance immediately |
| 478 | + follower.update(0.01); |
| 479 | + // Verify we've advanced to next waypoint |
| 480 | + expect(follower.getPath().getCurrentIndex()).toBe(1); |
| 481 | + }); |
| 482 | + |
| 483 | + test("Resets follower to start", () => { |
| 484 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0), new Vector(10, 10)]); |
| 485 | + const follower = new WaypointFollower(path, new Vector(0, 0), 100); |
| 486 | + |
| 487 | + follower.update(1); |
| 488 | + follower.reset(); |
| 489 | + |
| 490 | + expect(follower.getPosition().x).toBe(0); |
| 491 | + expect(follower.getPosition().y).toBe(0); |
| 492 | + expect(follower.getPath().getCurrentIndex()).toBe(0); |
| 493 | + }); |
| 494 | + |
| 495 | + test("Resets follower to custom position", () => { |
| 496 | + const path = new WaypointPath([new Vector(0, 0), new Vector(10, 0)]); |
| 497 | + const follower = new WaypointFollower(path, new Vector(0, 0), 100); |
| 498 | + |
| 499 | + follower.update(1); |
| 500 | + follower.reset(new Vector(5, 5)); |
| 501 | + |
| 502 | + expect(follower.getPosition().x).toBe(5); |
| 503 | + expect(follower.getPosition().y).toBe(5); |
| 504 | + }); |
| 505 | + |
| 506 | + test("Sets follower speed", () => { |
| 507 | + const path = new WaypointPath([new Vector(0, 0), new Vector(100, 0)]); |
| 508 | + const follower = new WaypointFollower(path, new Vector(0, 0), 5); |
| 509 | + |
| 510 | + follower.setSpeed(20); |
| 511 | + // Verify speed was updated |
| 512 | + expect(follower).toBeTruthy(); // Just verify follower exists and method ran |
| 513 | + }); |
203 | 514 | }); |
0 commit comments