Cool fairy lights
This commit is contained in:
parent
151890b585
commit
00a3fcdabc
|
@ -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,6 +32,7 @@ end
|
|||
|
||||
-- to manually handle what frame
|
||||
function Animation:DrawFrame(frame, x, y, rotate, sx, sy)
|
||||
if self.draw then
|
||||
if frame > self.frames then
|
||||
frame = self.frames
|
||||
end
|
||||
|
@ -42,6 +49,7 @@ function Animation:DrawFrame(frame, x, y, rotate, sx, sy)
|
|||
sy
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
-- to linearly animate
|
||||
function Animation:Animate()
|
||||
|
@ -63,6 +71,7 @@ end
|
|||
|
||||
-- to draw the current frame
|
||||
function Animation:Draw(x, y, rotate, sx, sy)
|
||||
if self.draw then
|
||||
local x = x or 0
|
||||
local y = y or 0
|
||||
local sx = sx or 1
|
||||
|
@ -76,3 +85,4 @@ function Animation:Draw(x, y, rotate, sx, sy)
|
|||
sy
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -165,6 +165,14 @@ function Player:HandleAnimation()
|
|||
if self.move_x ~= 0 then self.sprite_flip.x = math.sign(self.move_x) end
|
||||
|
||||
-- animation priority
|
||||
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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue