Posted 21 May 2012 - 03:15 PM
next up i want to work on the move function:
explanation:
what its doing is basicly checking what direction it has to move and changes the coordinates to the right ones
now i will make the AI system for a part! later i will implement it in the game loop
the first one does of cource nothing :P/>/>
the second one is gonna move the object in a direction at a random intervalmore AI examples:
this one meets with other NPC's, if you have them :D/>/>
ow and dont forget the most important one:
now the tick function:
now finnaly the game loop:
adding object's and labels to your game:
if you want to add a new object to the game just do this:
BTW:
left = direction 1
right = direction 3
up = direction 2
down = direction 4
have a look at the first part i edited some code to help me out!
outro:
this was my tutorial i hope it was pretty usefull
if i forgot something just post it in the comments
please leave a comment if you liked it or just click the green UP button over there —–>
thx, wilcomega
function move( obj, dir )
if dir == 0 then obj.x = obj.x - 1 end
if dir == 1 then obj.y = obj.y - 1 end
if dir == 3 then obj.x = obj.x + 1 end
if dir == 4 then obj.y = obj.y + 1 end
end
explanation:
Spoiler
what its doing is basicly checking what direction it has to move and changes the coordinates to the right ones
now i will make the AI system for a part! later i will implement it in the game loop
function AInone( obj, ev, p1, p2, p3, p4, p5 )
end
AIwanderTimer = os.startTimer( 5 )
function AIwander( obj, ev, p1, p2, p3, p4, p5 )
if ev == "timer" and p1 == AIwanderTimer then
local nDir = math.random( 1, 4 )
obj.dir = nDir
move( obj, nDir )
AIwanderTimer = os.startTimer( math.random( 1, 10 ) )
end
end
explanation:Spoiler
these are the functions for the diffrent AI'sthe first one does of cource nothing :P/>/>
the second one is gonna move the object in a direction at a random interval
Spoiler
this one is going to move the object towards the player:AIenemyTimer = os.startTimer( 1 )
function AIenemy( obj, ev, p1, p2, p3, p4, p5 )
if ev == "timer" and p1 == AIenemyTimer then
if player.y + 1 < obj.y then
obj.dir = 2
move( obj, 2 )
elseif player.y - 1 > obj.y then
obj.dir = 4
move( obj, 4 )
else
if player.x + 1 < obj.x then
obj.dir = 1
move( obj, 1 )
elseif player.x - 1 > obj.x then
obj.dir = 3
move( obj, 3 )
else
player.health = player.health - 1
end
end
end
AIenemyTimer = os.startTimer( 1 )
end
this one meets with other NPC's, if you have them :D/>/>
function AImeet( obj, ev, p1, p2, p3, p4, p5 )
local left = checkPosMove( obj, 1 )
local top = checkPosMove( obj, 2 )
local right = checkPosMove( obj, 3 )
local bottom = checkPosMove( obj, 4 )
if ev == "timer" and p1 == obj.meetTimer then
move( obj, 1 )
move( obj, 1 )
move( obj, 1 )
end
if left ~= false then
if left.name == "NPC" then --modefy this to you NPC name
obj.meetTimer = os.startTimer( math.random( 1, 5 ) )
obj.dir = 1
end
end
if top ~= false then
if left.name == "NPC" then
obj.meetTimer = os.startTimer( math.random( 1, 5 ) )
obj.dir = 2
end
end
if right ~= false then
if left.name == "NPC" then
obj.meetTimer = os.startTimer( math.random( 1, 5 ) )
obj.dir = 3
end
end
if bottom ~= false then
if left.name == "NPC" then
obj.meetTimer = os.startTimer( math.random( 1, 5 ) )
obj.dir = 4
end
end
end
ow and dont forget the most important one:
function AIkeyboard( obj, ev, p1, p2, p3, p4, p5 )
if ev == "key" then
if p1 == 17 then obj.dir = 2; move( obj, 2 ) end
if p1 == 30 then obj.dir = 1; move( obj, 1 ) end
if p1 == 31 then obj.dir = 4; move( obj, 4 ) end
if p1 == 32 then obj.dir = 3; move( obj, 3 ) end
end
end
now the tick function:
function tick( ev, p1, p2, p3, p4, p5 )
ticks = ticks + 1
for i = 0,#object do
for x = 0,#object[i].AI do
object[i].AI[x]( object[i], ev, p1, p2, p3, p4, p5 )
end
end
end
explanation:Spoiler
first it adds a tick next up it loop across the objects and inside the object it loop across ALL the AI's you enterd and executed them 1 by 1now finnaly the game loop:
frameSleep = os.startTimer( fps )
while true do
local ev, p1, p2, p3, p4, p5 = os.pullEvent()
tick( ev, p1, p2, p3, p4, p5 )
if ev == "timer" and p1 == frameSleep then
frameSleep = os.startTimer( fps )
end
end
adding object's and labels to your game:
if you want to add a new object to the game just do this:
player = Object:new({name = "Player", AI = {AIkeyboard}, sprite = {"<","^",">","V"}, x = 2, y = 2})
BTW:
left = direction 1
right = direction 3
up = direction 2
down = direction 4
have a look at the first part i edited some code to help me out!
outro:
this was my tutorial i hope it was pretty usefull
if i forgot something just post it in the comments
please leave a comment if you liked it or just click the green UP button over there —–>
thx, wilcomega