Newer
Older
import { WIDTH, HEIGHT, GAME_TITLE } from "./config.js";
import { Canvas } from "./canvas.js";
import { TextRenderer } from "./text.js";
import { Room, Object } from "./objects.js";
import { isKeyUp, whichKeyDown } from "./keyboard.js";
import { getParameter } from "./utils.js";
let currentFrame = 0;
let targetFrames = 60;
let lastFrameTime = performance.now();
canvas.fill("#222034");
let splash = new Image();
splash.src = assets.images.splash;
splash.onload = () => {
canvas.drawImage(splash, canvas.width / 2 - splash.width / 2, canvas.height / 2 - splash.height / 2);
let font = new Image();
font.src = assets.images.font;
font.onload = () => {
console.log("font loaded")
text = new TextRenderer(canvas, font);
window.onerror = (e) => {
// Entity class is here becuase otherwise every entity would need the canvas passed into it
class Entity extends Object {
constructor(x=0, y=0, spritesheet=null, sprite=null) {
this.x = x;
this.y = y;
this.sprite = sprite;
this.spritesheet = spritesheet;
let searchForRoom = (name) => {
// returns the room's index in the rooms array
for (let i = 0; i < rooms.length; i++) {
if (rooms[i].name == name) return i;
} throw new Error("Room not found:"+name+". Are you sure it's pushed?");
}
const changeRoom = (index) => {
currentRoom = rooms[index];
roomIndex = index;
loadingRoom.updateStatus = (status) => {
console.log(status);
canvas.fill("#222034");
canvas.drawImage(splash, canvas.width / 2 - splash.width / 2, canvas.height / 2 - splash.height / 2);
text.render(status, 0, 0);
}
debugRoom.init = () => {
if (!debug) changeRoom(searchForRoom("menu"));
}
debugStatuses.push("Current Frame:"+currentFrame+`(~${Math.round((currentFrame/targetFrames)*100)/100} sec)`);
}
const menuRoom = new Room("menu");
{"label": "Start Game", "action": _ => {changeRoom(searchForRoom("game"))}},
];
if (debug) menuRoom.options.push({"label": "Debug Room", "action": _ => {changeRoom(searchForRoom("debug"))}});
menuRoom.draw = () => {
canvas.fill("black");
}
menuRoom.drawGUI = () => {
text.render(GAME_TITLE, 8, 7*4);
for (let i = 0; i < menuRoom.options.length; i++) {
if (i == menuRoom.index) {
}
}
menuRoom.keyDown = (key) => {
if (pressedLastFrame.includes(key)) return;
const keyActions = {
ArrowUp: () => menuRoom.index--,
ArrowDown: () => menuRoom.index++,
Enter: () => menuRoom.options[menuRoom.index].action(),
};
const action = keyActions[key];
if (action) action();
if (menuRoom.index >= menuRoom.options.length) menuRoom.index = 0;
if (menuRoom.index < 0) menuRoom.index = menuRoom.options.length-1;
}
requestAnimationFrame(main);
let now = performance.now();
let delta = now - lastFrameTime;
if (delta < 1000 / targetFrames) return;
currentFrame++;
let currentKeys = whichKeyDown();
for (let i = 0; i < currentKeys.length; i++) {
if (isKeyUp(currentKeys[i]) && pressedLastFrame.includes(currentKeys[i])) continue;
currentRoom.keyDown(currentKeys[i]);
}
pressedLastFrame = currentKeys;
if (debug) {
text.render("FPS:" + Math.round(1000 / delta), 0, 0);
text.render(currentRoom.name, canvas.width-(text.charWidth*(currentRoom.name.length)), 0);
debugStatuses.push("Debug mode");
if (currentFrame <= 60*5) {
debugStatuses.push("Dimensions:"+canvas.width+"x"+canvas.height);
debugStatuses.push("Have fun!");
}
// console.debug(debugStatuses[i]);
if (typeof(debugStatuses[i]) == "string") text.render(debugStatuses[i], 0, canvas.height-text.charHeight*(debugStatuses.length-i));
if (typeof(debugStatuses[i]) == "object") {text.render(debugStatuses[i].msg, 0, canvas.height-text.charHeight*(debugStatuses.length-i)); debugStatuses[i].ttl--;}
let init = () => {
// begin loading all the assets.
currentRoom.updateStatus("Loading images...");
for (let image in assets.images) {
currentRoom.updateStatus("Loading image " + image);
let img = new Image();
img.src = assets.images[image];
img.onload = () => {
assets.images[image] = img;
}
}
(getParameter("room") ? changeRoom(searchForRoom(getParameter("room"))) : changeRoom(searchForRoom("menu")));