game-engine/public/script.js
2023-07-23 17:41:23 +01:00

185 lines
5.1 KiB
JavaScript

import { Scuti } from '../src/Scuti';
import { Room } from '../src/objects/rooms/Room';
import { FloorMaterial } from '../src/objects/rooms/materials/FloorMaterial';
import { WallMaterial } from '../src/objects/rooms/materials/WallMaterial';
import { FloorFurniture } from '../src/objects/furnitures/FloorFurniture';
import { WiredSelectionFilter } from '../src/objects/filters/WiredSelectionFilter';
import { WallFurniture } from '../src';
(async () => {
const renderer = new Scuti({
canvas: document.getElementById('app'),
width: window.innerWidth,
height: window.innerHeight
});
await renderer.loadResources('https://kozennnn.github.io/scuti-resources/');
const tileMap = 'xxxxxxxxxxxxxxxxxxxxxx\n' +
'xxxxxxxxxxxxxxxxxxxxxx\n' +
'xxxxxxxxxxxxxxxxxxxxxx\n' +
'xxxx11100001xxxxxxxxxx\n' +
'xxxx00000000xxxxxxxx0x\n' +
'xxx0000000000000xxxx00\n' +
'xxxx00000000xxxxxxxx00\n' +
'xxxx000xxx00xxxxxxxx00\n' +
'xxxx00000x00xx0xxxxx0x\n' +
'xxxxx00xxx00xx0000xx00\n' +
'xxxxx0000000xx0xx0xx0x\n' +
'xxxxx0000000xx0x0000x0\n' +
'xxxxx0000000xx0xx000x0\n' +
'xxxxxxxxx00xxx00000x00\n' +
'xxxxxxxxxxxxxxx000xx0x\n' +
'xxxxxxxxxxxxxxxxxxxxxx\n' +
'xxxxxxxxxxxxxxxxxxxxxx\n' +
'xxxxxx00x00xxxxxxxxxxx\n' +
'xxxxxxxxxxxxxxxxxxxxxx\n' +
'xxxxxxxxxxxxxxxxxxxxxx\n' +
'xxxxxxxx00xxxxxxxxxxxx\n' +
'xxxxx00000x0xxxxxxxxxx\n' +
'xxxxxxx00000000000xxxx\n' +
'xxxxxxxx0xx000000x00xx\n' +
'xxxxxxxxxxxxxxxx00xxxx\n';
const room = new Room(renderer, {
tileMap,
floorMaterial: new FloorMaterial(renderer, 110),
wallMaterial: new WallMaterial(renderer, 2301)
});
const furniture = new FloorFurniture({
id: 4967,
position: {
x: 5,
y: 4,
z: 0
},
direction: 2,
state: 1
});
room.objects.add(furniture);
furniture.onPointerDown = () => {
console.log('clicked');
};
const furniture3 = new FloorFurniture({
id: 8916,
position: {
x: 10,
y: 10,
z: 0
},
direction: 2,
state: 1
});
const furniture2 = new FloorFurniture({
id: 8916,
position: {
x: 8,
y: 10,
z: 0
},
direction: 2,
state: 1
});
const wallFurniture = new WallFurniture({
id: 4625,
position: {
x: -1,
y: 2,
offsetX: 2,
offsetY: -25
},
direction: 2,
state: 2
});
const wallFurniture2 = new WallFurniture({
id: 4032,
position: {
x: 3,
y: -1,
offsetX: 4,
offsetY: -30
},
direction: 4,
state: 1
});
room.objects.add(furniture3);
room.objects.add(furniture2);
room.objects.add(wallFurniture);
room.objects.add(wallFurniture2);
setTimeout(() => {
return wallFurniture.move({
x: -1,
y: 3,
offsetX: 2,
offsetY: -25
});
}, 3000);
setTimeout(() => {
return wallFurniture.move({
x: -1,
y: 5,
offsetX: 2,
offsetY: -25
});
}, 5000);
furniture3.onLoadComplete = () => {
console.log('loaded!');
};
room.tiles.onPointerDown = (event) => {
furniture.move(event.position);
};
document.onkeydown = (e) => {
e = e || window.event;
if (e.keyCode == '38') {
if (room.camera.zoomLevel <= 1) {
room.camera.zoomLevel = room.camera.zoomLevel * 2;
}
else {
room.camera.zoomLevel += 1;
}
}
else if (e.keyCode == '40') {
if (room.camera.zoomLevel <= 1) {
room.camera.zoomLevel = room.camera.zoomLevel / 2;
}
else {
room.camera.zoomLevel -= 1;
}
}
else if (e.keyCode == '37') {
furniture.rotate(4);
}
else if (e.keyCode == '39') {
const filter = new WiredSelectionFilter(0xffffff, 0x999999);
furniture.addFilter(filter);
}
};
})().catch((error) => {
return console.error(error);
});
function dice(room, x, y, z) {
const furni5 = new FloorFurniture({
position: {
x,
y,
z
},
direction: 0,
id: 284,
state: 1
});
room.objects.add(furni5);
let timeout;
furni5.onDoubleClick = (event) => {
console.log(event);
console.log('clicked furni5', event);
if (event.tag === 'activate') {
clearTimeout(timeout);
furni5.state = -1;
timeout = setTimeout(() => {
furni5.state = Math.floor(Math.random() * 6) + 1;
}, 1000);
}
else {
clearTimeout(timeout);
furni5.state = 0;
}
};
}