Animation system

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Animation system

Post by xNick1 »

Hi guys :D
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 :D
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

User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Animation system

Post by MadByte »

You could continue by organizing your code in a more flexible way.
Put everything related into functions, add support for spritebatches and different image and frames sizes.
There is a lot you can do with it ;)
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: Animation system

Post by xNick1 »

MadByte wrote:You could continue by organizing your code in a more flexible way.
Put everything related into functions, add support for spritebatches and different image and frames sizes.
There is a lot you can do with it ;)
Great, I'll read about what you just told me.
Thank you :D
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Animation system

Post by MadByte »

xNick1 wrote:
MadByte wrote:...add support for spritebatches and different image and frames sizes...
Great, I'll read about what you just told me.
Thank you :D
I need to correct myself. I meant spritesheets, not spritebatches. Good luck!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 197 guests