Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Player object
- local player = {
- x = 0,
- y = 0,
- speed = 100,
- }
- -- Camera object
- local camera = {
- x = 0,
- y = 0,
- stiffness = 2,
- }
- -- Sprite object
- local sprite = {
- x = 200,
- y = 200,
- }
- -- Helper function for lerping
- local function lerp(v0, v1, t)
- return v0*(1-t)+v1*t
- end
- -- This gets called every frame
- -- dt is the time between last frame and this frame. We can use it for framerate independant code
- function love.update(dt)
- -- Basic movement code
- if love.keyboard.isDown("w") then player.y = player.y - player.speed * dt end
- if love.keyboard.isDown("a") then player.x = player.x - player.speed * dt end
- if love.keyboard.isDown("s") then player.y = player.y + player.speed * dt end
- if love.keyboard.isDown("d") then player.x = player.x + player.speed * dt end
- -- Lerp the camera to the player
- camera.x = lerp(camera.x, player.x, camera.stiffness * dt)
- camera.y = lerp(camera.y, player.y, camera.stiffness * dt)
- end
- -- This gets called every frame
- function love.draw()
- -- We can offset our camera to the center of the screen as well
- -- This needs to happen on new variables since we dont want to mess with our REAL camera positions
- -- These new variables is what we will use as our camera position in this frame
- local camx = camera.x - love.graphics.getWidth()/2
- local camy = camera.y - love.graphics.getHeight()/2
- -- Draw the player as a red circle
- -- We offset by the camera position to make it the camera work properly
- love.graphics.setColor(1, 0, 0)
- love.graphics.circle("fill", player.x - camx, player.y - camy, 10) -- Drawmode, x, y, radius
- -- Draw the sprite as a green rectangle
- love.graphics.setColor(0, 1, 0)
- love.graphics.rectangle("fill", sprite.x - camx, sprite.y - camy, 20, 20) -- Drawmode, x, y, w, h
- end
Advertisement
Add Comment
Please, Sign In to add comment