Skip to content

Commit

Permalink
feat: decorate reset game
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Jul 9, 2024
1 parent 3c06e5a commit 6f7286e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
5 changes: 2 additions & 3 deletions examples/pong/game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ local function loop(std, game)
end

local function draw(std, game)
std.draw.color('black')
std.draw.rect('fill', 0, 0, game.width, game.height)
std.draw.clear('black')
std.draw.color('white')
std.draw.rect('fill', 4, game.player_pos, 8, game.player_size)
std.draw.rect('fill', game.ball_pos_x, game.ball_pos_y, game.ball_size, game.ball_size)
Expand All @@ -52,7 +51,7 @@ local function draw(std, game)
end

local function exit(std, game)
game.highscore = std.math.clamp(game.highscore, game.score, game.highscore)
game.highscore = std.math.max(game.highscore, game.score)
end

local P = {
Expand Down
14 changes: 13 additions & 1 deletion src/lib/common/decorators.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,20 @@ local function decorator_poly(limit_verts, func_draw_poly, func_draw_line)
end
end

local function decorator_reset(callbacks, std, game)
return function()
if callbacks.exit then
callbacks.exit(std, game)
end
if callbacks.init then
callbacks.init(std, game)
end
end
end

local P = {
poly=decorator_poly
poly=decorator_poly,
reset=decorator_reset
}

return P;
11 changes: 1 addition & 10 deletions src/lib/ginga/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,6 @@ local function std_draw_line(x1, y1, x2, y2)
canvas:drawLine(x1, y1, x2, y2)
end

local function std_game_reset()
if application.callbacks.exit then
application.callbacks.exit(std, game)
end
if application.callbacks.init then
application.callbacks.init(std, game)
end
end

local function std_game_exit()
if application.callbacks.exit then
application.callbacks.exit(std, game)
Expand Down Expand Up @@ -158,7 +149,7 @@ local function setup(evt)
std.draw.font=std_draw_font
std.draw.line=std_draw_line
std.draw.poly=decorators.poly(0, nil, std_draw_line)
std.game.reset=std_game_reset
std.game.reset=decorators.reset(application.callbacks, std, game)
std.game.exit=std_game_exit
game.width=w
game.height=h
Expand Down
1 change: 1 addition & 0 deletions src/lib/html5/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ local function browser_init(width, height)
application = (load(game_lua))()
std.math = zeebo_math
std.math.random = math.random
std.game.reset=decorators.reset(application.callbacks, std, game)
std.draw = browser_canvas
game.width = width
game.height = height
Expand Down
11 changes: 1 addition & 10 deletions src/lib/love2d/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,6 @@ local function std_draw_font(a,b)
-- TODO: not must be called in update
end

local function std_game_reset()
if application.callbacks.exit then
application.callbacks.exit(std, game)
end
if application.callbacks.init then
application.callbacks.init(std, game)
end
end

local function std_game_exit()
if application.callbacks.exit then
application.callbacks.exit(std, game)
Expand Down Expand Up @@ -121,7 +112,7 @@ function love.load(args)
std.draw.font=std_draw_font
std.draw.line=std_draw_line
std.draw.poly=decorators.poly(0, love.graphics.polygon)
std.game.reset=std_game_reset
std.game.reset=decorators.reset(application.callbacks, std, game)
std.game.exit=std_game_exit
game.width=w
game.height=h
Expand Down

0 comments on commit 6f7286e

Please sign in to comment.