enemy.script 935 B

1234567891011121314151617181920212223242526272829
  1. -- move game object back and forth from the current position to a target position
  2. local function move()
  3. local pos = go.get_position()
  4. local to = vmath.vector3(pos.x, 300, 0)
  5. local distance = pos.y - to.y
  6. local speed = 40
  7. local duration = distance / speed
  8. go.animate(".", "position", go.PLAYBACK_LOOP_PINGPONG, to, go.EASING_INOUTQUAD, duration)
  9. end
  10. function init(self)
  11. move()
  12. end
  13. function on_message(self, message_id, message, sender)
  14. if message_id == hash("contact_point_response") then
  15. if message.other_group == hash("bullet") then
  16. -- delete the bullet
  17. go.delete(message.other_id)
  18. -- get the position of the game object
  19. local pos = go.get_position()
  20. -- set a pushback direction based on the collision normal
  21. local to = pos + message.normal * 30
  22. -- knockback animation, then continue moving
  23. go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, to, go.EASING_OUTQUAD, 0.1, 0, move)
  24. end
  25. end
  26. end