Skip to content

Commit 7e4720d

Browse files
author
hai.lin
committed
add timer
1 parent 9690d91 commit 7e4720d

File tree

6 files changed

+73
-3
lines changed

6 files changed

+73
-3
lines changed

event.go

+32
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func (p *eventSink) call(wait bool, data interface{}, doSth func(*eventSink)) {
9191

9292
type eventSinkMgr struct {
9393
allWhenStart *eventSink
94+
allWhenTimer *eventSink
9495
allWhenKeyPressed *eventSink
9596
allWhenIReceive *eventSink
9697
allWhenBackdropChanged *eventSink
@@ -102,10 +103,12 @@ type eventSinkMgr struct {
102103
allWhenMoving *eventSink
103104
allWhenTurning *eventSink
104105
calledStart bool
106+
calledTimer bool
105107
}
106108

107109
func (p *eventSinkMgr) reset() {
108110
p.allWhenStart = nil
111+
p.allWhenTimer = nil
109112
p.allWhenKeyPressed = nil
110113
p.allWhenIReceive = nil
111114
p.allWhenBackdropChanged = nil
@@ -121,6 +124,7 @@ func (p *eventSinkMgr) reset() {
121124

122125
func (p *eventSinkMgr) doDeleteClone(this interface{}) {
123126
p.allWhenStart = p.allWhenStart.doDeleteClone(this)
127+
p.allWhenTimer = p.allWhenTimer.doDeleteClone(this)
124128
p.allWhenKeyPressed = p.allWhenKeyPressed.doDeleteClone(this)
125129
p.allWhenIReceive = p.allWhenIReceive.doDeleteClone(this)
126130
p.allWhenBackdropChanged = p.allWhenBackdropChanged.doDeleteClone(this)
@@ -145,6 +149,17 @@ func (p *eventSinkMgr) doWhenStart() {
145149
}
146150
}
147151

152+
func (p *eventSinkMgr) doWhenTimer(time float64) {
153+
if !p.calledTimer {
154+
p.allWhenTimer.asyncCall(false, time, func(ev *eventSink) {
155+
if ev.cond != nil && ev.cond(time) {
156+
ev.sink.(func(float64))(time)
157+
p.calledTimer = true
158+
}
159+
})
160+
}
161+
}
162+
148163
func (p *eventSinkMgr) doWhenKeyPressed(key Key) {
149164
p.allWhenKeyPressed.asyncCall(false, key, func(ev *eventSink) {
150165
ev.sink.(func(Key))(key)
@@ -232,6 +247,7 @@ type IEventSinks interface {
232247
OnMsg__0(onMsg func(msg string, data interface{}))
233248
OnMsg__1(msg string, onMsg func())
234249
OnStart(onStart func())
250+
OnTimer(time float64, onTimer func())
235251
Stop(kind StopKind)
236252
}
237253

@@ -274,6 +290,22 @@ func (p *eventSinks) OnStart(onStart func()) {
274290
}
275291
}
276292

293+
func (p *eventSinks) OnTimer(time float64, onTimer func()) {
294+
p.allWhenTimer = &eventSink{
295+
prev: p.allWhenTimer,
296+
pthis: p.pthis,
297+
sink: func(float64) {
298+
if debugEvent {
299+
log.Println("==> OnTimer", time, nameOf(p.pthis))
300+
}
301+
onTimer()
302+
},
303+
cond: func(data interface{}) bool {
304+
return data.(float64) >= time
305+
},
306+
}
307+
}
308+
277309
func (p *eventSinks) OnClick(onClick func()) {
278310
pthis := p.pthis
279311
p.allWhenClick = &eventSink{

game.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,8 @@ func (p *Game) handleEvent(event event) {
717717
p.sinkMgr.doWhenKeyPressed(ev.Key)
718718
case *eventStart:
719719
p.sinkMgr.doWhenStart()
720+
case *eventTimer:
721+
p.sinkMgr.doWhenTimer(ev.time)
720722
}
721723
}
722724

@@ -738,6 +740,8 @@ func (p *Game) eventLoop(me coroutine.Thread) int {
738740
}
739741
func (p *Game) logicLoop(me coroutine.Thread) int {
740742
for {
743+
gtime.Tick(gtime.DeltaTime())
744+
741745
tempItems := p.getTempShapes()
742746
for _, item := range tempItems {
743747
if result, ok := item.(interface{ onUpdate(float64) }); ok {
@@ -1197,11 +1201,16 @@ func (p *Game) Wait(secs float64) {
11971201
}
11981202

11991203
func (p *Game) Timer() float64 {
1200-
panic("todo")
1204+
return gtime.Timer()
12011205
}
12021206

12031207
func (p *Game) ResetTimer() {
1204-
panic("todo")
1208+
gtime.ResetTimer()
1209+
}
1210+
1211+
func (p *Game) OnTimerGreaterThan(secs float64, call func()) {
1212+
engine.Wait(secs)
1213+
gco.WaitMainThread(call)
12051214
}
12061215

12071216
// -----------------------------------------------------------------------------

gdspx.go

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (p *Game) syncUpdateLogic() error {
8585
p.startFlag.Do(func() {
8686
p.fireEvent(&eventStart{})
8787
})
88+
p.fireEvent(&eventTimer{time: p.Timer()})
8889

8990
return nil
9091
}

input.go

+4
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ type event interface{}
246246

247247
type eventStart struct{}
248248

249+
type eventTimer struct {
250+
time float64
251+
}
252+
249253
type eventKeyDown struct {
250254
Key Key
251255
}

internal/time/timer.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package time
2+
3+
var (
4+
gameTimer float64
5+
)
6+
7+
func Tick(deltaTime float64) {
8+
gameTimer += deltaTime
9+
}
10+
11+
func Timer() float64 {
12+
return gameTimer
13+
}
14+
15+
func ResetTimer() {
16+
gameTimer = 0
17+
}

monitor.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ import (
2020
"fmt"
2121
"log"
2222
"reflect"
23+
"strconv"
2324
"strings"
2425
"syscall"
2526

27+
gtime "github.com/goplus/spx/internal/time"
2628
"github.com/goplus/spx/internal/tools"
2729
"github.com/goplus/spx/internal/ui"
2830
"github.com/realdream-ai/mathf"
@@ -124,7 +126,8 @@ func getValueRef(target reflect.Value, name string, from int) reflect.Value {
124126
}
125127

126128
const (
127-
getVarPrefix = "getVar:"
129+
getVarPrefix = "getVar:"
130+
getTimerPrefix = "getProp:"
128131
)
129132

130133
func buildMonitorEval(g reflect.Value, t, val string) func() string {
@@ -142,6 +145,10 @@ func buildMonitorEval(g reflect.Value, t, val string) func() string {
142145
}
143146
}
144147
log.Println("[WARN] Monitor: var not found -", name, target)
148+
case strings.HasPrefix(val, getTimerPrefix):
149+
return func() string {
150+
return strconv.FormatFloat(gtime.Timer(), 'f', 3, 64)
151+
}
145152
default:
146153
log.Println("[WARN] Monitor: unknown command -", val)
147154
}

0 commit comments

Comments
 (0)