I'd like to do this so I can have power-ups be found, as well as stuff player can collect. It'll also enable me to draw background stuff to make it more interesting as It goes by.
So far this is my current approach: (Simplified, took out irrelevant UI stuff.) I'm using HUMP camera to lock cam to player, keeping them in middle. Acceleration/movement is basically having background move in opposite direction. Which I think can be applied to this game.
Code: Select all
-- Load other classes I need.
camera = require('camera')
function love.load(arg)
-- We'll change the default background colour to something a bit more cute. (i.e. Not black.)
love.graphics.setBackgroundColor(25, 25, 25)
-- Making the player entity. (Space ship.)
player = {
x = 344, -- X location.
y = 300, -- Y location.
v = 0, -- Player velocity.
img = nil, -- Will load later.
w = nil, -- Width of image.
h = nil -- Height of image.
}
player.img = love.graphics.newImage('assets/entities/player_ship.png')
player.w, player.h = player.img:getDimensions()
-- End making player.
-- Make background quad and image.
bgImg = love.graphics.newImage('assets/bg.png')
-- Background quad coordinates.
quadCoord = {
x = 0,
y = 0
}
bgQuad = love.graphics.newQuad(0, 0, 896 + bgImg:getWidth(), 600 + bgImg:getHeight(), bgImg:getDimensions())
bgImg:setWrap('repeat', 'repeat')
-- Init camera.
cam = camera(player.x, player.y)
end
function love.update(dt)
-- Lets get the location of the mouse. (Angle compared to the location of our player.)
mouse_angle = math.atan2(love.mouse.getY() - player.y, love.mouse.getX() - player.x)
-- Move the ship in the direction we're facing. Using acceleration and stuff.
-- Normal move.
if love.keyboard.isDown('w') then
if player.v < 1000 then
player.v = player.v + 100
if player.v > 1000 then
player.v = 1000
end
end
quadCoord.x = (quadCoord.x - math.cos(mouse_angle) * player.v * dt) % bgImg:getWidth()
quadCoord.y = (quadCoord.y - math.sin(mouse_angle) * player.v * dt) % bgImg:getHeight()
else
if player.v > 0 then
player.v = player.v - 10
end
quadCoord.x = (quadCoord.x - math.cos(mouse_angle) * player.v * dt) % bgImg:getWidth()
quadCoord.y = (quadCoord.y - math.sin(mouse_angle) * player.v * dt) % bgImg:getHeight()
end
cam:lockPosition(player.x, player.y)
end
function love.draw(dt)
-- Draw background.
love.graphics.draw(bgImg, bgQuad, quadCoord.x - bgImg:getWidth(), quadCoord.y - bgImg:getHeight())
-- Camera starts.
cam:attach()
-- Draw player.
love.graphics.draw(player.img, player.x, player.y, mouse_angle, 1, 1, player.w / 2, player.h / 2)
-- Camera ends. Draw any overlayed/UI stuff past this point.
cam:detach()
end