Lua_Spawnables_SimpleSpawnAndDespawn.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_SimpleSpawnAndDespawn():
  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 = ["PinkFlower"]
  20. flower_entity = entity.SearchBus(bus.Broadcast, 'SearchEntities', search_filter)
  21. assert spawner_entity is not None, f"Failed to find Spawner entity {condition}"
  22. assert len(flower_entity) == 0, f"Unexpectedly found PinkFlower entity {condition}"
  23. helper.init_idle()
  24. helper.open_level("Prefab", "Lua_Spawnables_SimpleSpawnDespawn")
  25. # Search for expected entities in edit mode
  26. validate_entities_in_edit_mode("before entering Game Mode")
  27. # Enter Game Mode and search for expected entities
  28. general.enter_game_mode()
  29. helper.wait_for_condition(lambda: EditorEntity(general.find_game_entity("Lua_Spawner")) is not None, 3.0)
  30. spawner_entity = EditorEntity(general.find_game_entity("Lua_Spawner"))
  31. helper.wait_for_condition(lambda: EditorEntity(general.find_game_entity("PinkFlower")) is not None, 3.0)
  32. flower_entity = EditorEntity(general.find_game_entity("PinkFlower"))
  33. assert spawner_entity, "Failed to find Spawner entity at runtime"
  34. assert flower_entity, "Failed to find PinkFlower entity at runtime"
  35. # Verify spawned entity is spawned with the correct position, rotation, and scale values
  36. expected_spawned_entity_position = azmath.Vector3(0.0, 0.0, 5.0)
  37. expected_spawned_entity_rotation = azmath.Vector3(0.0, 0.0, math.radians(90.0))
  38. expected_spawned_entity_scale = 5.0
  39. validate_spawned_entity_transform(flower_entity, expected_spawned_entity_position, expected_spawned_entity_rotation,
  40. expected_spawned_entity_scale)
  41. # Wait for Lua script to exit Game Mode on despawn of PinkFlower.spawnable and search for expected entities
  42. game_mode_exited = helper.wait_for_condition(lambda: not general.is_in_game_mode(), 10.0)
  43. assert game_mode_exited, "Lua script failed to exit Game Mode"
  44. validate_entities_in_edit_mode("after exiting Game Mode")
  45. if __name__ == "__main__":
  46. from editor_python_test_tools.utils import Report
  47. Report.start_test(Lua_Spawnables_SimpleSpawnAndDespawn)