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
|
function love.load()
fps = 30
upTime = love.timer.getTime()
nextTime = upTime
windowProfile = {
mode = {
width = 256,
height = 240,
},
scale = {
x = 2,
y = 2,
},
title = "GNU & Bola Brawlers",
}
love.graphics.setBackgroundColor(0, 232, 216)
love.graphics.setDefaultFilter('nearest', 'nearest')
love.window.setMode(windowProfile.mode.width * windowProfile.scale.x, windowProfile.mode.height * windowProfile.scale.y)
love.window.setTitle(windowProfile.title)
button = {
left = 'a',
right = 'd',
up = 'w',
down = 's',
a = 'j',
b = 'k',
select = 'g',
start = 'h',
quit = 'escape',
}
metaSprites = {
bola = {
image = love.graphics.newImage('multimedia/ppu_rp2c0x/meta_sprites/bola/default_sheet_color0_alpha.png'),
x = 24,
y = 32,
}
}
character = {
bola = {
orientation = 0,
acceleration = 0,
velocity = 125,
gravity = -500,
position = {
x = windowProfile.mode.width / 2,
y = windowProfile.mode.height / 2,
},
scale = {
x = 1,
y = 1,
},
origin = {
x = metaSprites.bola.x / 2,
y = metaSprites.bola.y / 2,
},
stand = require 'stand',
walk = require 'walk',
jump = require 'jump',
actionLeft = false,
actionRight = false,
actionUp = false,
actionDown = false,
actionA = false,
actionB = false,
},
}
sprite = {
bola = require 'stand',
}
end
function love.keypressed(key, scancode)
keypressed = function(character, metaSprites, sprite, key, scancode)
if scancode == button.quit then
love.event.quit()
end
if scancode == button.a then
character.actionA = true
character.actionDown = false
character.actionUp = false
sprite = require 'jump'
end
if scancode == button.left then
character.actionLeft = true
end
if scancode == button.right then
character.actionRight = true
end
if scancode == button.up and character.jump.isJumping == false then
character.actionUp = true
end
if scancode == button.down and character.jump.isJumping == false then
character.actionDown = true
end
end
keypressed(character.bola, metaSprites.bola, sprite.bola, key, scancode)
end
function love.keyreleased(key, scancode)
keyreleased = function(character, key, scancode)
if scancode == button.a then
character.actionA = false
end
if scancode == button.left then
character.actionLeft = false
end
if scancode == button.right then
character.actionRight = false
end
if scancode == button.up then
character.actionUp = false
end
if scancode == button.down then
character.actionDown = false
end
if scancode == button.b then
character.actionB = false
end
if scancode == button.a then
character.actionA = false
if character.jump.velocity ~= 0 then
character.jump.limitButtonJump = true
end
end
end
keyreleased(character.bola, key, scancode)
end
function love.update(dt)
nextTime = nextTime + (1 / fps)
animation = function(sprite, metaSprites, dt)
--Increase actual elapsed time with new dt
sprite.elapsedTime = sprite.elapsedTime + dt --AnimationStart
--In order to know if it is necessary to change the frame
if sprite.elapsedTime >= (1 / sprite.fps) then
sprite.elapsedTime = sprite.elapsedTime - (1 / sprite.fps)
if sprite.currentFrame == # sprite then --If current frame is equal to long sprite list
sprite.currentFrame = 1 -- Return to first frame
else
sprite.currentFrame = sprite.currentFrame + 1 --Next frame
end
end
metaSprites.quad = sprite[sprite.currentFrame] --Update with new fame
end
animation(sprite.bola, metaSprites.bola, dt)
motion = function(character, metaSprites, sprite, dt)
if character.jump.higher > 0 and character.actionA == true then
if character.jump.limitButtonJump == false then
character.jump.higher = character.jump.higher - dt
character.jump.velocity = character.jump.velocity + character.jump.height * (dt / character.jump.higherMax)
end
end
if character.actionLeft == true and character.actionRight == false then
sprite = require 'walk'
character.position.x = character.position.x - (character.velocity * dt)
character.scale.x = -1
end
if character.actionRight == true and character.actionLeft == false then
sprite = require 'walk'
character.position.x = character.position.x + (character.velocity * dt)
character.scale.x = 1
end
if character.actionUp == true and character.actionDown == false then
sprite = require 'walk'
character.position.y = character.position.y - (character.velocity * dt)
character.jump.ground = character.position.y
end
if character.actionDown == true and character.actionUp == false then
sprite = require 'walk'
character.position.y = character.position.y + (character.velocity * dt)
character.jump.ground = character.position.y
end
if character.actionUp == true and character.actionDown == true then
sprite = require 'stand'
elseif character.actionLeft == true and character.actionRight == true then
sprite = require 'stand'
elseif character.actionLeft == false and character.actionRight == false and character.actionUp == false and character.actionDown == false then
sprite = require 'stand'
end
return sprite
end
sprite.bola = motion(character.bola, metaSprites.bola, sprite.bola, dt)
gravity = function(character, dt)
if character.jump.velocity ~= 0 then
character.jump.isJumping = true
character.position.y = character.position.y + (character.jump.velocity * dt)
character.jump.velocity = character.jump.velocity - (character.gravity * dt)
end
if character.position.y > character.jump.ground then
character.jump.velocity = 0
character.position.y = character.jump.ground
character.jump.higher = character.jump.higherMax
character.jump.limitButtonJump = false
character.jump.isJumping = false
character.actionA = false
if love.keyboard.isScancodeDown(button.up) then
character.actionUp = true
elseif love.keyboard.isScancodeDown(button.down) then
character.actionDown = true
end
end
end
gravity(character.bola, dt)
limit = function(character, dt)
if character.position.x <= character.origin.x then
character.position.x = character.origin.x
elseif character.position.x >= windowProfile.mode.width - character.origin.x then
character.position.x = windowProfile.mode.width - character.origin.x
end
if character.position.y <= character.origin.y then
character.position.y = character.origin.y
elseif character.position.y >= windowProfile.mode.height - character.origin.y then
character.position.y = windowProfile.mode.height - character.origin.y
end
end
limit(character.bola, dt)
end
function love.draw()
local currentTime = love.timer.getTime()
if nextTime <= currentTime then
nextTime = currentTime
return
end
love.timer.sleep(nextTime - currentTime)
love.graphics.scale(windowProfile.scale.x, windowProfile.scale.y)
love.graphics.print('FPS: ' .. love.timer.getFPS(), 0, 0)
draw = function(metaSprites, character)
love.graphics.draw(
metaSprites.image,
metaSprites.quad,
character.position.x,
character.position.y,
character.orientation,
character.scale.x,
character.scale.y,
character.origin.x,
character.origin.y
)
end
draw(metaSprites.bola, character.bola)
end
|