From 00a3fcdabc88d027562aa4e2f0f7c4caab0262a9 Mon Sep 17 00:00:00 2001 From: lustlion Date: Thu, 20 Jan 2022 13:53:18 +0100 Subject: [PATCH] Cool fairy lights --- data/scripts/animation.lua | 64 +++++++++++++++++------------- data/scripts/entities/fairy.lua | 14 ++++--- data/scripts/entities/particle.lua | 23 ++++++++++- data/scripts/entities/player.lua | 10 ++++- data/scripts/entity.lua | 5 +++ data/scripts/lights.lua | 15 ++++++- 6 files changed, 95 insertions(+), 36 deletions(-) diff --git a/data/scripts/animation.lua b/data/scripts/animation.lua index 766cf04..5789256 100644 --- a/data/scripts/animation.lua +++ b/data/scripts/animation.lua @@ -10,11 +10,17 @@ function Animation:New(anim_data) o.subframe = 0 o.frame = 1 + o.draw = true + setmetatable(o, self) self.__index = self return o end +function Animation:Switch(status) + self.draw = status +end + function Animation:ChangeTo(anim_data) if anim_data.path == self.path then @@ -26,21 +32,23 @@ end -- to manually handle what frame function Animation:DrawFrame(frame, x, y, rotate, sx, sy) - if frame > self.frames then - frame = self.frames - end - local x = x or 0 - local y = y or 0 - local sx = sx or 1 - local sy = sy or 1 - love.graphics.draw( - self.imgs[frame], - math.floor(x - Camera.pos.x), - math.floor(y - Camera.pos.y), - rotate, - sx, - sy - ) + if self.draw then + if frame > self.frames then + frame = self.frames + end + local x = x or 0 + local y = y or 0 + local sx = sx or 1 + local sy = sy or 1 + love.graphics.draw( + self.imgs[frame], + math.floor(x - Camera.pos.x), + math.floor(y - Camera.pos.y), + rotate, + sx, + sy + ) + end end -- to linearly animate @@ -63,16 +71,18 @@ end -- to draw the current frame function Animation:Draw(x, y, rotate, sx, sy) - local x = x or 0 - local y = y or 0 - local sx = sx or 1 - local sy = sy or 1 - love.graphics.draw( - self.imgs[self.frame], - math.floor(x), - math.floor(y), - rotate, - sx, - sy - ) + if self.draw then + local x = x or 0 + local y = y or 0 + local sx = sx or 1 + local sy = sy or 1 + love.graphics.draw( + self.imgs[self.frame], + math.floor(x), + math.floor(y), + rotate, + sx, + sy + ) + end end diff --git a/data/scripts/entities/fairy.lua b/data/scripts/entities/fairy.lua index 0fb652b..180356e 100644 --- a/data/scripts/entities/fairy.lua +++ b/data/scripts/entities/fairy.lua @@ -14,7 +14,7 @@ Fairy = Entity:New(x,y) o:getBoundingBox(o.body) - o.lightRange = 55 + o.lightRange = 0 o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange) table.insert(LoadedEntities,o) @@ -35,18 +35,22 @@ function Fairy:Smart() local distance_y = self.target.y - self.pos.y local angle = GetAngleFromVector(distance_x,distance_y) local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2) + if distance < self.range then self.vel.x = 0 self.vel.y = 0 else - self.vel.x = math.cos(angle)*self.speed*distance/(8*game.scale) - self.vel.y = math.sin(angle)*self.speed*distance/(8*game.scale) + self.vel.x = math.cos(angle)*self.speed*distance/8 + self.vel.y = math.sin(angle)*self.speed*distance/8 end + local random_angle = 45 local particle_data = { animation = animation.particle.fairy, - direction = angle-math.rad(180), - speed = self.speed*distance/(16*game.scale) + direction = angle-math.rad(180+math.random(-random_angle,random_angle)), + speed = self.speed*distance/20, + light = 60, + min_light = 5 } Particle:New(self.pos.x,self.pos.y,particle_data) diff --git a/data/scripts/entities/particle.lua b/data/scripts/entities/particle.lua index fa79f0f..9563f41 100644 --- a/data/scripts/entities/particle.lua +++ b/data/scripts/entities/particle.lua @@ -15,6 +15,12 @@ Particle = Entity:New(x,y) o.sprite_alpha = particle_data.sprite_alpha or o.sprite_alpha o.sprite_flip = particle_data.sprite_flip or o.sprite_flip + if particle_data.light ~= nil then + o.light = CreateLight(o.pos.x,o.pos.y,particle_data.light) + o.max_light = particle_data.light + o.min_light = particle_data.min_light or 0 + end + o.animation_active = particle_data.animation_active or false o.time = 0.5 @@ -40,7 +46,17 @@ Particle = Entity:New(x,y) return o end +function Particle:Smart() + if self.light ~= nil then + self.light.pos.x = self.pos.x-self.target_offset.x + self.light.pos.y = self.pos.y-self.target_offset.y + end +end + function Particle:Kill() + if self.light ~= nil then + KillLight(self.light) + end if self.id ~= nil then for _, e in pairs(LoadedParticles) do if e.id > self.id then @@ -56,15 +72,18 @@ function Particle:HandleAnimation() self.body:Animate() self.timer = self.timer + current_dt self.sprite_alpha = (self.time-self.timer)/self.time + if self.light ~= nil then + self.light.range = math.max(self.min_light,self.sprite_alpha*self.max_light) + end if self.sprite_alpha < 0 then self:Kill() end self:Draw(self.body) end function Particle:DoPhysics() if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, objects.collisions) then - self.pos.x = self.pos.x + self.vel.x + self.pos.x = self.pos.x + self.vel.x * self.sprite_alpha end if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, objects.collisions) then - self.pos.y = self.pos.y + self.vel.y + self.pos.y = self.pos.y + self.vel.y * self.sprite_alpha end end diff --git a/data/scripts/entities/player.lua b/data/scripts/entities/player.lua index 7e3ffa0..e5dca20 100644 --- a/data/scripts/entities/player.lua +++ b/data/scripts/entities/player.lua @@ -165,7 +165,15 @@ function Player:HandleAnimation() if self.move_x ~= 0 then self.sprite_flip.x = math.sign(self.move_x) end -- animation priority - if self.vel.y > 1.25 then + if self.dashTimer > 0 then + self.body:Switch(false) + self.mask:Switch(false) + else + self.body:Switch(true) + self.mask:Switch(true) + end + + if self.vel.y > 1.25 then self.body = self.body:ChangeTo(animation.nancy.fall) self.mask = self.mask:ChangeTo(self.maskType.fall) elseif self.vel.y < 0 then diff --git a/data/scripts/entity.lua b/data/scripts/entity.lua index 54f4192..55a31c7 100644 --- a/data/scripts/entity.lua +++ b/data/scripts/entity.lua @@ -32,6 +32,11 @@ function Entity:Smart() end function Entity:Kill() + -- remove attached lights + if self.light ~= nil then + KillLight(self.light) + end + -- remove self if self.id ~= nil then for _, e in pairs(LoadedEntities) do if e.id > self.id then diff --git a/data/scripts/lights.lua b/data/scripts/lights.lua index 0e81773..703ebef 100644 --- a/data/scripts/lights.lua +++ b/data/scripts/lights.lua @@ -18,11 +18,24 @@ function CreateLight(x,y,range,lum,flicker) o.dim = 0 o.flicker_speed = flicker_speed or 60/12 o.flicker_time = 0 + table.insert(Lights,o) - + o.id = #Lights return o end +function KillLight(light) + if light.id ~= nil then + for _, e in pairs(Lights) do + if e.id > light.id then + e.id = e.id - 1 + end + end + table.remove(Lights,light.id) + end + light = nil +end + function SetDarkness() love.graphics.setColor(0,0,0,1) love.graphics.rectangle("fill",0,0,game.width ,game.height)