1
0

add animated sprites

This commit is contained in:
Rokas Puzonas 2022-08-05 01:26:44 +00:00
parent 513ad6d119
commit a445461075
6 changed files with 55 additions and 11 deletions

Binary file not shown.

View File

@ -20,8 +20,8 @@ function MainState:enter(_, host_socket)
local systems = {
require("systems.physics"),
require("systems.map"),
require("systems.player"),
require("systems.sprite"),
require("systems.player"),
require("systems.screen-scaler")
}

View File

@ -4,7 +4,7 @@ local rgb = require("helpers.rgb")
local DRAW_GRID = false
local GRID_COLOR = rgb(30, 30, 30)
local DRAW_COLLIDERS = false
local DRAW_COLLIDERS = true
local COLLIDER_COLOR = rgb(200, 20, 200)
function Debug:drawColliders()

View File

@ -2,8 +2,7 @@ local Physics = {}
local bump = require("lib.bump")
local Vec = require("lib.brinevector")
-- TODO: Tweak bump world `cellSize` at runtime,
-- when the map switches
-- TODO: Tweak bump world `cellSize` at runtime, when the map switches
function Physics:init()
self.bump = bump.newWorld()

View File

@ -99,6 +99,12 @@ function Player:update(dt)
e.bolt_count = e.bolt_count + 1
end
end
if e.acc.x ~= 0 or e.acc.y ~= 0 then
e.sprite.variant = "walk"
else
e.sprite.variant = "idle"
end
end
end
@ -142,7 +148,7 @@ function Player:spawnPlayer()
bolt_speed = 2000,
bolt_cooldown = 0.2,
bolt_friction = 0.98,
collider = {-6, -6, 6, 6}
collider = {-5, -5, 4, 8}
}
end

View File

@ -22,12 +22,12 @@ local function loadAsepriteSprite(path)
for _, frame in ipairs(ase.header.frames) do
for _, chunk in ipairs(frame.chunks) do
if chunk.type == FRAME_DATA_CHUNK then
print("frame data")
local cel = chunk.data
local buffer = love.data.decompress("data", "zlib", cel.data)
local data = love.image.newImageData(cel.width, cel.height, "rgba8", buffer)
local image = love.graphics.newImage(data)
local canvas = love.graphics.newCanvas(sprite.width, sprite.height)
canvas:setFilter("nearest")
-- you need to draw in a canvas before.
-- frame images can be of different sizes
@ -58,8 +58,17 @@ local function loadAsepriteSprite(path)
end
end
pprint(tags)
pprint(frames)
for _, tag in pairs(tags) do
local variant = {}
sprite.variants[tag.name] = variant
for i=tag.from,tag.to do
table.insert(variant, frames[i])
end
end
if not sprite.variants.default then
sprite.variants.default = {frames[0]}
end
return sprite
end
@ -71,10 +80,40 @@ function Sprite:addToWorld(group, e)
if group ~= "sprite" then return end
end
function Sprite:draw()
function Sprite:update(dt)
for _, e in ipairs(self.pool.groups.sprite.entities) do
love.graphics.setColor(1, 1, 1)
love.graphics.circle("fill", e.pos.x, e.pos.y, 10)
if e.sprite.name and all_sprites[e.sprite.name] then
local spritesheet = all_sprites[e.sprite.name]
local variant = spritesheet.variants[e.sprite.variant or "default"]
e.sprite.timer = (e.sprite.timer or 0) + dt
if variant then
if e.sprite.timer > 0.1 then
e.sprite.timer = e.sprite.timer % 0.1
e.sprite.frame = (e.sprite.frame or 1) % #variant + 1
end
end
end
end
end
function Sprite:draw()
love.graphics.setColor(1, 1, 1)
for _, e in ipairs(self.pool.groups.sprite.entities) do
local spritesheet = all_sprites[e.sprite.name]
if spritesheet then
if e.sprite.variant ~= e.sprite.last_variant then
e.sprite.frame = 1
end
local variant = spritesheet.variants[e.sprite.variant or "default"]
if variant then
local frame = variant[e.sprite.frame]
love.graphics.draw(frame.image, e.pos.x-spritesheet.width/2, e.pos.y-spritesheet.height/2)
end
e.sprite.last_variant = e.sprite.variant
else
love.graphics.circle("fill", e.pos.x, e.pos.y, 10)
end
end
end