-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplayer.lua
More file actions
102 lines (97 loc) · 3.12 KB
/
player.lua
File metadata and controls
102 lines (97 loc) · 3.12 KB
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
player = {}
function player:new(x, y, height, width, speed)
local p = {
x = x,
y = y,
height = height or 20, -- valor por defecto
width = width or 20,
speed = speed or 200,
-- ahora imágenes... muchas!
standingRight = love.graphics.newImage('sprites/mario_standing_right.png'),
movingRight = love.graphics.newImage('sprites/mario_moving_right.png'),
standingLeft = love.graphics.newImage('sprites/mario_standing_left.png'),
movingLeft = love.graphics.newImage('sprites/mario_moving_left.png'),
-- and the movement flags
left = true,
right = false,
grounded = false,
}
setmetatable(p, {__index = player})
return p
end
function player:update(dt)
-- movimiento del jugador
-- maybe change up key to only pressed once https://love2d.org/wiki/love.keypressed
if love.keyboard.isDown('up')then
io.write("key pressed: up\n")
self.y = self.y - self.speed * dt
end
-- solo podemos movernos abajo si no estamos en una plataforma
if love.keyboard.isDown('down') and self.grounded == false then
io.write("key pressed: down\n")
self.y = self.y + self.speed * dt
end
if love.keyboard.isDown('left') then
io.write("key pressed: left\n")
self.x = self.x - self.speed * dt
self.left = true
self.right = false
end
if love.keyboard.isDown('right') then
io.write("key pressed: right\n")
self.x = self.x + self.speed * dt
self.right = true
self.left = false
end
-- pseudogravedad
if self.grounded == false then
-- tenemos que disminuir la gravedad para que los saltos sean dinámicos,
-- si no hay aceleración
if love.keyboard.isDown('up') then
self.y = self.y + 30 * dt
else
self.y = self.y + 70 * dt
end
end
end
function player:draw()
-- si está en tierra
if self.grounded == true then
if self.left == true then
love.graphics.draw(self.standingLeft, self.x, self.y, nil, 0.3, 0.3)
elseif self.right == true then
love.graphics.draw(self.standingRight, self.x, self.y, nil, 0.3, 0.3)
else
io.write("Error!!!\n")
end
-- si está en aire
elseif self.grounded == false then
if self.left == true then
love.graphics.draw(self.movingLeft, self.x, self.y, nil, 0.3, 0.3)
elseif self.right == true then
love.graphics.draw(self.movingRight, self.x, self.y, nil, 0.3, 0.3)
else
io.write("Error!!!\n")
end
end
end
-- función que nos escribe las coordenadas del jugador en la consola
function player:log()
io.write("player position is: "..self.x..", "..self.y.."\n")
end
-- nos da la coordenada y inferior del jugdor dependiendo de la imagen
function player:bottom()
if self.grounded == true then
if self.left == true then
return math.floor(self.y - self.standingLeft:getHeight() + 220) -- no tengo ni idea de esto , pero funciona;)
else
return math.floor(self.y - self.standingRight:getHeight() + 220)
end
else
if self.left == true then
return math.floor(self.y - self.movingLeft:getHeight() + 220)
else
return math.floor(self.y - self.movingRight:getHeight() + 220)
end
end
end