ujs/src/scene.js

81 lines
1.4 KiB
JavaScript

import { Pointer } from './pointer.js';
export class Scene {
#canvas;
#context;
#layers;
constructor(app, width, height) {
this.#canvas = app.canvas;
this.#context = app.context;
this.#layers = Array();
this.setScreenSize(width, height);
}
run() {
this.#layers.forEach((layer) => {
layer.objects().forEach((item) => {
if (typeof item.ticker == 'function') {
item.ticker();
}
if (typeof item.draw == 'function') {
item.draw(this.#context, layer.options.debug);
} else {
if (item.constructor.name != 'Empty') {
console.log(`⛔ Error display '${item.constructor.name}' object.`);
}
}
});
});
}
addLayer(layer) {
this.#layers.push(layer);
}
removeLayers() {
this.#layers = Array();
}
// TODO: remove it
setScreenSize(w, h) {
this.#canvas.width = w;
this.#canvas.height = h;
}
}
export class SceneLayer {
#objects;
constructor(
name,
objects = Array(),
options = {
debug: false,
}
) {
// TODO: check types
this.#objects = Array();
this.options = options;
// check options || FIXIT:
if (typeof this.options.debug === 'undefined') {
this.options.debug = false;
}
objects.forEach((object) => {
this.#objects.push(object);
});
}
add(object) {
this.#objects.push(object);
}
objects() {
return this.#objects;
}
}