1hammerbro = class:new()
2
3function hammerbro:init(x, y)
4	--PHYSICS STUFF
5	self.startx = x
6	self.starty = y
7	self.x = x-6/16
8	self.y = y-12/16
9	self.speedy = 0
10	self.speedx = -hammerbrospeed
11	self.width = 12/16
12	self.height = 12/16
13	self.static = false
14	self.active = true
15	self.category = 20
16	self.mask = {true, false, false, false, false, true, false, false, false, true}
17	self.autodelete = true
18	self.gravity = 40
19
20	--IMAGE STUFF
21	self.drawable = true
22	self.graphic = hammerbrosimg
23	self.quad = hammerbrosquad[spriteset][1]
24	self.offsetX = 6
25	self.offsetY = 8
26	self.quadcenterX = 8
27	self.quadcenterY = 21
28
29	self.rotation = 0 --for portals
30
31	self.direction = "left"
32	self.animationtimer = 0
33	self.animationdirection = "left"
34
35	self.falling = false
36
37	self.quadi = 1
38	self.timer = hammerbrotime[math.random(2)]
39	self.timer2 = 0
40
41	self.jumping = false
42
43	self.shot = false
44end
45
46function hammerbro:update(dt)
47	--rotate back to 0 (portals)
48	self.rotation = math.mod(self.rotation, math.pi*2)
49	if self.rotation > 0 then
50		self.rotation = self.rotation - portalrotationalignmentspeed*dt
51		if self.rotation < 0 then
52			self.rotation = 0
53		end
54	elseif self.rotation < 0 then
55		self.rotation = self.rotation + portalrotationalignmentspeed*dt
56		if self.rotation > 0 then
57			self.rotation = 0
58		end
59	end
60
61	if self.shot then
62		self.speedy = self.speedy + shotgravity*dt
63
64		self.x = self.x+self.speedx*dt
65		self.y = self.y+self.speedy*dt
66
67		--check if goomba offscreen
68		if self.y > 18 then
69			return true
70		else
71			return false
72		end
73
74	else
75		if self.speedx < 0 then
76			if self.x < self.startx-16/16 then
77				self.speedx = hammerbrospeed
78			end
79		else
80			if self.x > self.startx then
81				self.speedx = -hammerbrospeed
82			end
83		end
84
85		self.timer = self.timer - dt
86		if self.timer <= 0 then
87			self:throwhammer(self.direction)
88			self.timer = hammerbrotime[math.random(2)]
89		end
90
91		self.timer2 = self.timer2 + dt
92		if self.timer2 > hammerbrojumptime then
93			self.timer2 = self.timer2 - hammerbrojumptime
94			--decide whether up or down
95			local dir
96			if self.y > 12 then
97				dir = "up"
98			elseif self.y < 6 then
99				dir = "down"
100			else
101				if math.random(2) == 1 then
102					dir = "up"
103				else
104					dir = "down"
105				end
106			end
107
108			if dir == "up" then
109				self.speedy = -hammerbrojumpforce
110				self.mask[2] = true
111				self.jumping = "up"
112			else
113				self.speedy = -hammerbrojumpforcedown
114				self.mask[2] = true
115				self.jumping = "down"
116				self.jumpingy = self.y
117			end
118		end
119
120		if self.jumping then
121			if self.jumping == "up" then
122				if self.speedy > 0 then
123					self.jumping = false
124					self.mask[2] = false
125				end
126			elseif self.jumping == "down" then
127				if self.y > self.jumpingy + 2 then
128					self.jumping = false
129					self.mask[2] = false
130				end
131			end
132		end
133
134		--turn around?
135		--find nearest player
136		closestplayer = 1
137		for i = 2, players do
138			local v = objects["player"][i]
139			if math.abs(self.x - v.x) < math.abs(self.x - objects["player"][closestplayer].x) then
140				closestplayer = i
141			end
142		end
143
144		if self.direction == "left" and objects["player"][closestplayer].x > self.x then
145			self.direction = "right"
146			self.animationdirection = "right"
147		elseif self.direction == "right" and objects["player"][closestplayer].x < self.x then
148			self.direction = "left"
149			self.animationdirection = "left"
150		end
151
152		self.animationtimer = self.animationtimer + dt
153		while self.animationtimer > hammerbroanimationspeed do
154			self.animationtimer = self.animationtimer - hammerbroanimationspeed
155			self.quadi = self.quadi + 1
156			if self.quadi == 3 then
157				self.quadi = 1
158			end
159			if self.timer < hammerbropreparetime then
160				self.quad = hammerbrosquad[spriteset][self.quadi+2]
161			else
162				self.quad = hammerbrosquad[spriteset][self.quadi]
163			end
164		end
165
166		if self.speedx > hammerbrospeed then
167			self.speedx = self.speedx - friction*dt
168			if self.speedx < hammerbrospeed then
169				self.speedx = hammerbrospeed
170			end
171		elseif self.speedx < -hammerbrospeed then
172			self.speedx = self.speedx + friction*dt
173			if self.speedx > hammerbrospeed then
174				self.speedx = -hammerbrospeed
175			end
176		end
177
178		return false
179	end
180end
181
182function hammerbro:throwhammer(dir)
183	table.insert(objects["hammer"], hammer:new(self.x, self.y, dir))
184end
185
186function hammerbro:stomp()--hehe hammerbro stomp
187	self:shotted()
188	self.speedy = 0
189end
190
191function hammerbro:shotted() --fireball, star, turtle
192	playsound(shotsound)
193	self.shot = true
194	self.speedy = -shotjumpforce
195	self.direction = dir or "right"
196	self.active = false
197	self.gravity = shotgravity
198	self.speedx = 0
199end
200
201function hammerbro:leftcollide(a, b)
202	self.speedx = goombaspeed
203
204	if a == "koopa" then
205		if b.small and b.speedx ~= 0 then
206			self:shotted("right")
207		end
208	elseif a == "bulletbill" then
209		self:shotted("right")
210	elseif a == "hammer" and b.killstuff then
211		self:shotted()
212	end
213
214	return false
215end
216
217function hammerbro:rightcollide(a, b)
218	self.speedx = -goombaspeed
219
220	if a == "koopa" then
221		if b.small and b.speedx ~= 0 then
222			self:shotted("left")
223		end
224	elseif a == "bulletbill" then
225		self:shotted("left")
226	elseif a == "hammer" and b.killstuff then
227		self:shotted()
228	end
229
230	return false
231end
232
233function hammerbro:ceilcollide(a, b)
234	if a == "player" or a == "box" then
235		self:stomp()
236	elseif a == "koopa" then
237		if b.small and b.speedx ~= 0 then
238			self:shotted("left")
239		end
240	elseif a == "bulletbill" then
241		self:shotted("right")
242	elseif a == "hammer" and b.killstuff then
243		self:shotted()
244	end
245end
246
247function hammerbro:startfall()
248
249end
250
251function hammerbro:floorcollide(a, b)
252	if a == "bulletbill" then
253		self:shotted("right")
254	elseif a == "hammer" and b.killstuff then
255		self:shotted()
256	end
257end
258
259function hammerbro:emancipate(a)
260	self:shotted()
261end
262
263function hammerbro:portaled()
264	self.jumping = false
265	self.mask[2] = false
266end
267
268-------------------------------
269hammer = class:new()
270
271function hammer:init(x, y, dir)
272	--PHYSICS STUFF
273	self.x = x
274	self.y = y-16/16
275	self.starty = self.y
276	self.speedy = -hammerstarty
277	self.speedx = -hammerspeed
278	if dir == "right" then
279		self.speedx = -self.speedx
280	end
281	self.width = 12/16
282	self.height = 12/16
283	self.static = false
284	self.active = true
285	self.category = 14
286	self.mask = {	true,
287					true, false, false, false, true,
288					true, true, true, true, true,
289					true, false, true, true, true,
290					true, true, true, false, true,
291					true, true, true, true, true,
292					true, true, true, true, true}
293	self.emancipatecheck = true
294	self.gravity = hammergravity
295	self.autodelete = true
296
297	--IMAGE STUFF
298	self.drawable = true
299	self.graphic = hammerimg
300	self.quadi = 1
301	self.quad = hammerquad[spriteset][1]
302	self.offsetX = 6
303	self.offsetY = 2
304	self.quadcenterX = 8
305	self.quadcenterY = 8
306
307	self.rotation = 0 --for portals
308	self.animationdirection = dir
309	self.timer = 0
310end
311
312function hammer:update(dt)
313	self.timer = self.timer + dt
314	while self.timer > hammeranimationspeed do
315		self.quadi = self.quadi + 1
316		if self.quadi == 5 then
317			self.quadi = 1
318		end
319		self.quad = hammerquad[spriteset][self.quadi]
320		self.timer = self.timer - hammeranimationspeed
321	end
322
323	if self.mask[20] and self.y > self.starty + 1 then
324		self.mask[20] = false
325	end
326end
327
328function hammer:leftcollide()
329	return false
330end
331
332function hammer:rightcollide()
333	return false
334end
335
336function hammer:floorcollide()
337	return false
338end
339
340function hammer:ceilcollide()
341	return false
342end
343
344function hammer:portaled()
345	self.killstuff = true
346end