Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Main code block:
- dofile("engineKernel")
- local PDIR="Applications/InstTest/"
- local objs={}
- objs.insts={}
- function addObject(x, y, color)
- if not color then color=colors.black end
- local nobj={}
- if(not x) then x=0 end
- if(not y) then y=0 end
- nobj.x=x
- nobj.y=y
- nobj.color=color
- function nobj:create() end
- function nobj:update() end
- function nobj:draw() end
- return nobj
- end
- local function objCopy(t)
- local t2 = {}
- for k,v in pairs(t) do t2[k] = v end
- return t2
- end
- function instanceCreate(obj, x, y, color)
- if(not x) then x=obj.x end
- if(not y) then y=obj.y end
- if(not color) then color = obj.color end
- local temp_n=#objs.insts+1
- local cO=objCopy(obj)
- cO.x=x
- cO.y=y
- cO.created=true
- cO.id=cO
- cO.color=color
- cO.__tbl__id__=temp_n
- table.insert(objs.insts, temp_n, cO)
- cO:create()
- return cO
- end
- function instanceDestroy(t)
- if(t.created==true) then
- local temp_id=t.__tbl__id__
- objs.insts[temp_id].created=false
- for k in pairs(t) do t[k]=nil end
- table.remove(objs.insts, temp_id)
- for k = temp_id, #objs.insts do objs.insts[k].__tbl__id__=k end
- end
- end
- function updateInsts()
- for i=1, #objs.insts do
- if(objs.insts[i]) then
- if(objs.insts[i].created==true) then
- objs.insts[i]:update()
- end
- end
- end
- end
- function drawInsts()
- for i=1, #objs.insts do
- if(objs.insts[i]) then
- if(objs.insts[i].created==true) then
- objs.insts[i]:draw()
- local newID=(#objs.insts+1)-i
- objs.insts[newID]:draw()
- end
- end
- end
- end
- function moveTo(t, speed, dir)
- t.x=t.x+speed*math.cos(dir*(math.pi/180))
- t.y=t.y+speed*math.sin(dir*(math.pi/180))
- end
- function pStart(pid)
- pThis=pid
- dofile(PDIR.."Instances/player.lua")
- dofile(PDIR.."Instances/pixel.lua")
- id=1
- instanceCreate(objPlayer)
- end
- function pStep()
- if(keyboard.press(keyboard.key.esc)) then
- program.close(pThis)
- end
- if(keyboard.press(keyboard.key.space)) then
- program.restart(pThis)
- end
- --After important stuff is done, update the instances!
- updateInsts()
- end
- function pDraw()
- local dtCol=draw.getcolor()
- draw.setcolor(colors.white)
- draw.rect(0,0,640,480,false)
- --After important stuff is done, redraw the instances!
- drawInsts()
- draw.setcolor(dtCol)
- end
- function pStop() end
- --objPlayer code block
- objPlayer=addObject(640/2, 480/2)
- function objPlayer:create()
- self.r=32
- self.count=0
- self.dir=0
- end
- function objPlayer:update()
- self.count=self.count+1
- if(keyboard.hold(keyboard.key.a)) then
- moveTo(self, 2, 180)
- local inst=instanceCreate(objPixel, self.x, self.y)
- inst.dir=0
- end
- if(keyboard.hold(keyboard.key.d)) then
- moveTo(self, 2, 0)
- local inst=instanceCreate(objPixel, self.x, self.y)
- inst.dir=180
- end
- if(keyboard.hold(keyboard.key.w)) then
- moveTo(self, 2, 270)
- local inst=instanceCreate(objPixel, self.x, self.y)
- inst.dir=90
- end
- if(keyboard.hold(keyboard.key.s)) then
- moveTo(self, 2, 90)
- local inst=instanceCreate(objPixel, self.x, self.y)
- inst.dir=270
- end
- end
- function objPlayer:draw()
- draw.setcolor(self.color)
- draw.circle(self.x, self.y, self.r, false)
- end
Advertisement
Add Comment
Please, Sign In to add comment