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