Skip to content

Commit 8bb1ad3

Browse files
author
Avaer Kazmer
committed
Major Unity emulation structure update
1 parent 1d5c1cc commit 8bb1ad3

File tree

1 file changed

+57
-37
lines changed

1 file changed

+57
-37
lines changed

vrarmik/Unity.js

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,13 @@ class Transform {
339339

340340
const gameObjects = [];
341341
class GameObject {
342-
constructor(name) {
342+
constructor(name, unity) {
343+
if (!unity) {
344+
throw new Error('bad game object initialization');
345+
}
346+
343347
this.name = name;
348+
this.unity = unity;
344349

345350
this.transform = new Transform();
346351
this.components = new Map();
@@ -350,62 +355,31 @@ class GameObject {
350355
AddComponent(Constructor) {
351356
let component = this.components.get(Constructor);
352357
if (component === undefined) {
353-
component = new Constructor(this.transform, this.components);
358+
component = new Constructor(this.transform, this.components, this.unity);
354359
this.components.set(Constructor, component);
355360
}
356361
return component;
357362
}
358363
AddChild(child) {
359364
this.transform.AddChild(child.transform);
360365
}
361-
static clearAll() {
362-
gameObjects.length = 0;
363-
}
364-
static startAll() {
365-
for (let i = 0; i < gameObjects.length; i++) {
366-
gameObjects[i].components.forEach(value => {
367-
value.Awake();
368-
});
369-
}
370-
for (let i = 0; i < gameObjects.length; i++) {
371-
gameObjects[i].components.forEach(value => {
372-
value.OnEnable();
373-
});
374-
}
375-
for (let i = 0; i < gameObjects.length; i++) {
376-
gameObjects[i].components.forEach(value => {
377-
value.Start();
378-
});
379-
}
380-
}
381-
static updateAll() {
382-
for (let i = 0; i < gameObjects.length; i++) {
383-
gameObjects[i].components.forEach(value => {
384-
value.Update();
385-
});
386-
}
387-
for (let i = 0; i < gameObjects.length; i++) {
388-
gameObjects[i].components.forEach(value => {
389-
value.LateUpdate();
390-
});
391-
}
392-
}
393366
}
394367

395368
class MonoBehavior {
396-
constructor(transform, components) {
397-
if (!transform || !components) {
369+
constructor(transform, components, unity) {
370+
if (!transform || !components || !unity) {
398371
throw new Error('bad component initialization');
399372
}
400373

401374
this.transform = transform;
402375
this.components = components;
376+
this.unity = unity;
403377
}
404378

405379
GetComponent(Constructor) {
406380
let component = this.components.get(Constructor);
407381
if (component === undefined) {
408-
component = new Constructor(this.transform, this.components);
382+
component = new Constructor(this.transform, this.components, this.unity);
409383
this.components.set(Constructor, component);
410384
}
411385
return component;
@@ -513,6 +487,51 @@ const XRSettings = {
513487
loadedDeviceName: 'OpenVR',
514488
};
515489

490+
class Unity {
491+
constructor() {
492+
this.gameObjects = [];
493+
}
494+
495+
makeGameObject(name) {
496+
const gameObject = new GameObject(name, this);
497+
this.gameObjects.push(gameObject);
498+
return gameObject;
499+
}
500+
501+
clearAll() {
502+
this.gameObjects.length = 0;
503+
}
504+
startAll() {
505+
for (let i = 0; i < this.gameObjects.length; i++) {
506+
this.gameObjects[i].components.forEach(value => {
507+
value.Awake();
508+
});
509+
}
510+
for (let i = 0; i < this.gameObjects.length; i++) {
511+
this.gameObjects[i].components.forEach(value => {
512+
value.OnEnable();
513+
});
514+
}
515+
for (let i = 0; i < this.gameObjects.length; i++) {
516+
this.gameObjects[i].components.forEach(value => {
517+
value.Start();
518+
});
519+
}
520+
}
521+
updateAll() {
522+
for (let i = 0; i < this.gameObjects.length; i++) {
523+
this.gameObjects[i].components.forEach(value => {
524+
value.Update();
525+
});
526+
}
527+
for (let i = 0; i < this.gameObjects.length; i++) {
528+
this.gameObjects[i].components.forEach(value => {
529+
value.LateUpdate();
530+
});
531+
}
532+
}
533+
}
534+
516535
export {
517536
Vector2,
518537
Vector3,
@@ -524,4 +543,5 @@ export {
524543
Mathf,
525544
PlayerPrefs,
526545
XRSettings,
546+
Unity,
527547
};

0 commit comments

Comments
 (0)