summaryrefslogtreecommitdiff
path: root/src/LivingVillage.Kernel/SimulationControl.fs
blob: 83e5f3693f0a91289b63ddb71bfd0029e852f929 (plain)
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
namespace LivingVillage.Kernel

type SimulationSpeed =
    | OneX
    | TwoX
    | FiveX

type SimulationControl =
    { Paused: bool
      Speed: SimulationSpeed }

module SimulationControl =

    let initial : SimulationControl =
        { Paused = false
          Speed = OneX }

    let setSpeed (speed: SimulationSpeed) (control: SimulationControl) : SimulationControl =
        { control with Speed = speed }

    let togglePause (control: SimulationControl) : SimulationControl =
        { control with Paused = not control.Paused }

    let initialForConfiguredSteps (configuredSteps: int) : SimulationControl * int option =
        let steps = if configuredSteps > 0 then configuredSteps else 1
        let control =
            match steps with
            | 2 -> setSpeed TwoX initial
            | 5 -> setSpeed FiveX initial
            | _ -> initial
        let legacySteps =
            match steps with
            | 1
            | 2
            | 5 -> None
            | value -> Some value
        control, legacySteps

    let stepsPerFrame (control: SimulationControl) : int =
        if control.Paused then
            0
        else
            match control.Speed with
            | OneX -> 1
            | TwoX -> 2
            | FiveX -> 5

    let stepsPerFrameWithLegacy (legacySteps: int option) (control: SimulationControl) : int =
        if control.Paused then
            0
        else
            match legacySteps with
            | Some steps when steps > 0 -> steps
            | _ -> stepsPerFrame control