-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.lua
87 lines (71 loc) · 1.61 KB
/
player.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
Class = require "lib/hump.class"
local anim8 = require 'lib/anim8'
Player = Class {
init = function(self, x, y, image)
self.x = x
self.y = y
self.image = image
local g = anim8.newGrid(96, 140, image:getWidth(), image:getHeight())
self.run_animation = anim8.newAnimation(g('1-4', '1-2'), 0.1)
self.jump_animation = anim8.newAnimation(g(2, 2), 1)
self.vidas = 3
end,
width = 96,
height = 140,
dy = 0,
state = "idle",
jumpingstate = "ascending",
speed = 160,
toppoint = 250,
}
function Player:reset()
self.dy = 0
self.state = "idle"
self.jumpingstate = "ascending"
self.speed = 160
self.x = 40
self.y = 380
end
function Player:update(dt)
self.run_animation:update(dt)
if self.state == "falling" then
self:fall()
elseif self.state == "jumping" then
self:jump()
elseif self.state == "backing" then
self:reset()
end
self.y = self.y + self.speed * self.dy * dt
end
function Player:draw()
if self.dy == 0 then
self.run_animation:draw(self.image, self.x, self.y)
else
self.jump_animation:draw(self.image, self.x, self.y)
end
end
function Player:fall()
self.dy = 1
if self.y >= 650 then
game.obstacle:continue()
end
if self.y >= 920 then
self:reset()
self.state = "reset"
self.vidas = self.vidas - 1
end
end
function Player:jump()
if self.jumpingstate == "ascending" then
self.dy = -1
if self.y <= self.toppoint then
self.jumpingstate = "descending"
end
else
self.dy = 1
if self.y >= 380 then
self:reset()
self.state = "reset"
end
end
end