ujs/src/scene.js

81 lines
1.4 KiB
JavaScript
Raw Permalink 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(app, width, height) {
this.#canvas = app.canvas;
this.#context = app.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') {
2023-04-23 19:41:18 +03:00
item.draw(this.#context, layer.options.debug);
2023-04-23 18:50:36 +03:00
} else {
2023-04-30 20:39:57 +03:00
if (item.constructor.name != 'Empty') {
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-23 19:28:58 +03:00
// TODO: remove it
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;
2023-04-23 19:41:18 +03:00
constructor(
name,
objects = Array(),
options = {
debug: false,
2023-12-01 15:55:39 +03:00
}
2023-04-23 19:41:18 +03:00
) {
2023-04-06 18:46:01 +03:00
// TODO: check types
this.#objects = Array();
2023-04-23 19:41:18 +03:00
this.options = options;
// check options || FIXIT:
if (typeof this.options.debug === 'undefined') {
this.options.debug = false;
}
2023-04-06 18:46:01 +03:00
objects.forEach((object) => {
this.#objects.push(object);
});
}
add(object) {
this.#objects.push(object);
}
objects() {
return this.#objects;
}
}