This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
wilcomega's profile picture

How to make a Game with OOP PART 2/2

Started by wilcomega, 21 May 2012 - 01:15 PM
wilcomega #1
Posted 21 May 2012 - 03:15 PM
next up i want to work on the move function:

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:
Spoilerthese are the functions for the diffrent AI's
the first one does of cource nothing :P/>/>
the second one is gonna move the object in a direction at a random interval
more AI examples:
Spoilerthis 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:
Spoilerfirst 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 1

now 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
Noodle #2
Posted 26 May 2012 - 12:24 AM
FIRST!
jk
But it seems easier to do this with a bunch of events and stuff..
wilcomega #3
Posted 26 May 2012 - 12:00 PM
FIRST!
jk
But it seems easier to do this with a bunch of events and stuff..

i argree with you,
but when i make a game i dont feel like making 5 functions for every object
so this is what comes in handy
EatenAlive3 #4
Posted 27 May 2012 - 05:23 PM
The post seems a little unclear, maybe you should include a sample file. Also, you should combine this with the first thread, rather than make it separate.

And these are called 'prototypes,' not classes. Lua doesn't support classes.
kazagistar #5
Posted 11 June 2012 - 05:58 PM
Sure lua supports classes. If by support you mean supports the syntactic sugar required to create a class system yourself, using some advanced metatable voodoo. Check it out: http://loop.luaforge.net/