Skip to content

Commit

Permalink
Fixed bug with creating multiple enemies
Browse files Browse the repository at this point in the history
Fixed bug with creating multiple enemies
  • Loading branch information
NickEnbachtov committed Jan 14, 2017
1 parent 8ebe7b7 commit c3fd0d8
Showing 1 changed file with 73 additions and 51 deletions.
124 changes: 73 additions & 51 deletions SimpleAI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function _M.newAI(params)
local lastPlayerNoticedPosition = x
local fireEnabled = false
local stopFireOnInit = true
local visionScanner = nil



if(spriteObj ~= nil) then
Expand All @@ -57,23 +57,36 @@ function _M.newAI(params)
obj.allowShoot = false
obj.shootVelocity = 2000
obj.fireImg = nil
obj.visionLength = 500
obj.visionLength = 300


-- Scanner for limited vision
visionScanner = display.newRect(group, obj.x, obj.y, 1, obj.height);
physics.addBody( visionScanner, "dynamic" )
visionScanner.type = "visionScanner"
visionScanner.isSensor = true
visionScanner.gravityScale = 0
visionScanner.alpha = 0
obj.visionScannerLeft = display.newRect(group, obj.x, obj.y, 20, obj.height);
physics.addBody( obj.visionScannerLeft, "dynamic" )
obj.visionScannerLeft.type = "visionScanner"
obj.visionScannerLeft.typeId = tostring( obj )
obj.visionScannerLeft.isSensor = true
obj.visionScannerLeft.gravityScale = 0
obj.visionScannerLeft.alpha = 0


obj.visionScannerRight = display.newRect(group, obj.x, obj.y, 20, obj.height);
physics.addBody( obj.visionScannerRight, "dynamic" )
obj.visionScannerRight.type = "visionScanner"
obj.visionScannerRight.typeId = tostring( obj )
obj.visionScannerRight.isSensor = true
obj.visionScannerRight.gravityScale = 0
obj.visionScannerRight.alpha = 0



---------------------
-- Methods
---------------------
-- ai detection
function obj:defaultActionOnVisualContactWithPlayer(event)
if(obj.type == "enemy") then
timer.performWithDelay( 10, getPlayerPosition(event.other) )
timer.performWithDelay( 10, obj:getPlayerPosition(event.other) )
end
end

Expand Down Expand Up @@ -160,7 +173,9 @@ function _M.newAI(params)
end

function obj:remove()
Runtime:removeEventListener( "enterFrame", actionAI )
Runtime:removeEventListener("enterFrame", run)
obj.visionScannerLeft:removeSelf( )
obj.visionScannerRight:removeSelf( )
display.remove( obj )
end

Expand Down Expand Up @@ -201,39 +216,39 @@ function _M.newAI(params)
---------------------
-- Functions
---------------------
function MoveAIRigth()
function obj:MoveAIRigth()
obj.x = obj.x + 1
obj.xScale = -1
end

function MoveAILeft()
function obj:MoveAILeft()
obj.x = obj.x - 1
obj.xScale = 1
end

function TurnAIRigth()
function obj:TurnAIRigth()
obj.xScale = -1
end

function TurnAILeft()
function obj:TurnAILeft()
obj.xScale = 1
end

function SwitchDirection()
function obj:SwitchDirection()
if(direction == 2) then
direction = 1
elseif(direction == 3) then
direction = 0
end
end

function getPlayerPosition(player)
function obj:getPlayerPosition(player)
lastPlayerNoticedPosition = player.x
obj.isFixedRotation = false
stalker = true
end

function moveObjToPlayerPosition()
function obj:moveObjToPlayerPosition()
if(stalker) then
transition.moveTo( obj, {x = lastPlayerNoticedPosition, time = 2000} )
obj.isFixedRotation = true
Expand All @@ -246,7 +261,7 @@ function _M.newAI(params)
-- end


function lookAIAhead( direction )
function obj:lookAIAhead( direction )
local scanBeam = display.newCircle(group,obj.x,obj.y,5)
physics.addBody( scanBeam, "dynamic" )
scanBeam.type = "scanBeam"
Expand Down Expand Up @@ -291,10 +306,11 @@ function _M.newAI(params)
obj:customActionOnVisualContactWithObjectsEnd(event)
end
end
if(event.other ~= obj and event.other.type ~= "fireBall") then
if((event.other ~= obj and event.other.type ~= "fireBall" and event.other.type ~= "scanBeam" and event.other.type ~= "enemy" and event.other.type ~= "visionScanner") or (event.other.type == "visionScanner" and event.other.typeId == tostring( obj )) ) then
self:removeSelf( )
self = nil
end


end

Expand All @@ -303,7 +319,7 @@ function _M.newAI(params)

end

function fireAIAhead( direction )
function obj:fireAIAhead( direction )
local fireBall = nil
if(obj.fireImg ~= nil) then
fireBall = display.newImage(group,obj.fireImg,obj.x,obj.y)
Expand Down Expand Up @@ -358,7 +374,7 @@ function _M.newAI(params)
obj:customActionOnAiFireToObjectsEnd(event)
end
end
if(event.other ~= obj and event.other.type ~= "scanBeam" and event.other.type ~="visionScanner") then
if(event.other ~= obj and event.other.type ~= "scanBeam" and event.other.type ~= "visionScanner") then
self:removeSelf( )
self = nil
end
Expand All @@ -370,11 +386,11 @@ function _M.newAI(params)

end

function activateExtraAction()
function obj:activateExtraAction()
extraAction = 0
end

function bossAction()
function obj:bossAction()
if(extraAction == 0 and runActionActivity == 1) then
obj:applyForce( 0, -101, obj.x, obj.y )
extraAction = 1
Expand All @@ -388,15 +404,19 @@ function _M.newAI(params)
---------------------
-- Render
---------------------
function actionAI( event )
function obj:actionAI( event )

lookAIAhead(direction) -- AI scan area ahead
if(direction == 0) then
visionScanner.x = obj.x - obj.visionLength -- left
elseif(direction == 1) then
visionScanner.x = obj.x + obj.visionLength -- right
end
visionScanner.y = obj.y
obj:lookAIAhead(direction) -- AI scan area ahead
-- if(direction == 0) then
-- visionScanner.x = obj.x - obj.visionLength -- left
-- elseif(direction == 1) then
-- visionScanner.x = obj.x + obj.visionLength -- right
-- end
obj.visionScannerLeft.x = obj.x - obj.visionLength -- left
obj.visionScannerRight.x = obj.x + obj.visionLength -- right

obj.visionScannerLeft.y = obj.y
obj.visionScannerRight.y = obj.y



Expand All @@ -410,52 +430,54 @@ function _M.newAI(params)

if(fireEnabled and obj.allowShoot) then
fireEnabled = false
fireAIAhead(direction)
obj:fireAIAhead(direction)
end

if(aiType == "patrol") then
if(obj.x >= (x-obj.limitLeft) and direction == 0) then
MoveAILeft()
obj:MoveAILeft()
elseif(obj.x <= (x+obj.limitRight) and direction == 1) then
MoveAIRigth()
end
obj:MoveAIRigth()
end

if(obj.type == "enemy") then
moveObjToPlayerPosition()
obj:moveObjToPlayerPosition()
if( obj.x == lastPlayerNoticedPosition ) then
obj.isFixedRotation = false
end
end

-- if obj follow the the player
if(obj.isFixedRotation == false) then
if(obj.x <= (x-obj.limitLeft)) then
direction = 1
elseif(obj.x >= (x+obj.limitRight)) then
direction = 0
end

-- if obj follow the the player
if(obj.isFixedRotation == false) then
if(obj.x <= (x-obj.limitLeft)) then
direction = 1
elseif(obj.x >= (x+obj.limitRight)) then
direction = 0
end
end
end

elseif(aiType == "guard") then
if(direction == 0) then
TurnAILeft()
obj:TurnAILeft()
direction = 2
timer.performWithDelay( obj.switchDirectionTime, SwitchDirection )
elseif(direction == 1) then
TurnAIRigth()
obj:TurnAIRigth()
direction = 3
timer.performWithDelay( obj.switchDirectionTime, SwitchDirection )
end

elseif(aiType == "boss") then
bossAction()
obj:bossAction()
end
obj:addExtraAction()
end

function run( ... )
obj:actionAI()
end

Runtime:addEventListener( "enterFrame", actionAI )
-- End of Functions
Runtime:addEventListener( "enterFrame", run )
-- End of Functions

return obj
end
Expand Down

0 comments on commit c3fd0d8

Please sign in to comment.