-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid
398 lines (305 loc) · 8.09 KB
/
Grid
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
--!strict
--Tentaizu
--[[
Acts as a container for all information about the grid, including storage system for Mine objects.
--]]
local player = game.Players.LocalPlayer
local starterPlayerScripts = player.PlayerScripts
local modules = starterPlayerScripts.Scripts.Modules
local replicatedStorage = game.ReplicatedStorage
local gridFolder = game.Workspace.Grid
local camera = game.Workspace.CurrentCamera
local mine = require(script:WaitForChild("Mine"))
local visuals = require(modules:WaitForChild("WorldInteraction"):WaitForChild("Visuals"))
local Janitor = require(replicatedStorage.Janitor)
local upperbounds = gridFolder.Upperbounds.Position
local lowerbounds = gridFolder.Lowerbounds.Position
local grid = {}
grid.__index = grid
local BOMBCOUNT = 40
local PADDING = 3
local OBJECTS = Vector3.new(13, 5, 5)
--local Mines = table.create(OBJECTS.X, table.create(OBJECTS.Y, table.create(OBJECTS.Z, nil)))
local Mines = {}
for x = 1, OBJECTS.X do
Mines[x] = {}
for y = 1, OBJECTS.Y do
Mines[x][y] = {}
for z = 1, OBJECTS.Z do
Mines[x][y][z] = nil--[insert data here]
end
end
end
--[[Start of local functions]]
--[[
Determines if a certain point is visible on screen.
@param pos
@return boolean
--]]
local function isVisible(pos : Vector3)
local _, isOnScreen = camera:WorldToScreenPoint(pos)
return isOnScreen
end
--[[
Determines if a position meets the constraints put forth by the grid
@param pos
@return boolean
--]]
local function isValidPos(pos : Vector3)
if pos.X <= 0 or pos.X > OBJECTS.X then
return false
end
if pos.Y <= 0 or pos.Y > OBJECTS.Y then
return false
end
if pos.Z <= 0 or pos.Z > OBJECTS.Z then
return false
end
if not isVisible(pos) then
return false
end
return true
end
--[[
Will update all neighbors of the cell at pos to accomodate for pos being a 'bomb'
@param pos
@return
--]]
local function updateNeighbors(pos : Vector3)
for x = pos.X - 1, pos.X + 1 do
for y = pos.Y - 1, pos.Y + 1 do
for z = pos.Z - 1, pos.Z + 1 do
if isValidPos(Vector3.new(x, y, z)) then
local mine = Mines[x][y][z]
mine.Neighbors += 1
end
end
end
end
end
--[[
Randomly place bombs across the grid
@param
@return
--]]
local function generateBombs()
local randomVectors = {}
local random = Random.new(math.floor(tick() / math.pi * 1000)) --Sets the seed
--Generates the bombs
local i = BOMBCOUNT
while i > 0 do
--The location of the bomb
local vec = Vector3.new(
random:NextInteger(1, OBJECTS.X),
random:NextInteger(1, OBJECTS.Y),
random:NextInteger(1, OBJECTS.Z))
--If location is not already a bomb, make it a bomb
if not table.find(randomVectors, vec) then
table.insert(randomVectors, vec)
--Assigns the status
Mines[vec.X][vec.Y][vec.Z].Bomb = true
--Updates neighboring tiles
updateNeighbors(vec)
i -= 1
end
end
end
--[[
Determines if a certain mine is visible or not
@param mine : Mine
@return boolean
--]]
local function isMineCovered(mine)
--'mine' can be a function value from Grid
if typeof(mine) ~= "table" then
warn("Invalid Parameter Given")
return false
end
if mine.isVisible or mine.Flagged then
return false
end
return true
end
local function getNeighbors(pos : Vector3) : {Vector3}
local neighbors = {}
for x = pos.X - 1, pos.X + 1, 1 do
for y = pos.Y - 1, pos.Y + 1, 1 do
for z = pos.Z - 1, pos.Z + 1, 1 do
local position = Vector3.new(x, y, z)
if pos ~= position and isValidPos(position) then
table.insert(neighbors, position)
end
end
end
end
return neighbors
end
--[[End of local functions]]
--[[Start of global functions]]
--[[
Places the number of appropriate mines into an evenly spaced grid.
@param
@return
--]]
function grid:placeCells()
local totalSpace = upperbounds - lowerbounds
local paddingVector = Vector3.new(PADDING, PADDING, PADDING)
local cellSize = (totalSpace -((OBJECTS + Vector3.new(1,1,1)) * (paddingVector))) / OBJECTS
if cellSize.X <= 0 or cellSize.Y <= 0 or cellSize.Z <= 0 then
warn("The given grid is too small for the number of objects given.", "The cell size is ", tostring(cellSize))
end
for x = 1, OBJECTS.X do
for y = 1, OBJECTS.Y do
for z = 1, OBJECTS.Z do
local index = Vector3.new(x, y, z)
--local sizePos = ((index + Vector3.new(.5,.5,.5)) * cellSize)
local pos = (paddingVector * index) + (cellSize / 2) + ((index - Vector3.one) * cellSize) + lowerbounds
local cf = CFrame.new(pos)
local name = tostring(x) .." " .. tostring(y) .. " " .. tostring(z)
local mine = mine.new()
local part = mine.Part
part.CFrame = cf
part.Size = cellSize
part.Name = name
part.Transparency = 0
part.Parent = gridFolder
mine.Part = part
Mines[x][y][z] = self._janitor:Add(mine, "Destroy")
end
end
end
generateBombs()
end
function grid:determineNearestMine(pos : Vector3) : Vector3?
--I could offset the position by the lowerbounds, and then divide by the total size of cell, and then round to get the cell.
local totalSpace = upperbounds - lowerbounds
local offset = pos - lowerbounds
local paddingVector = Vector3.new(PADDING, PADDING, PADDING)
local cellSize = (totalSpace -((OBJECTS + Vector3.new(1,1,1)) * (paddingVector))) / OBJECTS
--if offset.X == 0 or offset.Y == 0 or offset.Z == 0 then
-- warn("Division by zero would have occured. Terminating thread")
-- return;
--end
local cellMath = offset / cellSize + Vector3.new(.0001, .0001, .0001)
local cell = Vector3.new(
math.ceil(cellMath.X),
math.ceil(cellMath.Y),
math.ceil(cellMath.Z)
)
if isValidPos(cell) then
return cell
end
warn("Unable to determine the nearest mine. Terminating thread")
return nil;
end
--[[
Switches a mine's visibility and handles all of the behind the scenes of flagging.
@param mousePos : Vector3
@return
--]]
function grid:uncoverMineAtMousePos(mousePos : Vector3)
local pos = self:determineNearestMine(mousePos); if not pos then return end
self:uncoverMineAtCell(pos)
end
function grid:uncoverMineAtCell(pos : Vector3)
local mine = self:getMine(pos); if not mine then return end;
if mine.Flagged or mine.isVisible then
return
end
if mine.Bomb then
visuals.playerLose(mine)
return
end
mine.isVisible = true
if (mine.Neighbors == 0) then
--Autoremove adjacent cells
local neighbors = getNeighbors(pos)
for i, v in pairs(neighbors) do
self:uncoverMineAtCell(v)
end
end
----Fire visual script
visuals.UpdateMines(mine)
end
--[[
Returns the mine at a certain position
@param pos : vector3
@return Mine
]]
function grid:getMine(pos : Vector3)
return Mines[pos.X][pos.Y][pos.Z]
end
--[[
Toggle flag for the cell
@param pos
@return
--]]
function grid:FlagMine(pos : Vector3)
local position = self:determineNearestMine(pos)
if position == nil then
return
end
local mine = self:getMine(position)
print(mine.Part:GetFullName())
if not mine.isVisible then
mine.Flagged = not mine.Flagged
visuals.flagMine(mine)
end
end
--[[
Determines the number of non-visible mines left.
@param
@return int
--]]
function grid:getNumberOfCoveredCells()
local count = 0
for x = 1, #Mines do
for y = 1, #Mines[x] do
for z = 1, #Mines[x][y] do
if isMineCovered(Mines[x][y][z]) then
count += 1
end
end
end
end
return count
end
function grid:squashThirdDimension()
OBJECTS.Y = 1
end
function grid:getSize() : Vector3
return OBJECTS
end
--[[
Creates a new Grid object. Contains all properties of Grid, and also contains all mines in a grid.
@param
@return Grid
--]]
function grid.new()
--create grid
return setmetatable({
_janitor = Janitor.new(),
}, grid)
end
--[[
Clean up grid. Ends up cleaning up all 'Mine' objects as well.
@param
@return
]]
function grid:Destroy()
print("Destroying Grid")
local janitor = self._janitor
if janitor then
janitor:Destory()
self._janitor = nil
end
end
--function grid:GetCenter() : Vector3
-- return center
--end
--[[End of global functions]]
--[[
If I want to transform this script to allow for 3d, what I could do is
I can also set up inheritance, so that I can have all forms of grids inherit similar structure.
]]
return grid