player.script 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. go.property("camera_url", msg.url("/camera#camera")) -- URL of the camera component
  2. go.property("hud_url", msg.url("/ui#hud"))
  3. go.property("angle", -45) -- we use this property to animate the rotation of the player around the center of the scene
  4. function init(self)
  5. -- Get the IDs of the player view and UI objects
  6. self.player_view_id = go.get_id("player_view")
  7. self.player_ui_id = go.get_id("player_ui")
  8. -- Animate vertical position of the body
  9. local new_pos_y = go.get(self.player_view_id, "position.y") + 0.2
  10. go.animate(self.player_view_id, "position.y", go.PLAYBACK_LOOP_PINGPONG, new_pos_y, go.EASING_INOUTSINE, 2)
  11. -- Get the base position
  12. self.base_pos = go.get_position()
  13. -- Animate the angle to rotate the player around the center of the scene
  14. go.animate("#", "angle", go.PLAYBACK_LOOP_FORWARD, -3600 + self.angle, go.EASING_LINEAR, 200)
  15. end
  16. function final(self)
  17. end
  18. function update(self, dt)
  19. -- Update the position of the player based on the angle and the base position
  20. local radius = self.base_pos.z
  21. go.set_position(vmath.vector3(radius * math.sin(math.rad(self.angle)), self.base_pos.y, radius * math.cos(math.rad(self.angle))))
  22. -- Update the rotation of the player based on the angle
  23. go.set(".", "euler.y", self.angle + 90)
  24. -- Update the world transform of the player UI object and convert the world position to screen coordinates
  25. go.update_world_transform(self.player_ui_id)
  26. local world_pos = go.get_world_position(self.player_ui_id)
  27. local screen_pos = camera.world_to_screen(world_pos, self.camera_url)
  28. -- Send the screen position to the HUD script
  29. msg.post(self.hud_url, "update_data", { screen_position = screen_pos })
  30. end