Hey guys, for my implementation, I'll share it to you guys:
All of these are in the enemy's code, not the player's code!
For the distance check:
Code: Select all
function Thug:curDist(x1,y1,x2,y2)
local calc_x = math.abs(x1-x2)
local calc_y = math.abs(y1-y2)
local calc = math.sqrt((calc_x^2) + (calc_y^2))
return math.floor(calc)
end
from this
site...
as usual [x1,y1] is the player's coords and [x2,y2] is the enemy's.
then the y check:
Code: Select all
function Thug:yHit()
if ((p.y+150) >= (self.y)) and ((p.y) <=(self.y+32)) then
return true
else return false
end
end
this checks if the player's is hit vertically by your enemy, the code here gives the enemy a range of 150 px for his vertical LOS.
finally, the code for facing:
Code: Select all
function Thug:getPlayerFacing(pl)
local facing = false
if (self.x_vel < 0) and (pl.x < self.x) then
facing = true
elseif (self.x_vel < 0) and (pl.x > self.x) then
facing = false
elseif (self.x_vel > 0) and (pl.x > self.x) then
facing = true
elseif (self.x_vel > 0) and (pl.x < self.x) then
facing = false
end
self.towards = facing
end
Found this from a GM tutorial. This will check if the player and enemy are facing each other.
Now for implementing it to the code:
Code: Select all
dist = self:curDist(p.x, p.y, self.x, self.y) -- updates the player's distance
self:getPlayerFacing(p) -- my implementation of if the enemy faces the player.
-- check your distances
if self.towards and self:yHit() then -- using our vertical LOS and a towards check
if dist <= 150 then -- the distance is just close enough...
self.state = 2 -- to start a chase
end
end
That's my implementation so far. If it's helpful to some, use it. But remember to credit me for some of that. (I placed it in QUEUE and it's under the MIT license so you can use it anytime, but credit it. See
this for details.)