Repository navigation
Expand file tree
/
Copy pathmain.lua
More file actions
290 lines (267 loc) · 10.1 KB
/
Copy pathmain.lua
File metadata and controls
290 lines (267 loc) · 10.1 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then require("lldebugger").start() end
local VERSION = require('version')
local GAME = require('game')
local ENUMS = require('enums')
local MENU = require('menu')
local LEVEL_SELECT = require('level.select')
local LEVEL = require('level.main')
local SOUNDS = require('lib.sounds')
local SETTINGS = require('settings')
local MAPS = require('level.maps')
local FLOWFIELD = require('flowField')
local UTIL = require('level.util')
local TICK = require('lib.tick')
local SETTINGSMENU = require('settingsMenu')
local GAMEOVER = require('level.gameover')
local WINNER = require('level.winner')
local WAVES = require('level.waves')
local SAVE = require('lib.save')
local game
---initialize function
function love.load(arg)
local saveData = SAVE:load()
love.window.setTitle("Xenotactic v"..VERSION)
love.window.setVSync(1)
if SETTINGS.isMobile() then
SETTINGS.easyPlacementMode = true
local nativeWidth, nativeHeight = love.window.getDesktopDimensions()
love.window.setMode(nativeWidth, nativeHeight, {
fullscreen = true,
fullscreentype = "desktop",
resizable = false,
highdpi = true
})
--figure best resolution for current size 20:9
for _,resolution in ipairs(ENUMS.Resolutions) do
if nativeWidth >= resolution.w and nativeHeight >= resolution.h then
saveData.width = resolution.w
saveData.height = resolution.h
break
end
end
--create canvas with smaller resolution 16:9
SETTINGS.CANVAS.GAME = love.graphics.newCanvas(saveData.width, saveData.height)
SETTINGS:setResolution(saveData.width, saveData.height)
--offset to fit canvas into native screen
local renderWidth, renderHeight = love.graphics.getDimensions()
SETTINGS.CANVAS.SCALE = math.min(renderWidth / saveData.width, renderHeight / saveData.height)
SETTINGS.CANVAS.OFFSET.x = (renderWidth - saveData.width * SETTINGS.CANVAS.SCALE) / 2
SETTINGS.CANVAS.OFFSET.y = (renderHeight - saveData.height * SETTINGS.CANVAS.SCALE) / 2
else
love.window.setMode(saveData.width, saveData.height)
SETTINGS:setResolution(saveData.width, saveData.height)
SETTINGS.CANVAS.GAME = nil
end
--enforce 60 FPS for now
TICK.framerate = 60
love.audio.setVolume(saveData.volume)
game = GAME.initGame(saveData.level)
love.graphics.setNewFont("/assets/fonts/11_Visitor_TT1_BRK.ttf", 20)
MENU.load()
end
---update the state of the game every frame
---@param dt number delta between last frame and current
function love.update(dt)
if game.state == ENUMS.STATES.MENU then
MENU.update()
elseif game.state == ENUMS.STATES.LEVEL_SELECT then
LEVEL_SELECT:update()
elseif game.state == ENUMS.STATES.GAME then
LEVEL.update(game, dt)
elseif game.state == ENUMS.STATES.SETTINGS then
SETTINGSMENU:update(dt)
elseif game.state == ENUMS.STATES.GAME_OVER then
GAMEOVER:update()
elseif game.state == ENUMS.STATES.LEVEL_WIN then
WINNER:update(game)
end
end
---transpose canvas coordinates
local function screenToCanvasCoords(x,y)
if not SETTINGS.CANVAS.GAME then
return x,y
end
local cx = (x - SETTINGS.CANVAS.OFFSET.x) / SETTINGS.CANVAS.SCALE
local cy = (y - SETTINGS.CANVAS.OFFSET.y) / SETTINGS.CANVAS.SCALE
return cx, cy
end
---left or right mouse button is clicked
---@param x number x coordinate
---@param y number y coordinate
---@param mouseButton ENUMS.CLICK
function love.mousepressed(x, y, mouseButton)
local cx, cy = screenToCanvasCoords(x,y)
if game.state == ENUMS.STATES.MENU then
if mouseButton == ENUMS.CLICK.LEFT then
for key,button in pairs(MENU.Buttons) do
if button:clicked(cx, cy, mouseButton) then
if key == "PLAY" then
(SOUNDS.library["button_press"]):play()
LEVEL_SELECT:load()
game.state = ENUMS.STATES.LEVEL_SELECT
print("Moving game state to level select")
elseif key == "SETTINGS" then
SOUNDS.library["button_press"]:play()
SETTINGSMENU:load()
game.state = ENUMS.STATES.SETTINGS
elseif key == "QUIT" then
love.event.quit()
end
end
end
end
elseif game.state == ENUMS.STATES.LEVEL_SELECT then
if mouseButton == ENUMS.CLICK.LEFT then
for index, level in pairs(LEVEL_SELECT.Levels) do
if level:clicked(cx, cy, mouseButton) then
--load level
(SOUNDS.library["button_press2"]):play()
local map = MAPS["level_"..index]
WAVES:load()
local waves = WAVES["level"..index]
if index == 1 then
game.gameState = GAME.newGame(
60,
index,
UTIL:deepCopy(map),
waves,
FLOWFIELD:new(UTIL:deepCopy(map), ENUMS.FLOWFIELD.LONGITUDE)
)
else
game.gameState = GAME.newGame(
60,
index,
UTIL:deepCopy(map),
waves,
FLOWFIELD:new(UTIL:deepCopy(map), ENUMS.FLOWFIELD.LONGITUDE),
FLOWFIELD:new(UTIL:deepCopy(map), ENUMS.FLOWFIELD.LATITUDE)
)
end
local TILE_SET = MAPS.VISUAL_MAP["level_"..index]
LEVEL.load(index, TILE_SET)
game.state = ENUMS.STATES.GAME
print("Selected level: " .. index)
break
else
LEVEL_SELECT:mousepressed(cx, cy, mouseButton, game)
end
end
end
elseif game.state == ENUMS.STATES.SETTINGS then
SETTINGSMENU:mousepressed(cx, cy, mouseButton, game)
elseif game.state == ENUMS.STATES.GAME then
--game logic
LEVEL.mousepressed(game, cx, cy, mouseButton)
elseif game.state == ENUMS.STATES.LEVEL_WIN then
if mouseButton == ENUMS.CLICK.LEFT then
game.state = ENUMS.STATES.LEVEL_SELECT
LEVEL_SELECT:load()
end
elseif game.state == ENUMS.STATES.GAME_OVER then
if mouseButton == ENUMS.CLICK.LEFT then
game.state = ENUMS.STATES.LEVEL_SELECT
LEVEL_SELECT:load()
end
end
end
---draw on the screen every frame
function love.draw()
if SETTINGS.CANVAS.GAME then
love.graphics.setCanvas(SETTINGS.CANVAS.GAME)
love.graphics.clear()
end
if game.state == ENUMS.STATES.MENU then
MENU.draw()
elseif game.state == ENUMS.STATES.LEVEL_SELECT then
LEVEL_SELECT:draw(game)
elseif game.state == ENUMS.STATES.GAME then
LEVEL.draw(game.gameState)
elseif game.state == ENUMS.STATES.SETTINGS then
SETTINGSMENU:draw()
elseif game.state == ENUMS.STATES.GAME_OVER then
GAMEOVER:draw()
elseif game.state == ENUMS.STATES.LEVEL_WIN then
WINNER:draw()
else
--error
end
if SETTINGS.CANVAS.GAME then
love.graphics.setCanvas()
love.graphics.clear(0,0,0,1)
love.graphics.setColor(1,1,1)
love.graphics.draw(SETTINGS.CANVAS.GAME, SETTINGS.CANVAS.OFFSET.x, SETTINGS.CANVAS.OFFSET.y, 0, SETTINGS.CANVAS.SCALE, SETTINGS.CANVAS.SCALE)
end
end
function love.textinput(text)
if game.state == ENUMS.STATES.SETTINGS then
SETTINGSMENU:textinput(text)
end
end
function love.keypressed(key)
if game.state == ENUMS.STATES.GAME then
LEVEL:keypressed(key)
elseif game.state == ENUMS.STATES.LEVEL_SELECT then
if key == "escape" then
game.state = ENUMS.STATES.MENU
end
elseif game.state == ENUMS.STATES.SETTINGS then
if key == "escape" then
MENU.load()
game.state = ENUMS.STATES.MENU
end
SETTINGSMENU:keyPressed(key)
elseif game.state == ENUMS.STATES.GAME_OVER then
if key == "escape" then
game.state = ENUMS.STATES.LEVEL_SELECT
LEVEL_SELECT:load()
end
elseif game.state == ENUMS.STATES.LEVEL_WIN then
if key == "escape" then
game.state = ENUMS.STATES.LEVEL_SELECT
LEVEL_SELECT:load()
end
end
end
function love.keyreleased(key)
if game.state == ENUMS.STATES.GAME then
LEVEL:keyreleased(key)
end
end
function love.mousereleased(x,y,button)
local cx, cy = screenToCanvasCoords(x, y)
if game.state == ENUMS.STATES.GAME then
LEVEL:mousereleased(cx,cy,button)
end
end
function love.mousemoved(x,y,dx,dy)
local cx, cy = screenToCanvasCoords(x, y)
local cdx = (1/SETTINGS.CANVAS.SCALE) * dx
local cdy = (1/SETTINGS.CANVAS.SCALE) * dy
if game.state == ENUMS.STATES.GAME then
LEVEL:mousemoved(game.gameState,cx,cy,cdx,cdy)
end
end
function love.touchpressed(id, x, y, dx, dy, pressure)
local cx, cy = screenToCanvasCoords(x, y)
local cdx = (1/SETTINGS.CANVAS.SCALE) * dx
local cdy = (1/SETTINGS.CANVAS.SCALE) * dy
if game.state == ENUMS.STATES.GAME then
LEVEL:touchpressed(game, id, cx, cy, cdx, cdy, pressure)
end
end
function love.touchreleased(id, x, y, dx, dy, pressure)
local cx, cy = screenToCanvasCoords(x, y)
local cdx = (1/SETTINGS.CANVAS.SCALE) * dx
local cdy = (1/SETTINGS.CANVAS.SCALE) * dy
if game.state == ENUMS.STATES.GAME then
LEVEL:touchreleased(id, cx, cy, cdx, cdy, pressure)
end
end
function love.touchmoved(id, x, y, dx, dy, pressure)
local cx, cy = screenToCanvasCoords(x, y)
local cdx = (1/SETTINGS.CANVAS.SCALE) * dx
local cdy = (1/SETTINGS.CANVAS.SCALE) * dy
if game.state == ENUMS.STATES.GAME then
LEVEL:touchmoved(id, cx, cy, cdx, cdy, pressure)
end
end