Advertisement
Guest User

hero.script

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