LeDarkLua

My Lua API - TAKE A LOOK!

Jun 29th, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.13 KB | None | 0 0
  1. --Main code block:
  2. dofile("engineKernel")
  3.  
  4. local PDIR="Applications/InstTest/"
  5. local objs={}
  6. objs.insts={}
  7.  
  8. function addObject(x, y, color)
  9.     if not color then color=colors.black end
  10.     local nobj={}
  11.     if(not x) then x=0 end
  12.     if(not y) then y=0 end
  13.     nobj.x=x
  14.     nobj.y=y
  15.     nobj.color=color
  16.     function nobj:create() end
  17.     function nobj:update() end
  18.     function nobj:draw() end
  19.     return nobj
  20. end
  21.  
  22. local function objCopy(t)
  23.     local t2 = {}
  24.     for k,v in pairs(t) do t2[k] = v end
  25.     return t2
  26. end
  27.  
  28. function instanceCreate(obj, x, y, color)
  29.     if(not x) then x=obj.x end
  30.     if(not y) then y=obj.y end
  31.     if(not color) then color = obj.color end
  32.     local temp_n=#objs.insts+1
  33.     local cO=objCopy(obj)
  34.     cO.x=x
  35.     cO.y=y
  36.     cO.created=true
  37.     cO.id=cO
  38.     cO.color=color
  39.     cO.__tbl__id__=temp_n
  40.     table.insert(objs.insts, temp_n, cO)
  41.     cO:create()
  42.     return cO
  43. end
  44.  
  45. function instanceDestroy(t)
  46.         if(t.created==true) then
  47.             local temp_id=t.__tbl__id__
  48.             objs.insts[temp_id].created=false
  49.             for k in pairs(t) do t[k]=nil end
  50.             table.remove(objs.insts, temp_id)
  51.             for k = temp_id, #objs.insts do objs.insts[k].__tbl__id__=k end
  52.     end
  53. end
  54.  
  55. function updateInsts()
  56.     for i=1, #objs.insts do
  57.         if(objs.insts[i]) then
  58.             if(objs.insts[i].created==true) then
  59.                 objs.insts[i]:update()
  60.             end
  61.         end
  62.     end
  63. end
  64.  
  65. function drawInsts()
  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.                 local newID=(#objs.insts+1)-i
  71.                 objs.insts[newID]:draw()
  72.             end
  73.         end
  74.     end
  75. end
  76.  
  77. function moveTo(t, speed, dir)
  78.     t.x=t.x+speed*math.cos(dir*(math.pi/180))
  79.     t.y=t.y+speed*math.sin(dir*(math.pi/180))
  80. end
  81.  
  82. function pStart(pid)
  83.     pThis=pid
  84.     dofile(PDIR.."Instances/player.lua")
  85.     dofile(PDIR.."Instances/pixel.lua")
  86.     id=1
  87.     instanceCreate(objPlayer)
  88. end
  89.  
  90. function pStep()
  91.     if(keyboard.press(keyboard.key.esc)) then
  92.         program.close(pThis)
  93.     end
  94.     if(keyboard.press(keyboard.key.space)) then
  95.         program.restart(pThis)
  96.     end
  97.  
  98.     --After important stuff is done, update the instances!
  99.     updateInsts()
  100. end
  101.  
  102. function pDraw()
  103.     local dtCol=draw.getcolor()
  104.  
  105.     draw.setcolor(colors.white)
  106.     draw.rect(0,0,640,480,false)
  107.  
  108.     --After important stuff is done, redraw the instances!
  109.     drawInsts()
  110.  
  111.     draw.setcolor(dtCol)
  112. end
  113.  
  114. function pStop() end
  115.  
  116. --objPlayer code block
  117. objPlayer=addObject(640/2, 480/2)
  118.  
  119. function objPlayer:create()
  120.     self.r=32
  121.     self.count=0
  122.     self.dir=0
  123. end
  124.  
  125. function objPlayer:update()
  126.     self.count=self.count+1
  127.     if(keyboard.hold(keyboard.key.a)) then
  128.         moveTo(self, 2, 180)
  129.         local inst=instanceCreate(objPixel, self.x, self.y)
  130.         inst.dir=0
  131.     end
  132.     if(keyboard.hold(keyboard.key.d)) then
  133.         moveTo(self, 2, 0)
  134.         local inst=instanceCreate(objPixel, self.x, self.y)
  135.         inst.dir=180
  136.     end
  137.     if(keyboard.hold(keyboard.key.w)) then
  138.         moveTo(self, 2, 270)
  139.         local inst=instanceCreate(objPixel, self.x, self.y)
  140.         inst.dir=90
  141.     end
  142.     if(keyboard.hold(keyboard.key.s)) then
  143.         moveTo(self, 2, 90)
  144.         local inst=instanceCreate(objPixel, self.x, self.y)
  145.         inst.dir=270
  146.     end
  147. end
  148.  
  149. function objPlayer:draw()
  150.     draw.setcolor(self.color)
  151.     draw.circle(self.x, self.y, self.r, false)
  152. end
Advertisement
Add Comment
Please, Sign In to add comment