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.subframe = 0
|
||||||
o.frame = 1
|
o.frame = 1
|
||||||
|
|
||||||
|
o.draw = true
|
||||||
|
|
||||||
setmetatable(o, self)
|
setmetatable(o, self)
|
||||||
self.__index = self
|
self.__index = self
|
||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Animation:Switch(status)
|
||||||
|
self.draw = status
|
||||||
|
end
|
||||||
|
|
||||||
function Animation:ChangeTo(anim_data)
|
function Animation:ChangeTo(anim_data)
|
||||||
if anim_data.path == self.path
|
if anim_data.path == self.path
|
||||||
then
|
then
|
||||||
|
@ -26,21 +32,23 @@ end
|
||||||
|
|
||||||
-- to manually handle what frame
|
-- to manually handle what frame
|
||||||
function Animation:DrawFrame(frame, x, y, rotate, sx, sy)
|
function Animation:DrawFrame(frame, x, y, rotate, sx, sy)
|
||||||
if frame > self.frames then
|
if self.draw then
|
||||||
frame = self.frames
|
if frame > self.frames then
|
||||||
end
|
frame = self.frames
|
||||||
local x = x or 0
|
end
|
||||||
local y = y or 0
|
local x = x or 0
|
||||||
local sx = sx or 1
|
local y = y or 0
|
||||||
local sy = sy or 1
|
local sx = sx or 1
|
||||||
love.graphics.draw(
|
local sy = sy or 1
|
||||||
self.imgs[frame],
|
love.graphics.draw(
|
||||||
math.floor(x - Camera.pos.x),
|
self.imgs[frame],
|
||||||
math.floor(y - Camera.pos.y),
|
math.floor(x - Camera.pos.x),
|
||||||
rotate,
|
math.floor(y - Camera.pos.y),
|
||||||
sx,
|
rotate,
|
||||||
sy
|
sx,
|
||||||
)
|
sy
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- to linearly animate
|
-- to linearly animate
|
||||||
|
@ -63,16 +71,18 @@ end
|
||||||
|
|
||||||
-- to draw the current frame
|
-- to draw the current frame
|
||||||
function Animation:Draw(x, y, rotate, sx, sy)
|
function Animation:Draw(x, y, rotate, sx, sy)
|
||||||
local x = x or 0
|
if self.draw then
|
||||||
local y = y or 0
|
local x = x or 0
|
||||||
local sx = sx or 1
|
local y = y or 0
|
||||||
local sy = sy or 1
|
local sx = sx or 1
|
||||||
love.graphics.draw(
|
local sy = sy or 1
|
||||||
self.imgs[self.frame],
|
love.graphics.draw(
|
||||||
math.floor(x),
|
self.imgs[self.frame],
|
||||||
math.floor(y),
|
math.floor(x),
|
||||||
rotate,
|
math.floor(y),
|
||||||
sx,
|
rotate,
|
||||||
sy
|
sx,
|
||||||
)
|
sy
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,7 +14,7 @@ Fairy = Entity:New(x,y)
|
||||||
o:getBoundingBox(o.body)
|
o:getBoundingBox(o.body)
|
||||||
|
|
||||||
|
|
||||||
o.lightRange = 55
|
o.lightRange = 0
|
||||||
o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange)
|
o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange)
|
||||||
|
|
||||||
table.insert(LoadedEntities,o)
|
table.insert(LoadedEntities,o)
|
||||||
|
@ -35,18 +35,22 @@ function Fairy:Smart()
|
||||||
local distance_y = self.target.y - self.pos.y
|
local distance_y = self.target.y - self.pos.y
|
||||||
local angle = GetAngleFromVector(distance_x,distance_y)
|
local angle = GetAngleFromVector(distance_x,distance_y)
|
||||||
local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2)
|
local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2)
|
||||||
|
|
||||||
if distance < self.range then
|
if distance < self.range then
|
||||||
self.vel.x = 0
|
self.vel.x = 0
|
||||||
self.vel.y = 0
|
self.vel.y = 0
|
||||||
else
|
else
|
||||||
self.vel.x = math.cos(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*game.scale)
|
self.vel.y = math.sin(angle)*self.speed*distance/8
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local random_angle = 45
|
||||||
local particle_data = {
|
local particle_data = {
|
||||||
animation = animation.particle.fairy,
|
animation = animation.particle.fairy,
|
||||||
direction = angle-math.rad(180),
|
direction = angle-math.rad(180+math.random(-random_angle,random_angle)),
|
||||||
speed = self.speed*distance/(16*game.scale)
|
speed = self.speed*distance/20,
|
||||||
|
light = 60,
|
||||||
|
min_light = 5
|
||||||
}
|
}
|
||||||
|
|
||||||
Particle:New(self.pos.x,self.pos.y,particle_data)
|
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_alpha = particle_data.sprite_alpha or 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
|
||||||
|
|
||||||
|
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.animation_active = particle_data.animation_active or false
|
||||||
|
|
||||||
o.time = 0.5
|
o.time = 0.5
|
||||||
|
@ -40,7 +46,17 @@ Particle = Entity:New(x,y)
|
||||||
return o
|
return o
|
||||||
end
|
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()
|
function Particle:Kill()
|
||||||
|
if self.light ~= nil then
|
||||||
|
KillLight(self.light)
|
||||||
|
end
|
||||||
if self.id ~= nil then
|
if self.id ~= nil then
|
||||||
for _, e in pairs(LoadedParticles) do
|
for _, e in pairs(LoadedParticles) do
|
||||||
if e.id > self.id then
|
if e.id > self.id then
|
||||||
|
@ -56,15 +72,18 @@ function Particle:HandleAnimation()
|
||||||
self.body:Animate()
|
self.body:Animate()
|
||||||
self.timer = self.timer + current_dt
|
self.timer = self.timer + current_dt
|
||||||
self.sprite_alpha = (self.time-self.timer)/self.time
|
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
|
if self.sprite_alpha < 0 then self:Kill() end
|
||||||
self:Draw(self.body)
|
self:Draw(self.body)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Particle:DoPhysics()
|
function Particle:DoPhysics()
|
||||||
if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, objects.collisions) then
|
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
|
end
|
||||||
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, objects.collisions) then
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -165,7 +165,15 @@ function Player:HandleAnimation()
|
||||||
if self.move_x ~= 0 then self.sprite_flip.x = math.sign(self.move_x) end
|
if self.move_x ~= 0 then self.sprite_flip.x = math.sign(self.move_x) end
|
||||||
|
|
||||||
-- animation priority
|
-- 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.body = self.body:ChangeTo(animation.nancy.fall)
|
||||||
self.mask = self.mask:ChangeTo(self.maskType.fall)
|
self.mask = self.mask:ChangeTo(self.maskType.fall)
|
||||||
elseif self.vel.y < 0 then
|
elseif self.vel.y < 0 then
|
||||||
|
|
|
@ -32,6 +32,11 @@ function Entity:Smart()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Entity:Kill()
|
function Entity:Kill()
|
||||||
|
-- remove attached lights
|
||||||
|
if self.light ~= nil then
|
||||||
|
KillLight(self.light)
|
||||||
|
end
|
||||||
|
-- remove self
|
||||||
if self.id ~= nil then
|
if self.id ~= nil then
|
||||||
for _, e in pairs(LoadedEntities) do
|
for _, e in pairs(LoadedEntities) do
|
||||||
if e.id > self.id then
|
if e.id > self.id then
|
||||||
|
|
|
@ -18,11 +18,24 @@ function CreateLight(x,y,range,lum,flicker)
|
||||||
o.dim = 0
|
o.dim = 0
|
||||||
o.flicker_speed = flicker_speed or 60/12
|
o.flicker_speed = flicker_speed or 60/12
|
||||||
o.flicker_time = 0
|
o.flicker_time = 0
|
||||||
table.insert(Lights,o)
|
|
||||||
|
|
||||||
|
table.insert(Lights,o)
|
||||||
|
o.id = #Lights
|
||||||
return o
|
return o
|
||||||
end
|
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()
|
function SetDarkness()
|
||||||
love.graphics.setColor(0,0,0,1)
|
love.graphics.setColor(0,0,0,1)
|
||||||
love.graphics.rectangle("fill",0,0,game.width ,game.height)
|
love.graphics.rectangle("fill",0,0,game.width ,game.height)
|
||||||
|
|
Loading…
Reference in New Issue