|
6 | 6 | using System.Linq; |
7 | 7 | using BepuPhysics.Collidables; |
8 | 8 | using BepuPhysics.CollisionDetection; |
| 9 | +using Stride.BepuPhysics.Components; |
9 | 10 | using Stride.BepuPhysics.Constraints; |
10 | 11 | using Stride.BepuPhysics.Definitions; |
11 | 12 | using Stride.BepuPhysics.Definitions.Colliders; |
@@ -397,6 +398,103 @@ int TestRemovalUnsafe(BepuSimulation simulation) |
397 | 398 | } |
398 | 399 | } |
399 | 400 |
|
| 401 | + [Fact] |
| 402 | + public static void OnSimulationUpdateRemovalTest() |
| 403 | + { |
| 404 | + var game = new GameTest(); |
| 405 | + game.Script.AddTask(async () => |
| 406 | + { |
| 407 | + game.ScreenShotAutomationEnabled = false; |
| 408 | + |
| 409 | + var listOfUpdate = new List<int>(); |
| 410 | + var c2 = new StaticComponent { Collider = new CompoundCollider { Colliders = { new BoxCollider() } } }; |
| 411 | + |
| 412 | + var allEntities = game.SceneSystem.SceneInstance.RootScene.Entities; |
| 413 | + |
| 414 | + Entity a, b, c, d, e; |
| 415 | + allEntities.AddRange(new[] |
| 416 | + { |
| 417 | + a = new Entity { new SimUpdateListener { SimUpdate = () => { listOfUpdate.Add(0); } } }, |
| 418 | + b = new Entity { new SimUpdateListener { SimUpdate = () => { listOfUpdate.Add(1); } } }, |
| 419 | + c = new Entity { new SimUpdateListener { SimUpdate = () => { listOfUpdate.Add(2); } } }, |
| 420 | + d = new Entity { new SimUpdateListener { SimUpdate = () => { listOfUpdate.Add(3); } } }, |
| 421 | + new Entity { c2 }, |
| 422 | + }); |
| 423 | + |
| 424 | + var simulation = allEntities[0].GetSimulation(); |
| 425 | + |
| 426 | + |
| 427 | + // First test, check if every component received its update |
| 428 | + { |
| 429 | + await simulation.AfterUpdate(); |
| 430 | + |
| 431 | + Assert.Equal([0, 1, 2, 3], listOfUpdate); |
| 432 | + } |
| 433 | + |
| 434 | + |
| 435 | + // Second test, check if removing works appropriately |
| 436 | + { |
| 437 | + listOfUpdate.Clear(); |
| 438 | + allEntities.Remove(b); |
| 439 | + allEntities.Remove(c); |
| 440 | + |
| 441 | + await simulation.AfterUpdate(); |
| 442 | + |
| 443 | + // We've removed the second and third before running sim, |
| 444 | + // so only the first and fourth should report as having received the update |
| 445 | + Assert.Equal([0, 3], listOfUpdate); |
| 446 | + } |
| 447 | + |
| 448 | + |
| 449 | + // Clearing multiple listeners while running a listener |
| 450 | + { |
| 451 | + listOfUpdate.Clear(); |
| 452 | + allEntities.Remove(a); |
| 453 | + allEntities.Remove(d); |
| 454 | + |
| 455 | + var toRemove = new List<Entity>(); |
| 456 | + allEntities.AddRange(new[] |
| 457 | + { |
| 458 | + a = new Entity { new SimUpdateListener { SimUpdate = () => { listOfUpdate.Add(0); } } }, |
| 459 | + b = new Entity { new SimUpdateListener { SimUpdate = () => { listOfUpdate.Add(1); } } }, |
| 460 | + c = new Entity |
| 461 | + { |
| 462 | + new SimUpdateListener |
| 463 | + { |
| 464 | + SimUpdate = () => |
| 465 | + { |
| 466 | + listOfUpdate.Add(2); |
| 467 | + foreach (var entity in toRemove) |
| 468 | + allEntities.Remove(entity); |
| 469 | + } |
| 470 | + } |
| 471 | + }, |
| 472 | + d = new Entity { new SimUpdateListener { SimUpdate = () => { listOfUpdate.Add(3); } } }, |
| 473 | + e = new Entity { new SimUpdateListener { SimUpdate = () => { listOfUpdate.Add(4); } } } |
| 474 | + }); |
| 475 | + |
| 476 | + toRemove.AddRange([a, b, c, e]); |
| 477 | + |
| 478 | + await simulation.AfterUpdate(); |
| 479 | + |
| 480 | + // We've removed a, b and c right after running them, e before it could run and haven't removed d |
| 481 | + Assert.Equal([0, 1, 2, 3], listOfUpdate); |
| 482 | + } |
| 483 | + |
| 484 | + game.Exit(); |
| 485 | + }); |
| 486 | + RunGameTest(game); |
| 487 | + } |
| 488 | + |
| 489 | + private class SimUpdateListener : ScriptComponent, ISimulationUpdate |
| 490 | + { |
| 491 | + public Action? SimUpdate, AfterSimUpdate; |
| 492 | + |
| 493 | + public void SimulationUpdate(BepuSimulation simulation, float simTimeStep) => SimUpdate?.Invoke(); |
| 494 | + |
| 495 | + public void AfterSimulationUpdate(BepuSimulation simulation, float simTimeStep) => AfterSimUpdate?.Invoke(); |
| 496 | + } |
| 497 | + |
400 | 498 | private class ContactEvents : IContactHandler |
401 | 499 | { |
402 | 500 | public required bool NoContactResponse { get; init; } |
|
0 commit comments