-
Notifications
You must be signed in to change notification settings - Fork 0
/
star-system.go
117 lines (97 loc) · 2.56 KB
/
star-system.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
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
package starsystem
import (
"log"
"sync"
"time"
"github.com/dayaftereh/discover/server/game/persistence"
"github.com/dayaftereh/discover/server/game/persistence/types"
"github.com/dayaftereh/discover/server/game/universe/starsystem/objects"
"github.com/dayaftereh/discover/server/utils/atomic"
"github.com/dayaftereh/discover/server/game/engine"
"github.com/dayaftereh/discover/server/game/player"
"github.com/dayaftereh/discover/server/game/engine/world"
)
type StarSystem struct {
Name string
counter int64
lock sync.Mutex
close chan bool
running *atomic.AtomicBool
// the star system data
Data *types.StarSystem
// starSystem
sun *objects.Sun
// players
players map[string]*player.Player
playersObject map[string]int64
// Engine
clock *engine.Clock
world *world.World
// persistence
persistence *persistence.PersistenceManager
}
func NewStarSystem(name string, persistence *persistence.PersistenceManager) *StarSystem {
// create the star system
starSystem := &StarSystem{
Name: name,
counter: 0,
// players
playersObject: make(map[string]int64),
players: make(map[string]*player.Player),
// engine
world: world.NewWorld(),
clock: engine.NewClock(),
// close
close: make(chan bool),
running: atomic.NewAtomicBool(false),
// set persistence manager
persistence: persistence,
}
return starSystem
}
func (starSystem *StarSystem) loop() {
defer func() {
starSystem.running.Set(false)
log.Printf("closing star-system [ %s ] thread\n", starSystem.Name)
}()
// update by 30 fps
timer := (1000 / 30) * time.Millisecond
// start the clock
starSystem.clock.Start()
log.Printf("starting star-system [ %s ]\n", starSystem.Name)
for starSystem.running.Get() {
select {
case <-time.After(timer):
starSystem.update()
case <-starSystem.close:
return
}
}
}
func (starSystem *StarSystem) update() {
// lock the star system
starSystem.lock.Lock()
defer starSystem.lock.Unlock()
// get the delta for the update
delta := starSystem.clock.Delta()
// update the world
starSystem.world.Update(delta)
// push the world update for the players
starSystem.pushWorldUpdates()
}
func (starSystem *StarSystem) nextID() int64 {
// increment counter
starSystem.counter++
// get next id
return starSystem.counter
}
func (starSystem *StarSystem) Shutdown() {
// try to shutdown the star system
running := starSystem.running.GetAndSet(false)
// stop if the star system already shutdowned
if !running {
return
}
log.Printf("shutdown star-system with id [ %s ]\n", starSystem.Name)
starSystem.close <- true
}