Code:
Code: Select all
function love.load()
player = {
x = 0,
y = 0,
rotation = 0,
velocity = 0,
speed = 200,
maxVelocity = 1.0,
}
map = {
{0,0,0,0,0,0},
{0,0,0,1,0,1},
{0,1,0,1,0,0},
{0,1,1,1,0,0},
}
end
function aabb(ax, ay, aw, ah, bx, by, bw, bh)
local ax2,ay2,bx2,by2 = ax + aw, ay + ah, bx + bw, by + bh
return ax < bx2 and ax2 > bx and ay < by2 and ay2 > by
end
function love.update(dt)
if love.keyboard.isDown("w") then
player.velocity = player.velocity + 1 * dt
else
player.velocity = (player.velocity / 1.03)
end
if love.keyboard.isDown("r") then
player.x = 0
player.y = 0
end
player.rotation = math.atan2(love.mouse.getY() - player.y, love.mouse.getX() - player.x)
player.x = player.x + ((math.cos(player.rotation) * (player.speed * player.velocity)) * dt)
player.y = player.y + ((math.sin(player.rotation) * (player.speed * player.velocity)) * dt)
for i, v in ipairs(map) do
for j, k in ipairs(v) do
if k == 1 then
if aabb(player.x, player.y, 64, 64, j*64, i*64, 64, 64) then
player.velocity = -player.velocity
end
end
end
end
end
function love.draw()
for i, v in ipairs(map) do
for j, k in ipairs(v) do
if k == 1 then
love.graphics.setColor(0,255,64)
love.graphics.rectangle("fill", j*64, i*64, 64, 64)
end
end
end
love.graphics.setColor(0,255,255)
love.graphics.rectangle("fill", player.x, player.y, 64, 64)
love.graphics.setColor(255,128,0)
love.graphics.line(player.x+32, player.y+32, love.mouse.getX(), love.mouse.getY())
love.graphics.setColor(0,0,0)
end