forked from surdeus/gox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.go
381 lines (320 loc) · 6.89 KB
/
engine.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package gg
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/di4f/gods/maps"
//"fmt"
"time"
"slices"
"sync"
)
type GraphicsLibrary = ebiten.GraphicsLibrary
type RunOptions = ebiten.RunGameOptions
// The type represents order of drawing.
// Higher values are drawn later.
type Layer float64
func (l Layer) GetLayer() Layer {
return l
}
// Window configuration type.
type WindowConfig struct {
DebugInfo ebiten.DebugInfo
Options *RunOptions
// The title of the window.
Title string
// Width and height of the window
// in pixels.
Width,
Height int
// Optional settings with
// self describing names.
FixedSize,
Fullscreen,
VSync bool
}
// The main structure that represents current state of [game] engine.
type Engine struct {
wcfg *WindowConfig
// The main holder for objects.
// Uses the map structure to quickly
// delete and create new objects.
Objects maps.Map[Object, struct{}]
// The main camera to display in window.
// If is set to nil then the engine will panic.
Camera *Camera
// The same delta time for all frames
// and all objects.
lastTime time.Time
dt Float
// Temporary stuff
keys, prevKeys []Key
buttons MouseButtonMap
wheel Vector
cursorPos Vector
outerEvents, handleEvents EventChan
}
type engine Engine
// Get currently pressed keys.
func (e *Engine) Keys() []Key {
return e.keys
}
func (e *Engine) GraphicsLibrary() GraphicsLibrary {
return e.wcfg.DebugInfo.GraphicsLibrary
}
// Returns currently pressed buttons.
func (e *Engine) MouseButtons() []MouseButton {
ret := make([]MouseButton, len(e.buttons))
i := 0
for v := range e.buttons {
ret[i] = v
i++
}
slices.Sort(ret)
return ret
}
// Returns new empty Engine.
func NewEngine(
cfg *WindowConfig,
) *Engine {
/*w := Float(cfg.Width)
h := Float(cfg.Height)*/
ret := &Engine{}
ret.wcfg = cfg
ret.Camera = ret.NewCamera()
ret.outerEvents = make(EventChan)
ret.handleEvents = make(EventChan)
ret.Objects = maps.NewOrdered[Object, struct{}]()
ret.buttons = MouseButtonMap{}
return ret
}
// Get the real window size in the current context.
func (c *Engine) RealWinSize() Vector {
return V(
Float(c.wcfg.Width),
Float(c.wcfg.Height),
)
}
func (c *Engine) AbsWinSize() Vector {
return c.RealWinSize().Div(c.Camera.Scale)
}
func (e *Engine) EventInput() EventChan {
return e.outerEvents
}
// Add new object considering what
// interfaces it implements.
func (e *Engine) Add(b any) error {
object, _ := b.(Object)
if e.Objects.Has(object) {
return ObjectExistErr
}
/*o, ok := e.makeObject(b)
if !ok {
return ObjectNotImplementedErr
}*/
starter, ok := b.(Starter)
if ok {
starter.Start(&Context{
Engine: e,
})
}
e.Objects.Set(object, struct{}{})
return nil
}
// Delete object from Engine.
func (e *Engine) Del(b any) error {
object, _ := b.(Object)
if !e.Objects.Has(object) {
return ObjectNotExistErr
}
deleter, ok := b.(Deleter)
if ok {
deleter.Delete(&Context{
Engine: e,
})
}
e.Objects.Del(b)
return nil
}
var (
allButtons = []MouseButton{
MouseButton0,
MouseButton1,
MouseButton2,
MouseButton3,
MouseButton4,
}
)
func (e *Engine) IsPressed(k Key) bool {
keys := e.Keys()
for _, v := range keys {
if v == k {
return true
}
}
return false
}
func (e *Engine) IsButtoned(b MouseButton) bool {
_, ok := e.buttons[b]
return ok
}
func (e *Engine) Wheel() Vector {
return e.wheel
}
func (e *Engine) cursorPosition() Vector {
x, y := ebiten.CursorPosition()
return V(Float(x), Float(y))
}
func (e *Engine) CursorPosition() Vector {
return e.cursorPos
}
func (e *Engine) AbsCursorPosition() Vector {
m := &Matrix{}
m.Concat(e.Camera.AbsMatrix())
return e.CursorPosition().Apply(m)
}
func (e *engine) Update() error {
var wg sync.WaitGroup
eng := (*Engine)(e)
e.dt = time.Since(e.lastTime).Seconds()
for object := range e.Objects.KeyChan() {
updater, ok := object.(Updater)
if !ok {
continue
}
wg.Add(1)
go func() {
updater.Update(&Context{
Engine: eng,
})
wg.Done()
}()
}
wg.Wait()
e.prevKeys = e.keys
e.keys = inpututil.
AppendPressedKeys(e.keys[:0])
events := []any{}
btns := e.buttons
for _, btn := range allButtons {
if inpututil.IsMouseButtonJustPressed(btn) {
btns[btn] = struct{}{}
events = append(events, &MouseButtonDown{
MouseButton: btn,
})
} else if inpututil.IsMouseButtonJustReleased(btn) {
delete(btns, btn)
events = append(events, &MouseButtonUp{
MouseButton: btn,
})
}
}
x, y := ebiten.Wheel()
eng.wheel = V(x, y)
if !(eng.wheel.Eq(ZV)) {
events = append(events, &WheelChange{
Offset: eng.wheel,
})
}
keyDiff := diffEm(e.prevKeys, e.keys)
for _, key := range keyDiff {
var event any
if eng.IsPressed(key) {
event = &KeyDown{
Key: key,
}
} else {
event = &KeyUp{
Key: key,
}
}
events = append(events, event)
}
realPos := eng.cursorPosition()
if !realPos.Eq(eng.cursorPos) {
absM := eng.Camera.AbsMatrix()
absPrevPos :=eng.cursorPos.Apply(&absM)
absPos := realPos.Apply(&absM)
events = append(events, &MouseMove{
Real: realPos.Sub(eng.cursorPos),
Abs: absPos.Sub(absPrevPos),
})
eng.cursorPos = realPos
}
// Providing the events to the objects.
// Maybe should think of the better way,
// but for it is simple enough.
for object := range e.Objects.KeyChan() {
eventer, ok := object.(Eventer)
if ok {
wg.Add(1)
go func() {
defer wg.Done()
for _, event := range events {
eventer.Event(&Context{
Engine: eng,
Event: event,
})
}
}()
}
}
wg.Wait()
e.lastTime = time.Now()
return nil
}
func (e *engine) Draw(i *ebiten.Image) {
eng := (*Engine)(e)
m := map[Layer][]Drawer{}
for object := range eng.Objects.KeyChan() {
drawer, ok := object.(Drawer)
if !ok {
continue
}
l := drawer.GetLayer()
layer, ok := m[l]
// Create new if has no the layer
if !ok {
m[l] = []Drawer{drawer}
continue
}
m[l] = append(layer, drawer)
}
// Drawing layers.
layers := maps.NewSparse[Layer, []Drawer](nil, m)
for layer := range layers.Chan() {
for _, drawer := range layer {
drawer.Draw(&Context{
Engine: eng,
Image: i,
})
}
}
// Empty the buff to generate it again.
eng.Camera.buf = nil
}
func (e *engine) Layout(ow, oh int) (int, int) {
if e.wcfg.FixedSize {
return e.wcfg.Width, e.wcfg.Height
}
return ow, oh
}
// Return the delta time duration value.
func (e *Engine) DT() Float {
return e.dt
}
func (e *Engine) FPS() float64 {
return ebiten.ActualFPS()
}
func (e *Engine) TPS() float64 {
return ebiten.ActualTPS()
}
func (e *Engine) Run() error {
ebiten.ReadDebugInfo(&e.wcfg.DebugInfo)
ebiten.SetWindowTitle(e.wcfg.Title)
ebiten.SetWindowSize(e.wcfg.Width, e.wcfg.Height)
ebiten.SetWindowSizeLimits(1, 1, e.wcfg.Width, e.wcfg.Height)
ebiten.SetVsyncEnabled(e.wcfg.VSync)
e.lastTime = time.Now()
//fmt.Println(e.Objects)
return ebiten.RunGameWithOptions((*engine)(e), e.wcfg.Options)
}