spawner.script 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. -- Define different properties for different enemies:
  2. local ENEMY_TYPES = {
  3. random = {
  4. sprite = hash("ufoGreen"),
  5. health_points = 1,
  6. speed = vmath.vector3(40, -100, 0),
  7. is_random = true
  8. },
  9. diagonal = {
  10. sprite = hash("enemyRed2"),
  11. health_points = 2,
  12. speed = vmath.vector3(120, -80, 0),
  13. is_random = false
  14. },
  15. straight = {
  16. sprite = hash("enemyBlue4"),
  17. health_points = 3,
  18. speed = vmath.vector3(0, -40, 0),
  19. is_random = false
  20. }
  21. }
  22. function init(self)
  23. -- Acquire input focus here, so we can handle inputs:
  24. msg.post(".", "acquire_input_focus")
  25. end
  26. -- Helper function to spawn given enemy by its type:
  27. local function spawn_enemy(enemy_type)
  28. -- Select properties of the enemy by type:
  29. local properties = ENEMY_TYPES[enemy_type]
  30. -- Set initial position of the spawned ship.
  31. local position = go.get_position()
  32. -- This will make the position one out of (-180, -90, 0, 90, 180):
  33. position.x = position.x + math.random(-2,2) * 90
  34. -- Create enemy with passed properties
  35. factory.create("#enemyfactory", position, nil, properties)
  36. end
  37. function on_input(self, action_id, action)
  38. -- React to different key presses with spawning different enemies:
  39. if action_id == hash("key_1") and action.released then
  40. spawn_enemy("random")
  41. elseif action_id == hash("key_2") and action.released then
  42. spawn_enemy("diagonal")
  43. elseif action_id == hash("key_3") and action.released then
  44. spawn_enemy("straight")
  45. end
  46. end