player.script 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. go.property("camera_url", msg.url("/camera#camera"))
  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. --- Converts a world position to screen coordinates.
  5. -- This function transforms a 3D world position to 2D screen coordinates using the camera's
  6. -- view and projection matrices. The resulting coordinates are in screen space where (0,0)
  7. -- is the bottom-left corner of the screen.
  8. --
  9. -- @param world_position vector3 The world position to convert.
  10. -- @param camera_url url|string The camera component URL to use for the transformation.
  11. -- @return number screen_x The X coordinate in screen space.
  12. -- @return number screen_y The Y coordinate in screen space.
  13. -- @return number screen_z Always returns 0 (depth information is not preserved).
  14. local function world_to_screen(world_position, camera_url)
  15. local proj = camera.get_projection(camera_url)
  16. local view = camera.get_view(camera_url)
  17. local view_proj = proj * view
  18. local scr_coord = view_proj * vmath.vector4(world_position.x, world_position.y, world_position.z, 1)
  19. local w, h = window.get_size()
  20. scr_coord.x = (scr_coord.x / scr_coord.w + 1) * 0.5 * w
  21. scr_coord.y = (scr_coord.y / scr_coord.w + 1) * 0.5 * h
  22. return vmath.vector3(scr_coord.x, scr_coord.y, 0)
  23. end
  24. function init(self)
  25. -- Get the IDs of the player view and UI objects
  26. self.player_view_id = go.get_id("player_view")
  27. self.player_ui_id = go.get_id("player_ui")
  28. -- Animate vertical position of the body
  29. local new_pos_y = go.get(self.player_view_id, "position.y") + 0.2
  30. go.animate(self.player_view_id, "position.y", go.PLAYBACK_LOOP_PINGPONG, new_pos_y, go.EASING_INOUTSINE, 2)
  31. -- Get the base position
  32. self.base_pos = go.get_position()
  33. -- Animate the angle to rotate the player around the center of the scene
  34. go.animate("#", "angle", go.PLAYBACK_LOOP_FORWARD, -3600 + self.angle, go.EASING_LINEAR, 200)
  35. end
  36. function final(self)
  37. end
  38. function update(self, dt)
  39. -- Update the position of the player based on the angle and the base position
  40. local radius = self.base_pos.z
  41. go.set_position(vmath.vector3(radius * math.sin(math.rad(self.angle)), self.base_pos.y, radius * math.cos(math.rad(self.angle))))
  42. -- Update the rotation of the player based on the angle
  43. go.set(".", "euler.y", self.angle + 90)
  44. -- Update the world transform of the player UI object and convert the world position to screen coordinates
  45. go.update_world_transform(self.player_ui_id)
  46. local world_pos = go.get_world_position(self.player_ui_id)
  47. local screen_pos = world_to_screen(world_pos, self.camera_url)
  48. -- Send the screen position to the HUD script
  49. msg.post(self.hud_url, "update_data", { screen_position = screen_pos })
  50. end