1
- // The game is the interface that connects all related to a KAPLAY game state.
1
+ // The Game is the interface that connects all related to a KAPLAY game state.
2
2
// It contains the game object tree, game object events, scenes, etc.
3
3
4
+ // All in /game folder is stuff that uses/modify the game state.
5
+
4
6
import type { Asset } from "../assets/asset" ;
5
7
import type { SpriteData } from "../assets/sprite" ;
6
8
import type { FakeMouseComp } from "../ecs/components/misc/fakeMouse" ;
@@ -107,22 +109,33 @@ type CamData = {
107
109
transform : Mat23 ;
108
110
} ;
109
111
112
+ /**
113
+ * Creates the Game interface.
114
+ *
115
+ * This will create:
116
+ *
117
+ * - The root game object
118
+ * - The game object events
119
+ * - The camera data
120
+ *
121
+ * @returns A Game
122
+ */
110
123
export const createGame = ( ) : Game => {
111
- const game = {
124
+ const game : Game = {
112
125
gameObjLastId : 0 ,
113
- // general events
114
- events : new KEventHandler < GameEventMap & GameObjEventMap > ( ) ,
115
- // root game object
116
126
root : makeInternal ( [ ] , 0 ) as GameObj < TimerComp > ,
127
+ events : new KEventHandler < GameEventMap & GameObjEventMap > ( ) ,
128
+ cam : {
129
+ pos : null as Vec2 | null ,
130
+ scale : new Vec2 ( 1 ) ,
131
+ angle : 0 ,
132
+ shake : 0 ,
133
+ transform : new Mat23 ( ) ,
134
+ } ,
117
135
118
- // misc
119
- gravity : null as Vec2 | null ,
120
- scenes : { } as Record < SceneName , SceneDef > ,
121
- currentScene : null as SceneName | null ,
122
- layers : null as string [ ] | null ,
123
- defaultLayerIndex : 0 ,
136
+ // Systems
124
137
systems : [ ] , // all systems added
125
- // we allocate systems
138
+ // we allocate systems here
126
139
systemsByEvent : [
127
140
[ ] , // afterDraw
128
141
[ ] , // afterFixedUpdate
@@ -132,29 +145,34 @@ export const createGame = (): Game => {
132
145
[ ] , // beforeUpdate
133
146
] ,
134
147
135
- // default assets
148
+ // Scenes
149
+ scenes : { } as Record < SceneName , SceneDef > ,
150
+ currentScene : null as SceneName | null ,
151
+
152
+ // Layers
153
+ layers : null as string [ ] | null ,
154
+ defaultLayerIndex : 0 ,
155
+
156
+ // Gravity
157
+ gravity : null as Vec2 | null ,
158
+
159
+ // Assets
136
160
kaSprite : null as unknown as Asset < SpriteData > ,
137
161
boomSprite : null as unknown as Asset < SpriteData > ,
138
162
139
- // on screen log
163
+ // Logs
140
164
logs : [ ] as { msg : string | { toString ( ) : string } ; time : number } [ ] ,
141
165
142
- // camera
143
- cam : {
144
- pos : null as Vec2 | null ,
145
- scale : new Vec2 ( 1 ) ,
146
- angle : 0 ,
147
- shake : 0 ,
148
- transform : new Mat23 ( ) ,
149
- } ,
166
+ // Fake mouse API
167
+ fakeMouse : null ,
150
168
151
- defRNG : new RNG ( Date . now ( ) ) ,
169
+ // Some state
152
170
crashed : false ,
153
171
areaCount : 0 ,
154
- fakeMouse : null ,
155
172
allTextInputs : new Set < GameObj > ( ) ,
173
+ defRNG : new RNG ( Date . now ( ) ) ,
156
174
warned : new Set < string > ( ) ,
157
- } satisfies Game ;
175
+ } ;
158
176
159
177
game . root . use ( timer ( ) ) ;
160
178
game . gameObjLastId ++ ;
0 commit comments