1emancipationgrill = class:new()
2
3function emancipationgrill:init(x, y, dir)
4	self.x = x
5	self.y = y
6	self.dir = dir
7
8	self.destroy = false
9	if getTile(self.x, self.y) == true then
10		self.destroy = true
11	end
12
13	for i, v in pairs(emancipationgrills) do
14		local a, b = v:getTileInvolved(self.x, self.y)
15		if a and b == self.dir then
16			self.destroy = true
17		end
18	end
19
20	self.particles = {}
21	self.particles.i = {}
22	self.particles.dir = {}
23	self.particles.speed = {}
24	self.particles.mod = {}
25
26	self.involvedtiles = {}
27
28	--find start and end
29	if dir == "hor" then
30		local curx = self.x
31		while curx >= 1 and getTile(curx, self.y) == false do
32			self.involvedtiles[curx] = self.y
33			curx = curx - 1
34		end
35		self.startx = curx + 1
36
37		local curx = self.x
38		while curx <= mapwidth and getTile(curx, self.y) == false do
39			self.involvedtiles[curx] = self.y
40			curx = curx + 1
41		end
42		self.endx = curx - 1
43
44		self.range = math.floor(((self.endx - self.startx + 1 + emanceimgwidth/16)*16)*scale)
45	else
46		local cury = self.y
47		while cury >= 1 and getTile(self.x, cury) == false do
48			self.involvedtiles[cury] = self.x
49			cury = cury - 1
50		end
51		self.starty = cury + 1
52
53		local cury = self.y
54		while cury <= 15 and getTile(self.x, cury) == false do
55			self.involvedtiles[cury] = self.x
56			cury = cury + 1
57		end
58		self.endy = cury - 1
59
60		self.range = math.floor(((self.endy - self.starty + 1 + emanceimgwidth/16)*16)*scale)
61	end
62
63	for i = 1, self.range/16/scale do
64		table.insert(self.particles.i, math.random())
65		table.insert(self.particles.speed, (1-emanceparticlespeedmod)+math.random()*emanceparticlespeedmod*2)
66		if math.random(2) == 1 then
67			table.insert(self.particles.dir, 1)
68		else
69			table.insert(self.particles.dir, -1)
70		end
71		table.insert(self.particles.mod, math.random(4)-2)
72	end
73end
74
75function emancipationgrill:update(dt)
76	if self.destroy then
77		return true
78	end
79
80	for i, v in pairs(self.particles.i) do
81		self.particles.i[i] = self.particles.i[i] + emanceparticlespeed/(self.range/16/scale)*dt*self.particles.speed[i]
82		while self.particles.i[i] > 1 do
83			self.particles.i[i] = self.particles.i[i]-1
84			self.particles.speed[i] = (1-emanceparticlespeedmod)+math.random()*emanceparticlespeedmod*2
85			if math.random(2) == 1 then
86				self.particles.dir[i] = 1
87			else
88				self.particles.dir[i] = -1
89			end
90			self.particles.mod[i] = math.random(4)-2
91		end
92	end
93end
94
95function emancipationgrill:draw()
96	if self.destroy == false then
97		if self.dir == "hor" then
98			parstartleft = math.floor((self.startx-1-xscroll)*16*scale)
99			parstartright = math.floor((self.endx-1-xscroll)*16*scale)
100			love.graphics.setScissor(parstartleft, ((self.y-1)*16-2)*scale, self.range - emanceimgwidth*scale, scale*4)
101
102			love.graphics.setColor(unpack(emancelinecolor))
103			love.graphics.rectangle("fill", math.floor((self.startx-1-xscroll)*16*scale), ((self.y-1)*16-2)*scale, self.range, scale*4)
104			love.graphics.setColor(255, 255, 255)
105
106			for i, v in pairs(self.particles.i) do
107				local y = ((self.y-1)*16-self.particles.mod[i])*scale
108				if self.particles.dir[i] == 1 then
109					local x = parstartleft+self.range*v
110					love.graphics.draw(emanceparticleimg, math.floor(x), y, math.pi/2, scale, scale)
111				else
112					local x = parstartright-self.range*v
113					love.graphics.draw(emanceparticleimg, math.floor(x), y, -math.pi/2, scale, scale, 1)
114				end
115			end
116
117			love.graphics.setScissor()
118
119			--Sidethings
120			love.graphics.draw(emancesideimg, parstartleft, ((self.y-1)*16-4)*scale, 0, scale, scale)
121			love.graphics.draw(emancesideimg, parstartright+16*scale, ((self.y-1)*16+4)*scale, math.pi, scale, scale)
122		else
123			parstartup = math.floor((self.starty-1)*16*scale)
124			parstartdown = math.floor((self.endy-1)*16*scale)
125			love.graphics.setScissor(math.floor(((self.x-1-xscroll)*16+6)*scale), parstartup-8*scale, scale*4, self.range - emanceimgwidth*scale)
126
127			love.graphics.setColor(unpack(emancelinecolor))
128			love.graphics.rectangle("fill", math.floor(((self.x-1-xscroll)*16+6)*scale), parstartup-8*scale, scale*4, self.range - emanceimgwidth*scale)
129			love.graphics.setColor(255, 255, 255)
130
131			for i, v in pairs(self.particles.i) do
132				local x = ((self.x-1-xscroll)*16-self.particles.mod[i]+9)*scale
133				if self.particles.dir[i] == 1 then
134					local y = parstartup+self.range*v
135					love.graphics.draw(emanceparticleimg, math.floor(x), y, math.pi, scale, scale)
136				else
137					local y = parstartdown-self.range*v
138					love.graphics.draw(emanceparticleimg, math.floor(x), y, 0, scale, scale, 1)
139				end
140			end
141
142			love.graphics.setScissor()
143
144			--Sidethings
145			love.graphics.draw(emancesideimg, math.floor(((self.x-xscroll)*16-4)*scale), parstartup-8*scale, math.pi/2, scale, scale)
146			love.graphics.draw(emancesideimg, math.floor(((self.x-xscroll)*16-12)*scale), parstartdown+8*scale, -math.pi/2, scale, scale)
147		end
148	end
149end
150
151function emancipationgrill:getTileInvolved(x, y)
152	if self.dir == "hor" then
153		if self.involvedtiles[x] == y then
154			return true, "hor"
155		else
156			return false, "hor"
157		end
158	else
159		if self.involvedtiles[y] == x then
160			return true, "ver"
161		else
162			return false, "ver"
163		end
164	end
165end