-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.web.js
More file actions
97 lines (86 loc) · 3.4 KB
/
main.web.js
File metadata and controls
97 lines (86 loc) · 3.4 KB
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
const cs = {}
cs.load = function (options) {
this.options = options
// handy
this.clone = (object) => { return JSON.parse(JSON.stringify(object)) }
this.default = (want, ifnot) => { return want === undefined ? ifnot : want }
// 1. setup
this.cs = this
this.canvas = options.canvas
this.ctx = this.canvas.getContext('2d')
this.path = options.path
if (!options.path) {
const scriptTag = document.querySelector('#cs-main-web')
if (!scriptTag) {
throw 'ERROR: could not load parts.'
+ '\r\nneed options.path or id="cs-main-web" on script tag'
}
const path = new URL(scriptTag.src)
this.path = path.pathname.replace('/main.web.js', '')
}
this.maxSize = options.maxSize || 2000
this.start = options.start
this.userStep = options.step
this.userDraw = options.draw
this.userEndDraw = options.endDraw
this.progress = options.progress || function () {}
this.focus = options.focus || function () {}
this.version = options.version || Math.random()
this.global = options.global || {}
this.progress = options.progress || function () {}
this.focus = options.focus || function () {}
this.objects = options.objects || {}
this.script = options.scripts || {}
this.sprites = options.sprites || []
this.storages = options.storages || []
this.sounds = options.sounds || []
this.assets = {
scripts: options.assets && options.assets.scripts ? options.assets.scripts : [],
sprites: options.assets && options.assets.sprites ? options.assets.sprites : [],
storages: options.assets && options.assets.storages ? options.assets.storages : [],
sounds: options.assets && options.assets.sounds ? options.assets.sounds : [],
}
const parts = [
{ path: this.path + '/src/Camera' },
{ path: this.path + '/src/Destroy' },
{ path: this.path + '/src/Draw' },
{ path: this.path + '/src/Fps' },
{ path: this.path + '/src/Fullscreen' },
{ path: this.path + '/src/InputKeyboard' },
{ path: this.path + '/src/InputMouse' },
{ path: this.path + '/src/InputTouch' },
{ path: this.path + '/src/Loop' },
{ path: this.path + '/src/Loader' },
{ path: this.path + '/src/Math' },
{ path: this.path + '/src/Network' },
{ path: this.path + '/src/Object' },
{ path: this.path + '/src/Room' },
{ path: this.path + '/src/Scripts' },
{ path: this.path + '/src/Setup' },
{ path: this.path + '/src/Sound' },
{ path: this.path + '/src/Sprite' },
{ path: this.path + '/src/Storage' },
{ path: this.path + '/src/Surface' },
{ path: this.path + '/src/Timer' },
{ path: this.path + '/src/Vector' },
]
// 2. load
console.groupCollapsed('Loading Engine...')
this.loading = parts.length
const dateStartLoading = Date.now()
for (const part of parts) {
console.log('Loading Part: ' + part.path.split('/').pop())
const htmlScript = document.createElement('script')
htmlScript.src = `${part.path}.js?v=${this.version}`
htmlScript.onload = () => {
this.loading -= 1
if (this.loading <= 0) {
const engineLoadTime = Math.round(Date.now() - dateStartLoading)
console.groupEnd()
console.log(`Engine Loaded in ${engineLoadTime}ms`)
cs.loader.load() // loader will call cs.start()
}
}
document.body.append(htmlScript)
}
}