Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @property {SceneNode[]} children
- */
- class SceneNode {
- constructor(options) {
- this.id = options.id;
- this.parent = options.parent || null;
- this.children = [];
- }
- getRoot() {
- let node = this;
- while (true) {
- if (node.parent === null) {
- return node;
- }
- node = node.parent;
- }
- }
- addSceneNodeChild(child) {
- const oldChildParent = child.parent;
- if (oldChildParent) {
- oldChildParent.removeChild(child);
- }
- child.parent = this;
- this.children.push(child);
- }
- removeChild(child) {
- const index = this.children.indexOf(child);
- if (index === -1) {
- return;
- }
- this.children.splice(index, 1);
- child.parent = null;
- }
- addSceneNodeChildren(children) {
- const length = children.length;
- for (let i = 0; i < length; i++) {
- const child = children[i];
- this.addSceneNodeChild(child);
- }
- }
- update(deltaTime) {
- throw new Error("Should implemented");
- }
- updateChildren(deltaTime) {
- const length = this.children.length;
- for (let i = 0; i < length; i++) {
- const child = this.children[i];
- child.update(deltaTime);
- }
- }
- shouldRender(context) {
- return true;
- }
- render() {
- this.renderChildren();
- }
- renderChildren() {
- const length = this.children.length;
- for (let i = 0; i < length; i++) {
- const child = this.children[i];
- if (child.shouldRender()) {
- child.render();
- }
- }
- }
- debug(writer, depth = 0) {
- const space = " ".repeat(depth);
- const id = this.id ? `#${this.id} ` : '';
- const type = Object.getPrototypeOf(this).constructor.name;
- writer(`${space}${type} ${id}`);
- for (let child of this.children) {
- child.debug(writer, depth + 1);
- }
- }
- }
- class SceneManager extends SceneNode {
- constructor(options) {
- super(options);
- }
- }
- class Scene extends SceneNode {
- constructor(options) {
- super(options);
- }
- }
- class SceneLayer extends SceneNode {
- constructor(options) {
- super(options);
- }
- }
- class ChunkSceneNode extends SceneNode {
- constructor(options) {
- super(options);
- }
- }
- ///////////////////////////
- const sceneManager = new SceneManager({
- id: "manager",
- });
- const primaryScene = new Scene({
- id: "primary",
- });
- sceneManager.addSceneNodeChild(primaryScene);
- const chunksLayerSceneNode = new SceneLayer({
- id: "chunksLayer",
- });
- const guiSceneNode = new SceneLayer({
- id: "guiLayer",
- });
- primaryScene.addSceneNodeChild(chunksLayerSceneNode);
- primaryScene.addSceneNodeChild(guiSceneNode);
- sceneManager.debug(console.log);
- sceneManager.render();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement