Lua for GuildEd Engine
GuildEd is a 2D game engine made by the faculty at The Guildhall, SMU. The faculty was generous enough to allow me the opportunity to program elements to be implemented into the final product.
Ravish Nair (http://www.ravishnair.com) and I collaborated in an effort to code the AI pathnode element in GuildEd. Pathnodes are used to move actors from one point to the other. When actors collide with a pathnode, they look for the next assigned pathnode and move towards it.
Ravish Nair (http://www.ravishnair.com) and I collaborated in an effort to code the AI pathnode element in GuildEd. Pathnodes are used to move actors from one point to the other. When actors collide with a pathnode, they look for the next assigned pathnode and move towards it.
Code in Action
Code Excerpt
--[[ Function Name : EnemyPathnodeUpdate() Arguments : o Description : Checks position of Enemy and looks for the next assigned pathnode. Sets direction of enemy movement towards next pathnode. Repeats. Created by : Corey McKemy and Ravish Nair, 04/26/2012 Last Updated by : Corey McKemy and Ravish Nair, 05/09/2012 : Initial version Notes : Created so that enemy paths can be controlled and avoid falling to their death. --]] priority = -899 ui = { isFirst = { type = "bool", label = "Enemy active at level start", default = true }, name = { type = "string", label = "Name"}, nextName = { type = "string", label = "Next Name"}, } --CM and RN: Initializes and stores pathnode name, name of the next pathnode, and position into a global table: g.pathNodeTable function init() path = {name = nil, nextName = nil, positionX = nil, positionY = nil} temp = {} me = ActorBase() --CM and RN: Set collision to be a sensor. me.collision.SetCollision( x, y, iconW/pixelsPerUnit, iconH/pixelsPerUnit, "Box", RigidBody_SENSOR ) rigidBody:setCollisionCategory( 2 ) me.name = name me.nextName = nextName positionX = rigidBody:posX() positionY = rigidBody:posY() path.name = name path.nextName = nextName path.positionX = positionX path.positionY = positionY g.table.insert( g.pathNodeTable, path ) end function update( dt ) end --CM and RN: Updates enemy direction based on the next pathnode. function EnemyPathnodeUpdate( o ) if (name == o.currentNode.name)then for jj = 1, g.table.getn( g.pathNodeTable ) do if g.pathNodeTable[jj].name == nextName then --CM and RN: Updates the current pathnode o.currentNode = g.pathNodeTable[jj] --CM and RN: Sends enemy to the direction of the new current pathnode if g.pathNodeTable[jj].positionX > o.rigidBody:posX() + (( iconW/pixelsPerUnit )/2) then o.speed = g.math.abs(o.speed) else o.speed = -1 * (g.math.abs(o.speed)) end end end end end --RN: Updates enemy direction based on the next pathnode. function PlatformPathnodeUpdate( o ) if (name == o.PlatformCurrentNode.name)then for jj = 1, g.table.getn( g.pathNodeTable ) do if g.pathNodeTable[jj].name == nextName then --CM and RN: Updates the current pathnode o.PlatformCurrentNode = g.pathNodeTable[jj] --CM and RN: Sends enemy to the direction of the new current pathnode if g.pathNodeTable[jj].positionX > o.rigidBody:posX() + (( iconW/pixelsPerUnit )/2) then o.MoveSpeedX = g.math.abs(o.MoveSpeedX) else o.MoveSpeedX = -1 * (g.math.abs(o.MoveSpeedX)) end end end end end --CM and RN: Checks to see object of collision. function beginContact( o ) if ( o:isType("Enemy1") ) then EnemyPathnodeUpdate( o ) end if ( o:isType("MovingPlatform") ) then PlatformPathnodeUpdate( o ) end end