ujs/src/js/game.example.js

50 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-04-06 14:44:41 +03:00
import { App } from './app.js';
2023-04-06 18:46:01 +03:00
import { Scene, SceneLayer } from './scene.js';
import { Rect, StrokeRect } from './objects.js';
2023-04-06 14:44:41 +03:00
2023-04-23 17:01:17 +03:00
let Player = {
x: 10,
y: 10,
rect: null,
};
Player.rect = new Rect(Player.x, Player.y, 10, 10, 'black');
Player.rect.ticker = () => {
Player.rect.y = Player.y;
Player.rect.x = Player.x;
};
2023-04-06 18:46:01 +03:00
let app = new App(document.querySelector('canvas'), 400, 400);
let firstScene = new Scene(app.canvas, app.context, 400, 400);
let firstLayer = new SceneLayer('background', [
new Rect(50, 50, 100, 100, 'red'),
new StrokeRect(150, 150, 40, 40, 'green', 'blue', 1),
2023-04-23 17:01:17 +03:00
Player.rect,
2023-04-06 18:46:01 +03:00
]);
firstScene.addLayer(firstLayer);
app.scene = firstScene;
// add app view in document
2023-04-06 14:44:41 +03:00
window.addEventListener('DOMContentLoaded', () => {
2023-04-06 18:46:01 +03:00
document.body.appendChild(app.view);
2023-04-06 14:44:41 +03:00
});
2023-04-23 17:01:17 +03:00
document.addEventListener('keydown', (e) => {
switch (e.code) {
case 'ArrowUp':
Player.y -= 1;
break;
case 'ArrowDown':
Player.y += 1;
break;
case 'ArrowLeft':
Player.x -= 1;
break;
case 'ArrowRight':
Player.x += 1;
break;
}
});