Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. -- Player logic
  2.  
  3. -- these are the tweaks for the mechanics, feel free to change them for a different feeling
  4. -- acceleration factor to use when air-borne
  5. local air_acceleration_factor = 0.8
  6. -- max speed right/left
  7. local max_speed = 600
  8. -- gravity pulling the player down in pixel units
  9. local gravity = -1900
  10. -- take-off speed when jumping in pixel units
  11. local jump_takeoff_speed = 1200
  12.  
  13. -- pre-hashing ids improves performance
  14. local msg_contact_point_response = hash("contact_point_response")
  15. local msg_animation_done = hash("animation_done")
  16. local group_obstacle = hash("ground")
  17. local input_left = hash("left")
  18. local input_right = hash("right")
  19. local input_jump = hash("jump")
  20. local anim_walk = hash("walk")
  21. local anim_idle = hash("idle")
  22. local anim_jump = hash("jump")
  23. local anim_fall = hash("fall")
  24.  
  25. --physics.set_constant(/player#collisionobject, "continuous_collision", true)
  26.  
  27. function init(self)
  28. -- this lets us handle input in this script
  29. msg.post(".", "acquire_input_focus")
  30.  
  31. -- activate camera attached to the player collection
  32. -- this will send camera updates to the render script
  33. msg.post("#camera", "acquire_camera_focus")
  34. msg.post("@render:", "use_camera_projection")
  35.  
  36. -- initial player velocity
  37. self.velocity = vmath.vector3(0, 0, 0)
  38. -- the direction the player is facing
  39. self.facing_direction = 0
  40. -- support variable to keep track of collisions and separation
  41. self.correction = vmath.vector3()
  42. -- if the player stands on ground or not
  43. self.ground_contact = false
  44. -- the currently playing animation
  45. self.anim = nil
  46. end
  47.  
  48. local function play_animation(self, anim)
  49. -- only play animations which are not already playing
  50. if self.anim ~= anim then
  51. -- tell the sprite to play the animation
  52. sprite.play_flipbook("#sprite", anim)
  53. -- remember which animation is playing
  54. self.anim = anim
  55. end
  56. end
  57.  
  58. local function update_animations(self)
  59. -- make sure the player character faces the right way
  60. sprite.set_hflip("#sprite", self.facing_direction < 0)
  61. -- make sure the right animation is playing
  62. if self.ground_contact then
  63. if self.velocity.x == 0 then
  64. play_animation(self, anim_idle)
  65. else
  66. play_animation(self, anim_walk)
  67. end
  68. else
  69. if self.velocity.y > 0 then
  70. play_animation(self, anim_jump)
  71. else
  72. play_animation(self, anim_fall)
  73. end
  74. end
  75. end
  76.  
  77. function fixed_update(self, dt)
  78.  
  79. -- apply gravity
  80. self.velocity.y = self.velocity.y + gravity * dt
  81.  
  82. -- move player
  83. local pos = go.get_position()
  84. pos = pos + self.velocity * dt
  85. go.set_position(pos)
  86.  
  87. -- update animations based on state (ground, air, move and idle)
  88. update_animations(self)
  89.  
  90. -- reset volatile state
  91. self.correction = vmath.vector3()
  92. self.ground_contact = false
  93. -- self.wall_contact = false
  94. end
  95.  
  96. -- https://defold.com/manuals/physics/#resolving-kinematic-collisions
  97. local function handle_obstacle_contact(self, normal, distance)
  98. if distance > 0 then
  99. -- First, project the accumulated correction onto
  100. -- the penetration vector
  101. local proj = vmath.project(self.correction, normal * distance)
  102. if proj < 1 then -- default is 1 ME
  103. -- Only care for projections that does not overshoot.
  104. local comp = (distance - distance * proj) * normal
  105. -- Apply compensation
  106. go.set_position(go.get_position() + comp) -- + comp
  107. -- Accumulate correction done
  108. self.correction = self.correction + comp
  109. end
  110. end
  111.  
  112. -- collided with a wall
  113. -- stop horizontal movement
  114. if math.abs(normal.x) > 0.7 then
  115. self.wall_contact = true
  116. self.velocity.x = 0
  117. end
  118. -- collided with the ground
  119. -- stop vertical movement
  120. if math.abs(normal.y) > 0.7 then
  121. self.ground_contact = true
  122. self.velocity.y = 0
  123. end
  124. -- collided with the ceiling
  125. -- stop vertical movement
  126. if normal.y < -0.7 then
  127. self.velocity.y = 0
  128. end
  129. end
  130.  
  131. function on_message(self, message_id, message, sender)
  132. -- check if we received a contact point message
  133. if message_id == msg_contact_point_response then
  134. -- check that the object is something we consider an obstacle
  135. if message.group == group_obstacle then
  136.  
  137. handle_obstacle_contact(self, message.normal, message.distance)
  138. end
  139. end
  140. end
  141.  
  142. local function jump(self)
  143. -- set take-off speed
  144. self.velocity.y = jump_takeoff_speed
  145. -- play animation
  146. play_animation(self, anim_jump)
  147. self.ground_contact = false
  148. end
  149.  
  150. local function walk(self, direction)
  151. -- only change facing direction if direction is other than 0
  152. if direction ~= 0 then
  153. self.facing_direction = direction
  154. end
  155. -- update velocity and use different velocity on ground and in air
  156. if self.ground_contact then
  157. self.velocity.x = max_speed * direction
  158. else
  159. -- move slower in the air
  160. self.velocity.x = max_speed * air_acceleration_factor * direction
  161. end
  162. end
  163.  
  164. function on_input(self, action_id, action)
  165. if action_id == input_left then
  166. walk(self, -action.value)
  167. elseif action_id == input_right then
  168. walk(self, action.value)
  169. elseif action_id == input_jump then
  170. if action.pressed then
  171. jump(self)
  172. end
  173. end
  174. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement