-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain_Raylib.fsx
More file actions
185 lines (149 loc) · 5.26 KB
/
Copy pathMain_Raylib.fsx
File metadata and controls
185 lines (149 loc) · 5.26 KB
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
#r "nuget: Raylib-cs"
#load "App/Input.fsx"
#load "App/Scene.fsx"
#load "App/App.fsx"
#load "App/Snapshot.fsx"
open Raylib_cs
open type Raylib
open App
type CBool with member this.AsBool = this = CBool(true)
let Scale = 30
let FramesPerSecond = 60
module Root = Snapshot
let InputBindings =
[
Input.Action.Up, [|KeyboardKey.KEY_W; KeyboardKey.KEY_UP|]
Input.Action.Down, [|KeyboardKey.KEY_S; KeyboardKey.KEY_DOWN|]
Input.Action.Left, [|KeyboardKey.KEY_A; KeyboardKey.KEY_LEFT|]
Input.Action.Right, [|KeyboardKey.KEY_D; KeyboardKey.KEY_RIGHT|]
Input.Action.Select, [|KeyboardKey.KEY_SPACE; KeyboardKey.KEY_ENTER|]
Input.Action.Cancel, [|KeyboardKey.KEY_ESCAPE|]
Input.Action.StoreSnapshot, [|KeyboardKey.KEY_F5|]
Input.Action.RestoreSnapshot, [|KeyboardKey.KEY_F8|]
]
|> Map.ofList
let ColorMapping =
let color r g b =
let toByte (v:float) = v * 255.0 |> int |> min 255 |> max 0 |> byte
Color(toByte r, toByte g, toByte b, 255uy)
[
Scene.Color.Black, color 0.0 0.0 0.0
Scene.Color.White, color 0.9 0.9 0.9
Scene.Color.Gray, color 0.6 0.6 0.6
Scene.Color.Red, color 0.9 0 0
Scene.Color.DarkRed, color 0.6 0 0
Scene.Color.Green, color 0 0.9 0
Scene.Color.DarkGreen, color 0 0.6 0
Scene.Color.Blue, color 0 0 0.9
Scene.Color.DarkBlue, color 0 0 0.6
Scene.Color.DarkGray, color 0.3 0.3 0.3
// TODO add missing colors
]
|> Map.ofSeq
let ReadInput =
let baked =
Root.InputBindings
|> Seq.choose (fun (condition, action, event) ->
InputBindings
|> Map.tryFind action
|> Option.map (fun hwKeys ->
match condition with
| Input.Down ->
fun () ->
let any = hwKeys |> Array.exists (fun hwKey -> IsKeyDown(hwKey).AsBool)
if any then Some event else None
| Input.Pressed ->
fun () ->
let any = hwKeys |> Array.exists (fun hwKey -> IsKeyPressed(hwKey).AsBool)
if any then Some event else None
)
)
baked
|> Seq.choose (fun f -> f ())
let Draw (scene) =
let convertColor (color) =
ColorMapping
|> Map.tryFind color
|> Option.defaultValue (Color(255uy, 0uy, 255uy, 255uy))
scene
|> Seq.iter (function
| Scene.Rectangle data ->
let (x, y) = data.Position
let (width, height) = data.Size
DrawRectangle(x, y, width, height, convertColor data.Color)
| Scene.Text data ->
let (x, y) = data.Position
DrawText(data.Text, x, y, int data.Size, convertColor data.Color)
| Scene.Ellipse data ->
let (x, y) = data.Position
let (w, h) = data.Size
DrawEllipse(x + w / 2, y + h / 2, float32 w / 2.0f, float32 h / 2.0f, convertColor data.Color)
)
let mutable State =
Pcg.make (uint64 System.Environment.TickCount64)
|> Root.makeInitial
let mutable ActiveCommands = []
let StateUpdate (time) (event) =
let (pcg, (root, commands)) =
State
|> StateM.apply (fun root -> Root.Update root time event)
State <- (pcg, root)
ActiveCommands <- commands @ ActiveCommands
InitAudioDevice()
InitWindow(
Scale * Snake.Config.FieldSizeX,
Scale * Snake.Config.FieldSizeY,
"Functional Fun")
SetTargetFPS(FramesPerSecond)
DisableEventWaiting()
SetExitKey(LanguagePrimitives.EnumOfValue 0)
let watch = System.Diagnostics.Stopwatch.StartNew()
let mutable LastDrawnState = None
let mutable Quit = false
let Sounds = System.Collections.Generic.Dictionary<string, Sound>()
while not (WindowShouldClose().AsBool || Quit) do
PollInputEvents()
System.Threading.Thread.Sleep(1)
let activeCommands = ActiveCommands
ActiveCommands <- []
let time = watch.Elapsed
for event in ReadInput do
StateUpdate time event
for command in activeCommands do
match command with
| App.PlaySound path ->
let sound =
match Sounds.TryGetValue(path) with
| true, sound -> sound
| false, _ ->
let sound = LoadSound("Content/" + path)
Sounds.Add(path, sound)
sound
Raylib.PlaySound(sound)
|> ignore
| App.Quit -> Quit <- true
StateUpdate time Root.Tick
let (_, root) = State
if LastDrawnState <> Some root then
LastDrawnState <- Some root
let sceneGraph =
Scene.graph {
yield! Root.Draw root
if root.App.Mode = App.Mode.Menu then
yield Scene.Text {
Position = (1, 12)
Size = 1
Color = Scene.Color.DarkGray
Text = "[Space] Select\n[W] Up\n[S] Down\n[A] Left\n[D] Right"
}
}
|> Scene.scale (Scale, Scale)
BeginDrawing()
ClearBackground(Color.RAYWHITE)
sceneGraph
|> Scene.bake
|> Draw
EndDrawing()
CloseWindow()
for item in Sounds do UnloadSound(item.Value)
CloseAudioDevice()