function to improve entities moving on collisions and fixed accordingly

made math.round()
changed vector() and fixed accordingly
This commit is contained in:
lustlion
2022-03-16 18:50:10 +01:00
parent 4d94cc805d
commit 236e23177d
9 changed files with 189 additions and 113 deletions

View File

@@ -34,7 +34,6 @@ function Fairy:new(x,y)
end
function Fairy:doLogic()
if self:checkVisionLine(main_player,self.vision_range) then
self.target.x = main_player.pos.x + main_player.target_offset.x
@@ -63,31 +62,19 @@ function Fairy:doLogic()
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 angle = getAngleFromVector(vector(distance_x,distance_y))
local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2)
if distance < self.range then
self.vel.x = self.vel.x * 0.9
self.vel.y = self.vel.y * 0.9
local random_x = math.random(-1, 1)
local random_y = math.random(-1, 1)
self.vel.x = self.vel.x * 0.9 + random_x/10
self.vel.y = self.vel.y * 0.9 + random_y/10
else
self.vel.x = math.cos(angle)*self.speed
self.vel.y = math.sin(angle)*self.speed
end
self.particle_timer = self.particle_timer + 1
if self.particle_timer >= self.particle_time then
self.particle_timer = 0
local particle_data = {
animation = animation.particle.simple,
animation_speed = 1,
sprite_tint = hex2rgb("#fed100"),
sprite_alpha_fade = true,
direction = angle-math.rad(180+math.random(60)-30),
speed = 0.8*(distance/50),
speed_increase = -0.01,
time = 0.75
}
Particle:new(self.pos.x,self.pos.y,particle_data)
local random_x = math.random(-6, 6)
local random_y = math.random(-6, 6)
self.vel.x = math.cos(angle)*self.speed + random_x/10
self.vel.y = math.sin(angle)*self.speed + random_y/10
end
end
@@ -95,19 +82,42 @@ 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)
self.particle_timer = self.particle_timer + 1
if self.particle_timer >= self.particle_time then
local vector = vector(self.vel.x,self.vel.y)
local angle = getAngleFromVector(vector)
self.particle_timer = 0
local particle_data = {
animation = animation.particle.simple,
animation_speed = 1,
sprite_tint = hex2rgb("#fed100"),
sprite_alpha_fade = true,
direction = angle-math.rad(180+math.random(60)-30),
speed = 1,
speed_increase = -0.01,
time = 0.75
}
Particle:new(self.pos.x,self.pos.y,particle_data)
end
end
function Fairy:doPhysics()
local random_x = math.random(-4, 4)/10
local random_y = math.random(-4, 4)/10
self.vel.x = self.vel.x + random_x
self.vel.y = self.vel.y + random_y
self:moveWithCollision()
self.vel.x = 0
self.vel.y = 0
-- horizontal collision
self:moveX(
self.vel.x,
function()
self.vel.x = 0
end
)
-- vertical collision
self:moveY(
self.vel.y,
function()
self.vel.y = 0
end
)
-- final position
self:adjustLight()
end