yupplemayham/Resources/scripts/ai/grunt_behaviour.lua

101 lines
2.6 KiB
Lua

-- global vars
patrolDestination = { x=500.0, y=500.0, z=0.0 }
moveLeft = true
-- helper functions
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
function distance(a, b)
local dx = a.x - b.x
local dy = a.y - b.y
return math.sqrt(dx * dx + dy * dy)
end
function moveTo(actor, pos)
local a = actor
local p = pos
watchPosition(a, p)
if distance(a.position, p) < 50 then
return true
end
a:moveForward()
return false
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 t ~= nil then
-- print("target is at " .. target.position.x)
end
--if moveTo(actor, patrolDestination) == true then
-- patrolDestination = { x=math.random(400.0, 750.0), y=math.random(400.0, 750.0), z=0.0 }
--end
-- performRaycast returns if true if the raycast hits the target position it also sets the getter function
-- distFromWall, at bot creation distFromWall is and infinite float value.
-- This performRaycast function is highly discourage due to slow down
--if raycaster:performRaycast(actor.position, actor.rotation, target.position) == true then
--ai.state = AIState.Alert
--end
if raycaster:bresenhamRaycast(a.position, a.rotation, t.position) == true then
--target hit!
ai.state = AIState.Alert
end
if raycaster:distFromWall() < 3 then
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
-- print("target is at " .. target.position.x)
watchPosition(a, t.position)
end
--print("actor is alert at " .. actor.position.x)
if distance(a.position, t.position) > 300 then
a:moveForward()
end
a:shoot()
end