Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function init(self)
- msg.post(".", "acquire_input_focus")
- print("Initialized")
- self.speed = 0.5
- self.jumpStrength = 3
- self.gravity = 0.75
- self.velocity = vmath.vector3()
- self.velocityLimit = {}
- self.velocityLimit.horizontal = 10
- self.velocityLimit.falling = 5
- self.velocityLimit.rising = 10
- self.acceleration = vmath.vector3()
- self.accelerationLimit = 5
- self.direction = "right"
- self.grounded = false
- end
- function update(self, dt)
- local position = go.get_position()
- print("Absolute value of velocity.x is " .. math.abs(self.velocity.x))
- -- Change direction based on velocity.
- if self.acceleration.x > 0 then self.direction = "right" elseif self.acceleration.x < 0 then self.direction = "left" end
- -- Only apply gravity if you're not on the ground.
- if not self.grounded then self.acceleration.y = self.acceleration.y - self.gravity end
- -- If acceleration gets too high, set it to the limit based on the direction you're facing.
- if math.abs(self.acceleration) > self.accelerationLimit then
- if self.direction == "right" then self.acceleration = self.accelerationLimit
- elseif self.direction == "left" then self.acceleration = self.accelerationLimit * -1
- end
- end --if math.abs(self.acceleration)
- -- Update velocity based on acceleration.
- self.velocity.x = self.velocity.x + self.acceleration.x
- self.velocity.y = self.velocity.y + self.acceleration.y
- -- If horizontal velocity is too high, set it to the limit.
- if math.abs(self.velocity.x) > self.velocityLimit.horizontal then
- if self.direction == "right" then self.velocity.x = self.velocityLimit.horizontal
- elseif self.direction == "left" then self.velocity.x = self.velocityLimit.horizontal * -1
- end
- end -- if math.abs(self.velocity.x)
- -- If velocity is too high while falling, set it to the limit.
- if self.velocity.y < 0 then
- if math.abs(self.velocity.y) < self.velocityLimit.falling then self.velocity.y = self.velocityLimit.falling end
- -- If velocity is too high while rising, set it to the limit. Also use this check to set not grounded while in the air.
- elseif self.velocity.y > 0 then
- self.grounded = false
- if math.abs(self.velocity.y) > self.velocityLimit.rising then self.velocity.y = self.velocityLimit.rising end
- end -- if self.velocity.y
- position.x = position.x + self.velocity.x
- position.y = position.y + self.velocity.y
- go.set_position(position)
- end
- function on_message(self, message_id, message, sender)
- if message_id == hash("collision_response") then
- if message.other_group == hash("Terrain") then
- -- If you collide with terrain underneath you and you're not grounded, stop vertical velocity and set grounded to true.
- -- Otherwise, if you collide with terrain above you, just stop vertical velocity.
- if message.other_position.y < go.get_position().y and not self.grounded then
- self.velocity.y = 0
- self.grounded = true
- elseif message.other_position.y > go.get_position().y then self.velocity.y = 0
- end --if message.other_position.y
- end --if message.other_group
- end -- if message_id
- end
- function on_input(self, action_id, action)
- if action_id == hash("left") then
- self.acceleration.x = self.acceleration.x - self.speed
- print("Left detected!")
- elseif action_id == hash("right") then
- self.acceleration.x = self.acceleration.x + self.speed
- print("Right detected!")
- elseif action_id == hash("up") then
- self.acceleration.y = self.acceleration.y + self.jumpStrength
- print("Up detected!")
- end --if action_id
- end --function on_input
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement