Tjakka5

Untitled

Aug 16th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.83 KB | None | 0 0
  1. -- Player object
  2. local player = {
  3.    x = 0,
  4.    y = 0,
  5.    speed = 100,
  6. }
  7.  
  8. -- Camera object
  9. local camera = {
  10.    x = 0,
  11.    y = 0,
  12.    stiffness = 2,
  13. }
  14.  
  15. -- Sprite object
  16. local sprite = {
  17.    x = 200,
  18.    y = 200,
  19. }
  20.  
  21. -- Helper function for lerping
  22. local function lerp(v0, v1, t)
  23.    return v0*(1-t)+v1*t
  24. end
  25.  
  26. -- This gets called every frame
  27. -- dt is the time between last frame and this frame. We can use it for framerate independant code
  28. function love.update(dt)
  29.    -- Basic movement code
  30.    if love.keyboard.isDown("w") then player.y = player.y - player.speed * dt end
  31.    if love.keyboard.isDown("a") then player.x = player.x - player.speed * dt end
  32.    if love.keyboard.isDown("s") then player.y = player.y + player.speed * dt end
  33.    if love.keyboard.isDown("d") then player.x = player.x + player.speed * dt end
  34.  
  35.    -- Lerp the camera to the player
  36.    camera.x = lerp(camera.x, player.x, camera.stiffness * dt)
  37.    camera.y = lerp(camera.y, player.y, camera.stiffness * dt)
  38. end
  39.  
  40. -- This gets called every frame
  41. function love.draw()
  42.    -- We can offset our camera to the center of the screen as well
  43.    -- This needs to happen on new variables since we dont want to mess with our REAL camera positions
  44.    -- These new variables is what we will use as our camera position in this frame
  45.    local camx = camera.x - love.graphics.getWidth()/2
  46.    local camy = camera.y - love.graphics.getHeight()/2
  47.  
  48.    -- Draw the player as a red circle
  49.    -- We offset by the camera position to make it the camera work properly
  50.    love.graphics.setColor(1, 0, 0)
  51.    love.graphics.circle("fill", player.x - camx, player.y - camy, 10)  -- Drawmode, x, y, radius
  52.  
  53.    -- Draw the sprite as a green rectangle
  54.    love.graphics.setColor(0, 1, 0)
  55.    love.graphics.rectangle("fill", sprite.x - camx, sprite.y - camy, 20, 20) -- Drawmode, x, y, w, h
  56. end
Advertisement
Add Comment
Please, Sign In to add comment