-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
140 lines (104 loc) · 3.58 KB
/
main.lua
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
--[[
GD50
Match-3 Remake
Author: Colton Ogden
Match-3 has taken several forms over the years, with its roots in games
like Tetris in the 80s. Bejeweled, in 2001, is probably the most recognized
version of this game, as well as Candy Crush from 2012, though all these
games owe Shariki, a DOS game from 1994, for their inspiration.
The goal of the game is to match any three tiles of the same variety by
swapping any two adjacent tiles; when three or more tiles match in a line,
those tiles add to the player's score and are removed from play, with new
tiles coming from the ceiling to replace them.
As per previous projects, we'll be adopting a retro, NES-quality aesthetic.
Credit for graphics (amazing work!):
https://opengameart.org/users/buch
Credit for music (awesome track):
http://freemusicarchive.org/music/RoccoW/
Cool texture generator, used for background:
http://cpetry.github.io/TextureGenerator-Online/
]]
-- initialize our nearest-neighbor filter
love.graphics.setDefaultFilter('nearest', 'nearest')
-- this time, we're keeping all requires and assets in our Dependencies.lua file
require 'src/Dependencies'
-- physical screen dimensions
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
-- virtual resolution dimensions
VIRTUAL_WIDTH = 512
VIRTUAL_HEIGHT = 288
-- speed at which our background texture will scroll
BACKGROUND_SCROLL_SPEED = 80
function love.load()
-- window bar title
love.window.setTitle('Match 3')
-- seed the RNG
math.randomseed(os.time())
-- initialize our virtual resolution
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
vsync = true,
fullscreen = false,
resizable = true,
canvas = true
})
-- set music to loop and start
gSounds['music']:setLooping(true)
gSounds['music']:play()
-- initialize state machine with all state-returning functions
gStateMachine = StateMachine {
['start'] = function() return StartState() end,
['begin-game'] = function() return BeginGameState() end,
['play'] = function() return PlayState() end,
['game-over'] = function() return GameOverState() end
}
gStateMachine:change('start')
-- keep track of scrolling our background on the X axis
backgroundX = 0
-- initialize input table
love.keyboard.keysPressed = {}
-- mute music
musicMuted = false
end
function love.resize(w, h)
push:resize(w, h)
end
function love.keypressed(key)
-- add to our table of keys pressed this frame
love.keyboard.keysPressed[key] = true
end
function love.keyboard.wasPressed(key)
if love.keyboard.keysPressed[key] then
return true
else
return false
end
end
function love.update(dt)
-- mute music
if love.keyboard.wasPressed('m') then
if musicMuted then
gSounds['music']:play()
musicMuted = false
else
gSounds['music']:pause()
musicMuted = true
end
end
-- scroll background, used across all states
backgroundX = backgroundX - BACKGROUND_SCROLL_SPEED * dt
-- if we've scrolled the entire image, reset it to 0
if backgroundX <= -1024 + VIRTUAL_WIDTH - 4 + 51 then
backgroundX = 0
end
gStateMachine:update(dt)
love.keyboard.keysPressed = {}
end
function love.draw()
push:start()
-- scrolling background drawn behind every state
love.graphics.draw(gTextures['background'], backgroundX, 0)
gStateMachine:render()
push:finish()
end