ujs/examples/simple_scene/game.js

42 lines
875 B
JavaScript
Raw Normal View History

2023-11-14 00:44:26 +03:00
import * as ujs from './engine.js';
let app = new ujs.App(400, 400);
let player = {
x: 200 - 25,
y: 200 - 25,
obj: null,
};
player.obj = new ujs.Rect(player.x, player.y, 50, 50, 'red');
player.obj.ticker = () => {
player.obj.y = player.y;
player.obj.x = player.x;
};
let firstScene = new ujs.Scene(app.canvas, app.context, 400, 400);
let layerInstances = new ujs.SceneLayer('Instances', [player.obj]);
firstScene.addLayer(layerInstances);
app.scene = firstScene;
window.addEventListener('DOMContentLoaded', () => {
document.body.appendChild(app.view);
});
document.addEventListener('keydown', (e) => {
switch (e.code) {
case 'ArrowUp':
player.y -= 10;
break;
case 'ArrowDown':
player.y += 10;
break;
case 'ArrowLeft':
player.x -= 10;
break;
case 'ArrowRight':
player.x += 10;
break;
}
});