1gel = class:new()
2
3function gel:init(x, y, id)
4	self.id = id
5
6	--PHYSICS STUFF
7	self.x = x-14/16
8	self.y = y-12/16
9	self.speedy = 0
10	self.speedx = 0
11	self.width = 12/16
12	self.height = 12/16
13	self.static = false
14	self.active = true
15	self.category = 8
16	self.mask = {false, false, true, true, true, true, true, true, false, false, true}
17	self.gravity = 50
18	self.autodelete = true
19	self.timer = 0
20
21	--IMAGE STUFF
22	self.drawable = true
23	self.quad = gelquad[math.random(3)]
24	self.offsetX = 8
25	self.offsetY = 0
26	self.quadcenterX = 8
27	self.quadcenterY = 8
28	self.rotation = 0 --for portals
29	if self.id == 1 then
30		self.graphic = gel1img
31	elseif self.id == 2 then
32		self.graphic = gel2img
33	elseif self.id == 3 then
34		self.graphic = gel3img
35	end
36
37	self.destroy = false
38end
39
40function gel:update(dt)
41	if self.speedy > gelmaxspeed then
42		self.speedy = gelmaxspeed
43	end
44
45	self.rotation = 0
46
47	self.timer = self.timer + dt
48	if self.timer >= gellifetime then
49		return true
50	end
51
52	return self.destroy
53end
54
55function gel:leftcollide(a, b)
56	if self:globalcollide(a, b) then
57		return false
58	end
59	self.destroy = true
60	if a == "tile" then
61		local x, y = b.cox, b.coy
62
63		if (inmap(x+1, y) and tilequads[map[x+1][y][1]].collision) or (inmap(x, y) and tilequads[map[x][y][1]].collision == false) then
64			return
65		end
66
67		--see if adjsajcjet tile is a better fit
68		if math.floor(self.y+self.height/2)+1 ~= y then
69			if inmap(x, math.floor(self.y+self.height/2)+1) and tilequads[map[x][math.floor(self.y+self.height/2)+1][1]].collision then
70				y = math.floor(self.y+self.height/2)+1
71			end
72		end
73
74		map[x][y]["gels"]["right"] = self.id
75	end
76end
77
78function gel:rightcollide(a, b)
79	if self:globalcollide(a, b) then
80		return false
81	end
82	self.destroy = true
83	if a == "tile" then
84		local x, y = b.cox, b.coy
85
86		if (inmap(x-1, y) and tilequads[map[x-1][y][1]].collision) or (inmap(x, y) and tilequads[map[x][y][1]].collision == false) then
87			return
88		end
89
90		--see if adjsajcjet tile is a better fit
91		if math.floor(self.y+self.height/2)+1 ~= y then
92			if inmap(x, math.floor(self.y+self.height/2)+1) and tilequads[map[x][math.floor(self.y+self.height/2)+1][1]].collision then
93				y = math.floor(self.y+self.height/2)+1
94			end
95		end
96
97		map[x][y]["gels"]["left"] = self.id
98	end
99end
100
101function gel:floorcollide(a, b)
102	if self:globalcollide(a, b) then
103		return false
104	end
105	self.destroy = true
106	if a == "tile" then
107		local x, y = b.cox, b.coy
108
109		if (inmap(x, y-1) and tilequads[map[x][y-1][1]].collision) or (inmap(x, y) and tilequads[map[x][y][1]].collision == false) then
110			return
111		end
112
113		--see if adjsajcjet tile is a better fit
114		if math.floor(self.x+self.width/2)+1 ~= x then
115			if inmap(x, y) and tilequads[map[x][y][1]].collision then
116				x = math.floor(self.x+self.width/2)+1
117			end
118		end
119
120		if inmap(x, y) and tilequads[map[x][y][1]].collision then
121			if map[x][y]["gels"]["top"] == self.id then
122				if self.speedx > 0 then
123					for cox = x+1, x+self.speedx*0.2 do
124						if inmap(cox, y-1) and tilequads[map[cox][y][1]].collision == true and tilequads[map[cox][y-1][1]].collision == false then
125							if map[cox][y]["gels"]["top"] ~= self.id then
126								map[cox][y]["gels"]["top"] = self.id
127								break
128							end
129						else
130							break
131						end
132					end
133				elseif self.speedx < 0 then
134					for cox = x-1, x+self.speedx*0.2, -1 do
135						if inmap(cox, y-1) and tilequads[map[cox][y][1]].collision and tilequads[map[cox][y-1][1]].collision == false then
136							if map[cox][y]["gels"]["top"] ~= self.id then
137								map[cox][y]["gels"]["top"] = self.id
138								break
139							end
140						else
141							break
142						end
143					end
144				end
145			else
146				map[x][y]["gels"]["top"] = self.id
147			end
148		end
149	end
150end
151
152function gel:ceilcollide(a, b)
153	if self:globalcollide(a, b) then
154		return false
155	end
156	self.destroy = true
157	if a == "tile" then
158		local x, y = b.cox, b.coy
159		if not inmap(x, y+1) or tilequads[map[x][y+1][1]].collision == false then
160			local x, y = b.cox, b.coy
161
162			map[x][y]["gels"]["bottom"] = self.id
163		end
164	end
165end
166
167function gel:globalcollide(a, b)
168	if a == "tile" then
169		local x, y = b.cox, b.coy
170		if tilequads[map[x][y][1]].invisible then
171			return true
172		end
173	end
174end