The following is the function I'm using to determine slope, where x/y1 are the character's coords and x/y2 are the mouse's coords:
Code: Select all
function slopeFinder(x1, y1, x2, y2)
local xDif, yDif = (x1 - x2), (y1 - y2) -- ensure that x1/y1 are the player!
local slope = yDif/xDif
return slope, yDif, xDif
end
Code: Select all
layer.update = function(self, dt)
local speed = 50
mouseDist = math.floor(lume.distance(self.player.x, self.player.y, mX, mY)) -- find distance from character and mouse position
timeM = mouseDist/speed -- finds the time in seconds it'll take at the current speed to reach the required location.
slope, xDif, yDif = slopeFinder(self.player.x, self.player.y, mX, mY) -- acquire the variables I need from the first function.
mouseDown = love.mouse.isDown(1)
if mouseDown then
-- mouse stuff here, will post in another code snippet
end
end
Code: Select all
if lume.sign(slope) == 1 then -- positive
if xDif or yDif ~= 0 or inf then
self.player.x = self.player.x - (xMod) * dt
self.player.y = self.player.y - (yMod) * dt
end
elseif lume.sign(slope) == -1 then -- negative
if xDif or yDif ~= 0 or inf then
self.player.x = self.player.x + (xMod) * dt
self.player.y = self.player.y + (yMod) * dt
end
end
I've spent many, many hours toiling over this feature and I felt it was time to turn to the community for a nudge in the right direction. Is my current method almost correct/salvageable, or should I take an entirely different approach to this issue?
(attached will be the .love file and a pastebin paste. Much of the ground-work code comes from a tutorial on STI and Tiled implementation from Karai. I have received some help from Bartbes and EntranceJew on the IRC with some of the STI code. Thank you in advance to anybody who attempts to help out in solving this issue!)
Pastebin: http://pastebin.com/1JrrAjz4 (111 lines)
Gif of the issue: (please ignore strange colours/artifacting, and as a side note, are spoilers a thing on this forum?)