Advertisement
Guest User

Untitled

a guest
Aug 25th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. -- gravity pulling the player down in pixel units/sˆ2
  2. local gravity = -20
  3.  
  4. -- take-off speed when jumping in pixel units/s
  5. local jump_takeoff_speed = 900
  6.  
  7. go.property("lives", 4)
  8.  
  9. function init(self)
  10. -- this lets us handle input in this script
  11. msg.post(".", "acquire_input_focus")
  12. -- save position
  13. self.position = go.get_position()
  14. self.lives = go.get("#", "lives")
  15. msg.post("#", "reset")
  16. end
  17.  
  18. function final(self)
  19. msg.post(".", "release_input_focus")
  20. end
  21.  
  22. local function play_animation(self, anim)
  23. -- only play animations which are not already playing
  24. if self.anim ~= anim then
  25. -- tell the spine model to play the animation
  26. spine.play("#spinemodel", anim, go.PLAYBACK_LOOP_FORWARD, 0.15)
  27. -- remember which animation is playing
  28. self.anim = anim
  29. end
  30. end
  31.  
  32. local function update_animation(self)
  33. -- make sure the right animation is playing
  34. if self.ground_contact then
  35. play_animation(self, hash("run_right"))
  36. else
  37. if self.velocity.y > 0 then
  38. play_animation(self, hash("jump_right"))
  39. else
  40. play_animation(self, hash("fall_right"))
  41. end
  42. end
  43. end
  44.  
  45. function update(self, dt)
  46. local gravity = vmath.vector3(0, gravity, 0)
  47.  
  48. if not self.ground_contact then
  49. self.velocity = self.velocity + gravity
  50. end
  51.  
  52. -- apply it to the player character
  53. go.set_position(go.get_position() + self.velocity * dt)
  54.  
  55. update_animation(self)
  56.  
  57. -- reset volatile state
  58. self.correction = vmath.vector3()
  59. self.ground_contact = false
  60.  
  61. if self.teleport == true then
  62. self.position = vmath.vector3(100, self.position.y, self.position.z)
  63. go.set_position(self.position)
  64. end
  65. self.teleport = false
  66. end
  67.  
  68. local function handle_geometry_contact(self, normal, distance)
  69. -- project the correction vector onto the contact normal
  70. -- (the correction vector is the 0-vector for the first contact point)
  71. local proj = vmath.dot(self.correction, normal)
  72. -- calculate the compensation we need to make for this contact point
  73. local comp = (distance - proj) * normal
  74. -- add it to the correction vector
  75. self.correction = self.correction + comp
  76. -- apply the compensation to the player character
  77. go.set_position(go.get_position() + comp)
  78. -- check if the normal points enough up to consider the player standing on the ground
  79. -- (0.7 is roughly equal to 45 degrees deviation from pure vertical direction)
  80. if normal.y > 0.7 then
  81. self.ground_contact = true
  82. end
  83. -- project the velocity onto the normal
  84. proj = vmath.dot(self.velocity, normal)
  85. -- if the projection is negative, it means that some of the velocity points towards the contact point
  86. if proj < 0 then
  87. -- remove that component in that case
  88. self.velocity = self.velocity - proj * normal
  89. end
  90. end
  91.  
  92. function on_message(self, message_id, message, sender)
  93. if message_id == hash("reset") then
  94. self.velocity = vmath.vector3(0, 0, 0)
  95. self.correction = vmath.vector3()
  96. self.ground_contact = false
  97. self.anim = nil
  98. go.set(".", "euler.z", 0)
  99. go.set_position(self.position)
  100. msg.post("#collisionobject", "enable")
  101.  
  102. elseif message_id == hash("contact_point_response") then
  103. -- check if we received a contact point message
  104. if message.group == hash("danger") then
  105. -- Die and restart
  106. particlefx.play("#deathfx")
  107. play_animation(self, hash("die_right"))
  108. msg.post("#collisionobject", "disable")
  109. msg.post("hud", "set_lives", { lives = self.lives })
  110. go.animate(".", "euler.z", go.PLAYBACK_ONCE_FORWARD, 160, go.EASING_LINEAR, 0.7)
  111. go.animate(".", "position.y", go.PLAYBACK_ONCE_FORWARD, go.get_position().y - 200, go.EASING_INSINE, 0.5, 0.2,
  112. function()
  113. if self.lives > 1 then
  114. msg.post("controller#script", "reset")
  115. end
  116. self.lives = self.lives - 1
  117. if self.lives == 0 then
  118. msg.post("hud", "show_game_over")
  119. msg.post("#", "disable")
  120. else
  121. msg.post("#", "reset")
  122. end
  123. end)
  124.  
  125. elseif message.group == hash("geometry") then
  126. handle_geometry_contact(self, message.normal, message.distance)
  127. end
  128. end
  129. end
  130.  
  131. local function jump(self)
  132. -- only allow jump from ground
  133. if self.ground_contact then
  134. -- set take-off speed
  135. self.velocity.y = jump_takeoff_speed
  136. end
  137. end
  138.  
  139. local function abort_jump(self)
  140. -- cut the jump short if we are still going up
  141. if self.velocity.y > 0 then
  142. -- scale down the upwards speed
  143. self.velocity.y = self.velocity.y * 0.5
  144. end
  145. end
  146.  
  147. function on_input(self, action_id, action)
  148. if action_id == hash("jump")then
  149. jump(self)
  150. elseif action_id == hash("teleport") then
  151. print("teleported")
  152. self.teleport = true
  153. end
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement