Advertisement
revoblam

being develop is suffer...

Jun 3rd, 2020
1,294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.61 KB | None | 0 0
  1. debug = true
  2. bump = require 'libs.bump.bump'
  3. function love.load(arg)
  4.   world = nil;
  5.  
  6.   ground_0 = {}
  7.   ground_1 = {}
  8.  
  9.   --Player Setup
  10.     player = {
  11.       x = 16, --X aka Vertical Starting position.
  12.       y = 16, --Y aka Horizontal Starting position.
  13.       --Rudimentary Physics system here
  14.       xVelocity = 0, --Velocity in both the Vertical and Horizontal axes.
  15.       yVelocity = 0, -- ^^
  16.       acc = 100, --the players acceleration.
  17.       maxSpeed = 600, --the players top speed
  18.       friction = 20, --"slow our player down - we could toggle this situationally to create icy or slick platforms"
  19.       gravity = 80, --acceleration but for verticality.
  20.      
  21.       --Vars for jumping
  22.       isJumping = false, --States, this one for jumping
  23.       isGrounded = false, --and for the oposite, non-^
  24.       hasReachedMax = false, --Maxed the jump height?
  25.       jumpAcc = 500, --how fast the player goes up
  26.       jumpMaxSpeed = 9.5, --maximum jump speed.
  27.      
  28.       --storing variables.
  29.       img = nil, --This stores my sprite.
  30.     }
  31.  --setting bump up.
  32.  
  33.   world = bump.newWorld(16); --arg is tile size = 16
  34.  
  35.   --Creates player using newimage >>> look at the bottom of the func
  36.  
  37.    player.img = love.graphics.newImage('assets/playerPH.png')
  38.  
  39.   world:add(player, player.x, player.y, player.img:getWidth(), player.img:getHeight())
  40.  
  41.   --Draw a level (remember, collisions only!)
  42.   world:add(ground_0, 120, 360, 640, 16)
  43.   world:add(ground_1, 0, 448, 640, 32)
  44.   end
  45.  
  46. function love.keypressed(key)
  47.   if key == "escape" then
  48.     love.event.push("quit")
  49.   end
  50. end
  51. function love.update(dt)
  52.  
  53. local prevX, prevY = player.x, player.y
  54.   -- Apply *frrrriction*
  55.   player.xVelocity = player.xVelocity * (1 - math.min(dt * player.friction, 1));
  56.   player.yVelocity = player.yVelocity * (1 - math.min(dt * player.friction, 1));
  57.  
  58.    
  59.   --Apply Gravity
  60.   player.yVelocity= player.yVelocity + player.gravity * dt;
  61.  
  62.   if love.keyboard.isDown("left", "a")
  63.    and player.xVelocity > -player.maxSpeed
  64.    then player.xVelocity = player.xVelocity - player.acc * dt
  65.   elseif love.keyboard.isDown("right", "d")
  66.    and player.xVelocity < -player.maxSpeed
  67.   then player.xVelocity = player.xVelocity + player.acc * dt
  68. end
  69.  
  70.  
  71. --The jump code...the J U M P code...
  72.   if love.keyboard.isDown("up","w") then
  73.     if -player.yVelocity < player.jumpMaxSpeed
  74.     and not  player.hasReachedMax then
  75.      player.yVelocity = player.yVelocity- player.jumpAcc * dt
  76.     elseif math.abs(player.yVelocity) > player.jumpMaxSpeed then player.hasReachedMax = true
  77.     end
  78.    player.isGrounded = false; --We off da ground, babe!
  79.   end
  80.  
  81.  
  82. --these store the location the player will arrive at should
  83.   local goalx = player.x + player.xVelocity;
  84.   local goaly = player.y + player.yVelocity;
  85.  
  86.   player.filter = function(item, other)
  87.   local x, y, w, h = world:getRect(other)
  88.   local px, py, pw, ph = world:getRect(item)
  89.   local playerBottom = py + ph
  90.   local otherBottom = y + h
  91.  
  92.   if playerBottom <= y then
  93.     return 'slide'
  94.   end
  95. end
  96.     player.x, player.y, collisions, len = world:move(player, goalX, goalY, player.filter)
  97.  
  98.  
  99.   --check for collisions and sort that out.
  100.   for i, coll in ipairs(collisions) do
  101.   if coll.touch.y > goalY then
  102.     player.hasReachedMax = true
  103.     player.isGrounded = false
  104.   elseif coll.normal.y < 0 then
  105.     player.hasReachedMax = false
  106.     player.isGrounded = true
  107.   end
  108. end
  109. end
  110.  
  111. function love.draw(dt)
  112.   love.graphics.draw(player.img, player.x, player.y);
  113.   love.graphics.rectangle('fill', world:getRect(ground_0))
  114.   love.graphics.rectangle('fill', world:getRect(ground_1))
  115.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement