Advertisement
Guest User

New Lua Code!

a guest
Jun 13th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. --Initiate our tables!
  2. local objs={}
  3. objs.insts={}
  4.  
  5. function addObject(x, y, color) --Add our object
  6.     --if user will not enter the requerments, set them as default 0/colors.black
  7.     if not color then color=colors.black end
  8.     local nobj={} --Create a new table
  9.     if(not x) then x=0 end
  10.     if(not y) then y=0 end
  11.     nobj.x=x
  12.     nobj.y=y
  13.     nobj.color=color
  14.     function nobj:create() end
  15.     function nobj:update() end
  16.     function nobj:draw() end
  17.     return nobj--Return our table
  18. end
  19.  
  20. local function objCopy(t)--Copy the object
  21.     local t2 = {}--Create the new table, this is where I store the new instance
  22.     for k,v in pairs(t) do t2[k] = v end--Copy all of its keys to the new table
  23.     return t2--Return it
  24. end
  25.  
  26. function instanceCreate(obj, x, y, color) --Create a new instance
  27.     if(not x) then x=obj.x end
  28.     if(not y) then y=obj.y end
  29.     if(not color) then color = obj.color end
  30.     local temp_n=#objs.insts+1
  31.     local cO=objCopy(obj)--Copy the new instance
  32.     --Set the values
  33.     cO.x=x
  34.     cO.y=y
  35.     cO.created=true
  36.     cO.SELF=cO
  37.     cO.color=color
  38.     cO.id=temp_n
  39.     table.insert(objs.insts, temp_n, cO)--Insert the new instance into the objs.insts table with its unique id
  40.     cO:create()--Call the event
  41.     return cO--Return the id
  42. end
  43.  
  44. function instanceDestroy(t)--Instance destroy
  45.     if(t.created==true) then--If im created
  46.         local temp_id=t.id--get the instances id
  47.         objs.insts[temp_id].created=false--Say im not created
  48.         for k in pairs(t) do--Reset the table
  49.             t[k]=nil
  50.         end
  51.         table.remove(objs.insts, temp_id)--Remove 't' from the table
  52.     end
  53. end
  54.  
  55. function updateInsts()--Update instances
  56.     for i=1, #objs.insts do--Loop trough all instances in the table
  57.         if(objs.insts[i]) then--If it exists
  58.             if(objs.insts[i].created==true) then--If im created
  59.                 objs.insts[i]:update()--THen update me
  60.             end
  61.         end
  62.     end
  63. end
  64.  
  65. function drawInsts()--Same with draw event as in the update
  66.     for i=1, #objs.insts do
  67.         if(objs.insts[i]) then
  68.             if(objs.insts[i].created==true) then
  69.                 objs.insts[i]:draw()
  70.             end
  71.         end
  72.     end
  73. end
  74.  
  75. function moveToDirection(t, speed, dir)
  76.     t.x=t.x+speed*math.cos(dir*(math.pi/180))
  77.     t.y=t.y+speed*math.sin(dir*(math.pi/180))
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement