ujs/src/js/scene.js

66 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-02-16 00:04:40 +03:00
import { Pointer } from './pointer.js';
export class Scene {
2023-04-06 15:03:09 +03:00
#canvas;
#context;
2023-04-06 16:52:57 +03:00
#layers;
2023-04-06 15:03:09 +03:00
constructor(canvas, context, width, height) {
this.#canvas = canvas;
this.#context = context;
2023-04-06 16:52:57 +03:00
this.#layers = Array();
2023-04-06 14:44:41 +03:00
this.setScreenSize(width, height);
2023-02-16 00:04:40 +03:00
}
run() {
2023-04-23 17:01:17 +03:00
this.#layers.forEach((layer) => {
layer.objects().forEach((item) => {
if (typeof item.ticker == 'function') {
item.ticker();
}
2023-04-23 18:50:36 +03:00
if (typeof item.draw == 'function') {
item.draw(this.#context, true);
} else {
console.log(`⛔ Error display '${item.constructor.name}' object.`);
2023-04-06 17:15:27 +03:00
}
});
2023-04-06 16:52:57 +03:00
});
}
2023-04-06 17:15:27 +03:00
addLayer(layer) {
this.#layers.push(layer);
}
removeLayers() {
this.#layers = Array();
2023-02-16 00:04:40 +03:00
}
2023-04-06 14:44:41 +03:00
setScreenSize(w, h) {
2023-04-06 15:03:09 +03:00
this.#canvas.width = w;
this.#canvas.height = h;
2023-02-16 00:04:40 +03:00
}
}
2023-04-06 18:46:01 +03:00
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;
}
}