logicgate = class:new()
function logicgate:init(x, y, r)
self.x = x
self.y = y
self.cox = x
self.coy = y
self.r = r
--self.ef = ef --evaluation function
self.outtable = {}
self.intable = {}
self.state = "off"
self.initial = true
end
function logicgate:link()
if #self.r > 3 then
for j, w in pairs(outputs) do
for i, v in pairs(objects[w]) do
local haslink = false
for indx = 1, #self.r do
if self.r[indx] == "link" and tonumber(self.r[indx+1]) == v.cox and tonumber(self.r[indx+2]) == v.coy then
haslink = true
end
end
if haslink then
v:addoutput(self) --adds this entity as an output to v.
end
end
end
end
end
function logicgate:addoutput(a)
table.insert(self.outtable, a)
end
function logicgate:update(dt)
if self.initial then
self.initial = false
--[[if self.state1 == "on" and self.state2 == "on" then
self:input("on")
else
self:input("off")
end]]
end
end
function logicgate:draw(entname)
love.graphics.setColor(255, 255, 255)
--print(entname.." is index "..GetModIndex(entname))
local quad = entityquads[GetModIndex(entname)]
love.graphics.drawq(quad.image, quad.quad, math.floor((self.x-1-xscroll)*16*scale), ((self.y-1)*16-8)*scale, 0, scale, scale)
end
function logicgate:out(t)
for i = 1, #self.outtable do
if self.outtable[i].input then
self.outtable[i]:input(t, tostring(self))
end
end
end
function logicgate:input(t, e)
-- search through the input table and make sure it's not a dupe
local oldinput = false
local allon = true
local liston = ""
local listoff = ""
local listbroke = ""
for _,v in pairs(self.intable) do
if v[2] == e then
oldinput = true
if t=="on" or t=="off" then
v[1] = t
else
if v[1] == "on" then
v[1] = "off"
elseif v[1] == "off" then
v[1] = "on"
end
end
print(tostring(_).." changed to "..tostring(t))
end
if v[1] == "off" then
allon = false
listoff = listoff..tostring(_).." "
elseif v[1] == "on" then
liston = liston..tostring(_).." "
else
listbroke = listbroke..tostring(_).." "
end
end
if oldinput==false then
print("adding: "..tostring(#self.intable).." as "..tostring(t))
table.insert(self.intable, {t, e})
end
-- Prevent searching through again
if (t=="off" or allon==false) and self.state=="on" then
print("state change: off")
self.state = "off"
self:out("off")
elseif (allon and t=="on") and self.state=="off" then
print("state change: on")
self.state = "on"
self:out("on")
end
if liston ~= "" then
print("on: "..tostring(liston))
end
if listoff ~= "" then
print("off: "..tostring(listoff))
end
if listbroke ~= "" then
print("broke: "..tostring(listbroke))
end
end