function to improve entities moving on collisions and fixed accordingly
made math.round() changed vector() and fixed accordingly
This commit is contained in:
@@ -5,6 +5,7 @@ function Entity:new(x,y)
|
||||
local o = {}
|
||||
|
||||
o.pos = {x = x, y = y}
|
||||
o.move_remainder = {x = 0, y = 0}
|
||||
o.vel = {x = 0, y = 0}
|
||||
|
||||
o.direction = 0
|
||||
@@ -69,24 +70,53 @@ end
|
||||
function Entity:doLogic()
|
||||
end
|
||||
|
||||
function Entity:move()
|
||||
self.pos.x = self.pos.x + self.vel.x
|
||||
self.pos.y = self.pos.y + self.vel.y
|
||||
function Entity:moveX(amount, func)
|
||||
self.move_remainder.x = self.move_remainder.x + amount
|
||||
local move = math.round(self.move_remainder.x)
|
||||
if move ~= 0 then
|
||||
self.move_remainder.x = self.move_remainder.x - move
|
||||
local sign = math.sign(move)
|
||||
while math.round(move) ~= 0 do
|
||||
if not self:isCollidingAt(
|
||||
self.pos.x + sign,
|
||||
self.pos.y,
|
||||
LoadedObjects.Collisions
|
||||
) then
|
||||
self.pos.x = self.pos.x + sign
|
||||
move = move - sign
|
||||
if tostring(move) == "nan" then error() end
|
||||
else
|
||||
if func then
|
||||
func()
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Entity:moveWithCollision()
|
||||
-- horizontal collision
|
||||
if not self:isCollidingAt(self.pos.x + self.vel.x, self.pos.y, LoadedObjects.Collisions) then
|
||||
self.pos.x = self.pos.x + self.vel.x
|
||||
else
|
||||
self.vel.x = 0
|
||||
end
|
||||
|
||||
-- vertical collision
|
||||
if not self:isCollidingAt(self.pos.x, self.pos.y + self.vel.y, LoadedObjects.Collisions) then
|
||||
self.pos.y = self.pos.y + self.vel.y
|
||||
else
|
||||
self.vel.y = 0
|
||||
function Entity:moveY(amount, func)
|
||||
self.move_remainder.y = self.move_remainder.y + amount
|
||||
local move = math.round(self.move_remainder.y)
|
||||
if move ~= 0 then
|
||||
self.move_remainder.y = self.move_remainder.y - move
|
||||
local sign = math.sign(move)
|
||||
while math.round(move) ~= 0 do
|
||||
if not self:isCollidingAt(
|
||||
self.pos.x,
|
||||
self.pos.y + sign,
|
||||
LoadedObjects.Collisions
|
||||
) then
|
||||
self.pos.y = self.pos.y + sign
|
||||
move = move - sign
|
||||
if tostring(move) == "nan" then error() end
|
||||
else
|
||||
if func then
|
||||
func()
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -120,8 +150,9 @@ function Entity:checkVisionLine(entity,range)
|
||||
|
||||
local distance_x = target_x - self.pos.x
|
||||
local distance_y = target_y - self.pos.y
|
||||
local distance = vector(distance_x,distance_y)
|
||||
|
||||
local angle = getAngleFromVector(distance_x,distance_y)
|
||||
local angle = getAngleFromVector(distance)
|
||||
local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2)
|
||||
|
||||
local is_colliding = true
|
||||
@@ -227,8 +258,9 @@ function Entity:checkVisionLineDebug(entity,range)
|
||||
|
||||
local distance_x = target_x - self.pos.x
|
||||
local distance_y = target_y - self.pos.y
|
||||
local distance = vector(distance_x,distance_y)
|
||||
|
||||
local angle = getAngleFromVector(distance_x,distance_y)
|
||||
local angle = getAngleFromVector(distance)
|
||||
local distance = math.sqrt(distance_x ^ 2 + distance_y ^ 2)
|
||||
|
||||
if distance < range then
|
||||
@@ -277,6 +309,9 @@ function Entity:debug()
|
||||
end
|
||||
end
|
||||
|
||||
function Entity:doPhysics()
|
||||
end
|
||||
|
||||
function Entity:handleAnimation()
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user