-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
217 lines (179 loc) · 4.66 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
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
Width = love.graphics.getWidth()
Height = love.graphics.getHeight()
Images = {}
require("lib.30log")
Camera = nil
CameraX = 0
CameraY = 0
Some = require("lib.some.some")
require("args")
require("utils")
---@diagnostic disable-next-line: duplicate-set-field
function love.load()
love.keyboard.setKeyRepeat(true)
require("src.locale")
if not LoadLocale() then
SaveLocale()
end
require("src.stages")
for _, stage in ipairs(LoadStages) do
stage()
end
end
---@diagnostic disable-next-line: duplicate-set-field
function love.update(dt)
PlayTime = PlayTime + dt
for _, stage in ipairs(UpdateStages) do
stage(dt)
end
end
---@diagnostic disable-next-line: duplicate-set-field
function love.draw()
love.graphics.setFont(Font)
for _, stage in ipairs(DrawStages) do
stage()
end
end
---@diagnostic disable-next-line: duplicate-set-field
function love.mousepressed(mouseX, mouseY, button)
if Some.isInputGrabbed() then
Some:mousepressed(mouseX, mouseY, button)
return
end
if UpdateButtons() then
return
end
if mouseX < ButtonColumnCount * ButtonSize then
return
end
if UpdateUtilButtons() then
return
end
if button > 2 then
return
end
local x = mouseX - Width / 2 + CameraX
local y = mouseY - Height / 2 + CameraY
local a = math.ceil(x / CellSize)
local b = math.ceil(y / CellSize)
if a > CellAmount or b > CellAmount or a <= 0 or b <= 0 then
return
end
print(("Mouse %d: %d, %d"):format(button, a, b))
local under =
Cell:new(a, b, Cells[a][b].type, nil, Content:new(Cells[a][b].content.opts, Cells[a][b].content.amount))
local iserase = button == 2
if not iserase and (BuildSelectionNum == 0 or BuildSelection == CellType.NONE) then
return
end
if not iserase then
if
(BuildSelection == GameBuilder.cellTypes.core and IsCorePlased)
or (BuildSelection == CellType.NONE and BuildSelectionNum == 0)
then
goto exit
end
Cells[a][b].content.opts = DEFAULT_CONTENT_TYPE
Cells[a][b].content.amount = 0
Cells[a][b].direction = Rotation
Cells[a][b].type = BuildSelection
if BuildSelection == GameBuilder.cellTypes.core then
IsCorePlased = true
end
::exit::
end
if iserase and Cells[a][b].type ~= CellType.ORE then
if Cells[a][b].type == GameBuilder.cellTypes.core then
IsCorePlased = false
for k, v in pairs(Core) do
if type(v) == "number" then
Core[k] = nil
end
end
end
Cells[a][b].type = CellType.NONE
end
if not iserase and not Cells[a][b].under then
Cells[a][b].under = under
end
Cells[a][b].content.amount = 0
Cells[a][b].progress = 0
Cells[a][b].storage = {}
end
---@param key love.KeyConstant
---@diagnostic disable-next-line: duplicate-set-field
function love.keypressed(key, sc, rep)
if Some.isInputGrabbed() then
Some:keypressed(key, sc, rep)
return
end
-- TODO: Migrate this to keybind system(probably arrow keys)
if string.byte(key, 1, 1) >= string.byte("1", 1, 1) and string.byte(key, 1, 1) <= string.byte("9", 1, 1) then
local num = string.byte(key, 1, 1) - 48
if num <= #BuildableCellTypes then
BuildSelection = BuildableCellTypes[num]
BuildSelectionNum = num
end
return
end
local bind = KeyboardBinds[key]
if bind then
bind.callback()
end
end
---@diagnostic disable-next-line: duplicate-set-field
function love.resize()
Width = love.graphics.getWidth()
Height = love.graphics.getHeight()
for k, utilButton in ipairs(UtilButtons) do
utilButton.x = Width - ButtonSize * k
end
end
function love.mousemoved(x, y, dx, dy)
Some:mousemoved(x, y, dx, dy)
end
function love.textinput(t)
if Some.isInputGrabbed() then
Some:textinput(t)
return
end
end
-- TODO: Save storage and progress
function saveGame()
local s = ("return ({{ playTime = %f, seed = %d, generation = %d }, {\n"):format(
PlayTime,
love.math.getRandomSeed(),
MapGeneration
)
for x, _ in pairs(Cells) do
s = s .. "{\n"
for _, cell in pairs(Cells[x]) do
s = s .. CellToString(cell) .. ",\n"
end
s = s .. "},\n"
end
s = s .. "}})"
local ok, msg = love.filesystem.write("savedata.lua", s)
if not ok then
love.window.showMessageBox("Saving error!", "Failed to save: " .. msg, "error")
else
love.window.showMessageBox("Saved!", "Successfully saved!")
end
end
function loadGame()
local button = love.window.showMessageBox(
"Load",
"Do you really want to load the last save?\nAll unsaved progress will be lost!",
{ "Yes", "No", enterbutton = 1, escapebutton = 2 }
)
if button == 1 then
local savedata = require("savedata")
local info = savedata[1]
local save = savedata[2]
PlayTime = info.playTime or 0
love.math.setRandomSeed(info.seed or 0)
MapGeneration = info.generation or 1
Cells = save
love.window.showMessageBox("Loaded!", "Lastest save loaded!")
end
end