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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
|
namespace LivingVillage.Desktop
open System
open Microsoft.Xna.Framework
open Microsoft.Xna.Framework.Graphics
open Microsoft.Xna.Framework.Input
open LivingVillage.Kernel
open LivingVillage.Kernel.Sim
open LivingVillage.Desktop.ChineseText
open LivingVillage.Desktop.VillagePresentation
module PixelText =
let private glyphs =
[ 'A', [| "01110"; "10001"; "10001"; "11111"; "10001"; "10001"; "10001" |]
'B', [| "11110"; "10001"; "10001"; "11110"; "10001"; "10001"; "11110" |]
'C', [| "01111"; "10000"; "10000"; "10000"; "10000"; "10000"; "01111" |]
'D', [| "11110"; "10001"; "10001"; "10001"; "10001"; "10001"; "11110" |]
'E', [| "11111"; "10000"; "10000"; "11110"; "10000"; "10000"; "11111" |]
'F', [| "11111"; "10000"; "10000"; "11110"; "10000"; "10000"; "10000" |]
'G', [| "01111"; "10000"; "10000"; "10111"; "10001"; "10001"; "01111" |]
'H', [| "10001"; "10001"; "10001"; "11111"; "10001"; "10001"; "10001" |]
'I', [| "11111"; "00100"; "00100"; "00100"; "00100"; "00100"; "11111" |]
'J', [| "00111"; "00010"; "00010"; "00010"; "00010"; "10010"; "01100" |]
'K', [| "10001"; "10010"; "10100"; "11000"; "10100"; "10010"; "10001" |]
'L', [| "10000"; "10000"; "10000"; "10000"; "10000"; "10000"; "11111" |]
'M', [| "10001"; "11011"; "10101"; "10101"; "10001"; "10001"; "10001" |]
'N', [| "10001"; "11001"; "10101"; "10011"; "10001"; "10001"; "10001" |]
'O', [| "01110"; "10001"; "10001"; "10001"; "10001"; "10001"; "01110" |]
'P', [| "11110"; "10001"; "10001"; "11110"; "10000"; "10000"; "10000" |]
'Q', [| "01110"; "10001"; "10001"; "10001"; "10101"; "10010"; "01101" |]
'R', [| "11110"; "10001"; "10001"; "11110"; "10100"; "10010"; "10001" |]
'S', [| "01111"; "10000"; "10000"; "01110"; "00001"; "00001"; "11110" |]
'T', [| "11111"; "00100"; "00100"; "00100"; "00100"; "00100"; "00100" |]
'U', [| "10001"; "10001"; "10001"; "10001"; "10001"; "10001"; "01110" |]
'V', [| "10001"; "10001"; "10001"; "10001"; "10001"; "01010"; "00100" |]
'W', [| "10001"; "10001"; "10001"; "10101"; "10101"; "11011"; "10001" |]
'X', [| "10001"; "10001"; "01010"; "00100"; "01010"; "10001"; "10001" |]
'Y', [| "10001"; "10001"; "01010"; "00100"; "00100"; "00100"; "00100" |]
'Z', [| "11111"; "00001"; "00010"; "00100"; "01000"; "10000"; "11111" |]
'0', [| "01110"; "10001"; "10011"; "10101"; "11001"; "10001"; "01110" |]
'1', [| "00100"; "01100"; "00100"; "00100"; "00100"; "00100"; "01110" |]
'2', [| "01110"; "10001"; "00001"; "00010"; "00100"; "01000"; "11111" |]
'3', [| "11110"; "00001"; "00001"; "01110"; "00001"; "00001"; "11110" |]
'4', [| "00010"; "00110"; "01010"; "10010"; "11111"; "00010"; "00010" |]
'5', [| "11111"; "10000"; "10000"; "11110"; "00001"; "00001"; "11110" |]
'6', [| "01110"; "10000"; "10000"; "11110"; "10001"; "10001"; "01110" |]
'7', [| "11111"; "00001"; "00010"; "00100"; "01000"; "01000"; "01000" |]
'8', [| "01110"; "10001"; "10001"; "01110"; "10001"; "10001"; "01110" |]
'9', [| "01110"; "10001"; "10001"; "01111"; "00001"; "00001"; "01110" |]
('-', [| "00000"; "00000"; "00000"; "11111"; "00000"; "00000"; "00000" |])
('.', [| "00000"; "00000"; "00000"; "00000"; "00000"; "00110"; "00110" |])
('=', [| "00000"; "11111"; "00000"; "00000"; "11111"; "00000"; "00000" |])
(':', [| "00000"; "00110"; "00110"; "00000"; "00110"; "00110"; "00000" |])
(';', [| "00000"; "00110"; "00110"; "00000"; "00110"; "00100"; "01000" |])
('/', [| "00001"; "00010"; "00010"; "00100"; "01000"; "01000"; "10000" |])
('>', [| "10000"; "01000"; "00100"; "00010"; "00100"; "01000"; "10000" |])
('?', [| "01110"; "10001"; "00001"; "00010"; "00100"; "00000"; "00100" |]) ]
|> Map.ofList
let isRenderable (text: string) : bool =
not (isNull text)
&& text
|> Seq.forall (fun character ->
character = ' '
|| Map.containsKey (Char.ToUpperInvariant character) glyphs
|| CjkGlyphAtlas.contains character)
let draw (spriteBatch: SpriteBatch) (pixel: Texture2D) (cjkAtlas: Texture2D) (x: int) (y: int) (scale: int) (color: Color) (text: string) =
let mutable cursor = x
for character in text.ToUpperInvariant() do
if character = ' ' then
cursor <- cursor + (4 * scale)
elif CjkGlyphAtlas.sourceRectangle character |> Option.isSome then
let source = CjkGlyphAtlas.sourceRectangle character |> Option.get
let glyphPixels = 8 * scale
spriteBatch.Draw(
cjkAtlas,
Rectangle(cursor, y, glyphPixels, glyphPixels),
Nullable<Rectangle>(source),
color)
cursor <- cursor + glyphPixels
else
match glyphs |> Map.tryFind character with
| Some rows ->
for row in 0 .. rows.Length - 1 do
for column in 0 .. rows.[row].Length - 1 do
if rows.[row].[column] = '1' then
spriteBatch.Draw(pixel, Rectangle(cursor + column * scale, y + row * scale, scale, scale), color)
cursor <- cursor + (6 * scale)
| None ->
// Unsupported characters are intentionally blank, never a fake glyph.
cursor <- cursor + (6 * scale)
type LivingVillageGame() as this =
inherit Game()
let autoplay = Environment.GetEnvironmentVariable("LV_AUTOPLAY") = "1"
let autoplayFlow = Environment.GetEnvironmentVariable("LV_AUTOPLAY_FLOW") = "1"
let sampleMode = Environment.GetEnvironmentVariable("LV_AUTOPLAY_SAMPLE") = "1"
let daylightAutoplay = Environment.GetEnvironmentVariable("LV_AUTOPLAY_DAYLIGHT") = "1"
let automated = autoplay || autoplayFlow
let modeName =
if sampleMode then "sample"
elif autoplayFlow then "autoplay-flow"
elif autoplay then "autoplay"
else "keyboard"
let configuredStepsPerFrame =
match Environment.GetEnvironmentVariable("LV_STEPS_PER_FRAME") with
| null | "" -> 1
| s ->
match Int32.TryParse s with
| true, n when n > 0 -> n
| _ -> 1
let initialSimulationControl, initialLegacyStepsPerFrame =
SimulationControl.initialForConfiguredSteps configuredStepsPerFrame
let startSimulationControl =
if sampleMode then SimulationControl.setSpeed TwoX initialSimulationControl else initialSimulationControl
let savePath =
match Environment.GetEnvironmentVariable("LV_SAVE_PATH") with
| null | "" -> "living-village.save"
| path -> path
let graphics = new GraphicsDeviceManager(this)
let mutable spriteBatch = Unchecked.defaultof<SpriteBatch>
let mutable artTextures = Unchecked.defaultof<VillageArt.ArtTextures>
let mutable cjkAtlas = Unchecked.defaultof<Texture2D>
let mutable pixel = Unchecked.defaultof<Texture2D>
let renderPlan = VillageArt.sampleRenderPlan ()
let createInitialWorld () =
WorldBootstrap.initialWorldForDaylight daylightAutoplay 42UL Sim.npcCount
let mutable world = createInitialWorld ()
let mutable camera = Vector2.Zero
let mutable avatarFacing = VillageArt.SouthFacing
let mutable fpsFrames = 0
let mutable fpsSeconds = 0.0
let mutable prevKb = Unchecked.defaultof<KeyboardState>
let mutable relationView = false
let mutable relationSnapshot: float32[,] option = None
let mutable m5View = M5Interaction.initial
let mutable simulationControl = startSimulationControl
let mutable legacyStepsPerFrame = (if sampleMode then None else initialLegacyStepsPerFrame)
let mutable menu = MenuState.create startSimulationControl false
let mutable autoplayFrames = 0
let mutable flowStep = 0
let mutable sampleState = SampleScript.initial
do
graphics.PreferredBackBufferWidth <- 1280
graphics.PreferredBackBufferHeight <- 720
graphics.SynchronizeWithVerticalRetrace <- true
this.IsFixedTimeStep <- true
this.TargetElapsedTime <- TimeSpan.FromTicks(TimeSpan.TicksPerSecond / 60L)
this.Window.Title <- ChineseText.copy.SceneTitle
printfn $"mode={modeName} seed=42"
printfn "controls=WASD move E interact 1-6 choose Q observe Tab needs C chronicle L relations P pause F1/F2/F3 speed F6 save F7 load Esc close/exit"
member private this.CenterCamera() =
let viewport = this.GraphicsDevice.Viewport
let vw = float32 viewport.Width
let vh = float32 viewport.Height
let worldW = float32 (Sim.mapWidthTiles * Sim.tilePixels)
let worldH = float32 (Sim.mapHeightTiles * Sim.tilePixels)
let half = float32 Sim.tilePixels / 2.0f
let x = MathHelper.Clamp(world.Avatar.Pos.X + half - vw / 2.0f, 0.0f, max 0.0f (worldW - vw))
let y = MathHelper.Clamp(world.Avatar.Pos.Y + half - vh / 2.0f, 0.0f, max 0.0f (worldH - vh))
camera <- Vector2(x, y)
member private this.ResetWorldView() =
relationView <- false
relationSnapshot <- None
avatarFacing <- VillageArt.SouthFacing
m5View <- M5Interaction.refreshPrompt world M5Interaction.initial
this.CenterCamera()
member private this.StartNewGame() =
world <- createInitialWorld ()
this.ResetWorldView()
menu <- menu |> MenuState.setCurrentWorld true |> MenuState.enterGame
printfn $"new-game=ok seed=42 npcs={Sim.npcCount}"
member private this.SaveWorld (prefix: string) : bool =
try
WorldSave.saveToFile savePath world
m5View <- { m5View with Status = "save succeeded" }
printfn $"{prefix}=ok path={savePath} tick={world.Tick}"
true
with
| ex ->
m5View <- { m5View with Status = "save failed" }
printfn $"{prefix}=error path={savePath} message={ex.Message}"
false
member private this.LoadWorld (prefix: string) : Result<int64, string> =
match WorldSave.loadFromFile savePath with
| Ok nextWorld ->
world <- nextWorld
this.ResetWorldView()
printfn $"{prefix}=ok path={savePath} tick={world.Tick}"
Ok world.Tick
| Error failure ->
printfn $"{prefix}=error path={savePath} message={failure}"
Error failure
member private this.OpenMainMenu() =
relationView <- false
relationSnapshot <- None
m5View <- M5Interaction.initial
menu <- menu |> MenuState.setCurrentWorld true |> MenuState.openMain
printfn $"menu=open tick={world.Tick}"
member private this.ApplyMenuCommand (command: MenuCommand) =
match command with
| NoCommand -> ()
| ContinueGame ->
menu <- menu |> MenuState.setCurrentWorld true |> MenuState.enterGame
printfn $"continue=ok tick={world.Tick}"
| StartNewGame -> this.StartNewGame()
| LoadGame ->
match this.LoadWorld "load" with
| Ok _ ->
menu <- menu |> MenuState.setCurrentWorld true |> MenuState.enterGame
| Error failure ->
menu <- menu |> MenuState.showLoadError failure
| ApplySettings nextControl ->
simulationControl <- nextControl
legacyStepsPerFrame <- None
menu <- menu |> MenuState.setSettings nextControl
printfn $"simulation={M6Presentation.clockLabel simulationControl}"
| ExitGame ->
printfn "menu=exit"
this.Exit()
member private this.DispatchMenuInput (input: MenuInput) =
let nextMenu, command = MenuState.update input menu
menu <- nextMenu
this.ApplyMenuCommand command
member private this.UpdateMenu (pressed: Keys -> bool) =
let pressedAny (keys: Keys list) = keys |> List.exists pressed
let input =
if pressedAny [ Keys.Up; Keys.W ] then Some Up
elif pressedAny [ Keys.Down; Keys.S ] then Some Down
elif pressed Keys.Enter then Some Confirm
elif pressed Keys.Escape then Some Back
else None
match input with
| Some nextInput -> this.DispatchMenuInput nextInput
| None when (autoplay || sampleMode) && not autoplayFlow && menu.Page = MainMenu && not menu.HasCurrentWorld ->
autoplayFrames <- autoplayFrames + 1
if autoplayFrames >= 15 then
this.DispatchMenuInput Confirm
| None -> ()
member private this.RunSampleMode () =
let stageBefore = sampleState.Stage
let positionBefore = world.Avatar.Pos
let nextWorld, nextView, nextState = SampleScript.runFrame world m5View sampleState
if nextWorld.Avatar.Pos <> positionBefore then
avatarFacing <- VillageArt.facingAfterMovement positionBefore nextWorld.Avatar.Pos avatarFacing
world <- nextWorld
m5View <- nextView
sampleState <- nextState
this.CenterCamera()
if nextState.Stage <> stageBefore then
printfn $"sample stage={nextState.Stage} tick={nextWorld.Tick} pos=({nextWorld.Avatar.Pos.X:F0},{nextWorld.Avatar.Pos.Y:F0})"
if nextState.Stage = SampleScript.SampleComplete then
printfn (if nextState.TimedOut then "sample result=timeout" else "sample result=ok")
this.Exit()
member private this.UpdatePlaying (gameTime: GameTime) (kb: KeyboardState) (pressed: Keys -> bool) (pressedAny: Keys list -> bool) =
if sampleMode then
this.RunSampleMode()
else
this.UpdatePlayingManual gameTime kb pressed pressedAny
member private this.UpdatePlayingManual (gameTime: GameTime) (kb: KeyboardState) (pressed: Keys -> bool) (pressedAny: Keys list -> bool) =
let mutable loadFailed = false
let mutable leftGame = false
let lPressed = pressed Keys.L
let escapePressed = pressed Keys.Escape
if pressed Keys.P then
simulationControl <- SimulationControl.togglePause simulationControl
menu <- MenuState.setSettings simulationControl menu
printfn $"simulation={M6Presentation.clockLabel simulationControl}"
if pressed Keys.F1 then
simulationControl <- SimulationControl.setSpeed OneX simulationControl
legacyStepsPerFrame <- None
menu <- MenuState.setSettings simulationControl menu
printfn $"simulation={M6Presentation.clockLabel simulationControl}"
if pressed Keys.F2 then
simulationControl <- SimulationControl.setSpeed TwoX simulationControl
legacyStepsPerFrame <- None
menu <- MenuState.setSettings simulationControl menu
printfn $"simulation={M6Presentation.clockLabel simulationControl}"
if pressed Keys.F3 then
simulationControl <- SimulationControl.setSpeed FiveX simulationControl
legacyStepsPerFrame <- None
menu <- MenuState.setSettings simulationControl menu
printfn $"simulation={M6Presentation.clockLabel simulationControl}"
if pressed Keys.F6 then
this.SaveWorld "save" |> ignore
if pressed Keys.F7 then
match this.LoadWorld "load" with
| Ok _ -> menu <- MenuState.setCurrentWorld true menu
| Error failure ->
menu <- menu |> MenuState.setCurrentWorld true |> MenuState.showLoadError failure
loadFailed <- true
if not loadFailed && escapePressed then
if m5View.Panel <> WorldPanel then
let nextWorld, nextView = M5Interaction.apply ClosePanel world m5View
world <- nextWorld
m5View <- nextView
printfn $"m5 panel={M5Interaction.panelName m5View} status={m5View.Status} tick={world.Tick}"
elif relationView then
relationView <- false
relationSnapshot <- None
printfn $"relation view=off tick={world.Tick}"
else
this.OpenMainMenu()
leftGame <- true
if not loadFailed && not leftGame then
if lPressed && M5Interaction.worldInputAllowed m5View then
relationView <- not relationView
if relationView then relationSnapshot <- Some(Sim.relationMatrix world)
let state = if relationView then "on" else "off"
printfn $"relation view={state} tick={world.Tick}"
let m5Command =
if M5Interaction.worldInputAllowed m5View && pressed Keys.E then Some Interact
elif m5View.Panel = DialoguePanel && pressedAny [ Keys.D1; Keys.NumPad1 ] then Some Intent1
elif m5View.Panel = DialoguePanel && pressedAny [ Keys.D2; Keys.NumPad2 ] then Some Intent2
elif m5View.Panel = DialoguePanel && pressedAny [ Keys.D3; Keys.NumPad3 ] then Some Intent3
elif m5View.Panel = DialoguePanel && pressedAny [ Keys.D4; Keys.NumPad4 ] then Some Intent4
elif m5View.Panel = DialoguePanel && pressedAny [ Keys.D5; Keys.NumPad5 ] then Some Intent5
elif m5View.Panel = DialoguePanel && pressedAny [ Keys.D6; Keys.NumPad6 ] then Some Intent6
elif pressed Keys.Q then Some Observe
elif pressed Keys.Tab then Some ToggleNeeds
elif pressed Keys.C then Some ShowChronicle
else None
match m5Command with
| Some command ->
let nextWorld, nextView = M5Interaction.apply command world m5View
world <- nextWorld
m5View <- nextView
printfn $"m5 panel={M5Interaction.panelName m5View} status={m5View.Status} tick={world.Tick}"
| None -> ()
let panelOpen = m5View.Panel <> WorldPanel
if not relationView && not panelOpen then
let input =
if automated then
let t = float32 world.Tick
{ MoveX = MathF.Sin(t / 60.0f) * 0.8f
MoveY = MathF.Cos(t / 90.0f) * 0.6f }
else
{ MoveX =
(if kb.IsKeyDown(Keys.D) then 1.0f
elif kb.IsKeyDown(Keys.A) then -1.0f
else 0.0f)
MoveY =
(if kb.IsKeyDown(Keys.S) then 1.0f
elif kb.IsKeyDown(Keys.W) then -1.0f
else 0.0f) }
let ts = { Input = input }
let stepCount = SimulationControl.stepsPerFrameWithLegacy legacyStepsPerFrame simulationControl
if stepCount > 0 then
for _ in 1 .. stepCount do
let previousPosition = world.Avatar.Pos
world <- Sim.step ts world
avatarFacing <- VillageArt.facingAfterMovement previousPosition world.Avatar.Pos avatarFacing
this.CenterCamera()
m5View <- M5Interaction.refreshPrompt world m5View
member private this.RunAutoplayFlow() =
let moveDown count =
for _ in 1 .. count do
this.DispatchMenuInput Down
match flowStep with
| 0 ->
printfn "flow=main-menu"
this.DispatchMenuInput Confirm
printfn "flow=new-game"
flowStep <- 1
| 1 when menu.Page = Playing ->
this.OpenMainMenu()
printfn "flow=return-menu"
flowStep <- 2
| 2 when menu.Page = MainMenu ->
moveDown 3
this.DispatchMenuInput Confirm
printfn "flow=settings=open"
this.DispatchMenuInput Down
this.DispatchMenuInput Confirm
this.DispatchMenuInput Back
printfn "flow=settings=back"
flowStep <- 3
| 3 when menu.Page = MainMenu ->
moveDown 4
this.DispatchMenuInput Confirm
printfn "flow=controls=open"
this.DispatchMenuInput Back
printfn "flow=controls=back"
flowStep <- 4
| 4 when menu.Page = MainMenu ->
this.DispatchMenuInput Confirm
this.SaveWorld "flow=save" |> ignore
flowStep <- 5
| 5 when menu.Page = Playing ->
this.LoadWorld "flow=load" |> ignore
flowStep <- 6
| 6 ->
this.OpenMainMenu()
printfn "flow=exit"
this.Exit()
flowStep <- 7
| _ -> ()
override this.Initialize() =
base.Initialize()
prevKb <- Keyboard.GetState()
override _.LoadContent() =
spriteBatch <- new SpriteBatch(this.GraphicsDevice)
artTextures <- VillageArt.loadTextures this.GraphicsDevice
cjkAtlas <- CjkGlyphAtlas.load this.GraphicsDevice
pixel <- new Texture2D(this.GraphicsDevice, 1, 1)
pixel.SetData([| Color.White |])
override _.UnloadContent() =
if not (isNull artTextures.TileAtlas) then artTextures.TileAtlas.Dispose()
if not (isNull artTextures.CharacterAtlas) then artTextures.CharacterAtlas.Dispose()
if not (isNull artTextures.InteriorAtlas) then artTextures.InteriorAtlas.Dispose()
if not (isNull cjkAtlas) then cjkAtlas.Dispose()
if not (isNull pixel) then pixel.Dispose()
if not (isNull spriteBatch) then spriteBatch.Dispose()
override this.Update(gameTime: GameTime) =
let kb = Keyboard.GetState()
let pressed (key: Keys) = kb.IsKeyDown(key) && not (prevKb.IsKeyDown(key))
let pressedAny (keys: Keys list) = keys |> List.exists pressed
if autoplayFlow then
this.RunAutoplayFlow()
elif menu.Page = Playing then
this.UpdatePlaying gameTime kb pressed pressedAny
else
this.UpdateMenu pressed
fpsFrames <- fpsFrames + 1
fpsSeconds <- fpsSeconds + gameTime.ElapsedGameTime.TotalSeconds
if fpsSeconds >= 1.0 then
let fps = float fpsFrames / fpsSeconds
fpsFrames <- 0
fpsSeconds <- 0.0
printfn $"fps={fps:F1} tick={world.Tick} pos=({world.Avatar.Pos.X:F0},{world.Avatar.Pos.Y:F0})"
prevKb <- kb
member private this.DrawLine (a: Vector2) (b: Vector2) (thickness: float32) (color: Color) =
let d = b - a
let len = d.Length()
if len > 1.0f then
let angle = MathF.Atan2(d.Y, d.X)
spriteBatch.Draw(pixel, a, System.Nullable(), color, angle, Vector2.Zero, Vector2(len, thickness), SpriteEffects.None, 0.0f)
member private this.DrawRelationView() =
let profile = M6Presentation.profileAtTick world.Tick
this.GraphicsDevice.Clear(profile.Background)
let viewport = this.GraphicsDevice.Viewport
let vw = float32 viewport.Width
let vh = float32 viewport.Height
let mapW = float32 (Sim.mapWidthTiles * Sim.tilePixels)
let mapH = float32 (Sim.mapHeightTiles * Sim.tilePixels)
let margin = 48.0f
let scale = min ((vw - 2.0f * margin) / mapW) ((vh - 2.0f * margin) / mapH)
let ox = (vw - mapW * scale) / 2.0f
let oy = (vh - mapH * scale) / 2.0f
let toScreen (p: Vec2) : Vector2 = Vector2(ox + p.X * scale, oy + p.Y * scale)
spriteBatch.Begin()
match relationSnapshot with
| Some m ->
let n = Array2D.length1 m
for i in 0 .. n - 1 do
for j in i + 1 .. n - 1 do
let r = m.[i, j]
if abs r > Sim.relationThreshold then
let a = toScreen world.Npcs.[i].Pos
let b = toScreen world.Npcs.[j].Pos
let color = if r > 0.0f then Color.LimeGreen else Color.IndianRed
let thickness = Sim.clamp (1.5f + 3.0f * abs r) 1.5f 12.0f
this.DrawLine a b thickness color
for npc in world.Npcs do
let p = toScreen npc.Pos
spriteBatch.Draw(pixel, Rectangle(int p.X - 5, int p.Y - 5, 10, 10), Color.Cyan)
| None -> ()
spriteBatch.End()
override this.Draw(gameTime: GameTime) =
if menu.Page <> Playing then
this.DrawMenu()
elif relationView then
this.DrawRelationView()
this.DrawM5Overlay()
else
this.DrawWorldView()
member private this.DrawMenu() =
this.DrawWorldView()
let viewport = this.GraphicsDevice.Viewport
let width = viewport.Width
let height = viewport.Height
let panelWidth = min 720 (max 1 (width - 64))
let panelHeight = min 640 (max 1 (height - 48))
let panelX = (width - panelWidth) / 2
let panelY = (height - panelHeight) / 2
let innerX = panelX + 32
let textScale = 3
let lineHeight = 30
let selectedColor = Color(245, 222, 120)
let textColor = Color(210, 226, 220)
let mutedColor = Color(140, 170, 170)
let drawText (x: int) (y: int) (color: Color) (text: string) =
PixelText.draw spriteBatch pixel cjkAtlas x y textScale color text
let drawSelection (index: int) (label: string) (y: int) =
let selected = index = menu.Selected
let prefix = if selected then ">" else " "
drawText innerX y (if selected then selectedColor else textColor) (sprintf "%s %s" prefix label)
spriteBatch.Begin()
spriteBatch.Draw(pixel, Rectangle(0, 0, width, height), Color(4, 9, 14, 190))
spriteBatch.Draw(pixel, Rectangle(panelX, panelY, panelWidth, panelHeight), Color(11, 19, 27, 245))
spriteBatch.Draw(pixel, Rectangle(panelX, panelY, panelWidth, 6), Color(92, 180, 190))
drawText innerX (panelY + 28) (Color(235, 246, 232)) ChineseText.copy.SceneTitle
let contentY = panelY + 100
match menu.Page with
| MainMenu ->
drawText innerX (panelY + 70) mutedColor (ChineseText.menuHeading MainMenuHeading)
MenuState.items menu
|> List.iteri (fun index item -> drawSelection index (MenuState.itemLabel item) (contentY + index * lineHeight))
drawText innerX (panelY + panelHeight - 54) mutedColor "UP DOWN 选择 ENTER 确认"
| Settings ->
drawText innerX (panelY + 70) mutedColor (ChineseText.menuHeading SettingsHeading)
MenuState.settings
|> List.iteri (fun index setting -> drawSelection index (MenuState.settingsLabel setting) (contentY + index * lineHeight))
drawText innerX (panelY + panelHeight - 84) mutedColor (sprintf "当前 %s" (M6Presentation.clockLabel menu.Settings))
drawText innerX (panelY + panelHeight - 54) mutedColor "ENTER 应用 ESC 返回"
| Controls ->
drawText innerX (panelY + 70) mutedColor (ChineseText.menuHeading ControlsHeading)
MenuState.controlRows
|> List.iteri (fun index row ->
drawText innerX (contentY + index * 25) textColor (sprintf "%s %s" row.Keys row.Action))
drawText innerX (panelY + panelHeight - 28) mutedColor "ESC 返回"
| LoadError ->
drawText innerX (panelY + 70) (Color(240, 130, 120)) "读取失败"
let message = menu.Error |> Option.defaultValue "暂无存档"
let visible = if message.Length > 34 then message.Substring(0, 34) else message
drawText innerX contentY textColor visible
drawText innerX (contentY + lineHeight * 2) mutedColor "ENTER ESC 返回"
| Playing -> ()
spriteBatch.End()
member private this.DrawWorldView() =
let profile = M6Presentation.profileAtTick world.Tick
let viewport = this.GraphicsDevice.Viewport
this.GraphicsDevice.Clear(profile.Background)
spriteBatch.Begin()
match m5View.HomeMode with
| Inside _ ->
VillageArt.drawInterior spriteBatch pixel artTextures viewport.Width viewport.Height renderPlan.Interior profile.WorldTint
let interiorAvatarSpec = VillageArt.avatarSpriteSpec avatarFacing world.Tick
VillageArt.drawInteriorCharacter spriteBatch artTextures viewport.Width viewport.Height world.Avatar.Pos interiorAvatarSpec profile.WorldTint
| Outside ->
VillageArt.drawWorld
spriteBatch
artTextures
camera
viewport.Width
viewport.Height
renderPlan
profile.WorldTint
let avatarSpec = VillageArt.avatarSpriteSpec avatarFacing world.Tick
VillageArt.drawCharacter spriteBatch artTextures camera world.Avatar.Pos avatarSpec profile.WorldTint
for npc in world.Npcs do
let spec = VillageArt.npcSpriteSpecAtTarget npc.Id npc.Pos npc.Mind.Target world.Tick
VillageArt.drawCharacter spriteBatch artTextures camera npc.Pos spec profile.WorldTint
spriteBatch.End()
this.DrawM5Overlay()
member private this.DrawM5Overlay() =
let viewport = this.GraphicsDevice.Viewport
let panelWidth = min 460 (max 1 (viewport.Width - 32))
let panelHeight = min 520 (max 1 (viewport.Height - 32))
let panelX = max 16 (viewport.Width - panelWidth - 16)
let panelY = 16
let panel = Rectangle(panelX, panelY, panelWidth, panelHeight)
let innerX = panelX + 16
let innerWidth = panelWidth - 32
let textScale = 2
let lineHeight = 18
let maxChars = max 1 (innerWidth / (6 * textScale))
let maxLines = max 1 ((panelHeight - 32) / lineHeight)
let profile = M6Presentation.profileAtTick world.Tick
let m6Lines = [ sprintf "时间 %s %s" (M6Presentation.dayNightLabel profile.Mode) (M6Presentation.clockLabel simulationControl) ]
let lines = (m6Lines @ M5Interaction.panelLines world m5View) |> List.truncate maxLines
spriteBatch.Begin()
spriteBatch.Draw(pixel, panel, Color(15, 19, 25, 220))
spriteBatch.Draw(pixel, Rectangle(panelX, panelY, panelWidth, 4), Color(92, 180, 190))
lines
|> List.iteri (fun index line ->
let visible = if line.Length > maxChars then line.Substring(0, maxChars) else line
let color = if index = 0 then Color(220, 235, 220) else Color(190, 205, 210)
PixelText.draw spriteBatch pixel cjkAtlas innerX (panelY + 12 + index * lineHeight) textScale color visible)
spriteBatch.End()
|