forked from gophergala2016/gophette
-
Notifications
You must be signed in to change notification settings - Fork 2
/
input_recording.go
56 lines (48 loc) · 934 Bytes
/
input_recording.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
)
var (
recordingInput = false
replayingInput = false
recordedCharacterIndex = 1
)
type inputRecord struct {
frame int
event InputEvent
}
var (
inputs []inputRecord
frame int
)
func init() {
if replayingInput {
inputs = recordedInputs
}
}
func recordInput(event InputEvent) {
if recordingInput && event.CharacterIndex == recordedCharacterIndex {
inputs = append(inputs, inputRecord{frame: frame, event: event})
}
}
func saveRecordedInputs() {
input := bytes.NewBuffer(nil)
input.WriteString(`package main
var recordedInputs = []inputRecord{
`)
for i := range inputs {
fmt.Fprintf(
input,
"\t{%v, InputEvent{%v, %v, %v}},\n",
inputs[i].frame,
inputs[i].event.Action,
inputs[i].event.Pressed,
inputs[i].event.CharacterIndex,
)
}
input.WriteString(`}
`)
ioutil.WriteFile("./recorded_inputs.go", input.Bytes(), 0777)
}