Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- debug = true
- bump = require 'libs.bump.bump'
- function love.load(arg)
- world = nil;
- ground_0 = {}
- ground_1 = {}
- --Player Setup
- player = {
- x = 16, --X aka Vertical Starting position.
- y = 16, --Y aka Horizontal Starting position.
- --Rudimentary Physics system here
- xVelocity = 0, --Velocity in both the Vertical and Horizontal axes.
- yVelocity = 0, -- ^^
- acc = 100, --the players acceleration.
- maxSpeed = 600, --the players top speed
- friction = 20, --"slow our player down - we could toggle this situationally to create icy or slick platforms"
- gravity = 80, --acceleration but for verticality.
- --Vars for jumping
- isJumping = false, --States, this one for jumping
- isGrounded = false, --and for the oposite, non-^
- hasReachedMax = false, --Maxed the jump height?
- jumpAcc = 500, --how fast the player goes up
- jumpMaxSpeed = 9.5, --maximum jump speed.
- --storing variables.
- img = nil, --This stores my sprite.
- }
- --setting bump up.
- world = bump.newWorld(16); --arg is tile size = 16
- --Creates player using newimage >>> look at the bottom of the func
- player.img = love.graphics.newImage('assets/playerPH.png')
- world:add(player, player.x, player.y, player.img:getWidth(), player.img:getHeight())
- --Draw a level (remember, collisions only!)
- world:add(ground_0, 120, 360, 640, 16)
- world:add(ground_1, 0, 448, 640, 32)
- end
- function love.keypressed(key)
- if key == "escape" then
- love.event.push("quit")
- end
- end
- function love.update(dt)
- local prevX, prevY = player.x, player.y
- -- Apply *frrrriction*
- player.xVelocity = player.xVelocity * (1 - math.min(dt * player.friction, 1));
- player.yVelocity = player.yVelocity * (1 - math.min(dt * player.friction, 1));
- --Apply Gravity
- player.yVelocity= player.yVelocity + player.gravity * dt;
- if love.keyboard.isDown("left", "a")
- and player.xVelocity > -player.maxSpeed
- then player.xVelocity = player.xVelocity - player.acc * dt
- elseif love.keyboard.isDown("right", "d")
- and player.xVelocity < -player.maxSpeed
- then player.xVelocity = player.xVelocity + player.acc * dt
- end
- --The jump code...the J U M P code...
- if love.keyboard.isDown("up","w") then
- if -player.yVelocity < player.jumpMaxSpeed
- and not player.hasReachedMax then
- player.yVelocity = player.yVelocity- player.jumpAcc * dt
- elseif math.abs(player.yVelocity) > player.jumpMaxSpeed then player.hasReachedMax = true
- end
- player.isGrounded = false; --We off da ground, babe!
- end
- --these store the location the player will arrive at should
- local goalx = player.x + player.xVelocity;
- local goaly = player.y + player.yVelocity;
- player.filter = function(item, other)
- local x, y, w, h = world:getRect(other)
- local px, py, pw, ph = world:getRect(item)
- local playerBottom = py + ph
- local otherBottom = y + h
- if playerBottom <= y then
- return 'slide'
- end
- end
- player.x, player.y, collisions, len = world:move(player, goalX, goalY, player.filter)
- --check for collisions and sort that out.
- for i, coll in ipairs(collisions) do
- if coll.touch.y > goalY then
- player.hasReachedMax = true
- player.isGrounded = false
- elseif coll.normal.y < 0 then
- player.hasReachedMax = false
- player.isGrounded = true
- end
- end
- end
- function love.draw(dt)
- love.graphics.draw(player.img, player.x, player.y);
- love.graphics.rectangle('fill', world:getRect(ground_0))
- love.graphics.rectangle('fill', world:getRect(ground_1))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement