Skip to content

Commit c5362d9

Browse files
committed
Converted BlockMap to abstract data type.
1 parent b9fc64f commit c5362d9

3 files changed

Lines changed: 166 additions & 144 deletions

File tree

Nu/Nu/BlockMap/BlockMap.fs

Lines changed: 156 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -195,162 +195,183 @@ type Config =
195195
static member initial =
196196
{ CastShadows = true }
197197

198-
type [<SymbolicExpansion>] BlockMap =
199-
{ Generated : bool
200-
EditPlane : EditPlane // plane currently containing cursor
201-
LayersVisible : int
202-
Cursor : Vector3i
203-
Selection : Selection
204-
Palette : Palette
205-
PaletteSelection : int
206-
PaintHeight : int
207-
Passes : Map<string, Pass>
208-
Config : Config
209-
Scale : Vector3
210-
Chunk : Chunk }
211-
212-
(* Properties *)
213-
214-
member this.Size =
215-
this.Chunk.BoundsI.Size.V3 * this.Scale
198+
[<RequireQualifiedAccess>]
199+
module BlockMap =
200+
201+
type [<SymbolicExpansion>] BlockMap =
202+
private
203+
{ Generated_ : bool
204+
EditPlane_ : EditPlane // plane currently containing cursor
205+
LayersVisible_ : int
206+
Cursor_ : Vector3i
207+
Selection_ : Selection
208+
Palette_ : Palette
209+
PaletteSelection_ : int
210+
PaintHeight_ : int
211+
Passes_ : Map<string, Pass>
212+
Config_ : Config
213+
Scale_ : Vector3
214+
Chunk_ : Chunk }
215+
216+
(* Properties *)
217+
218+
member this.Generated = this.Generated_
219+
member this.EditPlane = this.EditPlane_
220+
member this.LayersVisible = this.LayersVisible_
221+
member this.Cursor = this.Cursor_
222+
member this.Selection = this.Selection_
223+
member this.Palette = this.Palette_
224+
member this.PaletteSelection = this.PaletteSelection_
225+
member this.PaintHeight = this.PaintHeight_
226+
member this.Passes = this.Passes_
227+
member this.Config = this.Config_
228+
member this.Scale = this.Scale_
229+
member this.Size = this.Chunk_.BoundsI.Size.V3 * this.Scale_
230+
member this.Chunk = this.Chunk_
216231

217232
(* Low-Level API *)
218233

219-
static member setEditPlane plane blockMap =
220-
{ blockMap with EditPlane = plane }
234+
let setGenerated generated blockMap =
235+
{ blockMap with Generated_ = generated }
236+
237+
let mapGenerated mapper blockMap =
238+
{ blockMap with Generated_ = mapper blockMap.Generated_ }
221239

222-
static member mapEditPlane mapper blockMap =
223-
{ blockMap with EditPlane = mapper blockMap.EditPlane }
240+
let setEditPlane plane blockMap =
241+
{ blockMap with EditPlane_ = plane }
224242

225-
static member setLayersVisible layersVisible blockMap =
226-
{ blockMap with LayersVisible = layersVisible }
243+
let mapEditPlane mapper blockMap =
244+
{ blockMap with EditPlane_ = mapper blockMap.EditPlane_ }
227245

228-
static member mapLayersVisible mapper blockMap =
229-
{ blockMap with LayersVisible = mapper blockMap.LayersVisible }
246+
let setLayersVisible layersVisible blockMap =
247+
{ blockMap with LayersVisible_ = layersVisible }
230248

231-
static member setCursor (cursor : Vector3i) blockMap =
232-
if blockMap.Chunk.BoundsI.ContainsExclusive cursor = ContainmentType.Disjoint then
249+
let mapLayersVisible mapper blockMap =
250+
{ blockMap with LayersVisible_ = mapper blockMap.LayersVisible_ }
251+
252+
let setCursor (cursor : Vector3i) blockMap =
253+
if blockMap.Chunk_.BoundsI.ContainsExclusive cursor = ContainmentType.Disjoint then
233254
failwith "Block cursor position must be within the block map chunk bounds."
234-
{ blockMap with Cursor = cursor }
255+
{ blockMap with Cursor_ = cursor }
235256

236-
static member mapCursor mapper blockMap =
237-
{ blockMap with Cursor = mapper blockMap.Cursor }
257+
let mapCursor mapper blockMap =
258+
{ blockMap with Cursor_ = mapper blockMap.Cursor_ }
238259

239-
static member setSelection selection blockMap =
260+
let setSelection selection blockMap =
240261
// TODO: check selection for appropriate boundedness.
241-
{ blockMap with Selection = selection }
262+
{ blockMap with Selection_ = selection }
242263

243-
static member mapSelection mapper blockMap =
244-
{ blockMap with Selection = mapper blockMap.Selection }
264+
let mapSelection mapper blockMap =
265+
{ blockMap with Selection_ = mapper blockMap.Selection_ }
245266

246-
static member setPalette palette blockMap =
267+
let setPalette palette blockMap =
247268
if palette.Styles.Length = 0 then
248269
failwith "Block palette must contain at least one block style."
249270
let paletteSelection =
250-
if blockMap.PaletteSelection < Array.length palette.Styles
251-
then blockMap.PaletteSelection
271+
if blockMap.PaletteSelection_ < Array.length palette.Styles
272+
then blockMap.PaletteSelection_
252273
else 0
253274
{ blockMap with
254-
Palette = palette
255-
PaletteSelection = paletteSelection }
275+
Palette_ = palette
276+
PaletteSelection_ = paletteSelection }
256277

257-
static member mapPalette mapper blockMap =
258-
let palette = mapper blockMap.Palette
259-
BlockMap.setPalette palette blockMap
278+
let mapPalette mapper blockMap =
279+
let palette = mapper blockMap.Palette_
280+
setPalette palette blockMap
260281

261-
static member setPaletteSelection paletteSelection blockMap =
262-
if paletteSelection < 0 || paletteSelection >= Array.length blockMap.Palette.Styles then
282+
let setPaletteSelection paletteSelection blockMap =
283+
if paletteSelection < 0 || paletteSelection >= Array.length blockMap.Palette_.Styles then
263284
failwith "Block palette selection must be within the range of the block palette styles."
264-
{ blockMap with PaletteSelection = paletteSelection }
285+
{ blockMap with PaletteSelection_ = paletteSelection }
265286

266-
static member mapPaletteSelection mapper blockMap =
267-
let paletteSelection = mapper blockMap.PaletteSelection
268-
BlockMap.setPaletteSelection paletteSelection blockMap
287+
let mapPaletteSelection mapper blockMap =
288+
let paletteSelection = mapper blockMap.PaletteSelection_
289+
setPaletteSelection paletteSelection blockMap
269290

270-
static member setPaintHeight paintHeight blockMap =
271-
{ blockMap with PaintHeight = max 1 paintHeight }
291+
let setPaintHeight paintHeight blockMap =
292+
{ blockMap with PaintHeight_ = max 1 paintHeight }
272293

273-
static member mapPaintHeight mapper blockMap =
274-
let paintHeight = mapper blockMap.PaintHeight
275-
BlockMap.setPaintHeight paintHeight blockMap
294+
let mapPaintHeight mapper blockMap =
295+
let paintHeight = mapper blockMap.PaintHeight_
296+
setPaintHeight paintHeight blockMap
276297

277-
static member setPasses passes blockMap =
278-
{ blockMap with Passes = passes }
298+
let setPasses passes blockMap =
299+
{ blockMap with Passes_ = passes }
279300

280-
static member mapPasses mapper blockMap =
281-
let passes = mapper blockMap.Passes
282-
BlockMap.setPasses passes blockMap
301+
let mapPasses mapper blockMap =
302+
let passes = mapper blockMap.Passes_
303+
setPasses passes blockMap
283304

284-
static member setConfig config blockMap =
285-
{ blockMap with Config = config }
305+
let setConfig config blockMap =
306+
{ blockMap with Config_ = config }
286307

287-
static member mapConfig mapper blockMap =
288-
let config = mapper blockMap.Config
289-
BlockMap.setConfig config blockMap
308+
let mapConfig mapper blockMap =
309+
let config = mapper blockMap.Config_
310+
setConfig config blockMap
290311

291-
static member setScale scale blockMap =
292-
{ blockMap with Scale = scale }
312+
let setScale scale blockMap =
313+
{ blockMap with Scale_ = scale }
293314

294-
static member mapScale mapper blockMap =
295-
let scale = mapper blockMap.Scale
296-
BlockMap.setScale scale blockMap
315+
let mapScale mapper blockMap =
316+
let scale = mapper blockMap.Scale_
317+
setScale scale blockMap
297318

298-
static member setChunk chunk blockMap =
319+
let setChunk chunk blockMap =
299320
if chunk.Blocks.Count = 0 then
300321
failwith "Block map chunk must contain a block chunk with at least one block."
301-
{ blockMap with Chunk = chunk }
322+
{ blockMap with Chunk_ = chunk }
302323

303-
static member mapChunk mapper blockMap =
304-
{ blockMap with Chunk = mapper blockMap.Chunk }
324+
let mapChunk mapper blockMap =
325+
{ blockMap with Chunk_ = mapper blockMap.Chunk_ }
305326

306-
static member getBlockOpt positionI blockMap =
307-
Chunk.getBlockOpt positionI blockMap.Chunk
327+
let getBlockOpt positionI blockMap =
328+
Chunk.getBlockOpt positionI blockMap.Chunk_
308329

309-
static member setBlockOpt (positionI : Vector3i) blockOpt blockMap =
310-
{ blockMap with Chunk = Chunk.setBlockOpt positionI blockOpt blockMap.Chunk }
330+
let setBlockOpt (positionI : Vector3i) blockOpt blockMap =
331+
{ blockMap with Chunk_ = Chunk.setBlockOpt positionI blockOpt blockMap.Chunk_ }
311332

312-
static member mapBlockOpt mapper positionI blockMap =
313-
{ blockMap with Chunk = Chunk.mapBlockOpt mapper positionI blockMap.Chunk }
333+
let mapBlockOpt mapper positionI blockMap =
334+
{ blockMap with Chunk_ = Chunk.mapBlockOpt mapper positionI blockMap.Chunk_ }
314335

315-
static member getBlock positionI blockMap =
316-
Chunk.getBlock positionI blockMap.Chunk
336+
let getBlock positionI blockMap =
337+
Chunk.getBlock positionI blockMap.Chunk_
317338

318-
static member setBlock positionI block blockMap =
319-
{ blockMap with Chunk = Chunk.setBlock positionI block blockMap.Chunk }
339+
let setBlock positionI block blockMap =
340+
{ blockMap with Chunk_ = Chunk.setBlock positionI block blockMap.Chunk_ }
320341

321-
static member mapBlock mapper positionI blockMap =
322-
{ blockMap with Chunk = Chunk.mapBlock mapper positionI blockMap.Chunk }
342+
let mapBlock mapper positionI blockMap =
343+
{ blockMap with Chunk_ = Chunk.mapBlock mapper positionI blockMap.Chunk_ }
323344

324345
(* Derived API *)
325346

326-
static member getBounds position (blockMap : BlockMap) =
347+
let getBounds position (blockMap : BlockMap) =
327348
let size = blockMap.Size
328349
Box3 (position - size * 0.5f, size)
329350

330-
static member tryGetStyle styleIndex blockMap =
331-
Palette.tryGetStyle styleIndex blockMap.Palette
351+
let tryGetStyle styleIndex blockMap =
352+
Palette.tryGetStyle styleIndex blockMap.Palette_
332353

333-
static member tryGetSelectedColor blockMap =
334-
match BlockMap.tryGetStyle blockMap.PaletteSelection blockMap with
354+
let tryGetSelectedColor blockMap =
355+
match tryGetStyle blockMap.PaletteSelection_ blockMap with
335356
| Some style -> Some style.Color
336357
| None -> None
337358

338-
static member addPass passName pass blockMap =
339-
{ blockMap with Passes = Map.add passName pass blockMap.Passes }
359+
let addPass passName pass blockMap =
360+
{ blockMap with Passes_ = Map.add passName pass blockMap.Passes_ }
340361

341-
static member removePass passName blockMap =
342-
{ blockMap with Passes = Map.remove passName blockMap.Passes }
362+
let removePass passName blockMap =
363+
{ blockMap with Passes_ = Map.remove passName blockMap.Passes_ }
343364

344-
static member tryGetBlockColor block blockMap =
345-
Block.tryGetColor block blockMap.Palette
365+
let tryGetBlockColor block blockMap =
366+
Block.tryGetColor block blockMap.Palette_
346367

347368
(* High-Level API *)
348369

349-
static member tryPickPositionI (ray : Ray3) blockMapPosition (blockMap : BlockMap) =
350-
let bounds = BlockMap.getBounds blockMapPosition blockMap
351-
match blockMap.EditPlane with
370+
let tryPickPositionI (ray : Ray3) blockMapPosition (blockMap : BlockMap) =
371+
let bounds = getBounds blockMapPosition blockMap
372+
match blockMap.EditPlane_ with
352373
| YNeg | YPos ->
353-
let gridY = single blockMap.Cursor.Y * single blockMap.Scale.Y - bounds.Size.Y * 0.5f
374+
let gridY = single blockMap.Cursor_.Y * single blockMap.Scale_.Y - bounds.Size.Y * 0.5f
354375
let gridCenter = bounds.Center + v3 0.0f gridY 0.0f
355376
let plane = Plane3 (gridCenter, v3Up)
356377
let intersectionTOpt = plane.Intersection ray
@@ -360,16 +381,16 @@ type [<SymbolicExpansion>] BlockMap =
360381
if intersection.X >= 0.0f && intersection.Z >= 0.0f then
361382
let positionI =
362383
v3i
363-
(int (intersection.X / blockMap.Scale.X))
364-
(blockMap.Cursor.Y + if blockMap.EditPlane.IsYNeg then -1 else 0)
365-
(int (intersection.Z / blockMap.Scale.Z))
366-
if blockMap.Chunk.BoundsI.ContainsExclusive positionI <> ContainmentType.Disjoint
384+
(int (intersection.X / blockMap.Scale_.X))
385+
(blockMap.Cursor_.Y + if blockMap.EditPlane_.IsYNeg then -1 else 0)
386+
(int (intersection.Z / blockMap.Scale_.Z))
387+
if blockMap.Chunk_.BoundsI.ContainsExclusive positionI <> ContainmentType.Disjoint
367388
then Some positionI
368389
else None
369390
else None
370391
else None
371392
| XNeg | XPos ->
372-
let gridX = single blockMap.Cursor.X * single blockMap.Scale.X - bounds.Size.X * 0.5f
393+
let gridX = single blockMap.Cursor_.X * single blockMap.Scale_.X - bounds.Size.X * 0.5f
373394
let gridCenter = bounds.Center + v3 gridX 0.0f 0.0f
374395
let plane = Plane3 (gridCenter, v3Right)
375396
let intersectionTOpt = plane.Intersection ray
@@ -379,16 +400,16 @@ type [<SymbolicExpansion>] BlockMap =
379400
if intersection.Y >= 0.0f && intersection.Z >= 0.0f then
380401
let positionI =
381402
v3i
382-
(blockMap.Cursor.X + if blockMap.EditPlane.IsXNeg then -1 else 0)
383-
(int (intersection.Y / blockMap.Scale.Y))
384-
(int (intersection.Z / blockMap.Scale.Z))
385-
if blockMap.Chunk.BoundsI.ContainsExclusive positionI <> ContainmentType.Disjoint
403+
(blockMap.Cursor_.X + if blockMap.EditPlane_.IsXNeg then -1 else 0)
404+
(int (intersection.Y / blockMap.Scale_.Y))
405+
(int (intersection.Z / blockMap.Scale_.Z))
406+
if blockMap.Chunk_.BoundsI.ContainsExclusive positionI <> ContainmentType.Disjoint
386407
then Some positionI
387408
else None
388409
else None
389410
else None
390411
| ZNeg | ZPos ->
391-
let gridZ = single blockMap.Cursor.Z * single blockMap.Scale.Z - bounds.Size.Z * 0.5f
412+
let gridZ = single blockMap.Cursor_.Z * single blockMap.Scale_.Z - bounds.Size.Z * 0.5f
392413
let gridCenter = bounds.Center + v3 0.0f 0.0f gridZ
393414
let plane = Plane3 (gridCenter, v3Forward)
394415
let intersectionTOpt = plane.Intersection ray
@@ -398,46 +419,48 @@ type [<SymbolicExpansion>] BlockMap =
398419
if intersection.X >= 0.0f && intersection.Y >= 0.0f then
399420
let positionI =
400421
v3i
401-
(int (intersection.X / blockMap.Scale.X))
402-
(int (intersection.Y / blockMap.Scale.Y))
403-
(blockMap.Cursor.Z + if blockMap.EditPlane.IsZNeg then -1 else 0)
404-
if blockMap.Chunk.BoundsI.ContainsExclusive positionI <> ContainmentType.Disjoint
422+
(int (intersection.X / blockMap.Scale_.X))
423+
(int (intersection.Y / blockMap.Scale_.Y))
424+
(blockMap.Cursor_.Z + if blockMap.EditPlane_.IsZNeg then -1 else 0)
425+
if blockMap.Chunk_.BoundsI.ContainsExclusive positionI <> ContainmentType.Disjoint
405426
then Some positionI
406427
else None
407428
else None
408429
else None
409430

410-
static member paint (positionI : Vector3i) blockMap =
411-
match BlockMap.tryGetStyle blockMap.PaletteSelection blockMap with
431+
let paint (positionI : Vector3i) blockMap =
432+
match tryGetStyle blockMap.PaletteSelection_ blockMap with
412433
| Some style ->
413434
List.fold (fun (blockMap : BlockMap) i ->
414435
let offsetI =
415-
match blockMap.EditPlane with
436+
match blockMap.EditPlane_ with
416437
| XPos -> v3i i 0 0
417438
| XNeg -> v3i -i 0 0
418439
| YPos -> v3i 0 i 0
419440
| YNeg -> v3i 0 -i 0
420441
| ZPos -> v3i 0 0 i
421442
| ZNeg -> v3i 0 0 -i
422-
let block = Block.make blockMap.PaletteSelection 0 style.Properties
423-
BlockMap.setBlock (positionI + offsetI) block blockMap)
424-
blockMap [0 .. dec blockMap.PaintHeight]
443+
let block = Block.make blockMap.PaletteSelection_ 0 style.Properties
444+
setBlock (positionI + offsetI) block blockMap)
445+
blockMap [0 .. dec blockMap.PaintHeight_]
425446
| None -> blockMap
426447

427-
static member initial =
448+
let initial =
428449
let chunk = Chunk.initial
429-
{ Generated = false
430-
EditPlane = YPos
431-
LayersVisible = chunk.BoundsI.Max.Y
432-
Cursor = chunk.BoundsI.Size / 2
433-
Selection = Selection.initial
434-
Palette = Palette.initial
435-
PaletteSelection = 0
436-
PaintHeight = 1
437-
Passes = Map.empty
438-
Config = Config.initial
439-
Scale = v3One
440-
Chunk = chunk }
450+
{ Generated_ = false
451+
EditPlane_ = YPos
452+
LayersVisible_ = chunk.BoundsI.Max.Y
453+
Cursor_ = chunk.BoundsI.Size / 2
454+
Selection_ = Selection.initial
455+
Palette_ = Palette.initial
456+
PaletteSelection_ = 0
457+
PaintHeight_ = 1
458+
Passes_ = Map.empty
459+
Config_ = Config.initial
460+
Scale_ = v3One
461+
Chunk_ = chunk }
462+
463+
type BlockMap = BlockMap.BlockMap
441464

442465
[<RequireQualifiedAccess>]
443466
module ProcessFns =

0 commit comments

Comments
 (0)