ujs/src/js/scene.js

67 lines
1.2 KiB
JavaScript

import { Pointer } from './pointer.js';
export class Scene {
#canvas;
#context;
#layers;
constructor(canvas, context, width, height) {
this.#canvas = canvas;
this.#context = 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, true);
} else {
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()) {
// TODO: check types
this.#objects = Array();
objects.forEach((object) => {
this.#objects.push(object);
});
}
add(object) {
this.#objects.push(object);
}
objects() {
return this.#objects;
}
}