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
|
package main
import (
"time"
)
func (ui *gameui) Replay() {
g := ui.g
dl := g.DrawLog
if len(dl) == 0 {
return
}
g.DrawLog = nil
rep := &replay{ui: ui, frames: dl, frame: 0}
if ColorBase03 == Color256Base03 {
rep.color256 = true
}
rep.Run()
}
type replay struct {
ui *gameui
frames []drawFrame
undo [][]cellDraw
frame int
auto bool
speed time.Duration
evch chan repEvent
color256 bool
}
type repEvent int
const (
ReplayNext repEvent = iota
ReplayPrevious
ReplayTogglePause
ReplayQuit
ReplaySpeedMore
ReplaySpeedLess
)
func (rep *replay) Run() {
rep.auto = true
rep.speed = 1
rep.evch = make(chan repEvent, 100)
rep.undo = [][]cellDraw{}
go func(r *replay) {
r.PollKeyboardEvents()
}(rep)
for {
e := rep.PollEvent()
switch e {
case ReplayNext:
if rep.frame >= len(rep.frames) {
break
} else if rep.frame < 0 {
rep.frame = 0
}
rep.DrawFrame()
rep.frame++
case ReplayPrevious:
if rep.frame <= 1 {
break
} else if rep.frame >= len(rep.frames) {
rep.frame = len(rep.frames)
}
rep.frame--
rep.UndoFrame()
case ReplayQuit:
return
case ReplayTogglePause:
rep.auto = !rep.auto
case ReplaySpeedMore:
rep.speed *= 2
if rep.speed > 16 {
rep.speed = 16
}
case ReplaySpeedLess:
rep.speed /= 2
if rep.speed < 1 {
rep.speed = 1
}
}
}
}
func (rep *replay) DrawFrame() {
ui := rep.ui
df := rep.frames[rep.frame]
rep.undo = append(rep.undo, []cellDraw{})
j := len(rep.undo) - 1
for _, dr := range df.Draws {
i := ui.GetIndex(dr.X, dr.Y)
c := ui.g.DrawBuffer[i]
if rep.color256 {
dr.Cell.Fg = rep.ui.Map16ColorTo256(dr.Cell.Fg)
dr.Cell.Bg = rep.ui.Map16ColorTo256(dr.Cell.Bg)
} else {
dr.Cell.Bg = ui.Map256ColorTo16(dr.Cell.Bg)
dr.Cell.Fg = ui.Map256ColorTo16(dr.Cell.Fg)
}
rep.undo[j] = append(rep.undo[j], cellDraw{Cell: c, X: dr.X, Y: dr.Y})
ui.SetGenCell(dr.X, dr.Y, dr.Cell.R, dr.Cell.Fg, dr.Cell.Bg, dr.Cell.InMap)
}
ui.Flush()
ui.g.DrawLog = nil
}
func (rep *replay) UndoFrame() {
ui := rep.ui
df := rep.undo[len(rep.undo)-1]
for _, dr := range df {
ui.SetGenCell(dr.X, dr.Y, dr.Cell.R, dr.Cell.Fg, dr.Cell.Bg, dr.Cell.InMap)
}
rep.undo = rep.undo[:len(rep.undo)-1]
ui.Flush()
ui.g.DrawLog = nil
}
func (rep *replay) PollEvent() (in repEvent) {
if rep.auto && rep.frame <= len(rep.frames)-1 && rep.frame >= 0 {
var d time.Duration
if rep.frame > 0 {
d = rep.frames[rep.frame].Time.Sub(rep.frames[rep.frame-1].Time)
} else {
d = 0
}
if d >= 2*time.Second {
d = 2 * time.Second
}
d = d / rep.speed
if d <= 10*time.Millisecond {
d = 10 * time.Millisecond
}
t := time.NewTimer(d)
select {
case in = <-rep.evch:
case <-t.C:
in = ReplayNext
}
t.Stop()
} else {
in = <-rep.evch
}
return in
}
func (rep *replay) PollKeyboardEvents() {
for {
e := rep.ui.PollEvent()
if e.interrupt {
rep.evch <- ReplayNext
continue
}
switch e.key {
case "Q", "q", "\x1b":
rep.evch <- ReplayQuit
return
case "p", "P", " ":
rep.evch <- ReplayTogglePause
case "+", ">":
rep.evch <- ReplaySpeedMore
case "-", "<":
rep.evch <- ReplaySpeedLess
case ".", "6", "j", "n", "f":
rep.evch <- ReplayNext
case "4", "k", "N", "b":
rep.evch <- ReplayPrevious
default:
if !e.mouse {
break
}
switch e.button {
case 0:
rep.evch <- ReplayNext
case 1:
rep.evch <- ReplayTogglePause
case 2:
rep.evch <- ReplayPrevious
}
}
}
}
|