-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsim.go
199 lines (158 loc) · 4.27 KB
/
sim.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package game
import (
"fmt"
"net/http"
"text/template"
"github.com/ghthor/aodd/game/datastore"
"github.com/ghthor/engine/rpg2d"
"github.com/ghthor/engine/rpg2d/coord"
"github.com/ghthor/engine/rpg2d/entity"
"github.com/ghthor/engine/rpg2d/quad"
"github.com/ghthor/engine/sim/stime"
)
// Store actor's indexed by id
type actorIndex map[rpg2d.ActorId]*actor
// Type used to wrap a running simulation interface
// and start and stop the actor's IO muxer.
// Also adds and removes actors from the
// simulation's actor index.
type simulation struct {
actorIndex
rpg2d.RunningSimulation
}
func (s simulation) ConnectActor(a rpg2d.Actor) {
switch a := a.(type) {
case *actor:
a.startIO()
s.actorIndex[a.Id()] = a
default:
panic(fmt.Sprint("unexpected sim.Actor:", a))
}
s.RunningSimulation.ConnectActor(a)
}
func (s simulation) RemoveActor(a rpg2d.Actor) {
s.RunningSimulation.RemoveActor(a)
switch a := a.(type) {
case *actor:
a.stopIO()
delete(s.actorIndex, a.Id())
default:
panic(fmt.Sprint("unexpected sim.Actor:", a))
}
}
type indexHandler struct {
tmpl *template.Template
settings ClientSettings
}
func (index indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := index.tmpl.Execute(w, index.settings)
if err != nil {
http.Error(w, fmt.Sprint("template error:", err), http.StatusInternalServerError)
}
}
// A set of data that the index page template will
// have access to when it is executed.
type ClientSettings struct {
JsMain string
WebsocketURL string
Simulation SimulationSettings
}
// Part of the ClientSettings that are rpg2d.Simulation specific
type SimulationSettings struct {
Width, Height int
}
type ShardConfig struct {
// Is the resultant http server going to be
// run using TLS or just standard HTTP.
IsHTTPS bool
// LAddr for the http server
LAddr,
// Path to javascript directory
JsDir,
// The javascript module that require.js should
// call as the javascript main.
JsMain,
// Path to the graphic asset directory
AssetDir,
// Path to the css directory
CssDir string
// A template for the index page. The template
// will be executed with a game.ClientSettings{} struct.
IndexTmpl *template.Template
// A mux that http server will use. Is provided so the
// user can extend the server with additional routes.
Mux *http.ServeMux
// A handler that will be used instead of the Mux
// when setting the Handler field of the *http.Server.
// If Handler is nil, the Mux will be used instead.
Handler http.Handler
}
func NewSimShard(c ShardConfig) (*http.Server, error) {
// TODO pull this information from a datastore
bounds := coord.Bounds{
coord.Cell{-1024, 1024},
coord.Cell{1023, -1023},
}
quadTree, err := quad.New(bounds, 40, nil)
if err != nil {
return nil, err
}
terrainMap, err := rpg2d.NewTerrainMap(bounds, string(rpg2d.TT_GRASS))
if err != nil {
return nil, err
}
now := stime.Time(0)
actorIndex := make(actorIndex)
entityIdGen := entity.NewIdGenerator()
simDef := rpg2d.SimulationDef{
FPS: 40,
// Initial World State
Now: now,
QuadTree: quadTree,
TerrainMap: terrainMap,
UpdatePhaseHandler: updatePhase{actorIndex},
InputPhaseHandler: inputPhase{actorIndex, entityIdGen},
NarrowPhaseHandler: newNarrowPhase(actorIndex),
}
runningSim, err := simDef.Begin()
if err != nil {
return nil, err
}
datastore := datastore.NewMemDatastore()
wsRoute := "/actor/socket/gob"
var wsUrl string
if c.IsHTTPS {
wsUrl = "wss://"
} else {
wsUrl = "ws://"
}
wsUrl += c.LAddr + wsRoute
indexHandler := indexHandler{
c.IndexTmpl,
ClientSettings{
c.JsMain,
wsUrl,
SimulationSettings{
Width: quadTree.Bounds().Width(),
Height: quadTree.Bounds().Height(),
},
},
}
mux := c.Mux
mux.Handle("/", indexHandler)
mux.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir(c.JsDir))))
mux.Handle("/asset/", http.StripPrefix("/asset/", http.FileServer(http.Dir(c.AssetDir))))
mux.Handle("/css/", http.StripPrefix("/css/", http.FileServer(http.Dir(c.CssDir))))
mux.Handle(wsRoute, newGobWebsocketHandler(simulation{
actorIndex,
runningSim,
}, datastore, entityIdGen))
defaultHandler := c.Handler
if defaultHandler == nil {
defaultHandler = mux
}
return &http.Server{
Addr: c.LAddr,
Handler: defaultHandler,
}, nil
}