ScriptCanvas_SpawnEntityWithPhysComponents.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 : C6224408
  7. # Test Case Title : Entity using PhysX nodes in Script Canvas can be spawned.
  8. # fmt: off
  9. class Tests:
  10. enter_game_mode = ("Entered game mode", "Failed to enter game mode")
  11. spawn_point_position = ("Spawn_Point is above the terrain", "Spawn_Point is below the terrain")
  12. Ball_0_found = ("Ball_0 entity is valid", "Ball_0 entity is not valid")
  13. Spawn_Point_found = ("Spawn_Point entity is valid", "Spawn_Point entity is not valid")
  14. Terrain_found = ("Terrain entity is valid", "Terrain entity is not valid")
  15. ball_spawned = ("A ball has been spawned", "A ball has not been spawned")
  16. exit_game_mode = ("Exited game mode", "Couldn't exit game mode")
  17. # fmt: on
  18. def ScriptCanvas_SpawnEntityWithPhysComponents():
  19. """
  20. Summary:
  21. A spawner is set to spawn a sphere with downward velocity after a set amount of time. This action is controlled
  22. by a scriptcanvas.
  23. Level Description:
  24. Ball_0 - Existing only for user reference, a prefab is taken from Ball_0 and used by the Spawn_point entity.
  25. Is set with velocity in the negative -z direction; has sphere shaped Collider, Rigid Body, Sphere shape.
  26. Spawn_Point - Serves as the world location to pass into an attached Script Canvas that will spawn Ball_0 at its
  27. location using the Prefab > Spawning nodes.
  28. Terrain - Surface for spawned ball to bounce against; has terrain
  29. Script Canvas - The script canvas after a delay of 0.5 seconds tells the Spawn_Point entity to spawn.
  30. Expected Behavior:
  31. Script canvas will spawn a ball and collide with the terrain component.
  32. Test Steps:
  33. 1) Open Level
  34. 2) Enter Game Mode
  35. 3) Initialize and validate entities
  36. 4) Set up handler for terrain collision
  37. 5) Wait for terrain collision
  38. 6) Log Results
  39. 7) Exit Game Mode
  40. 8) Close Editor
  41. Note:
  42. - This test file must be called from the Open 3D Engine Editor command terminal
  43. - Any passed and failed tests are written to the Editor.log file.
  44. Parsing the file or running a log_monitor are required to observe the test results.
  45. :return: None
  46. """
  47. import os
  48. import sys
  49. from editor_python_test_tools.utils import Report
  50. from editor_python_test_tools.utils import TestHelper as helper
  51. import azlmbr.legacy.general as general
  52. import azlmbr.bus
  53. import azlmbr.entity
  54. # Constants
  55. FLOAT_THRESHOLD = sys.float_info.epsilon
  56. TIMEOUT = 3.0
  57. # Global Variables
  58. id_list = []
  59. # Helper Functions
  60. class Collision:
  61. collision_on_terrain = False
  62. class Entity:
  63. def __init__(self, name, has_rigid_body):
  64. self.id = general.find_game_entity(name)
  65. self.name = name
  66. self.initial_position = azlmbr.components.TransformBus(azlmbr.bus.Event, "GetWorldTranslation", self.id)
  67. id_list.append(self.id)
  68. class Tests:
  69. found = None
  70. def validate_entity(self):
  71. self.Tests.found = Tests.__dict__[self.name + "_found"]
  72. Report.critical_result(self.Tests.found, self.id.isValid())
  73. def spawn_point_position_check(spawn_point, terrain):
  74. position_valid = (
  75. spawn_point.initial_position.z > terrain.initial_position.z
  76. )
  77. Report.critical_result(Tests.spawn_point_position, position_valid)
  78. def on_terrain_collision(arg):
  79. if arg[0] not in id_list: # ensures that the collision is with a new entity
  80. Collision.collision_on_terrain = True
  81. Report.info("Something new has collided with the Terrain")
  82. # Main Script
  83. helper.init_idle()
  84. # 1) Open Level
  85. helper.open_level("Physics", "ScriptCanvas_SpawnEntityWithPhysComponents")
  86. # 2) Enter Game Mode
  87. helper.enter_game_mode(Tests.enter_game_mode)
  88. # 3) Initialize and validated entities
  89. ball_0 = Entity("Ball_0", True)
  90. spawn_point = Entity("Spawn_Point", False)
  91. terrain = Entity("Terrain", False)
  92. entity_list = [ball_0, spawn_point, terrain]
  93. for entity in entity_list:
  94. entity.validate_entity()
  95. spawn_point_position_check(spawn_point, terrain)
  96. # 4) Set up handler for terrain collision
  97. handler = azlmbr.physics.CollisionNotificationBusHandler()
  98. handler.connect(terrain.id)
  99. handler.add_callback("OnCollisionBegin", on_terrain_collision)
  100. # 5) Wait for terrain collision
  101. helper.wait_for_condition(lambda: Collision.collision_on_terrain, TIMEOUT)
  102. # 6) Log Results
  103. Report.result(Tests.ball_spawned, Collision.collision_on_terrain)
  104. # 7) Exit Game Mode
  105. helper.exit_game_mode(Tests.exit_game_mode)
  106. if __name__ == "__main__":
  107. from editor_python_test_tools.utils import Report
  108. Report.start_test(ScriptCanvas_SpawnEntityWithPhysComponents)