-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfatknight.lua
298 lines (266 loc) · 7.05 KB
/
fatknight.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
require "collisions"
local fatknight = {}
fatknight.__index = fatknight
function newFatknight(n)
n.type = "fatknight"
n.width = 24
n.height = 32
n.xspeed = 0
n.yspeed = 0
n.xaccel = 0.5
n.yaccel = 0.17
n.direction = "left"
n.stance = "stand"
n.ATTACKING = 0
n.speedlimit = 1
n.behavior = "sleeping"
n.HIT = 0
n.GUARD = 0
n.sword = nil
n.hp = 100
n.DIYING = 0
n.KNOCK = 0
n.target = false
n.animations = {
stand = {
left = newAnimation(IMG_fatknight_stand_left, 96, 64, 2, 10),
right = newAnimation(IMG_fatknight_stand_right, 96, 64, 2, 10)
},
hit = {
left = newAnimation(IMG_fatknight_hit_left, 96, 64, 2, 60),
right = newAnimation(IMG_fatknight_hit_right, 96, 64, 2, 60)
},
run = {
left = newAnimation(IMG_fatknight_run_left, 96, 64, 1, 10),
right = newAnimation(IMG_fatknight_run_right, 96, 64, 1, 10)
},
fall = {
left = newAnimation(IMG_fatknight_fall_left, 96, 64, 1, 10),
right = newAnimation(IMG_fatknight_fall_right, 96, 64, 1, 10)
},
attack = {
left = newAnimation(IMG_fatknight_attack_left, 96, 64, 3, 10),
right = newAnimation(IMG_fatknight_attack_right, 96, 64, 3, 10)
},
dead = {
left = newAnimation(IMG_fatknight_dead_left, 96, 64, 3, 10),
right = newAnimation(IMG_fatknight_dead_right, 96, 64, 3, 10)
},
knock = {
left = newAnimation(IMG_fatknight_knock_left, 96, 64, 3, 10),
right = newAnimation(IMG_fatknight_knock_right, 96, 64, 3, 10)
},
guard = {
left = newAnimation(IMG_fatknight_guard_left, 96, 64, 3, 10),
right = newAnimation(IMG_fatknight_guard_right, 96, 64, 3, 10)
},
}
n.anim = n.animations[n.stance][n.direction]
return setmetatable(n, fatknight)
end
function fatknight:on_the_ground()
return solid_at(self.x + 8, self.y + 32, self)
or solid_at(self.x + 22, self.y + 32, self)
end
function fatknight:distance(ch)
local dX = (self.x + self.width/2) - (ch.x + ch.width/2)
local dY = (self.y + self.height/2) - (ch.y + ch.height/2)
return math.sqrt( ( dX^2 ) + ( dY^2 ) )
end
function fatknight:update(dt)
local otg = self:on_the_ground()
if self.hp <= 0 and otg and self.HIT == 0 then
self.xspeed = 0
self.behavior = "sleeping"
self.target = false
self.DIYING = self.DIYING + 1
end
-- gravity
if not otg then
self.yspeed = self.yspeed + self.yaccel
if (self.yspeed > 3) then self.yspeed = 3 end
self.y = self.y + self.yspeed
end
-- apply speed
self.x = self.x + self.xspeed;
if self:distance(character1) < 96 and self.ATTACKING == 0 and self.HIT == 0 and self.GUARD == 0
and self.hp > 0 and self.KNOCK == 0 and character1.hp > 0 then
self.behavior = "follow"
self.target = character1
end
if character2 and self:distance(character2) < 96 and self.ATTACKING == 0 and self.HIT == 0 and self.GUARD == 0
and self.hp > 0 and self.KNOCK == 0 and character2.hp > 0 then
self.behavior = "follow"
self.target = character2
end
if self.target and self:distance(self.target) > 256 and self.HIT == 0 and self.GUARD == 0
and self.ATTACKING == 0 and self.KNOCK == 0 then
self.behavior = "sleeping"
self.target = false
end
if self.target and self:distance(self.target) < 42 and self.HIT == 0 and self.GUARD == 0
and self.ATTACKING == 0 and self.hp > 0 and self.KNOCK == 0 and self.target.hp > 0 then
self.behavior = "attacking"
self.ATTACKING = 70
self.xspeed = 0
end
if self.ATTACKING == 40 then
entities_remove(self.sword)
self.sword = newFatknightsword({holder = self})
table.insert(entities, self.sword)
sfx_sword:play()
end
if self.ATTACKING == 38 then
entities_remove(self.sword)
end
if self.behavior == "follow" and self.HIT == 0 and self.GUARD == 0 and self.ATTACKING == 0 then
if (self.x-self.width/2) - (self.target.x-self.target.width/2) > 0 then
self.direction = "left"
self.xspeed = -0.75
else
self.direction = "right"
self.xspeed = 0.75
end
end
if self.HIT > 0 or self.GUARD > 0 then
if self.xspeed > 0 then
self.xspeed = self.xspeed - 0.05
if self.xspeed < 0 then
self.xspeed = 0
end
elseif self.xspeed < 0 then
self.xspeed = self.xspeed + 0.05;
if self.xspeed > 0 then
self.xspeed = 0
end
end
end
-- animations
if self.DIYING > 0 then
self.stance = "dead"
elseif self.KNOCK > 0 then
self.stance = "knock"
elseif self.HIT > 0 then
self.stance = "hit"
elseif self.GUARD > 0 then
self.stance = "guard"
elseif self.ATTACKING > 0 then
self.stance = "attack"
elseif self:on_the_ground() then
if self.xspeed == 0 then
self.stance = "stand"
else
self.stance = "run"
end
else
if self.yspeed > 0 then
self.stance = "fall"
-- else
-- self.stance = "jump"
end
end
local anim = self.animations[self.stance][self.direction]
-- always animate from first frame
if anim ~= self.anim then
anim.timer = 0
end
self.anim = anim;
self.anim:update(dt)
if self.ATTACKING > 0 then
self.ATTACKING = self.ATTACKING - 1
end
if self.HIT > 0 then
self.HIT = self.HIT - 1
end
if self.GUARD > 0 then
self.GUARD = self.GUARD - 1
end
if self.KNOCK > 0 then
self.KNOCK = self.KNOCK - 1
end
solid_collisions(self)
end
function fatknight:draw()
self.anim:draw(self.x-32-4, self.y-32)
end
function fatknight:cancel_attack()
self.ATTACKING = 0
entities_remove(self.sword)
end
function fatknight:on_collide(e1, e2, dx, dy)
if e2.type == "ground" then
if math.abs(dy) < math.abs(dx) and dy ~= 0 then
self.yspeed = 0
self.y = self.y + dy
if dy < -1 and not self.using_ladder then
sfx_step:play()
end
end
if math.abs(dx) < math.abs(dy) and dx ~= 0 then
self.xspeed = 0
self.x = self.x + dx
end
elseif e2.type == "sword" and self.HIT == 0 and self.GUARD == 0 and self.hp > 0 then
local dmg = 18
if self.direction == e2.direction or math.abs(e2.y-self.y) > 8 then
sfx_fkhit:play()
self.behavior = "follow"
self.target = e2.holder
self.HIT = 32
self:cancel_attack()
if self.target.x < self.x then
self.xspeed = 2
else
self.xspeed = -2
end
if self.target.direction == self.direction then
dmg = dmg * 1.5
end
self.hp = self.hp - dmg
table.insert(effects, newNotif({x=self.x, y=self.y, text=dmg}))
if self.hp <= 0 then
sfx_fatknightdie:play()
end
else
self.GUARD = 16
self:cancel_attack()
if self.target.x < self.x then
self.xspeed = 1
else
self.xspeed = -1
end
sfx_shield:play()
end
elseif e2.type == "magicarrow" and self.HIT == 0 and self.GUARD == 0 and self.hp > 0 then
local dmg = 22
if self.direction == e2.direction or math.abs(e2.y-self.y) < 16 then
sfx_fkhit:play()
self.behavior = "follow"
self.target = e2.holder
self.HIT = 32
self:cancel_attack()
if self.target.x < self.x then
self.xspeed = 2
else
self.xspeed = -2
end
if self.target.direction == self.direction then
dmg = dmg * 1.5
end
self.hp = self.hp - dmg
table.insert(effects, newNotif({x=self.x, y=self.y, text=dmg}))
if self.hp <= 0 then
sfx_fatknightdie:play()
end
else
self.GUARD = 16
self:cancel_attack()
if self.target.x < self.x then
self.xspeed = 1
else
self.xspeed = -1
end
sfx_shield:play()
end
end
end