Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Initiate our tables!
- local objs={}
- objs.insts={}
- function addObject(x, y, color) --Add our object
- --if user will not enter the requerments, set them as default 0/colors.black
- if not color then color=colors.black end
- local nobj={} --Create a new table
- 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--Return our table
- end
- local function objCopy(t)--Copy the object
- local t2 = {}--Create the new table, this is where I store the new instance
- for k,v in pairs(t) do t2[k] = v end--Copy all of its keys to the new table
- return t2--Return it
- end
- function instanceCreate(obj, x, y, color) --Create a new instance
- 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)--Copy the new instance
- --Set the values
- cO.x=x
- cO.y=y
- cO.created=true
- cO.SELF=cO
- cO.color=color
- cO.id=temp_n
- table.insert(objs.insts, temp_n, cO)--Insert the new instance into the objs.insts table with its unique id
- cO:create()--Call the event
- return cO--Return the id
- end
- function instanceDestroy(t)--Instance destroy
- if(t.created==true) then--If im created
- local temp_id=t.id--get the instances id
- objs.insts[temp_id].created=false--Say im not created
- for k in pairs(t) do--Reset the table
- t[k]=nil
- end
- table.remove(objs.insts, temp_id)--Remove 't' from the table
- end
- end
- function updateInsts()--Update instances
- for i=1, #objs.insts do--Loop trough all instances in the table
- if(objs.insts[i]) then--If it exists
- if(objs.insts[i].created==true) then--If im created
- objs.insts[i]:update()--THen update me
- end
- end
- end
- end
- function drawInsts()--Same with draw event as in the update
- for i=1, #objs.insts do
- if(objs.insts[i]) then
- if(objs.insts[i].created==true) then
- objs.insts[i]:draw()
- end
- end
- end
- end
- function moveToDirection(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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement