Skip to content

Commit 3c966eb

Browse files
author
hai.lin
committed
add timer
1 parent 9690d91 commit 3c966eb

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

event.go

+10
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ type IEventSinks interface {
232232
OnMsg__0(onMsg func(msg string, data interface{}))
233233
OnMsg__1(msg string, onMsg func())
234234
OnStart(onStart func())
235+
OnTimer(time float64, onTimer func())
235236
Stop(kind StopKind)
236237
}
237238

@@ -274,6 +275,15 @@ func (p *eventSinks) OnStart(onStart func()) {
274275
}
275276
}
276277

278+
func (p *eventSinks) OnTimer(time float64, onTimer func()) {
279+
if value, ok := timerFunc[p.pthis]; ok {
280+
timerFunc[p.pthis] = append(value, TimerFunc{Time: time, Func: onTimer})
281+
} else {
282+
var timers []TimerFunc
283+
timerFunc[p.pthis] = append(timers, TimerFunc{Time: time, Func: onTimer})
284+
}
285+
}
286+
277287
func (p *eventSinks) OnClick(onClick func()) {
278288
pthis := p.pthis
279289
p.allWhenClick = &eventSink{

game.go

+31-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525
"path/filepath"
2626
"reflect"
27+
"strconv"
2728
"sync"
2829
"time"
2930
"unsafe"
@@ -71,6 +72,7 @@ var (
7172
isSchedInMain bool
7273
mainSchedTime time.Time
7374
lastSchedTime time.Time
75+
timerFunc map[threadObj][]TimerFunc
7476
)
7577

7678
func SetDebug(flags dbgFlags) {
@@ -80,6 +82,11 @@ func SetDebug(flags dbgFlags) {
8082
debugPerf = (flags & DbgFlagPerf) != 0
8183
}
8284

85+
type TimerFunc struct {
86+
Time float64
87+
Func func()
88+
}
89+
8390
// -------------------------------------------------------------------------------------
8491

8592
type Shape interface {
@@ -190,6 +197,7 @@ func (p *Game) initGame(sprites []Sprite) *Game {
190197

191198
// Gopt_Game_Main is required by Go+ compiler as the entry of a .gmx project.
192199
func Gopt_Game_Main(game Gamer, sprites ...Sprite) {
200+
timerFunc = make(map[threadObj][]TimerFunc)
193201
lastSchedTime = time.Now()
194202
g := game.initGame(sprites)
195203
g.gamer_ = game
@@ -744,6 +752,26 @@ func (p *Game) logicLoop(me coroutine.Thread) int {
744752
result.onUpdate(gtime.DeltaTime())
745753
}
746754
}
755+
756+
if len(timerFunc) > 0 {
757+
var deleteFuncs []threadObj
758+
for key, timers := range timerFunc {
759+
if isSprite(key) && key.(*SpriteImpl).HasDestroyed {
760+
deleteFuncs = append(deleteFuncs, key)
761+
} else {
762+
for _, value := range timers {
763+
if value.Time <= gtime.Timer() {
764+
value.Func()
765+
deleteFuncs = append(deleteFuncs, key)
766+
}
767+
}
768+
}
769+
}
770+
for _, funcKey := range deleteFuncs {
771+
delete(timerFunc, funcKey)
772+
}
773+
}
774+
747775
engine.WaitNextFrame()
748776
}
749777
}
@@ -1197,11 +1225,12 @@ func (p *Game) Wait(secs float64) {
11971225
}
11981226

11991227
func (p *Game) Timer() float64 {
1200-
panic("todo")
1228+
result, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", gtime.Timer()), 64)
1229+
return result
12011230
}
12021231

12031232
func (p *Game) ResetTimer() {
1204-
panic("todo")
1233+
gtime.ResetTimer()
12051234
}
12061235

12071236
// -----------------------------------------------------------------------------

internal/time/time.go

+1
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ func Update(scale float64, realDuration float64, duration float64, delta float64
7272
deltaTime = delta
7373
curFrame += 1
7474
fps = pfps
75+
gameTimer += deltaTime
7576
}

internal/time/timer.go

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

monitor.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ func getValueRef(target reflect.Value, name string, from int) reflect.Value {
124124
}
125125

126126
const (
127-
getVarPrefix = "getVar:"
127+
getVarPrefix = "getVar:"
128+
getTimerPrefix = "getProp:"
128129
)
129130

130131
func buildMonitorEval(g reflect.Value, t, val string) func() string {
@@ -142,6 +143,16 @@ func buildMonitorEval(g reflect.Value, t, val string) func() string {
142143
}
143144
}
144145
log.Println("[WARN] Monitor: var not found -", name, target)
146+
case strings.HasPrefix(val, getTimerPrefix):
147+
methodName := val[len(getTimerPrefix):]
148+
m := g.FieldByName("Game").Addr().MethodByName(methodName)
149+
150+
if m.IsValid() {
151+
return func() string {
152+
return fmt.Sprint(m.Call(nil)[0].Interface())
153+
}
154+
}
155+
log.Println("[WARN] Monitor: prop not found -", methodName, target)
145156
default:
146157
log.Println("[WARN] Monitor: unknown command -", val)
147158
}

0 commit comments

Comments
 (0)