ujs/src/main.js

77 lines
1.9 KiB
JavaScript
Raw Normal View History

2023-02-16 00:04:40 +03:00
import { Settings } from './settings.js';
import { Pointer } from './pointer.js';
2023-04-30 15:26:00 +03:00
import { Scene, SceneLayer } from './scene.js';
2023-04-30 20:39:57 +03:00
import { Rect, StrokeRect, Sprite, TiledSprite, Empty } from './objects.js';
2023-02-16 00:04:40 +03:00
2023-04-06 14:44:41 +03:00
export class App {
2023-04-23 17:11:41 +03:00
#version;
2023-04-06 18:46:01 +03:00
constructor(
2023-12-01 16:35:43 +03:00
width,
height,
2023-04-06 18:46:01 +03:00
options = {
backgroundColor: '#ffcc68',
2023-04-23 17:11:41 +03:00
welcome: true,
2023-12-01 15:55:39 +03:00
}
2023-04-06 18:46:01 +03:00
) {
2023-04-30 15:55:44 +03:00
this.#version = 'beeeeeeta';
2023-04-23 17:11:41 +03:00
2023-12-01 16:35:43 +03:00
// Create CANVAS element
2023-04-06 18:46:01 +03:00
this.view = document.createElement('canvas');
this.canvas = this.view;
2023-04-06 15:03:09 +03:00
this.context = this.canvas.getContext('2d');
2023-12-01 16:35:43 +03:00
this.options = options;
2023-04-23 17:11:41 +03:00
// check options || FIXIT:
if (typeof this.options.backgroundColor === 'undefined') {
this.options.backgroundColor = '#ffcc68';
}
if (typeof this.options.welcome === 'undefined') {
this.options.welcome = true;
}
this.scene = new Scene(this, width, height);
2023-02-16 00:04:40 +03:00
2023-04-30 16:39:54 +03:00
const logStrings = [
'ujs engine',
`version: ${this.#version}`,
'feedback: iiiypuk {dog} fastmail.fm',
2023-12-01 16:16:20 +03:00
'sources: https://git.a2s.su/emilecok/ujs',
2023-04-30 16:39:54 +03:00
];
2023-04-23 17:11:41 +03:00
if (this.options.welcome) {
2023-04-30 16:39:54 +03:00
logStrings.forEach((str) => {
console.log(str);
});
2023-04-23 17:11:41 +03:00
}
2023-02-16 00:04:40 +03:00
Pointer.init();
2023-12-01 16:35:43 +03:00
Settings.fpsPrevTime = Date.now();
2023-02-16 00:04:40 +03:00
2023-12-01 16:35:43 +03:00
// Start :)
this.#run();
2023-02-16 00:04:40 +03:00
}
#run = () => {
2023-12-01 15:55:39 +03:00
// Calculating FPS
2023-12-01 16:14:25 +03:00
let fpsNewTime = Date.now();
Settings.fpsDelta = (fpsNewTime - Settings.fpsPrevTime) / 1000;
Settings.fpsPrevTime = fpsNewTime;
Settings.fps = Math.round(1 / Settings.fpsDelta);
2023-02-16 00:04:40 +03:00
2023-12-01 16:35:43 +03:00
// Clear canvas
2023-04-06 18:46:01 +03:00
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
2023-12-01 16:35:43 +03:00
// Fill canvas by default background color
2023-04-06 18:46:01 +03:00
this.context.fillStyle = this.options.backgroundColor;
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
2023-12-01 16:35:43 +03:00
// Draw scene
2023-02-16 00:04:40 +03:00
this.scene.run();
requestAnimationFrame(this.#run);
2023-04-06 18:03:40 +03:00
};
2023-02-16 00:04:40 +03:00
}
2023-04-30 15:26:00 +03:00
2023-04-30 20:39:57 +03:00
export { Scene, SceneLayer, Empty, Rect, StrokeRect, Sprite, TiledSprite };