-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainplayermove.lua
539 lines (438 loc) · 13.2 KB
/
mainplayermove.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
require "loadsprite"
require "cameram"
require "load"
function getPosition(player)
return player.x - width/2, player.y - height/2
end
function checkoutframe(curr_anim,curr_frame)
--do a switch state for the animation
if curr_anim =="throw" then
return curr_frame >= 6 and 6 or curr_frame
elseif curr_anim == "shoot_up" then
return curr_frame > 9 and 1 or curr_frame
elseif curr_anim == "idle_crouch" then
return curr_frame > 9 and 1 or curr_frame
else
return curr_frame >= 12 and 1 or curr_frame
end
end
--]]
function getplayer()
local sprite = {}
sprite.player = loadplayer()
if sprite.player == nil then return nil end
sprite.curr_anim_body = "idle_body"
sprite.curr_anim_leg = "idle_leg"
sprite.curr_frame_body = 1
sprite.curr_frame_leg = 1
sprite.elapsedtime = 0
sprite.timethrowbombs = 0
sprite.time_scale = 1
sprite.pointplayer = 0
sprite.endanim = 0
return sprite
end
alignement = 2
local function alignmentlegs( curr_frame_leg )
if curr_frame_leg == 1 then
alignement = 4
elseif curr_frame_leg == 2 then
alignement = 6
elseif curr_frame_leg == 3 then
alignement = 7
elseif curr_frame_leg == 4 then
alignement = 1
elseif curr_frame_leg == 5 then
alignement = -3
elseif curr_frame_leg == 6 then
alignement = 1
elseif curr_frame_leg == 7 then
alignement = 2
elseif curr_frame_leg == 8 then
alignement = 5
elseif curr_frame_leg == 9 then
alignement = 4
elseif curr_frame_leg == 10 then
alignement = -4
elseif curr_frame_leg == 11 then
alignement = -3
else
alignement = 0
end
end
--update move of grenate
local function UpdateGrenate(player,dt)
local gravity = 450.8
local bombs = player.bombs
local ground = player.ground+40
--draw hope parabolic coordinates for grenate
for i,v in ipairs(bombs) do
v.elapsedbombs = v.elapsedbombs + dt
v.x = v.x0 + v.speedx*v.elapsedbombs*v.reverse
v.y = v.y0 - (v.speedy*v.elapsedbombs - 0.5*gravity*v.elapsedbombs^2)
end
--remove bombs if go outside of screen
for i = #bombs,1,-1 do
local v = bombs[i]
if (v.x < camera.x -5 ) or (v.x > width + 5+camera.x )
or (v.y > ground ) then --or (v.y > height + 5)then
table.remove(bombs,i)
end
end
end
local function UpdateBullets(bullets,dt)
for i,v in ipairs(bullets) do
if v.up then
v.y = v.y + v.speed*dt*-1
else
v.x = v.x + v.speed*dt*v.reverse
end
end
--remove bullets from table
for i = #bullets,1,-1 do
local v = bullets[i]
if (v.x < camera.x -10) or (v.x > width + camera.x )
or (v.y < -5 ) or (v.y > height + 5)then
table.remove(bullets,i)
end
end
end
--move the animation sprite iterate on array of animation
function UpdatePlayer(sprite,dt)
sprite.elapsedtime = sprite.elapsedtime + dt
local outl = sprite.player.movement[sprite.curr_anim_leg][sprite.curr_frame_leg]
if sprite.elapsedtime > sprite.player.frame_duration * sprite.time_scale then
if outl == nil then sprite.curr_frame_leg = 1 end
if sprite.curr_frame_leg < #sprite.player.movement[sprite.curr_anim_leg] then
sprite.curr_frame_leg = sprite.curr_frame_leg + 1
alignmentlegs(sprite.curr_frame_leg)
else
sprite.curr_frame_leg = 1
alignement = 0
end
--check if null
local outb = sprite.player.movement[sprite.curr_anim_body][sprite.curr_frame_body]
if outb == nil then sprite.curr_frame_body = 1 end
--move sprite for body
if sprite.curr_frame_body < #sprite.player.movement[sprite.curr_anim_body] then
sprite.curr_frame_body = sprite.curr_frame_body + 1
else
--means end of sprite
if sprite.player.dead then
deathsound:play()
sprite.endanim = 1
return
end
sprite.player.shoot = false
sprite.player.throw = false
if sprite.player.crouch then
sprite.curr_frame_body = 4
else
sprite.curr_frame_body = 1
end
end
sprite.elapsedtime = 0
end
if #sprite.player.bombs > 0 then UpdateGrenate(sprite.player,dt) end
if #sprite.player.bullets > 0 then UpdateBullets(sprite.player.bullets,dt) end
end
local function sign(x)
return x > 0 and 1 or x < 0 and -1
end
--draw player only if run
--and check if shoot or throw bomb or only move
local function drawrunning(sprite,rev)
sprite.curr_anim_leg = "move_leg"
--alignment body and legs for run
local alignment = alignement*sign(rev)
love.graphics.draw(
sprite.player.img_div,
sprite.player.movement[sprite.curr_anim_leg][sprite.curr_frame_leg],
sprite.player.x - (alignment)- rev*15,
sprite.player.y + 40,
0,
rev,
2
)
--check the state of player
local y = 0
if sprite.player.shoot then
sprite.curr_anim_body = "shoot_move"
if sprite.player.up then
sprite.curr_anim_body = "shoot_up"
if sprite.curr_frame_body <= 3 then
y = 80
else
y = 30
end
end
elseif sprite.player.throw then
sprite.curr_anim_body = "throw"
elseif sprite.player.up then
sprite.curr_anim_body = "body_up"
y = 6
else
sprite.curr_anim_body = "move_body"
end
--print(sprite.curr_anim_body, sprite.curr_frame_body)
--check if out of bounds
sprite.curr_frame_body = checkoutframe(sprite.curr_anim_body,sprite.curr_frame_body)
love.graphics.draw(
sprite.player.img_div,
sprite.player.movement[sprite.curr_anim_body][sprite.curr_frame_body],
sprite.player.x- rev*15,
sprite.player.y-y,
0,
rev,
2
)
--]]
end
local function drawjump(sprite,rev)
sprite.curr_anim_leg = "jump_leg"
--local variable for alignment sprite body and legs
local y = -1
local d = 14
--check if shoot in air
if sprite.player.shoot then
sprite.curr_anim_body = "shoot"
d = 0
if sprite.player.up then
sprite.curr_anim_body = "shoot_up"
if sprite.curr_frame_body <= 3 then
y = 80
else
y = 30
end
end
elseif sprite.player.throw then
sprite.curr_anim_body = "throw"
d = 0
elseif sprite.player.up then
sprite.curr_anim_body = "body_up"
d,y = 0,4
else
sprite.curr_anim_body = "jump_body"
end
--alignment body and legs for the jump
d = d*sign(rev)
--draw legs
love.graphics.draw(
sprite.player.img_div,
sprite.player.movement[sprite.curr_anim_leg][sprite.curr_frame_leg],
sprite.player.x + d - rev*15,
sprite.player.y + 40,
0,
rev,
2
)
--print(sprite.curr_anim_body, sprite.curr_frame_body)
--check if out of array
sprite.curr_frame_body = checkoutframe(sprite.curr_anim_body,sprite.curr_frame_body)
love.graphics.draw(
sprite.player.img_div,
sprite.player.movement[sprite.curr_anim_body][sprite.curr_frame_body],
sprite.player.x- rev*15,
sprite.player.y - y,
0,
rev,
2
)
--]]
end
--draw player on crouch only one sprite
local function drawcrouch(sprite,rev)
if sprite.curr_frame_body > 9 then
sprite.curr_frame_body = 4 end
local y = -25
if sprite.player.shoot then
sprite.curr_anim_body = "shoot_crouch"
elseif sprite.player.throw then
sprite.curr_anim_body = "throw_crouch"
elseif sprite.player.run then
sprite.curr_anim_body = "move_crouch"
else
sprite.curr_anim_body = "idle_crouch"
if sprite.curr_frame_body <=3 then y = 0 end
end
--print(sprite.curr_anim_body, sprite.curr_frame_body)
sprite.curr_frame_body = checkoutframe(sprite.curr_anim_body,sprite.curr_frame_body)
love.graphics.draw(
sprite.player.img_div,
sprite.player.movement[sprite.curr_anim_body][sprite.curr_frame_body],
sprite.player.x - rev*14,
sprite.player.y - y,
0,
rev,
2
)
end
--check while look up if shoot
local function drawup(sprite,rev)
--sprite.curr_frame_body
if sprite.player.run then
sprite.curr_anim_leg = "move_leg"
else
sprite.curr_anim_leg = "idle_leg_up"
end
love.graphics.draw(
sprite.player.img_div,
sprite.player.movement[sprite.curr_anim_leg][1],
sprite.player.x - rev*15,
sprite.player.y + 40,
0,
rev,
2
)
local y = 6
if sprite.player.shoot then
sprite.curr_anim_body = "shoot_up"
if sprite.curr_frame_body <= 3 then
y = 80
else
y = 30
end
else
sprite.curr_anim_body = "body_up"
end
--print(sprite.curr_anim_body, sprite.curr_frame_body)
sprite.curr_frame_body = checkoutframe(sprite.curr_anim_body,sprite.curr_frame_body)
love.graphics.draw(
sprite.player.img_div,
sprite.player.movement[sprite.curr_anim_body][sprite.curr_frame_body],
sprite.player.x - rev*15,
sprite.player.y - y,
0,
rev,
2
)
end
--check in idle if shoot of throw bomb and if go out of array
local function stateidledraw(sprite)
sprite.curr_anim_body = "idle_body"
if sprite.player.shoot then sprite.curr_anim_body = "shoot"
elseif sprite.player.throw then sprite.curr_anim_body = "throw" return end
if sprite.player.movement[sprite.curr_anim_body][sprite.curr_frame_body] == nil then
sprite.curr_frame_body = 1
end
end
function DrawPlayer(sprite)
--if run 2 draw for body and for legs
local rev = sprite.player.reverse*2
--check if there is bombs or bullet for draw on screen
if #sprite.player.bombs > 0 then
DrawGrenate(sprite.player.bombs,sprite.player.grenate)
end
if #sprite.player.bullets > 0 then
DrawBullet(sprite.player.bullets,sprite.player)
end
--check what state the player is jump crouch run up or idle
if sprite.player.dead then
drawdeath(sprite,rev)
elseif sprite.player.y_speed ~= 0 then
drawjump(sprite,rev)
elseif sprite.player.crouch then
drawcrouch(sprite,rev)
elseif sprite.player.run then
drawrunning(sprite,rev)
elseif sprite.player.up then
drawup(sprite,rev)
else
local y = 40
stateidledraw(sprite)
--call 2 draw for body and for leg
love.graphics.draw(
sprite.player.img_div,
sprite.player.movement[sprite.curr_anim_leg][1],
sprite.player.x- rev*15,
sprite.player.y + y ,
0,
rev,
2
)
--print(sprite.curr_anim_body,sprite.curr_frame_body)
love.graphics.draw(
sprite.player.img_div,
sprite.player.movement[sprite.curr_anim_body][sprite.curr_frame_body],
sprite.player.x- rev*15,
sprite.player.y,
0,
rev,
2
)
end
end
--jump character with is sprite associate
function Jump(player)
if player.y_speed == 0 then
player.y_speed = player.jumph
end
end
--check gravity and ground after jump
function jumpgravity(sprite,dt)
local player = sprite.player
player.y = player.y + player.y_speed*dt
player.y_speed = player.y_speed - player.gravity*dt
if player.y > player.ground then
player.y_speed = 0
player.y = player.ground
sprite.curr_anim_leg = "idle_leg"
sprite.curr_frame_leg = 1
end
end
local function drawdeathburn(sprite,rev)
love.graphics.draw(
sprite.player.img_div,
sprite.player.movement[sprite.curr_anim_body][sprite.curr_frame_body],
sprite.player.x- rev*15,
sprite.player.y - 20 ,
0,
rev,
2
)
end
local function drawdeathroll(sprite,rev)
love.graphics.draw(
sprite.player.img_div,
sprite.player.movement[sprite.curr_anim_body][sprite.curr_frame_body],
sprite.player.x- rev*15,
sprite.player.y ,
0,
rev,
2
)
end
local function drawdeathknife(sprite,rev)
love.graphics.draw(
sprite.player.img_div,
sprite.player.movement[sprite.curr_anim_body][sprite.curr_frame_body],
sprite.player.x- rev*15,
sprite.player.y ,
0,
rev,
2
)
end
function drawdeath(sprite,rev)
--check various type of death
if sprite.curr_anim_body == "death_burn" then drawdeathburn(sprite,rev)
elseif sprite.curr_anim_body == "death_rolling" then drawdeathroll(sprite,rev)
else drawdeathknife(sprite,rev) end
end
--draw bombs image grenate
function DrawGrenate(bombs,grenate)
for i,v in ipairs(bombs) do
love.graphics.draw(grenate,v.x,v.y,0,2,2)
end
end
--draw bullet on screen
function DrawBullet(bullets,player)
for i,v in ipairs(bullets) do
local rev = v.reverse*2
if v.up then
love.graphics.draw(player.img,player.bulletup,v.x,v.y,0,rev,2)
else
love.graphics.draw(player.img,player.bullet,v.x,v.y,0,rev,2)
end
end
end