diff --git a/match-3/main.lua b/match-3/main.lua index ad8f5b0..0d77b30 100644 --- a/match-3/main.lua +++ b/match-3/main.lua @@ -45,6 +45,7 @@ VIRTUAL_HEIGHT = 288 BACKGROUND_SCROLL_SPEED = 80 function love.load() + -- window bar title love.window.setTitle('Match 3') @@ -84,6 +85,7 @@ function love.resize(w, h) end function love.keypressed(key) + -- add to our table of keys pressed this frame love.keyboard.keysPressed[key] = true end @@ -97,6 +99,7 @@ function love.keyboard.wasPressed(key) end function love.update(dt) + -- scroll background, used across all states backgroundX = backgroundX - BACKGROUND_SCROLL_SPEED * dt diff --git a/match-3/src/Board.lua b/match-3/src/Board.lua index cdcfefa..0bf35ca 100644 --- a/match-3/src/Board.lua +++ b/match-3/src/Board.lua @@ -30,12 +30,14 @@ function Board:initializeTiles() table.insert(self.tiles, {}) for tileX = 1, 8 do + -- create a new tile at X,Y with a random color and variety table.insert(self.tiles[tileY], Tile(tileX, tileY, math.random(18), math.random(6))) end end while self:calculateMatches() do + -- recursively initialize if matches were returned so we always have -- a matchless board on start self:initializeTiles() @@ -61,10 +63,12 @@ function Board:calculateMatches() -- every horizontal tile for x = 2, 8 do + -- if this is the same color as the one we're trying to match... if self.tiles[y][x].color == colorToMatch then matchNum = matchNum + 1 else + -- set this as the new color we want to watch for colorToMatch = self.tiles[y][x].color @@ -74,6 +78,7 @@ function Board:calculateMatches() -- go backwards from here by matchNum for x2 = x - 1, x - matchNum, -1 do + -- add each tile to the match that's in that match table.insert(match, self.tiles[y][x2]) end @@ -185,12 +190,15 @@ function Board:getFallingTiles() local y = 8 while y >= 1 do + -- if our last tile was a space... local tile = self.tiles[y][x] if space then + -- if the current tile is *not* a space, bring this down to the lowest space if tile then + -- put the tile in the correct spot in the board and fix its grid positions self.tiles[spaceY][x] = tile tile.gridY = spaceY diff --git a/match-3/src/Dependencies.lua b/match-3/src/Dependencies.lua index 1ec4f84..5bb7be4 100644 --- a/match-3/src/Dependencies.lua +++ b/match-3/src/Dependencies.lua @@ -56,6 +56,7 @@ gTextures = { } gFrames = { + -- divided into sets for each tile type in this game, instead of one large -- table of Quads ['tiles'] = GenerateTileQuads(gTextures['main']) diff --git a/match-3/src/Tile.lua b/match-3/src/Tile.lua index 2bccaee..02b5187 100644 --- a/match-3/src/Tile.lua +++ b/match-3/src/Tile.lua @@ -14,6 +14,7 @@ Tile = Class{} function Tile:init(x, y, color, variety) + -- board positions self.gridX = x self.gridY = y @@ -28,6 +29,7 @@ function Tile:init(x, y, color, variety) end function Tile:render(x, y) + -- draw shadow love.graphics.setColor(34, 32, 52, 255) love.graphics.draw(gTextures['main'], gFrames['tiles'][self.color][self.variety], diff --git a/match-3/src/Util.lua b/match-3/src/Util.lua index 7b47969..c7063af 100644 --- a/match-3/src/Util.lua +++ b/match-3/src/Util.lua @@ -25,6 +25,7 @@ function GenerateTileQuads(atlas) -- 9 rows of tiles for row = 1, 9 do + -- two sets of 6 cols, different tile varietes for i = 1, 2 do tiles[counter] = {} diff --git a/match-3/src/states/BeginGameState.lua b/match-3/src/states/BeginGameState.lua index 82f692b..ea4f5de 100644 --- a/match-3/src/states/BeginGameState.lua +++ b/match-3/src/states/BeginGameState.lua @@ -15,6 +15,7 @@ BeginGameState = Class{__includes = BaseState} function BeginGameState:init() + -- start our transition alpha at full, so we fade in self.transitionAlpha = 255 @@ -26,6 +27,7 @@ function BeginGameState:init() end function BeginGameState:enter(def) + -- grab level # from the def we're passed self.level = def.level @@ -45,13 +47,16 @@ function BeginGameState:enter(def) Timer.tween(0.25, { [self] = {levelLabelY = VIRTUAL_HEIGHT / 2 - 8} }) + -- after that, pause for one second with Timer.after :finish(function() Timer.after(1, function() + -- then, animate the label going down past the bottom edge Timer.tween(0.25, { [self] = {levelLabelY = VIRTUAL_HEIGHT + 30} }) + -- once that's complete, we're ready to play! :finish(function() gStateMachine:change('play', { @@ -69,6 +74,7 @@ function BeginGameState:update(dt) end function BeginGameState:render() + -- render board of tiles self.board:render() diff --git a/match-3/src/states/PlayState.lua b/match-3/src/states/PlayState.lua index f0286fd..ec7b4b5 100644 --- a/match-3/src/states/PlayState.lua +++ b/match-3/src/states/PlayState.lua @@ -19,6 +19,7 @@ PlayState = Class{__includes = BaseState} function PlayState:init() + -- start our transition alpha at full, so we fade in self.transitionAlpha = 255 @@ -55,6 +56,7 @@ function PlayState:init() end function PlayState:enter(params) + -- grab level # from the params we're passed self.level = params.level @@ -75,6 +77,7 @@ function PlayState:update(dt) -- go back to start if time runs out if self.timer <= 0 then + -- clear timers from prior PlayStates Timer.clear() @@ -87,6 +90,7 @@ function PlayState:update(dt) -- go to next level if we surpass score goal if self.score >= self.scoreGoal then + -- clear timers from prior PlayStates -- always clear before you change state, else next state's timers -- will also clear! @@ -119,6 +123,7 @@ function PlayState:update(dt) -- if we've pressed enter, to select or deselect a tile... if love.keyboard.wasPressed('enter') or love.keyboard.wasPressed('return') then + -- if same tile as currently highlighted, deselect local x = self.boardHighlightX + 1 local y = self.boardHighlightY + 1 @@ -137,6 +142,7 @@ function PlayState:update(dt) gSounds['error']:play() self.highlightedTile = nil else + -- swap grid positions of tiles local tempX = self.highlightedTile.gridX local tempY = self.highlightedTile.gridY @@ -159,6 +165,7 @@ function PlayState:update(dt) [self.highlightedTile] = {x = newTile.x, y = newTile.y}, [newTile] = {x = self.highlightedTile.x, y = self.highlightedTile.y} }) + -- once the swap is finished, we can tween falling blocks as needed :finish(function() self:calculateMatches() @@ -205,6 +212,7 @@ function PlayState:calculateMatches() -- as a result of falling blocks once new blocks have finished falling self:calculateMatches() end) + -- if no matches, we can continue playing else self.canInput = true @@ -217,6 +225,7 @@ function PlayState:render() -- render highlighted tile if it exists if self.highlightedTile then + -- multiply so drawing white rect makes it brighter love.graphics.setBlendMode('add') diff --git a/match-3/src/states/StartState.lua b/match-3/src/states/StartState.lua index f3c6bf3..f13a5ea 100644 --- a/match-3/src/states/StartState.lua +++ b/match-3/src/states/StartState.lua @@ -17,6 +17,7 @@ local positions = {} StartState = Class{__includes = BaseState} function StartState:init() + -- currently selected menu item self.currentMenuItem = 1 @@ -42,6 +43,7 @@ function StartState:init() -- time for a color change if it's been half a second self.colorTimer = Timer.every(0.075, function() + -- shift every color to the next, looping the last to front -- assign it to 0 so the loop below moves it to 1, default start self.colors[0] = self.colors[6] @@ -70,6 +72,7 @@ function StartState:update(dt) -- as long as can still input, i.e., we're not in a transition... if not self.pauseInput then + -- change menu selection if love.keyboard.wasPressed('up') or love.keyboard.wasPressed('down') then self.currentMenuItem = self.currentMenuItem == 1 and 2 or 1 @@ -79,6 +82,7 @@ function StartState:update(dt) -- switch to another state via one of the menu options if love.keyboard.wasPressed('enter') or love.keyboard.wasPressed('return') then if self.currentMenuItem == 1 then + -- tween, using Timer, the transition rect's alpha to 255, then -- transition to the BeginGame state after the animation is over Timer.tween(1, { @@ -105,9 +109,11 @@ function StartState:update(dt) end function StartState:render() + -- render all tiles and their drop shadows for y = 1, 8 do for x = 1, 8 do + -- render shadow first love.graphics.setColor(0, 0, 0, 255) love.graphics.draw(gTextures['main'], positions[(y - 1) * x + x], @@ -137,6 +143,7 @@ end axis as needed, relative to the center. ]] function StartState:drawMatch3Text(y) + -- draw semi-transparent rect behind MATCH 3 love.graphics.setColor(255, 255, 255, 128) love.graphics.rectangle('fill', VIRTUAL_WIDTH / 2 - 76, VIRTUAL_HEIGHT / 2 + y - 11, 150, 58, 6) @@ -157,6 +164,7 @@ end Draws "Start" and "Quit Game" text over semi-transparent rectangles. ]] function StartState:drawOptions(y) + -- draw rect behind start and quit game text love.graphics.setColor(255, 255, 255, 128) love.graphics.rectangle('fill', VIRTUAL_WIDTH / 2 - 76, VIRTUAL_HEIGHT / 2 + y, 150, 58, 6)