Skip to content

Commit 2edeee5

Browse files
authored
Patch 1.0.1 (#13)
* Fix: timed mode game end * fix: Timed mode initial time * fix: Undo before any move clears game field * refactor: rename Game.won to game.gameEnded * setup luacheck * lint
1 parent e73f8d7 commit 2edeee5

File tree

12 files changed

+57
-27
lines changed

12 files changed

+57
-27
lines changed

.luacheckrc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
std = "lua51"
2+
3+
unused_args = false
4+
5+
read_globals = {
6+
"Button",
7+
"Label",
8+
"VDiv",
9+
"HDiv",
10+
"Switcher",
11+
"Font",
12+
"GUI",
13+
}
14+
15+
globals = {
16+
"love",
17+
"rgb",
18+
}
19+
20+
exclude_files = {
21+
"yalg"
22+
}

main.lua

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
local Config = require("src.Config")
2-
local Game = require("src.Game")
31
local Sounds = require("src.Sounds")
42

53
local menu = require("src.menu.menu")
64

7-
local game = nil
8-
local config = nil
9-
105
function love.load()
116
Sounds.playLooping("loop")
127
menu:getWidget("options"):load()

src/Cell.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ local function hex(hexstring)
1212
assert(hexstring:sub(0, 1)=="#", "Color strings must be in hex format!")
1313

1414
-- extract string bits
15-
r = hexstring:sub(2, 3)
16-
g = hexstring:sub(4, 5)
17-
b = hexstring:sub(6, 7)
15+
local r = hexstring:sub(2, 3)
16+
local g = hexstring:sub(4, 5)
17+
local b = hexstring:sub(6, 7)
1818

1919
-- convert to decimal
2020
r = tonumber(r, 16)

src/CellPool.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
local CellPool = {}
7-
local SaveRestore = require("src.SaveRestore")
87

98
-- constructor
109
function CellPool:new(game)

src/Game.lua

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,27 @@ function Game:new(config, parentWidget, gamemode)
3030
o.cells = CellPool(o)
3131

3232
-- game state
33-
o.time = ((gamemode == "timed") and 30) or 0
33+
o.time = ((gamemode == "timed") and 300) or 0
3434
o.biggestYet = 2
3535

3636
-- load saved game
3737
SaveRestore.load(o)
3838

3939
-- time updater
40-
o.TM:periodic(function (self)
41-
local dt = self.gamemode == "normal" and 1 or -1
42-
self.time = self.time + dt
40+
o.TM:periodic(function (game)
41+
local dt = game.gamemode == "normal" and 1 or -1
42+
game.time = game.time + dt
4343
end, 1, 0, o)
4444

4545

4646
-- time's up detector task
4747
if gamemode == "timed" then
48-
o.TM:run(function (self)
49-
while self.time > 0 do
48+
o.TM:run(function (game)
49+
while game.time > 0 do
5050
coroutine.yield()
5151
end
52-
self:endGame()
52+
game.gameEnded = true
53+
game:endGame()
5354
end, o)
5455
end
5556

@@ -61,7 +62,12 @@ function Game:drawInfo()
6162
-- draw time
6263
love.graphics.setFont(self.config.gameFont)
6364
love.graphics.setColor(rgb(255, 255, 255))
64-
love.graphics.printf(("%d:%02d"):format(math.floor(self.time / 60), self.time % 60), 0, 10*self.config.hP, self.config.width, "center")
65+
love.graphics.printf(
66+
("%d:%02d"):format(
67+
math.floor(self.time / 60),
68+
self.time % 60),
69+
0, 10*self.config.hP, self.config.width, "center"
70+
)
6571
end
6672

6773

@@ -97,7 +103,7 @@ end
97103

98104

99105
function Game:quit()
100-
if self.won then return end
106+
if self.gameEnded then return end
101107

102108
-- fast-forward to finish all animations
103109
while self.animating do

src/Path.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ function Path:mergeAnimation()
195195
if currElem.value > self.game.biggestYet then
196196
if currElem.value == VICTORYVALUE then
197197
Sounds.play("victory")
198-
self.game.won = true
198+
self.game.gameEnded = true
199199
else
200200
Sounds.play("newBiggest")
201201
end

src/SaveRestore.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ local function loadCellsData(gamemode)
8181
end
8282

8383

84-
function loadGameData(gamemode)
84+
local function loadGameData(gamemode)
8585
local info = love.filesystem.getInfo(GAMEDATAFILE:format(gamemode))
8686
if not info then return end
8787
-- check if file exists

src/Sounds.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ function Sounds.getSource(name)
2222
-- fill in cache
2323
if not cache[name] then
2424
local filename = ("asset/%s.ogg"):format(name)
25-
local fileInfo = assert(love.filesystem.getInfo(filename), ("Requested sound file %s can not be found!"):format(filename))
25+
local fileInfo = assert(
26+
love.filesystem.getInfo(filename),
27+
("Requested sound file %s can not be found!"):format(filename)
28+
)
2629
local fileSize = fileInfo.size
2730
local sourceMode = (fileSize > StreamSize) and "stream" or "static"
2831
cache[name] = love.audio.newSource(filename, sourceMode)

src/Undo.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ setmetatable(Undo, {__call = Undo.new})
2323
function Undo:backup()
2424
-- deep copies the game state for backup
2525
self.time = self.game.time
26+
self.biggestYet = self.game.biggestYet
2627
self.cells = {}
2728

2829
for i, C in ipairs(self.game.cells.cells) do
@@ -36,6 +37,10 @@ end
3637

3738

3839
function Undo:restore()
40+
-- do not restore if no data
41+
-- (no backup was made yet)
42+
if self.biggestYet == 0 then return end
43+
3944
-- deep copy the saved data back to game
4045
self.game.time = self.time
4146

src/menu/GameWrapper.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function GameWrapper:quit()
7979
self.game:quit()
8080

8181
-- pressing back OR winning?
82-
if not self.game.won then
82+
if not self.game.gameEnded then
8383
self:getWidget("switcher").selected = "mainMenu"
8484
else
8585
-- game end screen
@@ -89,7 +89,7 @@ function GameWrapper:quit()
8989
self:getWidget("gameOverLbl").text =
9090
(self.game.gamemode=="normal") and "You won!" or "Time's up"
9191

92-
self:getWidget("resultLbl").text =
92+
self:getWidget("resultLbl").text =
9393
(self.game.gamemode=="normal") and ("Time: %d:%02d"):format(self.game.time/60, self.game.time%60)
9494
or ("Maximum value: %d"):format(self.game.biggestYet)
9595
end

0 commit comments

Comments
 (0)