C111111_RigidBody_EnablingGravityWorksUsingNotificationsPoC.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. # Test case ID : C111111
  7. # Test Case Title : Check that Gravity works
  8. # fmt:off
  9. class Tests:
  10. enter_game_mode = ("Entered game mode", "Failed to enter game mode")
  11. find_ball = ("Entity Ball found", "Ball not found")
  12. find_terrain = ("Entity Terrain found", "Terrain not found")
  13. gravity_started_disabled = ("Gravity started disabled", "Gravity didn't start disabled")
  14. gravity_set_enabled = ("Gravity has been enabled", "Gravity wasn't enabled")
  15. ball_fell = ("Ball fell", "Ball didn't fall")
  16. exit_game_mode = ("Exited game mode", "Couldn't exit game mode")
  17. # fmt:on
  18. def C111111_RigidBody_EnablingGravityWorksUsingNotificationsPoC():
  19. # Setup path
  20. import os
  21. import sys
  22. import ImportPathHelper as imports
  23. imports.init()
  24. from editor_python_test_tools.utils import Report
  25. from editor_python_test_tools.utils import TestHelper as helper
  26. import azlmbr.legacy.general as general
  27. import azlmbr.bus
  28. helper.init_idle()
  29. # 1) Open level
  30. helper.open_level("Physics", "EnablingGravityWorks")
  31. # 2) Enter game mode
  32. helper.enter_game_mode(Tests.enter_game_mode)
  33. # 3) Retrieve entities
  34. ball_id = general.find_game_entity("Ball")
  35. Report.result(Tests.find_ball, ball_id.IsValid())
  36. terrain_id = general.find_game_entity("Terrain")
  37. Report.result(Tests.find_terrain, terrain_id.IsValid())
  38. # 4) Make sure gravity is off from the start
  39. gravity_enabled = azlmbr.physics.RigidBodyRequestBus(azlmbr.bus.Event, "IsGravityEnabled", ball_id)
  40. Report.result(Tests.gravity_started_disabled, not gravity_enabled)
  41. # 5) Activate gravity
  42. Report.info("Enabling Gravity")
  43. azlmbr.physics.RigidBodyRequestBus(azlmbr.bus.Event, "ForceAwake", ball_id)
  44. azlmbr.physics.RigidBodyRequestBus(azlmbr.bus.Event, "SetGravityEnabled", ball_id, True)
  45. gravity_enabled = azlmbr.physics.RigidBodyRequestBus(azlmbr.bus.Event, "IsGravityEnabled", ball_id)
  46. Report.result(Tests.gravity_set_enabled, gravity_enabled)
  47. # 6) Listen to collision events seconds so it falls down
  48. class TouchGround:
  49. value = False
  50. def on_collision_begin(args):
  51. other_id = args[0]
  52. if other_id.Equal(terrain_id):
  53. Report.info("Touched ground")
  54. TouchGround.value = True
  55. handler = azlmbr.physics.CollisionNotificationBusHandler()
  56. handler.connect(ball_id)
  57. handler.add_callback("OnCollisionBegin", on_collision_begin)
  58. helper.wait_for_condition(lambda: TouchGround.value, 3.0)
  59. Report.result(Tests.ball_fell, TouchGround.value)
  60. # 7) Exit game mode
  61. helper.exit_game_mode(Tests.exit_game_mode)
  62. if __name__ == "__main__":
  63. import ImportPathHelper as imports
  64. imports.init()
  65. from editor_python_test_tools.utils import Report
  66. Report.start_test(C111111_RigidBody_EnablingGravityWorksUsingNotificationsPoC)