-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.go
356 lines (293 loc) · 7.85 KB
/
demo.go
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
//go:build fnfdemo
// This is a small script to generate demo recording.
// Made for me to record trailer video.
//
// Format :
// "binding name" "Press or Release" "relative frame"
// or
// NOP "relative frame"
//
// Example :
// key P 12
// key R 15
// NOP 30
package fnf
import (
"fmt"
"os"
"strconv"
"strings"
"unicode/utf8"
rl "github.com/gen2brain/raylib-go/raylib"
)
const DemoRecordKey = rl.KeyF8
const DemoPlayKey = rl.KeyF10
const DemoEventDumpPath string = "./demo-dump.txt"
const DemoEventLoadPath string = "./demo-play.txt"
// how long should we wait before the demo
const DemoWaitDuration = 200
func init() {
AddSuffixToVersionTag("-DEMO")
DebugPrintPersist("demo", "true")
}
type DemoEventType int
const (
DemoEventTypePress DemoEventType = iota
DemoEventTypeRelease
DemoEventTypeNOP
)
type DemoEvent struct {
Frame int64
RelativeFrame int64
Key FnfBinding
Type DemoEventType
}
var TheDemoManager struct {
frameCounter int64
isRecording bool
isPlaying bool
eventsRecorded []DemoEvent
eventsToPlay []DemoEvent
playStartFrame int64
playEventIndex int
isKeyDown [FnfBindingSize]bool
}
func UpdateDemoState() {
am := &TheDemoManager
// record and play logic
wasRecording := am.isRecording
if rl.IsKeyPressed(DemoRecordKey) {
am.isRecording = !am.isRecording
// check if we are playing and if it is,
// set isRecording to false
if am.isRecording {
if am.isPlaying {
DisplayAlert("can't record while playing")
am.isRecording = false
}
}
if wasRecording && !am.isRecording && len(am.eventsRecorded) > 0 {
// update relative frame
lastFrame := am.eventsRecorded[0].Frame
for i := range am.eventsRecorded {
am.eventsRecorded[i].RelativeFrame = am.eventsRecorded[i].Frame - lastFrame
lastFrame = am.eventsRecorded[i].Frame
}
// build dump string
var builder strings.Builder
for _, event := range am.eventsRecorded {
pressOrRelease := "P"
if event.Type == DemoEventTypeRelease {
pressOrRelease = "R"
}
line := fmt.Sprintf("%s %s %d\n",
event.Key.String(), pressOrRelease, event.RelativeFrame)
builder.WriteString(line)
}
// try to save dump
var dumpPath string
var err error
if dumpPath, err = RelativePath(DemoEventDumpPath); err != nil {
ErrorLogger.Printf("failed to save event dump %v", err)
DisplayAlert("failed to save event dump")
goto DUMP_END
}
if err = os.WriteFile(dumpPath, []byte(builder.String()), 0664); err != nil {
ErrorLogger.Printf("failed to save event dump %v", err)
DisplayAlert("failed to save event dump")
goto DUMP_END
}
DisplayAlert(fmt.Sprintf("saved events to %s", DemoEventDumpPath))
DUMP_END:
}
if !wasRecording && am.isRecording {
am.eventsRecorded = am.eventsRecorded[:0] // empty it out
}
}
wasPlaying := am.isPlaying
if rl.IsKeyPressed(DemoPlayKey) {
am.isPlaying = !am.isPlaying
// check if we are recording and if it is,
// set isPlaying to false
if am.isPlaying {
if am.isRecording {
DisplayAlert("can't play while recording")
am.isPlaying = false
}
}
// load events to play
if !wasPlaying && am.isPlaying {
am.eventsToPlay = am.eventsToPlay[:0]
var err error
var filePath string
filePath, err = RelativePath(DemoEventLoadPath)
var file []byte
if err == nil {
file, err = os.ReadFile(filePath)
}
var fileStr string
if err == nil {
if utf8.Valid(file) {
fileStr = string(file)
} else {
err = fmt.Errorf("file %s is not a valid utf8 string", DemoEventLoadPath)
}
}
if err != nil {
ErrorLogger.Printf("failed to load events %v", err)
DisplayAlert("failed to load events")
am.isPlaying = false
} else { // we do play!
am.playStartFrame = am.frameCounter
am.playEventIndex = 0
// parse demo file
strToBind := make(map[string]FnfBinding)
for binding := FnfBinding(0); binding < FnfBindingSize; binding++ {
strToBind[binding.String()] = binding
}
lines := strings.Split(fileStr, "\n")
var frame int64 = am.frameCounter + DemoWaitDuration
for _, line := range lines {
// try to find comment
if index := strings.Index(line, "//"); index >= 0 {
line = line[:index]
}
fields := strings.Fields(line)
if len(fields) >= 2 && fields[0] == "NOP" {
if relativeFrame, convErr := strconv.ParseInt(fields[1], 10, 64); convErr == nil {
am.eventsToPlay = append(am.eventsToPlay, DemoEvent{
Frame: relativeFrame + frame,
Type: DemoEventTypeNOP,
})
frame += relativeFrame
}
} else if len(fields) >= 3 {
ok := true
var key FnfBinding
if ok {
key, ok = strToBind[fields[0]]
}
var eventType DemoEventType
if ok {
if fields[1] == "P" {
eventType = DemoEventTypePress
} else if fields[1] == "R" {
eventType = DemoEventTypeRelease
} else {
ok = false
}
}
var relativeFrame int64
if ok {
var convErr error
relativeFrame, convErr = strconv.ParseInt(fields[2], 10, 64)
if convErr != nil {
ok = false
}
if relativeFrame < 0 {
ok = false
}
}
if ok {
am.eventsToPlay = append(am.eventsToPlay, DemoEvent{
Frame: relativeFrame + frame,
Key: key,
Type: eventType,
})
frame += relativeFrame
}
}
}
}
}
// demo playing canceled due to user input
// display the status
if wasPlaying && !am.isPlaying {
DisplayAlert("demo play canceled")
StopDemoPressingKeys()
}
}
if am.isRecording {
for binding := FnfBinding(0); binding < FnfBindingSize; binding++ {
keyDown := rl.IsKeyDown(TheKM[binding])
if am.isKeyDown[binding] != keyDown {
if keyDown {
am.eventsRecorded = append(am.eventsRecorded, DemoEvent{
Frame: am.frameCounter,
Key: binding,
Type: DemoEventTypePress,
})
} else {
am.eventsRecorded = append(am.eventsRecorded, DemoEvent{
Frame: am.frameCounter,
Key: binding,
Type: DemoEventTypeRelease,
})
}
}
am.isKeyDown[binding] = keyDown
}
} else if am.isPlaying {
for ; am.playEventIndex < len(am.eventsToPlay); am.playEventIndex++ {
event := am.eventsToPlay[am.playEventIndex]
frame := event.Frame
if frame <= am.frameCounter {
if event.Type != DemoEventTypeNOP {
rlEvent := rl.AutomationEvent{}
rlEvent.Params[0] = TheKM[event.Key]
if event.Type == DemoEventTypePress {
rlEvent.Type = 2 // raylib's INPUT_KEY_DOWN
} else if event.Type == DemoEventTypeRelease {
rlEvent.Type = 1 // raylib's INPUT_KEY_UP
}
rl.PlayAutomationEvent(rlEvent)
}
} else {
break
}
}
if am.playEventIndex >= len(am.eventsToPlay) {
am.isPlaying = false
StopDemoPressingKeys()
}
}
am.frameCounter++
}
func StopDemoPressingKeys() {
for binding := FnfBinding(0); binding < FnfBindingSize; binding++ {
rlEvent := rl.AutomationEvent{}
rlEvent.Params[0] = TheKM[binding]
rlEvent.Type = 1 // raylib's INPUT_KEY_UP
rl.PlayAutomationEvent(rlEvent)
}
}
func DrawDemoState() {
am := &TheDemoManager
if am.isRecording {
const radius = 20
const margin = 15
rl.DrawCircle(
SCREEN_WIDTH-(radius+margin), (radius + margin),
radius,
ToRlColor(FnfColor{255, 0, 0, 255}),
)
} else if am.isPlaying {
if am.frameCounter-am.playStartFrame < DemoWaitDuration {
rl.DrawRectangle(
0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
ToRlColor(FnfColor{76, 237, 116, 255}),
)
}
}
}
func dumpDemoEvents(events []DemoEvent) {
for i, event := range events {
pressOrRelease := "P"
if event.Type == DemoEventTypeRelease {
pressOrRelease = "R"
}
line := fmt.Sprintf("%d: \"%s\" \"%s\" F:\"%d\" RF:\"%d\"",
i, event.Key.String(), pressOrRelease, event.Frame, event.RelativeFrame)
fmt.Println(line)
}
}