75 lines
1.7 KiB
Lua
75 lines
1.7 KiB
Lua
-- global vars
|
|
MoveLeft = true
|
|
-- helper functions
|
|
local function watchPosition(actor, pos)
|
|
local y = pos.y - actor.position.y
|
|
local x = pos.x - actor.position.x
|
|
local rotation = math.atan2(y, x)
|
|
actor.rotation = math.deg(rotation)
|
|
end
|
|
|
|
local function distance(a, b)
|
|
local dx = a.x - b.x
|
|
local dy = a.y - b.y
|
|
return math.sqrt(dx * dx + dy * dy)
|
|
end
|
|
|
|
-- Behaviour Functions called on AI
|
|
|
|
-- These functions are ai behaviour functions called in the game
|
|
-- The AI will be spawned in idle mode, so if you want to put the bot into patrol mode
|
|
-- It's on you to do that in this function.
|
|
function idle(actor, target)
|
|
local a = actor
|
|
local t = target
|
|
if t ~= nil then
|
|
-- print("target is at " .. target.position.x)
|
|
-- watchPosition(actor, target.position)
|
|
ai.state = AIState.Patrol
|
|
a.rotation = 180
|
|
end
|
|
a:shoot()
|
|
--print("actor is idling at " .. actor.position.x)
|
|
end
|
|
|
|
-- It is most appropriate to put any patrolling behaviour into this function of course
|
|
function patrol(actor, target)
|
|
local a = actor
|
|
local t = target
|
|
if raycaster:bresenhamRaycast(a.position, a.rotation, t.position) == true then
|
|
--target hit!
|
|
ai.state = AIState.Alert
|
|
end
|
|
if raycaster:distFromWall() < 3 then
|
|
local upOrDown = math.random(2)
|
|
if moveLeft == true then
|
|
if upOrDown == 1 then
|
|
a.rotation = 180
|
|
else
|
|
a.rotation = 270
|
|
end
|
|
moveLeft = false
|
|
else
|
|
if upOrDown == 1 then
|
|
a.rotation = 0
|
|
else
|
|
a.rotation = 90
|
|
end
|
|
moveLeft = true
|
|
end
|
|
end
|
|
a:moveForward()
|
|
end
|
|
|
|
-- the ai has found the player, this is what function is called
|
|
function alert(actor, target)
|
|
local a = actor
|
|
local t = target
|
|
if target ~= nil then
|
|
watchPosition(a, t.position)
|
|
end
|
|
if distance(a.position, t.position) > 300 then
|
|
a:moveForward()
|
|
end
|
|
a:shoot()
|
|
end
|