I'm new to LOVE.
Some days ago I wrote a very simple animation system for my character and you helped me with some little things
I now added the walking animations and other stuff.
Do you think it's ok or it could be improved?
Do you notice any syntax/performance flaws?
Code: Select all
local wWidth, wHeight = love.graphics.getDimensions()
local player = {}
player.width = love.graphics.newImage("img/s1.png"):getWidth()
player.height = love.graphics.newImage("img/s1.png"):getHeight()
player.x = 0
player.y = wHeight - player.height
player.direction = 'still'
player.still = {
love.graphics.newImage("img/s1.png"),
love.graphics.newImage("img/s2.png"),
love.graphics.newImage("img/s3.png")
}
player.walkLeft = {
love.graphics.newImage("img/wl1.png"),
love.graphics.newImage("img/wl2.png"),
love.graphics.newImage("img/wl3.png")
}
player.walkRight = {
love.graphics.newImage("img/wr1.png"),
love.graphics.newImage("img/wr2.png"),
love.graphics.newImage("img/wr3.png")
}
player.punch = {
love.graphics.newImage("img/p1.png"),
love.graphics.newImage("img/p2.png"),
love.graphics.newImage("img/p3.png")
}
local timeStill = 0.9
local timePassedStill = 0
local selectedStill = 1
local selectedLeft = 1
local selectedRight = 1
local selectedPunch = 1
local timeLeft = 0.2
local timePassedLeft = 0
local timeRight = 0.2
local timePassedRight = 0
local timePunch = 0.5
local timePassedPunch = 0
function love.load()
end
function love.update(dt)
player.direction = 'still'
timePassedStill = timePassedStill + 1 * dt
timePassedLeft = timePassedLeft + 1 * dt
timePassedRight = timePassedRight + 1 * dt
timePassedPunch = timePassedPunch + 1 * dt
if timePassedStill > timeStill then
timePassedStill = 0
if selectedStill == 3 then
selectedStill = 1
else
selectedStill = selectedStill + 1
end
end
if love.keyboard.isDown('left') then
player.x = player.x - 200 * dt
player.direction = 'left'
if timePassedLeft > timeLeft then
timePassedLeft = 0
if selectedLeft == 3 then
selectedLeft = 1
else
selectedLeft = selectedLeft + 1
end
end
end
if love.keyboard.isDown('right') then
player.x = player.x + 200 * dt
player.direction = 'right'
if timePassedRight > timeRight then
timePassedRight = 0
if selectedRight == 3 then
selectedRight = 1
else
selectedRight = selectedRight + 1
end
end
end
if love.keyboard.isDown("p") then
punch = true
if timePassedPunch > timePunch then
timePassedPunch = 0
if selectedPunch == 3 then
selectedPunch = 1
else
selectedPunch = selectedPunch + 1
end
end
end
playerCollisions()
end
function love.draw()
if player.direction == 'still' then
love.graphics.draw(player.still[selectedStill], player.x, player.y)
end
if player.direction == 'left' then
love.graphics.draw(player.walkLeft[selectedLeft], player.x, player.y)
end
if player.direction == 'right' then
love.graphics.draw(player.walkRight[selectedRight], player.x, player.y)
end
if punch then
love.graphics.draw(player.punch[selectedPunch], player.x, player.y)
punch = false
end
end
function love.keypressed(k)
if k == 'escape' then
love.event.push('quit')
end
end
function playerCollisions()
if player.x < 0 then
player.x = 0
end
if player.x > wWidth - player.width then
player.x = wWidth - player.width
end
end