This repository has been archived by the owner on Dec 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Engine.ts
192 lines (167 loc) · 6.48 KB
/
Engine.ts
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
import CameraAPI from "./lib/utils/CameraAPI"
import ENVIRONMENT from "./static/ENVIRONMENT"
import Loop from "./Loop";
import SSAO from "./runtime/SSAO";
import DirectionalShadows from "./runtime/DirectionalShadows";
import ConversionAPI from "./lib/math/ConversionAPI";
import Physics from "./runtime/Physics";
import FrameComposition from "./runtime/FrameComposition";
import GPU from "./GPU";
import OmnidirectionalShadows from "./runtime/OmnidirectionalShadows";
import PhysicsAPI from "./lib/rendering/PhysicsAPI";
import FileSystemAPI from "./lib/utils/FileSystemAPI";
import ScriptsAPI from "./lib/utils/ScriptsAPI";
import UIAPI from "./lib/rendering/UIAPI";
import LightProbe from "./instances/LightProbe";
import Entity from "./instances/Entity";
import DynamicMap from "./resource-libs/DynamicMap";
import GPUAPI from "./lib/rendering/GPUAPI";
import EntityAPI from "./lib/utils/EntityAPI";
import ResourceEntityMapper from "./resource-libs/ResourceEntityMapper";
import ResourceManager from "./runtime/ResourceManager";
export default class Engine {
static #development = false
static #onLevelLoadListeners = new DynamicMap<string, Function>()
static UILayouts = new Map()
static isDev = true
static #environment: number = ENVIRONMENT.DEV
static #isReady = false
static #initializationWasTried = false
static #initialized = false
static #loadedLevel: Entity
static removeLevelLoaderListener(id: string) {
Engine.#onLevelLoadListeners.delete(id)
}
static addLevelLoaderListener(id: string, callback: Function) {
Engine.#onLevelLoadListeners.set(id, callback)
}
static get entities(): DynamicMap<string, Entity> {
return ResourceEntityMapper.entities
}
static get queryMap(): Map<string, Entity> {
return ResourceEntityMapper.queryMap
}
static get isReady() {
return Engine.#isReady
}
static get loadedLevel(): Entity {
return Engine.#loadedLevel
}
static get developmentMode() {
return Engine.#development
}
static get environment(): number {
return Engine.#environment
}
static set environment(data: number) {
Engine.isDev = data === ENVIRONMENT.DEV
Engine.#environment = data
if (Engine.isDev)
CameraAPI.updateAspectRatio()
}
static async initializeContext(canvas: HTMLCanvasElement, mainResolution: { w: number, h: number } | undefined, readAsset: Function, devAmbient: boolean) {
if (Engine.#initialized)
return
Engine.#initialized = true
Engine.#development = devAmbient
await GPU.initializeContext(canvas, mainResolution)
FileSystemAPI.initialize(readAsset)
FrameComposition.initialize()
await SSAO.initialize()
OmnidirectionalShadows.initialize()
DirectionalShadows.initialize()
await PhysicsAPI.initialize()
ConversionAPI.canvasBBox = GPU.canvas.getBoundingClientRect()
const OBS = new ResizeObserver(() => {
const bBox = GPU.canvas.getBoundingClientRect()
ConversionAPI.canvasBBox = bBox
CameraAPI.aspectRatio = bBox.width / bBox.height
CameraAPI.updateProjection()
})
OBS.observe(GPU.canvas.parentElement)
OBS.observe(GPU.canvas)
Engine.#isReady = true
GPU.skylightProbe = new LightProbe(128)
if (Engine.#initializationWasTried)
Engine.start()
}
static async startSimulation() {
Engine.environment = ENVIRONMENT.EXECUTION
UIAPI.buildUI(GPU.canvas.parentElement)
const entities = Engine.entities.array
for (let i = 0; i < entities.length; i++) {
const current = entities[i]
PhysicsAPI.registerRigidBody(current)
}
await ScriptsAPI.updateAllScripts()
}
static start() {
if (!Loop.isExecuting && Engine.#isReady) {
Physics.start()
ResourceManager.start()
Loop.start()
} else
Engine.#initializationWasTried = true
}
static stop() {
Loop.stop()
ResourceManager.stop()
Physics.stop()
}
static async loadLevel(levelID: string, cleanEngine?: boolean) {
if (!levelID || Engine.#loadedLevel?.id === levelID && !cleanEngine)
return []
try {
if (cleanEngine) {
GPU.meshes.forEach(m => GPUAPI.destroyMesh(m))
GPU.textures.forEach(m => GPUAPI.destroyTexture(m.id))
GPU.materials.clear()
}
const asset = await FileSystemAPI.readAsset(levelID)
const {entities, entity} = JSON.parse(asset)
let levelEntity
if (!entity)
levelEntity = EntityAPI.getNewEntityInstance(levelID, true)
else
levelEntity = EntityAPI.parseEntityObject({...entity, isCollection: true})
if (!levelEntity.name)
levelEntity.name = "New level"
levelEntity.parentID = undefined
Engine.#replaceLevel(levelEntity)
const allEntities = []
for (let i = 0; i < entities.length; i++) {
try {
const entity = EntityAPI.parseEntityObject(entities[i])
for (let i = 0; i < entity.scripts.length; i++)
await ScriptsAPI.linkScript(entity, entity.scripts[i].id)
const imgID = entity.spriteComponent?.imageID
if (imgID) {
const textures = GPU.textures
if (!textures.get(imgID))
await FileSystemAPI.loadTexture(imgID)
}
const uiID = entity.uiComponent?.uiLayoutID
const file = FileSystemAPI.readAsset(uiID)
if (file)
Engine.UILayouts.set(uiID, file)
allEntities.push(entity)
} catch (err) {
console.error(err)
}
}
EntityAPI.addGroup(allEntities)
} catch (err) {
console.error(err)
}
Engine.#onLevelLoadListeners.array.forEach(callback => callback())
}
static #replaceLevel(newLevel?: Entity) {
const oldLevel = Engine.#loadedLevel
Engine.#loadedLevel = newLevel
if (oldLevel) {
EntityAPI.removeEntity(oldLevel)
}
if (newLevel)
EntityAPI.addEntity(newLevel)
}
}