1lightbridge = class:new()
2
3function lightbridge:init(x, y, dir, r)
4	self.cox = x
5	self.coy = y
6	self.dir = dir
7	self.r = r
8
9	self.childtable = {}
10
11	self.enabled = true
12	self:updaterange()
13end
14
15function lightbridge:link()
16	self.outtable = {}
17	if #self.r > 2 then
18		for j, w in pairs(outputs) do
19			for i, v in pairs(objects[w]) do
20				if tonumber(self.r[4]) == v.cox and tonumber(self.r[5]) == v.coy then
21					v:addoutput(self)
22					self.enabled = false
23				end
24			end
25		end
26	end
27end
28
29function lightbridge:input(t)
30	if t == "on" then
31		self.enabled = true
32		self:updaterange()
33	elseif t == "off" then
34		self.enabled = false
35		self:updaterange()
36	else
37		self.enabled = not self.enabled
38		self:updaterange()
39	end
40end
41
42function lightbridge:update(dt)
43
44end
45
46function lightbridge:draw()
47	local rot = 0
48	if self.dir == "up" then
49		rot = math.pi*1.5
50	elseif self.dir == "down" then
51		rot = math.pi*0.5
52	elseif self.dir == "left" then
53		rot = math.pi
54	end
55
56	love.graphics.draw(lightbridgesideimg, math.floor((self.cox-xscroll-.5)*16*scale), (self.coy-1)*16*scale, rot, scale, scale, 8, 8)
57end
58
59function lightbridge:updaterange()
60	for i, v in pairs(self.childtable) do
61		v.destroy = true
62	end
63	self.childtable = {}
64
65	if self.enabled == false then
66		return
67	end
68
69	local dir = self.dir
70	local startx, starty = self.cox, self.coy
71	local x, y = self.cox, self.coy
72
73	local firstcheck = true
74	local quit = false
75	while x >= 1 and x <= mapwidth and y >= 1 and y <= 15 and tilequads[map[x][y][1]].collision == false and (x ~= startx or y ~= starty or dir ~= self.dir or firstcheck == true) and quit == false do
76		firstcheck = false
77
78		if dir == "right" then
79			x = x + 1
80			table.insert(objects["lightbridgebody"], lightbridgebody:new(self, x-1, y, "hor"))
81		elseif dir == "left" then
82			x = x - 1
83			table.insert(objects["lightbridgebody"], lightbridgebody:new(self, x+1, y, "hor"))
84		elseif dir == "up" then
85			y = y - 1
86			table.insert(objects["lightbridgebody"], lightbridgebody:new(self, x, y+1, "ver"))
87		elseif dir == "down" then
88			y = y + 1
89			table.insert(objects["lightbridgebody"], lightbridgebody:new(self, x, y-1, "ver"))
90		end
91
92		--check if current block is a portal
93		local portalx, portaly, portalfacing, infacing = getPortal(x, y)
94		if portalx ~= false and ((dir == "left" and infacing == "right") or (dir == "right" and infacing == "left") or (dir == "up" and infacing == "down") or (dir == "down" and infacing == "up")) then
95			x, y = portalx, portaly
96			dir = portalfacing
97
98			if dir == "right" then
99				x = x + 1
100			elseif dir == "left" then
101				x = x - 1
102			elseif dir == "up" then
103				y = y - 1
104			elseif dir == "down" then
105				y = y + 1
106			end
107		end
108
109		--doors
110		for i, v in pairs(objects["door"]) do
111			if v.active then
112				if v.dir == "ver" then
113					if x == v.cox and (y == v.coy or y == v.coy-1) then
114						quit = true
115					end
116				elseif v.dir == "hor" then
117					if y == v.coy and (x == v.cox or x == v.cox+1) then
118						quit = true
119					end
120				end
121			end
122		end
123	end
124end
125
126function lightbridge:addChild(t)
127	table.insert(self.childtable, t)
128end
129
130------------------------------------
131
132lightbridgebody = class:new()
133
134function lightbridgebody:init(parent, x, y, dir)
135	parent:addChild(self)
136	self.cox = x
137	self.coy = y
138	self.dir = dir
139
140	--PHYSICS STUFF
141	if dir == "hor" then
142		self.x = x-1
143		self.y = y-9/16
144		self.width = 1
145		self.height = 1/8
146	else
147		self.x = x-9/16
148		self.y = y-1
149		self.width= 1/8
150		self.height = 1
151	end
152	self.static = true
153	self.active = true
154	self.category = 28
155
156	self.mask = {true}
157
158	self:pushstuff()
159end
160
161function lightbridgebody:pushstuff()
162	local col = checkrect(self.x, self.y, self.width, self.height, {"box", "player"})
163	for i = 1, #col, 2 do
164		local v = objects[col[i]][col[i+1]]
165		if self.dir == "ver" then
166			if v.speedx >= 0 then
167				if #checkrect(self.x + self.width, v.y, v.width, v.height, {"exclude", v}, true) > 0 then
168					v.x = self.x - v.width
169				else
170					v.x = self.x + self.width
171				end
172			else
173				if #checkrect(self.x - v.width, v.y, v.width, v.height, {"exclude", v}, true) > 0 then
174					v.x = self.x + self.width
175				else
176					v.x = self.x - v.width
177				end
178			end
179		elseif self.dir == "hor" then
180			if v.speedy <= 0 then
181				if #checkrect(v.x, self.y - v.height, v.width, v.height, {"exclude", v}, true) > 0 then
182					v.y = self.y + self.height
183				else
184					v.y = self.y - v.height
185				end
186			else
187				if #checkrect(v.x, self.y + self.height, v.width, v.height, {"exclude", v}, true) > 0 then
188					v.y = self.y - v.height
189				else
190					v.y = self.y + self.height
191				end
192			end
193		end
194	end
195end
196
197function lightbridgebody:update(dt)
198	if self.destroy then
199		return true
200	else
201		return false
202	end
203end
204
205function lightbridgebody:draw()
206	love.graphics.setColor(255, 255, 255)
207
208	if self.dir == "hor" then
209		love.graphics.draw(lightbridgeimg, math.floor((self.cox-xscroll-1)*16*scale), (self.coy-20/16)*16*scale, 0, scale, scale)
210	else
211		love.graphics.draw(lightbridgeimg, math.floor((self.cox-xscroll-5/16)*16*scale), (self.coy-1)*16*scale, math.pi/2, scale, scale, 8, 1)
212	end
213end