Advertisement
Guest User

Untitled

a guest
Oct 24th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 KB | None | 0 0
  1. function init(self)
  2.     msg.post(".", "acquire_input_focus")
  3.     print("Initialized")
  4.  
  5.     self.speed = 0.5
  6.     self.jumpStrength = 3
  7.     self.gravity = 0.75
  8.     self.velocity = vmath.vector3()
  9.    
  10.     self.velocityLimit = {}
  11.     self.velocityLimit.horizontal = 10
  12.     self.velocityLimit.falling = 5
  13.     self.velocityLimit.rising = 10
  14.    
  15.     self.acceleration = vmath.vector3()
  16.     self.accelerationLimit = 5
  17.    
  18.     self.direction = "right"
  19.     self.grounded = false
  20. end
  21.  
  22. function update(self, dt)
  23.     local position = go.get_position()
  24.     print("Absolute value of velocity.x is " .. math.abs(self.velocity.x))
  25.     -- Change direction based on velocity.
  26.     if self.acceleration.x > 0 then self.direction = "right" elseif self.acceleration.x < 0 then self.direction = "left" end
  27.  
  28.     -- Only apply gravity if you're not on the ground.
  29.     if not self.grounded then self.acceleration.y = self.acceleration.y - self.gravity end
  30.  
  31.     -- If acceleration gets too high, set it to the limit based on the direction you're facing.
  32.     if math.abs(self.acceleration) > self.accelerationLimit then
  33.         if self.direction == "right" then self.acceleration = self.accelerationLimit
  34.         elseif self.direction == "left" then self.acceleration = self.accelerationLimit * -1
  35.         end
  36.     end --if math.abs(self.acceleration)
  37.  
  38.     -- Update velocity based on acceleration.
  39.     self.velocity.x = self.velocity.x + self.acceleration.x
  40.     self.velocity.y = self.velocity.y + self.acceleration.y
  41.  
  42.     -- If horizontal velocity is too high, set it to the limit.
  43.     if math.abs(self.velocity.x) > self.velocityLimit.horizontal then
  44.         if self.direction == "right" then self.velocity.x = self.velocityLimit.horizontal
  45.         elseif self.direction == "left" then self.velocity.x = self.velocityLimit.horizontal * -1
  46.         end
  47.     end -- if math.abs(self.velocity.x)
  48.  
  49.     -- If velocity is too high while falling, set it to the limit.
  50.     if self.velocity.y < 0 then
  51.         if math.abs(self.velocity.y) < self.velocityLimit.falling then self.velocity.y = self.velocityLimit.falling end
  52.    
  53.     -- If velocity is too high while rising, set it to the limit. Also use this check to set not grounded while in the air.
  54.     elseif self.velocity.y > 0 then
  55.         self.grounded = false
  56.         if math.abs(self.velocity.y) > self.velocityLimit.rising then self.velocity.y = self.velocityLimit.rising end
  57.     end -- if self.velocity.y
  58.    
  59.     position.x = position.x + self.velocity.x
  60.     position.y = position.y + self.velocity.y
  61.     go.set_position(position)
  62.    
  63. end
  64.  
  65.  
  66. function on_message(self, message_id, message, sender)
  67.     if message_id == hash("collision_response") then
  68.         if message.other_group == hash("Terrain") then
  69.  
  70.             -- If you collide with terrain underneath you and you're not grounded, stop vertical velocity and set grounded to true.
  71.             -- Otherwise, if you collide with terrain above you, just stop vertical velocity.
  72.             if message.other_position.y < go.get_position().y and not self.grounded then
  73.                 self.velocity.y = 0
  74.                 self.grounded = true
  75.             elseif message.other_position.y > go.get_position().y then self.velocity.y = 0
  76.             end --if message.other_position.y
  77.         end --if message.other_group
  78.     end -- if message_id
  79. end
  80.  
  81. function on_input(self, action_id, action)
  82.  
  83.     if action_id == hash("left") then
  84.         self.acceleration.x = self.acceleration.x - self.speed
  85.         print("Left detected!")
  86.     elseif action_id == hash("right") then
  87.         self.acceleration.x = self.acceleration.x + self.speed
  88.         print("Right detected!")
  89.     elseif action_id == hash("up") then
  90.         self.acceleration.y = self.acceleration.y + self.jumpStrength
  91.         print("Up detected!")
  92.     end --if action_id
  93.  
  94.  
  95. end --function on_input
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement