improvements to particles and animation can now have variable speed

This commit is contained in:
lustlion 2022-03-09 06:04:36 +01:00
parent e8242f6564
commit d3796a0204
6 changed files with 38 additions and 23 deletions

View File

@ -1,6 +1,6 @@
Animation = {} Animation = {}
function Animation:new(anim_data) function Animation:new(anim_data,speed)
local o = {} local o = {}
o.path = anim_data.path o.path = anim_data.path
@ -8,6 +8,7 @@ function Animation:new(anim_data)
o.imgs = anim_data.imgs o.imgs = anim_data.imgs
o.subframe = 0 o.subframe = 0
o.frame = 1 o.frame = 1
o.speed = speed or 1
setmetatable(o, self) setmetatable(o, self)
self.__index = self self.__index = self
@ -46,11 +47,11 @@ end
function Animation:animate() function Animation:animate()
if self.frames[self.frame] ~= 0 then if self.frames[self.frame] ~= 0 then
-- try to animate -- try to animate
self.subframe = self.subframe + current_dt self.subframe = self.subframe + 1
if self.subframe > self.frames[self.frame] then if self.subframe > self.frames[self.frame]*game.framerate then
self.subframe = self.subframe - self.frames[self.frame] self.subframe = self.subframe - self.frames[self.frame]*game.framerate
self.frame = self.frame + 1 self.frame = self.frame + self.speed
end end
-- cycle -- cycle

View File

@ -78,10 +78,13 @@ function Fairy:doLogic()
local particle_data = { local particle_data = {
animation = animation.particle.simple, animation = animation.particle.simple,
animation_speed = 1,
sprite_tint = hex2rgb("#fed100"), sprite_tint = hex2rgb("#fed100"),
sprite_alpha_fade = true,
direction = angle-math.rad(180+math.random(60)-30), direction = angle-math.rad(180+math.random(60)-30),
speed = 0.8*(distance/50), speed = 0.8*(distance/50),
speed_increase = -0.01, speed_increase = -0.01,
time = 0.75
} }
Particle:new(self.pos.x,self.pos.y,particle_data) Particle:new(self.pos.x,self.pos.y,particle_data)
end end

View File

@ -15,12 +15,13 @@ function Particle:new(x,y,particle_data)
o.sprite_scale = particle_data.sprite_scale or o.sprite_scale o.sprite_scale = particle_data.sprite_scale or o.sprite_scale
o.sprite_tint = particle_data.sprite_tint or o.sprite_tint o.sprite_tint = particle_data.sprite_tint or o.sprite_tint
o.sprite_alpha = particle_data.sprite_alpha or o.sprite_alpha o.sprite_alpha = particle_data.sprite_alpha or o.sprite_alpha
o.sprite_alpha_fade = particle_data.sprite_alpha_fade or false
o.sprite_alpha_base = o.sprite_alpha o.sprite_alpha_base = o.sprite_alpha
o.sprite_flip = particle_data.sprite_flip or o.sprite_flip o.sprite_flip = particle_data.sprite_flip or o.sprite_flip
o.animation_active = particle_data.animation_active or false
o.time = 0.5 o.time = particle_data.time or nil
if o.time ~= nil then o.time = o.time * game.framerate end
o.timer = 0 o.timer = 0
o.vel = { o.vel = {
@ -39,14 +40,12 @@ function Particle:new(x,y,particle_data)
-- animations -- animations
if particle_data.animation ~= nil then if particle_data.animation ~= nil then
o.body = Animation:new(particle_data.animation) o.body = Animation:new(particle_data.animation,particle_data.animation_speed)
o:centerOffset(o.body) o:centerOffset(o.body)
o:createBox(o.body) o:createBox(o.body)
if not o.animation_active then
o.body.speed = 0
end
end end
-- particle id handled differently from other entities
table.insert(LoadedObjects.Particles,o) table.insert(LoadedObjects.Particles,o)
o.id = #LoadedObjects.Particles o.id = #LoadedObjects.Particles
@ -71,13 +70,12 @@ function Particle:kill()
end end
function Particle:handleAnimation() function Particle:handleAnimation()
self.timer = self.timer + current_dt self.timer = self.timer + 1
self.sprite_alpha = self.sprite_alpha_base*(self.time-self.timer)/self.time
if self.light ~= nil then if self.sprite_alpha_fade ~= false then
self:adjustLight() self.sprite_alpha = self.sprite_alpha_base*(self.time-self.timer)/self.time
self.light.range = self.light_range * self.sprite_alpha/2
end end
if self.sprite_alpha < 0 then self:kill() end
if self.body ~= nil then if self.body ~= nil then
self.body:animate() self.body:animate()
self:draw(self.body) self:draw(self.body)
@ -93,6 +91,15 @@ function Particle:doPhysics()
end end
-- move -- move
self:moveWithCollision() self:moveWithCollision()
if self.light ~= nil then
self:adjustLight()
self.light.range = self.light_range * self.sprite_alpha/2
end
if self.time ~= nil then
if self.timer >= self.time then self:kill() end
end
end end
function Particle:debug() function Particle:debug()

View File

@ -212,8 +212,10 @@ function Player:doPhysics()
-- dash particle -- dash particle
local particle_data = { local particle_data = {
animation = self.body, animation = self.body,
animation_speed = 0,
sprite_tint = hex2rgb("#fed100"), sprite_tint = hex2rgb("#fed100"),
sprite_alpha = 0.5, sprite_alpha = 0.5,
time = 0.2,
sprite_flip = { sprite_flip = {
x = self.sprite_flip.x, x = self.sprite_flip.x,
y = self.sprite_flip.y y = self.sprite_flip.y
@ -245,8 +247,10 @@ function Player:doPhysics()
local particle_data = { local particle_data = {
animation = self.body, animation = self.body,
animation_speed = 0,
sprite_tint = hex2rgb("#fed100"), sprite_tint = hex2rgb("#fed100"),
sprite_alpha = 0.5, sprite_alpha = 0.5,
time = 0.05,
sprite_flip = { sprite_flip = {
x = self.sprite_flip.x, x = self.sprite_flip.x,
y = self.sprite_flip.y y = self.sprite_flip.y

View File

@ -34,8 +34,12 @@ function stepGame()
if Keybind:checkPressed(Keybind.debug.reposition) then if Keybind:checkPressed(Keybind.debug.reposition) then
if not editor_mode then if not editor_mode then
main_player.pos.x, main_player.pos.y = 16,-10 main_player.pos.x, main_player.pos.y = 75,50
end end
for _, entity in pairs(LoadedObjects.Entities) do
if entity.id ~= main_player.id then entity:kill() end
end
activateSpawns()
end end
if Keybind:checkPressed(Keybind.debug.reload) then if Keybind:checkPressed(Keybind.debug.reload) then

View File

@ -103,10 +103,6 @@ function love.update(dt)
return return
end end
if Keybind:checkPressed(Keybind.debug.respawn) then
activateSpawns()
end
if love.keyboard.isDown("f7") then if love.keyboard.isDown("f7") then
local test_prompt = Prompt:new({ local test_prompt = Prompt:new({
name = "test prompt", name = "test prompt",