Skip to content

Commit

Permalink
commenting style
Browse files Browse the repository at this point in the history
  • Loading branch information
coltonoscopy committed Jul 9, 2018
1 parent 3326e03 commit 995491f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions match-3/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ VIRTUAL_HEIGHT = 288
BACKGROUND_SCROLL_SPEED = 80

function love.load()

-- window bar title
love.window.setTitle('Match 3')

Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
8 changes: 8 additions & 0 deletions match-3/src/Board.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions match-3/src/Dependencies.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
2 changes: 2 additions & 0 deletions match-3/src/Tile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Tile = Class{}

function Tile:init(x, y, color, variety)

-- board positions
self.gridX = x
self.gridY = y
Expand All @@ -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],
Expand Down
1 change: 1 addition & 0 deletions match-3/src/Util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
Expand Down
6 changes: 6 additions & 0 deletions match-3/src/states/BeginGameState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
BeginGameState = Class{__includes = BaseState}

function BeginGameState:init()

-- start our transition alpha at full, so we fade in
self.transitionAlpha = 255

Expand All @@ -26,6 +27,7 @@ function BeginGameState:init()
end

function BeginGameState:enter(def)

-- grab level # from the def we're passed
self.level = def.level

Expand All @@ -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', {
Expand All @@ -69,6 +74,7 @@ function BeginGameState:update(dt)
end

function BeginGameState:render()

-- render board of tiles
self.board:render()

Expand Down
9 changes: 9 additions & 0 deletions match-3/src/states/PlayState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
PlayState = Class{__includes = BaseState}

function PlayState:init()

-- start our transition alpha at full, so we fade in
self.transitionAlpha = 255

Expand Down Expand Up @@ -55,6 +56,7 @@ function PlayState:init()
end

function PlayState:enter(params)

-- grab level # from the params we're passed
self.level = params.level

Expand All @@ -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()

Expand All @@ -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!
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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')

Expand Down
8 changes: 8 additions & 0 deletions match-3/src/states/StartState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local positions = {}
StartState = Class{__includes = BaseState}

function StartState:init()

-- currently selected menu item
self.currentMenuItem = 1

Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -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, {
Expand All @@ -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],
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 995491f

Please sign in to comment.