Lua_Spawnables_NestedSpawn.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. def Lua_Spawnables_NestedSpawn():
  7. import math
  8. import azlmbr.entity as entity
  9. import azlmbr.bus as bus
  10. import azlmbr.math as azmath
  11. import azlmbr.legacy.general as general
  12. from editor_python_test_tools.editor_entity_utils import EditorEntity
  13. from editor_python_test_tools.utils import TestHelper as helper
  14. from Prefab.tests.PrefabTestUtils import validate_spawned_entity_transform
  15. def validate_entities_in_edit_mode(condition):
  16. search_filter = entity.SearchFilter()
  17. search_filter.names = ["Lua_Spawner"]
  18. spawner_entity = entity.SearchBus(bus.Broadcast, 'SearchEntities', search_filter)[0]
  19. search_filter.names = ["Nested_Lua_Spawner"]
  20. nested_spawner_entity = entity.SearchBus(bus.Broadcast, 'SearchEntities', search_filter)
  21. search_filter.names = ["PinkFlower"]
  22. flower_entity = entity.SearchBus(bus.Broadcast, 'SearchEntities', search_filter)
  23. assert spawner_entity is not None, f"Failed to find Spawner entity {condition}"
  24. assert len(nested_spawner_entity) == 0, f"Unexpectedly found Nested_Lua_Spawner entity {condition}"
  25. assert len(flower_entity) == 0, f"Unexpectedly found PinkFlower entity {condition}"
  26. helper.init_idle()
  27. helper.open_level("Prefab", "Lua_Spawnables_NestedSpawn")
  28. # Search for expected entities in edit mode
  29. validate_entities_in_edit_mode("before entering Game Mode")
  30. # Enter Game Mode and search for expected entities
  31. general.enter_game_mode()
  32. helper.wait_for_condition(lambda: EditorEntity(general.find_game_entity("Lua_Spawner")) is not None, 3.0)
  33. spawner_entity = EditorEntity(general.find_game_entity("Lua_Spawner"))
  34. helper.wait_for_condition(lambda: EditorEntity(general.find_game_entity("Nested_Lua_Spawner")) is not None, 3.0)
  35. nested_spawner_entity = EditorEntity(general.find_game_entity("Nested_Lua_Spawner"))
  36. helper.wait_for_condition(lambda: EditorEntity(general.find_game_entity("PinkFlower")) is not None, 3.0)
  37. flower_entity = EditorEntity(general.find_game_entity("PinkFlower"))
  38. assert spawner_entity, "Failed to find Spawner entity at runtime"
  39. assert nested_spawner_entity, "Failed to find Nested_Lua_Spawner entity at runtime"
  40. assert flower_entity, "Failed to find PinkFlower entity at runtime"
  41. # Verify nested spawner entity is spawned with the correct position, rotation, and scale values
  42. expected_spawned_entity_position = azmath.Vector3(5.0, 0.0, 0.0)
  43. expected_spawned_entity_rotation = azmath.Vector3(0.0, 0.0, math.radians(90.0))
  44. expected_spawned_entity_scale = 5.0
  45. validate_spawned_entity_transform(nested_spawner_entity, expected_spawned_entity_position, expected_spawned_entity_rotation,
  46. expected_spawned_entity_scale)
  47. # Verify flower entity is spawned by nested spawner with the correct position, rotation, and scale values
  48. expected_spawned_entity_position = azmath.Vector3(5.0, 0.0, 0.0)
  49. expected_spawned_entity_rotation = azmath.Vector3(0.0, 0.0, math.radians(90.0))
  50. expected_spawned_entity_scale = 5.0
  51. validate_spawned_entity_transform(flower_entity, expected_spawned_entity_position, expected_spawned_entity_rotation,
  52. expected_spawned_entity_scale)
  53. # Exit Game Mode and search for expected entities
  54. general.exit_game_mode()
  55. game_mode_exited = helper.wait_for_condition(lambda: not general.is_in_game_mode(), 5.0)
  56. assert game_mode_exited, "Lua script failed to exit Game Mode"
  57. validate_entities_in_edit_mode("after exiting Game Mode")
  58. if __name__ == "__main__":
  59. from editor_python_test_tools.utils import Report
  60. Report.start_test(Lua_Spawnables_NestedSpawn)