lua-logic/sim/port.lua
2021-02-03 09:17:33 -06:00

69 lines
1.3 KiB
Lua

PortTypes = {
output = 0,
input = 1
}
PortDirections = {
[0] = {-1, 0, 0},
[1] = {0, 1, 0},
[2] = {1, 0, 0},
[3] = {0, -1, 0},
[4] = {0, 0, 1},
[5] = {0, 0, -1}
}
Port = {
logictype = 1,
}
function Port.new(self, type, direction, position, causeupdate)
local o = {
type = type,
direction = direction,
position = position,
causeupdate = causeupdate,
state = false,
gate = nil,
group = nil,
}
setmetatable(o, self)
self.__index = self
return o
end
function Port.setstate(self, state)
if state ~= self.state then
self.state = state
Simulation.queuegroup(sim, self.group)
end
end
function Port.setinputstate(self, state)
if state ~= self.state then
self.state = state
if self.causeupdate then
Simulation.queuegate(sim, self.gate)
end
end
end
function Port.getconnectionposition(self)
local offset = PortDirections[self.direction]
return {self.position[1]+offset[1], self.position[2]+offset[2], self.position[3]+offset[3]}
end
function Port.isrising(self)
if self.group == nil then
return false
end
return self.group.state and (self.group.updatetick == sim.currenttick)
end
function Port.isfalling(self)
if self.group == nil then
return false
end
return self.group.state == false and (self.group.updatetick == sim.currenttick)
end