-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathaging-timelistener.go
More file actions
46 lines (39 loc) · 828 Bytes
/
aging-timelistener.go
File metadata and controls
46 lines (39 loc) · 828 Bytes
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
package main
import "mud"
type LifeStage struct {
StageNo int
Name string
StageChangeDelay int
Pre func(AgingTimeListener)
Post func(AgingTimeListener)
}
type AgingTimeListener interface {
mud.TimeListener
LifeStages() map[int]LifeStage
Stage() LifeStage
LastChange() int
SetStage(LifeStage)
SetStageChanged(int)
}
func addLs(ls LifeStage, lifeStages map[int]LifeStage) {
lifeStages[ls.StageNo] = ls
}
func AgeLoop(a AgingTimeListener) {
for {
now := <- a.Ping()
stage := a.Stage()
if now > (a.LastChange() + stage.StageChangeDelay) {
if(stage.Post != nil) {
a.Stage().Post(a)
}
if(stage.StageChangeDelay > 0) {
nextStage := (stage.StageNo + 1)
a.SetStage(a.LifeStages()[nextStage])
a.SetStageChanged(now)
}
if(a.Stage().Pre != nil) {
a.Stage().Pre(a)
}
}
}
}