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
|
---@diagnostic disable: undefined-global
backgrounds = require("assets.backgrounds.backgrounds-data")
sprite_height = 128
sprite_width = 64
deltaTimeMultiplier = 60
groundTiles = {
{ x = 250, y = HEIGHT - 300, width = 200, height = 30 },
{ x = WIDTH - 250 - 200, y = HEIGHT - 300, width = 200, height = 30 },
{ x = 0, y = HEIGHT - 100, width = WIDTH, height = 100 }
}
function SafeInitCharacter(character, default_x, default_y)
character.x = default_x
character.x_velocity = 1
character.y = default_y
character.y_velocity = 1
character.current_sprite = character.asset_dir .. "/sprites/idle.png"
character.default_sprite = character.current_sprite
character.can_jump = false
character.knockback_counter = 0
character.combo_chain = 0
character.can_apply_knockback = false
end
function Setup()
local bgIndex = math.random(1, #backgrounds)
setBgImage(backgrounds[bgIndex])
SafeInitCharacter(player1Character, 250, 150)
SafeInitCharacter(player2Character, WIDTH - 250 - (250 / 2), 150)
end
gravity = 1.2 -- POSITIVE gravity
function DrawGroundTiles()
for _, tile in ipairs(groundTiles) do
queueRectForRender(tile.x, tile.y, tile.width, tile.height, 100, 100, 100, 255) -- gray boxes
end
end
function IsOnGround(character)
for _, tile in ipairs(groundTiles) do
local characterFeetY = character.y + sprite_height
local isWithinX = character.x + sprite_width > tile.x and character.x < tile.x + tile.width
local isTouchingY = characterFeetY >= tile.y and characterFeetY <= tile.y + tile.height
local isFalling = character.y_velocity >= 0
if isWithinX and isTouchingY and isFalling then
character.y = tile.y - sprite_height
return true
end
end
return false
end
function IsHittingCeiling(character)
for _, tile in ipairs(groundTiles) do
local characterHeadY = character.y
local isWithinX = character.x + sprite_width > tile.x and character.x < tile.x + tile.width
local isTouchingY = characterHeadY <= tile.y + tile.height and characterHeadY >= tile.y
if isWithinX and isTouchingY then
character.y = tile.y + tile.height
character.y_velocity = 0
return true
end
end
return false
end
function IsTouchingWall(character, direction)
for _, tile in ipairs(groundTiles) do
local charBottom = character.y + sprite_height
local charTop = character.y
local tileBottom = tile.y + tile.height
local tileTop = tile.y
local verticalOverlap = charBottom > tileTop and charTop < tileBottom
if direction == "left" then
local nextX = character.x - character.speed
local willCollide = nextX < tile.x + tile.width and character.x >= tile.x + tile.width
if willCollide and verticalOverlap then
character.x = tile.x + tile.width
return true
end
elseif direction == "right" then
local nextX = character.x + sprite_width + character.speed
local willCollide = nextX > tile.x and character.x + sprite_width <= tile.x
if willCollide and verticalOverlap then
character.x = tile.x - sprite_width
return true
end
end
end
return false
end
function HandleP1Input()
if Input.isKeyPressedOnce("W") and player1Character.can_jump then
player1Character.y_velocity = player1Character.jump_strength * -1.0
player1Character.can_jump = false
end
if Input.isKeyDown("D") then
if not IsTouchingWall(player1Character, "right") then
player1Character.x_velocity = player1Character.speed
else
player1Character.x_velocity = 0
end
end
if Input.isKeyDown("A") then
if not IsTouchingWall(player1Character, "left") then
player1Character.x_velocity = -player1Character.speed
else
player1Character.x_velocity = 0
end
end
if Input.isKeyPressedOnce("F") then
PerformPunch(player1Character, player2Character)
end
end
function HandleP2Input()
if Input.isKeyPressedOnce("UP") and player2Character.can_jump then
player2Character.y_velocity = player2Character.jump_strength * -1.0
player2Character.can_jump = false
end
if Input.isKeyDown("RIGHT") then
if not IsTouchingWall(player2Character, "right") then
player2Character.x_velocity = player2Character.speed
else
player2Character.x_velocity = 0
end
end
if Input.isKeyDown("LEFT") then
if not IsTouchingWall(player2Character, "left") then
player2Character.x_velocity = -player2Character.speed
else
player2Character.x_velocity = 0
end
end
if Input.isKeyPressedOnce("Right Alt") or Input.isKeyPressedOnce("Right Option") then
PerformPunch(player2Character, player1Character)
end
end
function DrawUI()
local fontFile = "assets/fonts/OpenSans-Bold.ttf"
local fontSize = 24
local text = player1Character.name
local x = 20
local y = 20
queueTextForRender(text, fontFile, x, y, fontSize, 0, 0, 0, 255)
local text = player2Character.name
local textWidth = getTextWidth(fontFile, fontSize, text)
local x = WIDTH - textWidth - 20
local y = 20
queueTextForRender(text, fontFile, x, y, fontSize, 0, 0, 0, 255)
local fontFile = "assets/fonts/OpenSans-MediumItalic.ttf"
local fontSize = 34
local text = tonumber(player1Character.knockback_counter)
local x = 40
local y = 40
queueTextForRender(text, fontFile, x, y, fontSize, 0, 0, 0, 255)
local fontFile = "assets/fonts/OpenSans-MediumItalic.ttf"
local fontSize = 34
local text = tostring(player2Character.knockback_counter)
local textWidth = getTextWidth(fontFile, fontSize, text)
local x = WIDTH - textWidth - 40
queueTextForRender(text, fontFile, x, y, fontSize, 0, 0, 0, 255)
end
function Respawn(character)
SafeInitCharacter(character, 250, 150)
end
function ApplyKnockback(attacker, target)
local knockbackMultiplierX = 0.47
local knockbackMultiplierY = 0.01
target.y_velocity = -10 - (target.knockback_counter * knockbackMultiplierY)
local direction = 1
if target.x < attacker.x then
direction = -1
end
target.x_velocity = target.knockback_counter * knockbackMultiplierX * direction
-- target.x = target.x + direction * (target.knockback_counter * 0.7)
end
function ApplyMinimalKnockback(attacker, target)
local knockbackMultiplierX = 0.47
local knockbackMultiplierY = 0.01
local knockbackCountMinimal = 5
target.y_velocity = -10 - (knockbackCountMinimal * knockbackMultiplierY)
local direction = 1
if target.x < attacker.x then
direction = -1
end
target.x_velocity = knockbackCountMinimal * knockbackMultiplierX * direction
-- target.x = target.x + direction * (target.knockback_counter * 0.7)
end
function KillPlayer(character)
character.lives = character.lives - 1
character.knockback_counter = 0
character.combo_chain = 0
if character.lives > 0 then
Respawn(character)
end
end
function RegisterHit(attacker, target, damage)
target.knockback_counter = target.knockback_counter + damage
attacker.combo_chain = attacker.combo_chain + 1
if attacker.combo_chain >= 4 then
ApplyKnockback(attacker, target)
attacker.combo_chain = 0
else
ApplyMinimalKnockback(attacker, target)
end
if target.knockback_counter >= 1000 then
KillPlayer(target)
end
end
function PerformPunch(attacker, target)
local punch_range = 27
local punch_height = 80
-- Determine direction of target relative to attacker
local punch_direction = (target.x > attacker.x) and "right" or "left"
-- Set punch sprite
attacker.current_sprite = attacker.asset_dir .. "/sprites/punch_" .. punch_direction .. ".png"
-- Reset sprite after a short delay (e.g., 0.15s)
Timer.clearAllTimers()
Timer.after(0.15, function()
attacker.current_sprite = attacker.default_sprite
end)
-- Define hitbox based on punch direction
local hitbox = {
x = attacker.x + (punch_direction == "right" and sprite_width or -punch_range),
y = attacker.y + (sprite_height / 2) - (punch_height / 2),
width = punch_range,
height = punch_height
}
local targetHitbox = {
x = target.x,
y = target.y,
width = sprite_width,
height = sprite_height
}
local function isColliding(a, b)
return a.x < b.x + b.width and
a.x + a.width > b.x and
a.y < b.y + b.height and
a.y + a.height > b.y
end
if isColliding(hitbox, targetHitbox) then
RegisterHit(attacker, target, attacker.hit_strength)
end
end
function ApplyPhysics(character)
local deltaTimeMultiplier = 60 -- Target 60FPS
character.y_velocity = character.y_velocity + gravity * deltaTime * deltaTimeMultiplier
character.y = character.y + character.y_velocity * deltaTime * deltaTimeMultiplier
character.x = character.x + character.x_velocity * deltaTime * deltaTimeMultiplier
character.x_velocity = character.x_velocity * 0.99
if character.y_velocity > 0 then
-- Falling
if IsOnGround(character) then
character.y_velocity = 0
character.can_jump = true
end
elseif character.y_velocity < 0 then
-- Going upward
IsHittingCeiling(character)
end
if not IsOnGround(character) then
character.can_jump = false
end
if character.y > HEIGHT + 50 then
KillPlayer(character)
end
end
function Update()
-- Apply physics
ApplyPhysics(player1Character)
ApplyPhysics(player2Character)
-- Render characters
queueTextureForRender(player1Character.current_sprite, math.floor(player1Character.x), math.floor(player1Character.y))
queueTextureForRender(player2Character.current_sprite, math.floor(player2Character.x), math.floor(player2Character.y))
-- Environment
DrawGroundTiles()
-- Input
HandleP1Input()
HandleP2Input()
-- UI
DrawUI()
-- Timer
Timer.update(deltaTime)
end
|