EntityManager.cpp 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <boost/test/unit_test.hpp>
  16. #include "core/EntityManager.h"
  17. BOOST_AUTO_TEST_CASE(TestEntityManagerInteractionPicksTheEntityTheCharacterFaces){
  18. // The maximum angle is 90 degrees, and no candidate yet.
  19. const float max_angle = 90.0f;
  20. const float no_distance = 1000.0f;
  21. BOOST_CHECK(EntityManager::IsBetterInteractionTarget(30.0f, 1.0f, max_angle, no_distance));
  22. // Facing the character more directly is better, whatever the order.
  23. BOOST_CHECK(EntityManager::IsBetterInteractionTarget(5.0f, 1.0f, 30.0f, 1.0f));
  24. BOOST_CHECK(!EntityManager::IsBetterInteractionTarget(30.0f, 1.0f, 5.0f, 1.0f));
  25. // A candidate further from the front than the best one is not better.
  26. BOOST_CHECK(!EntityManager::IsBetterInteractionTarget(120.0f, 1.0f, max_angle, no_distance));
  27. // The same angle: the nearest wins, and the first one is kept on a tie.
  28. BOOST_CHECK(EntityManager::IsBetterInteractionTarget(10.0f, 0.5f, 10.0f, 1.0f));
  29. BOOST_CHECK(!EntityManager::IsBetterInteractionTarget(10.0f, 1.0f, 10.0f, 0.5f));
  30. BOOST_CHECK(!EntityManager::IsBetterInteractionTarget(10.0f, 1.0f, 10.0f, 1.0f));
  31. // Two characters side by side, in the list order Jessie then Wedge: facing Jessie must
  32. // choose Jessie, and facing Wedge must choose Wedge.
  33. const float jessie_angle_facing_jessie = 4.0f;
  34. const float wedge_angle_facing_jessie = 40.0f;
  35. float best_angle = max_angle;
  36. float best_distance = no_distance;
  37. int chosen = -1;
  38. const float angles[] = {jessie_angle_facing_jessie, wedge_angle_facing_jessie};
  39. for (int i = 0; i < 2; ++i){
  40. if (EntityManager::IsBetterInteractionTarget(angles[i], 0.2f, best_angle, best_distance)){
  41. best_angle = angles[i];
  42. best_distance = 0.2f;
  43. chosen = i;
  44. }
  45. }
  46. BOOST_CHECK_EQUAL(chosen, 0);
  47. }
  48. BOOST_AUTO_TEST_CASE(TestEntityManagerLinesTriggerEvenIfAScriptZeroedTheRadius){
  49. // A script sets the radius of the player to zero while a scene plays: lines must still work.
  50. BOOST_CHECK_CLOSE(EntityManager::TriggerReach(0.0f), 0.21f, 0.01f);
  51. BOOST_CHECK_CLOSE(EntityManager::TriggerReach(0.005f), 0.21f, 0.01f);
  52. // A radius that a script chose is respected.
  53. BOOST_CHECK_CLOSE(EntityManager::TriggerReach(0.265625f), 0.265625f, 0.01f);
  54. BOOST_CHECK_CLOSE(EntityManager::TriggerReach(0.078125f), 0.078125f, 0.01f);
  55. }
  56. BOOST_AUTO_TEST_CASE(TestEntityManagerScriptedWalksAreNotStoppedByTheWalkmeshEdge){
  57. // The player and the characters that wander are stopped.
  58. BOOST_CHECK(EntityManager::WalkmeshEdgeBlocks(true, false));
  59. BOOST_CHECK(EntityManager::WalkmeshEdgeBlocks(false, false));
  60. // Even if a script moves the player, the player stays on the walkmesh.
  61. BOOST_CHECK(EntityManager::WalkmeshEdgeBlocks(true, true));
  62. // A character a script walks to a position is not.
  63. BOOST_CHECK(!EntityManager::WalkmeshEdgeBlocks(false, true));
  64. }
  65. BOOST_AUTO_TEST_CASE(TestEntityManager){
  66. BOOST_CHECK_CLOSE(EntityManager::ScriptedMoveStep(0.5f, 0.0f, 0.1f), 0.1f, 0.01f);
  67. BOOST_CHECK_CLOSE(EntityManager::ScriptedMoveStep(0.05f, 0.0f, 0.1f), 0.05f, 0.01f);
  68. BOOST_CHECK_CLOSE(EntityManager::ScriptedMoveStep(0.55f, 0.5f, 0.1f), 0.05f, 0.01f);
  69. BOOST_CHECK_EQUAL(EntityManager::ScriptedMoveStep(0.4f, 0.5f, 0.1f), 0.0f);
  70. BOOST_CHECK_EQUAL(EntityManager::ScriptedMoveStep(1.0f, 0.0f, 0.0f), 0.0f);
  71. for (float frame_time : {1.0f / 120.0f, 1.0f / 60.0f, 1.0f / 30.0f}){
  72. float distance = 2.0f;
  73. for (int step = 0; step < 1000 && distance > 0.0001f; ++step){
  74. distance -= EntityManager::ScriptedMoveStep(distance, 0.0f, 6.0f * frame_time);
  75. }
  76. BOOST_CHECK_SMALL(distance, 0.0001f);
  77. }
  78. }