Mothback/data/scripts/entities/fairy.lua

76 lines
2.0 KiB
Lua
Raw Normal View History

Fairy = Entity:New(x,y)
function Fairy:New(x,y)
local o = Entity:New(x,y)
o.pos = {x = x, y = y}
o.speed = 0.23
o.range = 20
o.target = {x = x, y = y}
-- animations
o.body = Animation:New(animation.fairy.flying)
o:centerOffset(o.body)
o:getBoundingBox(o.body)
2022-01-20 13:06:40 +00:00
o.lightRange = 0
o.light = CreateLight(o.pos.x,o.pos.y,o.lightRange)
table.insert(LoadedEntities,o)
o.id = #LoadedEntities
setmetatable(o, self)
self.__index = self
return o
end
function Fairy:Smart()
self.light.pos.x = self.pos.x-self.target_offset.x
self.light.pos.y = self.pos.y-self.target_offset.y
self.target.x = main_Player.pos.x - main_Player.target_offset.x
self.target.y = main_Player.pos.y - main_Player.target_offset.y - 10
local distance_x = self.target.x - self.pos.x
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)
end
local particle_data = {
animation = animation.particle.fairy,
direction = angle-math.rad(180),
2022-01-20 13:06:40 +00:00
speed = self.speed*distance/(16*game.scale),
light = 85
}
Particle:New(self.pos.x,self.pos.y,particle_data)
end
function Fairy:HandleAnimation()
self.body:Animate()
--if self:isCollidingWith(main_Player) then self.sprite_tint = {1,0,0} else self.sprite_tint = {1,1,1} end
self:Draw(self.body)
end
function Fairy:DoPhysics()
2022-01-18 19:29:43 +00:00
local random_x = math.random(-4, 4)/100
local random_y = math.random(-4, 4)/100
self.vel.x = self.vel.x + random_x
self.vel.y = self.vel.y + random_y
-- move
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
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
end
end