Advertisement
lignite0

ROBO24 - scene

Oct 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @property {SceneNode[]} children
  3.  */
  4. class SceneNode {
  5.  
  6.     constructor(options) {
  7.         this.id = options.id;
  8.         this.parent = options.parent || null;
  9.         this.children = [];
  10.     }
  11.  
  12.     getRoot() {
  13.         let node = this;
  14.         while (true) {
  15.             if (node.parent === null) {
  16.                 return node;
  17.             }
  18.             node = node.parent;
  19.         }
  20.     }
  21.  
  22.     addSceneNodeChild(child) {
  23.         const oldChildParent = child.parent;
  24.         if (oldChildParent) {
  25.             oldChildParent.removeChild(child);
  26.         }
  27.         child.parent = this;
  28.         this.children.push(child);
  29.     }
  30.  
  31.     removeChild(child) {
  32.         const index = this.children.indexOf(child);
  33.         if (index === -1) {
  34.             return;
  35.         }
  36.         this.children.splice(index, 1);
  37.         child.parent = null;
  38.     }
  39.  
  40.     addSceneNodeChildren(children) {
  41.         const length = children.length;
  42.         for (let i = 0; i < length; i++) {
  43.             const child = children[i];
  44.             this.addSceneNodeChild(child);
  45.         }
  46.     }
  47.  
  48.     update(deltaTime) {
  49.         throw new Error("Should implemented");
  50.     }
  51.  
  52.     updateChildren(deltaTime) {
  53.         const length = this.children.length;
  54.         for (let i = 0; i < length; i++) {
  55.             const child = this.children[i];
  56.             child.update(deltaTime);
  57.         }
  58.     }
  59.  
  60.     shouldRender(context) {
  61.         return true;
  62.     }
  63.  
  64.     render() {
  65.         this.renderChildren();
  66.     }
  67.  
  68.     renderChildren() {
  69.         const length = this.children.length;
  70.         for (let i = 0; i < length; i++) {
  71.             const child = this.children[i];
  72.             if (child.shouldRender()) {
  73.                 child.render();
  74.             }
  75.         }
  76.     }
  77.  
  78.     debug(writer, depth = 0) {
  79.         const space = "  ".repeat(depth);
  80.         const id = this.id ? `#${this.id} ` : '';
  81.         const type = Object.getPrototypeOf(this).constructor.name;
  82.         writer(`${space}${type} ${id}`);
  83.         for (let child of this.children) {
  84.             child.debug(writer, depth + 1);
  85.         }
  86.     }
  87. }
  88.  
  89. class SceneManager extends SceneNode {
  90.     constructor(options) {
  91.         super(options);
  92.     }
  93. }
  94.  
  95. class Scene extends SceneNode {
  96.     constructor(options) {
  97.         super(options);
  98.     }
  99. }
  100.  
  101. class SceneLayer extends SceneNode {
  102.     constructor(options) {
  103.         super(options);
  104.     }
  105. }
  106.  
  107. class ChunkSceneNode extends SceneNode {
  108.     constructor(options) {
  109.         super(options);
  110.     }
  111. }
  112.  
  113. ///////////////////////////
  114.  
  115. const sceneManager = new SceneManager({
  116.     id: "manager",
  117. });
  118.  
  119. const primaryScene = new Scene({
  120.     id: "primary",
  121. });
  122.  
  123. sceneManager.addSceneNodeChild(primaryScene);
  124.  
  125. const chunksLayerSceneNode = new SceneLayer({
  126.     id: "chunksLayer",
  127. });
  128.  
  129. const guiSceneNode = new SceneLayer({
  130.     id: "guiLayer",
  131. });
  132.  
  133. primaryScene.addSceneNodeChild(chunksLayerSceneNode);
  134. primaryScene.addSceneNodeChild(guiSceneNode);
  135.  
  136. sceneManager.debug(console.log);
  137.  
  138. sceneManager.render();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement