function love.load() fps = 30 upTime = love.timer.getTime() nextTime = upTime windowProfile = { mode = { width = 256, height = 240, }, scale = { x = 2, y = 2, }, title = "GNU & Bola Brawlers", } love.graphics.setBackgroundColor(0, 232, 216) love.graphics.setDefaultFilter('nearest', 'nearest') love.window.setMode(windowProfile.mode.width * windowProfile.scale.x, windowProfile.mode.height * windowProfile.scale.y) love.window.setTitle(windowProfile.title) button = { left = 'a', right = 'd', up = 'w', down = 's', a = 'j', b = 'k', select = 'g', start = 'h', quit = 'escape', } metaSprites = { bola = { image = love.graphics.newImage('multimedia/ppu_rp2c0x/meta_sprites/bola/default_sheet_color0_alpha.png'), x = 24, y = 32, } } character = { bola = { orientation = 0, acceleration = 0, velocity = 125, gravity = -500, position = { x = windowProfile.mode.width / 2, y = windowProfile.mode.height / 2, }, scale = { x = 1, y = 1, }, origin = { x = metaSprites.bola.x / 2, y = metaSprites.bola.y / 2, }, stand = require 'stand', walk = require 'walk', jump = require 'jump', actionLeft = false, actionRight = false, actionUp = false, actionDown = false, actionA = false, actionB = false, }, } sprite = { bola = require 'stand', } end function love.keypressed(key, scancode) keypressed = function(character, metaSprites, sprite, key, scancode) if scancode == button.quit then love.event.quit() end if scancode == button.a then character.actionA = true character.actionDown = false character.actionUp = false sprite = require 'jump' end if scancode == button.left then character.actionLeft = true end if scancode == button.right then character.actionRight = true end if scancode == button.up and character.jump.isJumping == false then character.actionUp = true end if scancode == button.down and character.jump.isJumping == false then character.actionDown = true end end keypressed(character.bola, metaSprites.bola, sprite.bola, key, scancode) end function love.keyreleased(key, scancode) keyreleased = function(character, key, scancode) if scancode == button.a then character.actionA = false end if scancode == button.left then character.actionLeft = false end if scancode == button.right then character.actionRight = false end if scancode == button.up then character.actionUp = false end if scancode == button.down then character.actionDown = false end if scancode == button.b then character.actionB = false end if scancode == button.a then character.actionA = false if character.jump.velocity ~= 0 then character.jump.limitButtonJump = true end end end keyreleased(character.bola, key, scancode) end function love.update(dt) nextTime = nextTime + (1 / fps) animation = function(sprite, metaSprites, dt) --Increase actual elapsed time with new dt sprite.elapsedTime = sprite.elapsedTime + dt --AnimationStart --In order to know if it is necessary to change the frame if sprite.elapsedTime >= (1 / sprite.fps) then sprite.elapsedTime = sprite.elapsedTime - (1 / sprite.fps) if sprite.currentFrame == # sprite then --If current frame is equal to long sprite list sprite.currentFrame = 1 -- Return to first frame else sprite.currentFrame = sprite.currentFrame + 1 --Next frame end end metaSprites.quad = sprite[sprite.currentFrame] --Update with new fame end animation(sprite.bola, metaSprites.bola, dt) motion = function(character, metaSprites, sprite, dt) if character.jump.higher > 0 and character.actionA == true then if character.jump.limitButtonJump == false then character.jump.higher = character.jump.higher - dt character.jump.velocity = character.jump.velocity + character.jump.height * (dt / character.jump.higherMax) end end if character.actionLeft == true and character.actionRight == false then sprite = require 'walk' character.position.x = character.position.x - (character.velocity * dt) character.scale.x = -1 end if character.actionRight == true and character.actionLeft == false then sprite = require 'walk' character.position.x = character.position.x + (character.velocity * dt) character.scale.x = 1 end if character.actionUp == true and character.actionDown == false then sprite = require 'walk' character.position.y = character.position.y - (character.velocity * dt) character.jump.ground = character.position.y end if character.actionDown == true and character.actionUp == false then sprite = require 'walk' character.position.y = character.position.y + (character.velocity * dt) character.jump.ground = character.position.y end if character.actionUp == true and character.actionDown == true then sprite = require 'stand' elseif character.actionLeft == true and character.actionRight == true then sprite = require 'stand' elseif character.actionLeft == false and character.actionRight == false and character.actionUp == false and character.actionDown == false then sprite = require 'stand' end return sprite end sprite.bola = motion(character.bola, metaSprites.bola, sprite.bola, dt) gravity = function(character, dt) if character.jump.velocity ~= 0 then character.jump.isJumping = true character.position.y = character.position.y + (character.jump.velocity * dt) character.jump.velocity = character.jump.velocity - (character.gravity * dt) end if character.position.y > character.jump.ground then character.jump.velocity = 0 character.position.y = character.jump.ground character.jump.higher = character.jump.higherMax character.jump.limitButtonJump = false character.jump.isJumping = false character.actionA = false if love.keyboard.isScancodeDown(button.up) then character.actionUp = true elseif love.keyboard.isScancodeDown(button.down) then character.actionDown = true end end end gravity(character.bola, dt) limit = function(character, dt) if character.position.x <= character.origin.x then character.position.x = character.origin.x elseif character.position.x >= windowProfile.mode.width - character.origin.x then character.position.x = windowProfile.mode.width - character.origin.x end if character.position.y <= character.origin.y then character.position.y = character.origin.y elseif character.position.y >= windowProfile.mode.height - character.origin.y then character.position.y = windowProfile.mode.height - character.origin.y end end limit(character.bola, dt) end function love.draw() local currentTime = love.timer.getTime() if nextTime <= currentTime then nextTime = currentTime return end love.timer.sleep(nextTime - currentTime) love.graphics.scale(windowProfile.scale.x, windowProfile.scale.y) love.graphics.print('FPS: ' .. love.timer.getFPS(), 0, 0) draw = function(metaSprites, character) love.graphics.draw( metaSprites.image, metaSprites.quad, character.position.x, character.position.y, character.orientation, character.scale.x, character.scale.y, character.origin.x, character.origin.y ) end draw(metaSprites.bola, character.bola) end