Advertisement
BigSHinyToys

atempt at oop kinda

Apr 25th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.10 KB | None | 0 0
  1. --[[
  2.         just messing with oop pribbly a mega fail
  3.         by BigSHinyToys
  4. ]]--
  5. local function class(tPrivate,tFunctions)
  6.     local tClass = tFunctions
  7.    
  8.     local mt = {
  9.         __index = tClass
  10.     }
  11.    
  12.     tClass.new = function()
  13.         local tObj = {}
  14.         for key,value in pairs(tPrivate) do
  15.             tObj[key] = value
  16.         end
  17.         setmetatable(tObj,mt)
  18.         return tObj
  19.     end
  20.    
  21.     return tClass
  22. end
  23.  
  24. local counter = class(
  25.     {
  26.         value = 0,
  27.     },
  28.     {
  29.         get = function(self)
  30.             return self.value
  31.         end,
  32.         set = function(self,input)
  33.             self.value = input and tonumber(input) or error("Unexpected input type : "..type(input))
  34.         end,
  35.         inc = function(self)
  36.             self.value = self.value + 1
  37.         end,
  38.         dec = function(self)
  39.             self.value = self.value - 1
  40.         end,
  41.         reset = function(self)
  42.             self.value = 0
  43.         end
  44.     }
  45. )
  46.  
  47. local count = counter.new()
  48.  
  49. while true do
  50.     term.setCursorPos(1,1)
  51.     print(count:get())
  52.     local event = {os.pullEvent()}
  53.     if event[1] == "key" then
  54.         if event[2] == 200 then -- up key
  55.             count:inc()
  56.         elseif event[2] == 208 then -- down key
  57.             count:dec()
  58.         elseif event[2] == 14 then
  59.             break
  60.         end
  61.     end
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement