Simple artificial intelligence for Corona SDK.
easy connect to the project
light, fast and easy in use
flexible
extensiable
Released version 1.8
Plugin version 1.8
Overview

Creates an object with specific behavior, which can contact (fire, visual contact, collision) with the object having type "player" (and other objects if needed).
Usage

1.Download the latest version of file "SimpleAI.lua" or download latest version of plugin
2.Put it in your project root directory (for example root/classes/SimpleAI.lua or put plugin folder in to the root/plugin)
3.Connect file SimpleAI.lua to your level file
-- Load class library
local newAI = require('classes.SimpleAI').newAI -- because we put file "SimpleAI.lua" to the directory "classes"
-- Load plugin library
local SimpleAI = require "plugin.SimpleAI"
4.Create enemy object
-- Class example
local enemy = newAI({group = yourGroup, img = "img.png", x = 100, y = 50, ai_type = "patrol"})
-- Plugin example
local enemy = SimpleAI.newAI(yourGroup, "enemy.png", 100, 50, "patrol")
5.Enjoy
Syntax

newAI( group, img, x, y [, ai_type [, sprite ]] )
group (required)
GroupObject. Display group in which to insert the AI object.
img (required)
String. The name of the image file to load, relative to
baseDir
(or
system.ResourceDirectory
by default).
x (required)
Number. The x coordinate of the AI object.
y (required)
Number. The y coordinate of the AI object.
ai_type (optional)
String. The name of the AI type, wich determines the specific behavior of AI. Available options: "patrol" (default), "guard", "boss".
sprite (optional)
Array. Array with animation data ({sheet, sequences}) This is only required if you intend to create an object from a sprite object instead of static image. Default value is {} if the parameter is not provided.
Properties

Methods

Examples

Create enemy object, which will contact with player object
Plugin sample code
-- Load plugin library
local SimpleAI = require "plugin.SimpleAI"

local physics = require( "physics" )
physics.start()

local mainGroup = display.newGroup( );

-- forward declarations and other locals
local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth*0.5, display.contentHeight*0.5

-- player instance
local player = display.newImage(mainGroup, "player.png", halfW, display.contentHeight-200);
physics.addBody( player, { density=1.0, friction=0.3, bounce=0.2 } )
player.type = "player"

-- enemy instance
local enemy = SimpleAI.newAI(mainGroup, "enemy.png", halfW-50, display.contentHeight-300)

-- now "enemy" will contact with "player"
Lua class sample code
-- Load class library
local newAI = require('classes.SimpleAI').newAI

local physics = require( "physics" )
physics.start()

-- forward declarations and other locals
local screenW, screenH, halfW, halfH = display.contentWidth, display.contentHeight, display.contentWidth*0.5, display.contentHeight*0.5

-- player instance
local player = display.newImage(mainGroup, "player.png", halfW, display.contentHeight-200);
physics.addBody( player, { density=1.0, friction=0.3, bounce=0.2 } )
player.type = "player"

-- enemy instance
local enemy = newAI({group = mainGroup, img = "enemy.png", x = halfW-50, y = display.contentHeight-500, ai_type = "patrol"})

-- now "enemy" will contact with "player"

Create animated enemy objects
-- Load class library
local newAI = require('classes.SimpleAI').newAI

---------------------
-- Sprite Animation
---------------------
local sheetOptions =
{
    width = 512,
    height = 256,
    numFrames = 8
}

local sheet = graphics.newImageSheet( "sprites-cat-running.png", sheetOptions )

-- sequences table
local sequences = {
    -- first sequence (consecutive frames)
    {
        name = "normalRun",
        start = 1,
        count = 8,
        time = 800,
        loopCount = 0
    },
    -- next sequence (non-consecutive frames)
    {
        name = "fastRun",
        frames = { 1,3,5,7 },
        time = 400,
        loopCount = 0
    },
}

local sprite = {sheet, sequences} // Create array with animation data

-- enemy instance
local enemy = newAI({group = mainGroup, img = "enemy.png", x = 100, y = 100, ai_type = "patrol", sprite = sprite})
-- now enemy will be animated

-- to create another enemy animated instance
local enemy_1 = newAI({group = mainGroup, img = "enemy.png", x = 200, y = 100, ai_type = "patrol", sprite = sprite})

SimpleAI class inheritance
For example, let's create a class "DogAI" inherited from "SimpleAI" class. First need put following code into the DogAI.lua file, and save DogAI.lua file next to the SimpleAI.lua in the classes directory
-----------------------------------------------------------------------------------------
--
-- DogAI class
--
-----------------------------------------------------------------------------------------
-- Load plugin library
local SimpleAI = require "plugin.SimpleAI"

local _M = {}

function _M.newDogAI(params)
	local img = params.img
	local group = params.group	
	local x = params.x
	local y = params.y
	local aiType = params.ai_type or "patrol"
	local sprite = params.sprite or {}
	
	local DogObj = SimpleAI.newAI(group, img, x, y, aiType, sprite)
	
	-- Derived class method addExtraAction. Override method addExtraAction
	function DogObj:addExtraAction()
		print("test Dog class")
	end
	
	return DogObj
end

return _M
At your level file create enemy dog instance
local newDogAI = require('classes.DogAI').newDogAI

-- enemy dog instance
local dogEnemy = newDogAI({group = mainGroup, img = "dogEnemy.png", x = halfW-50, y = display.contentHeight-300, ai_type = "guard"})

-- in output "test Dog class" text should be displayed
-- now we can create as many dog objects as we wish