-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
179 lines (146 loc) · 5.4 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require 'src/Dependencies'
function love.load()
love.graphics.setDefaultFilter('nearest', 'nearest')
math.randomseed(os.time())
love.window.setTitle('Breakout')
gFonts = {
['small'] = love.graphics.newFont('fonts/font.ttf', 8),
['medium'] = love.graphics.newFont('fonts/font.ttf', 16),
['large'] = love.graphics.newFont('fonts/font.ttf', 32)
}
gTextures = {
['background'] = love.graphics.newImage('graphics/background.png'),
['main'] = love.graphics.newImage('graphics/breakout.png'),
['arrows'] = love.graphics.newImage('graphics/arrows.png'),
['hearts'] = love.graphics.newImage('graphics/hearts.png'),
['particle'] = love.graphics.newImage('graphics/particle.png')
}
gFrames = {
['paddles'] = GenerateQuadsPaddles(gTextures['main']),
['balls'] = GenerateQuadsBalls(gTextures['main']),
['bricks'] = GenerateQuadsBricks(gTextures['main']),
['hearts'] = GenerateQuads(gTextures['hearts'], 10, 9),
['arrows'] = GenerateQuads(gTextures['arrows'], 24, 24),
}
gSounds = {
['paddle-hit'] = love.audio.newSource('sounds/paddle_hit.wav', 'static'),
['score'] = love.audio.newSource('sounds/score.wav', 'static'),
['wall-hit'] = love.audio.newSource('sounds/wall_hit.wav', 'static'),
['confirm'] = love.audio.newSource('sounds/confirm.wav', 'static'),
['select'] = love.audio.newSource('sounds/select.wav', 'static'),
['no-select'] = love.audio.newSource('sounds/no-select.wav', 'static'),
['brick-hit-1'] = love.audio.newSource('sounds/brick-hit-1.wav', 'static'),
['brick-hit-2'] = love.audio.newSource('sounds/brick-hit-2.wav', 'static'),
['hurt'] = love.audio.newSource('sounds/hurt.wav', 'static'),
['victory'] = love.audio.newSource('sounds/victory.wav', 'static'),
['recover'] = love.audio.newSource('sounds/recover.wav', 'static'),
['high-score'] = love.audio.newSource('sounds/high_score.wav', 'static'),
['pause'] = love.audio.newSource('sounds/pause.wav', 'static'),
['music'] = love.audio.newSource('sounds/music.wav', 'static')
}
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
vsync = true,
fullscreen = false,
resizable = true
})
gStateMachine = StateMachine {
['start'] = function() return StartState() end,
['play'] = function() return PlayState() end,
['serve'] = function() return ServeState() end,
['game-over'] = function() return GameOverState() end,
['victory'] = function() return VictoryState() end,
['high-scores'] = function() return HighScoreState() end,
['enter-high-score'] = function() return EnterHighScoreState() end,
['paddle-select'] = function() return PaddleSelectState() end,
}
gStateMachine:change('start', {
highScores = loadHighScores()
})
gSounds['music']:play()
gSounds['music']:setLooping(true)
love.keyboard.keysPressed = {}
end
function love.resize(w, h)
push:resize(w, h)
end
function love.update(dt)
gStateMachine:update(dt)
love.keyboard.keysPressed = {}
end
function love.keypressed(key)
love.keyboard.keysPressed[key] = true
end
function love.keyboard.wasPressed(key)
return love.keyboard.keysPressed[key] and true or false
end
function love.draw()
push:apply('start')
local backgroundWidth = gTextures['background']:getWidth()
local backgroundHeight = gTextures['background']:getHeight()
love.graphics.draw(gTextures['background'],
0, 0,
0,
VIRTUAL_WIDTH / (backgroundWidth - 1), VIRTUAL_HEIGHT / (backgroundHeight - 1))
gStateMachine:render()
displayFPS()
push:apply('end')
end
function displayFPS()
love.graphics.setFont(gFonts['small'])
love.graphics.setColor(0, 1, 0, 1)
love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 5, 5)
end
function renderScore(score)
love.graphics.setFont(gFonts['small'])
love.graphics.print('Score:', VIRTUAL_WIDTH - 60, 5)
love.graphics.printf(tostring(score), VIRTUAL_WIDTH - 50, 5, 40, 'right')
end
function renderHealth(health)
local healthX = VIRTUAL_WIDTH - 100
for i = 1, health do
love.graphics.draw(
gTextures['hearts'],
gFrames['hearts'][1],
healthX, 4
)
healthX = healthX + 11
end
for i = 1, 3 - health do
love.graphics.draw(
gTextures['hearts'],
gFrames['hearts'][2],
healthX, 4
)
end
end
function loadHighScores()
love.filesystem.setIdentity('breakout')
if not love.filesystem.getInfo('breakout.lst') then
local scores = ''
for i = 10, 1, -1 do
scores = scores .. 'CTO\n'
scores = scores .. tostring(i * 1000) .. '\n'
end
love.filesystem.write('breakout.lst', scores)
end
local name = true
local currentName = nil
local counter = 1
local scores = {}
for i = 1, 10 do
scores[i] = {
name = nil,
score = nil
}
end
for line in love.filesystem.lines('breakout.lst') do
if name then
scores[counter].name = string.sub(line, 1, 3)
else
scores[counter].score = tonumber(line)
counter = counter + 1
end
name = not name
end
return scores
end