Advertisement
Guest User

logicgate.lua

a guest
Mar 9th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.09 KB | None | 0 0
  1. logicgate = class:new()
  2.  
  3. function logicgate:init(x, y, r)
  4.     self.x = x
  5.     self.y = y
  6.     self.cox = x
  7.     self.coy = y
  8.     self.r = r
  9.     --self.ef = ef --evaluation function
  10.    
  11.     self.outtable = {}
  12.     self.intable = {}
  13.     self.state = "off"
  14.     self.initial = true
  15. end
  16.  
  17. function logicgate:link()
  18.     if #self.r > 3 then
  19.         for j, w in pairs(outputs) do
  20.             for i, v in pairs(objects[w]) do
  21.                 local haslink = false
  22.                 for indx = 1, #self.r do
  23.                     if self.r[indx] == "link" and tonumber(self.r[indx+1]) == v.cox and tonumber(self.r[indx+2]) == v.coy then
  24.                         haslink = true
  25.                     end
  26.                 end
  27.                 if haslink then
  28.                     v:addoutput(self) --adds this entity as an output to v.
  29.                 end
  30.             end
  31.         end
  32.     end
  33. end
  34.  
  35. function logicgate:addoutput(a)
  36.     table.insert(self.outtable, a)
  37. end
  38.  
  39. function logicgate:update(dt)
  40.     if self.initial then
  41.         self.initial = false
  42.         --[[if self.state1 == "on" and self.state2 == "on" then
  43.             self:input("on")
  44.         else
  45.             self:input("off")
  46.         end]]
  47.     end
  48. end
  49.  
  50. function logicgate:draw(entname)
  51.     love.graphics.setColor(255, 255, 255)
  52.  
  53.     --print(entname.." is index "..GetModIndex(entname))
  54.     local quad = entityquads[GetModIndex(entname)]
  55.    
  56.     love.graphics.drawq(quad.image, quad.quad, math.floor((self.x-1-xscroll)*16*scale), ((self.y-1)*16-8)*scale, 0, scale, scale)
  57. end
  58.  
  59. function logicgate:out(t)
  60.     for i = 1, #self.outtable do
  61.         if self.outtable[i].input then
  62.             self.outtable[i]:input(t, tostring(self))
  63.         end
  64.     end
  65. end
  66.  
  67. function logicgate:input(t, e)
  68.     -- search through the input table and make sure it's not a dupe
  69.     local oldinput = false
  70.     local allon = true
  71.     local liston = ""
  72.     local listoff = ""
  73.     local listbroke = ""
  74.    
  75.     for _,v in pairs(self.intable) do
  76.         if v[2] == e then
  77.             oldinput = true
  78.             if t=="on" or t=="off" then
  79.                 v[1] = t
  80.             else
  81.                 if v[1] == "on" then
  82.                     v[1] = "off"
  83.                 elseif v[1] == "off" then
  84.                     v[1] = "on"
  85.                 end
  86.             end
  87.             print(tostring(_).." changed to "..tostring(t))
  88.         end
  89.         if v[1] == "off" then
  90.             allon = false
  91.             listoff = listoff..tostring(_).." "
  92.         elseif v[1] == "on" then
  93.             liston = liston..tostring(_).." "
  94.         else
  95.             listbroke = listbroke..tostring(_).." "
  96.         end
  97.     end
  98.     if oldinput==false then
  99.         print("adding: "..tostring(#self.intable).." as "..tostring(t))
  100.         table.insert(self.intable, {t, e})
  101.     end
  102.     -- Prevent searching through again
  103.     if (t=="off" or allon==false) and self.state=="on" then
  104.         print("state change: off")
  105.         self.state = "off"
  106.         self:out("off")
  107.     elseif (allon and t=="on") and self.state=="off" then
  108.         print("state change: on")
  109.         self.state = "on"
  110.         self:out("on")
  111.     end
  112.     if liston ~= "" then
  113.         print("on: "..tostring(liston))
  114.     end
  115.     if listoff ~= "" then
  116.         print("off: "..tostring(listoff))
  117.     end
  118.     if listbroke ~= "" then
  119.         print("broke: "..tostring(listbroke))
  120.     end
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement