game-engine/index.js

141 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

import { GAME_HEIGHT, GAME_WIDTH, TILE_SIZE } from "./modules/constants.js";
2024-09-13 21:45:59 -05:00
import {
Camera,
CanvasResizer,
2024-09-14 18:38:50 -05:00
FpsCounter,
2024-09-13 21:45:59 -05:00
GameObject,
MapManagement,
2024-09-20 15:55:32 -05:00
Player,
SpriteSheet,
2024-09-13 21:45:59 -05:00
} from "./modules/game-objects/index.js";
2024-09-14 23:19:41 -05:00
const backgroundMaps = [
2024-09-13 21:45:59 -05:00
{
name: "overworld",
imageId: "overworld",
elementId: "level1",
2024-09-14 23:19:41 -05:00
layer: 0,
2024-09-13 21:45:59 -05:00
selected: true,
},
{ name: "ocean", imageId: "overworld", elementId: "level2" },
];
2024-09-14 23:19:41 -05:00
const foregroundMaps = [
{
name: "overworld",
imageId: "overworld",
elementId: "level1",
layer: 1,
selected: true,
},
{ name: "ocean", imageId: "overworld", elementId: "level2", layer: 2 },
2024-09-14 23:19:41 -05:00
];
const foregroundCollisionMaps = [
{
name: "overworld",
imageId: "overworld",
elementId: "level1",
layer: 2,
selected: true,
collision: true,
},
{
name: "ocean",
imageId: "overworld",
elementId: "level2",
layer: 2,
collision: true,
},
];
2024-09-13 21:45:59 -05:00
const clicableObjects = ["debug", "level1", "level2"];
const playerAnimations = {
idle: {
left: [51],
right: [17],
top: [34],
bottom: [0],
},
walk: {
left: [51, 52, 53, 54],
right: [17, 18, 19, 20],
top: [34, 35, 36, 37],
bottom: [0, 1, 2, 3],
},
};
2024-09-13 21:45:59 -05:00
class Game extends GameObject {
constructor({ canvas }) {
super();
2024-09-13 16:14:53 -05:00
2024-09-13 21:45:59 -05:00
const canvasResizer = new CanvasResizer({
canvas: canvas,
width: GAME_WIDTH,
height: GAME_HEIGHT,
percentage: 0.9,
});
const player = new Player({
speed: 1,
gameObjects: [new SpriteSheet({ imageId: "character", tileHeight: 32 })],
animations: playerAnimations,
x: 6 * TILE_SIZE,
y: 4 * TILE_SIZE,
width: TILE_SIZE,
height: 2 * TILE_SIZE,
});
2024-09-13 23:30:28 -05:00
const camera = new Camera({
2024-09-14 23:19:41 -05:00
gameObjects: [
new MapManagement({ maps: backgroundMaps }),
new MapManagement({ maps: foregroundMaps }),
player,
new MapManagement({ maps: foregroundCollisionMaps }),
2024-09-14 23:19:41 -05:00
],
target: player,
2024-09-13 23:30:28 -05:00
});
const fpsCounter = new FpsCounter();
2024-09-14 18:38:50 -05:00
this.gameObjects = [canvasResizer, camera, fpsCounter];
2024-09-13 16:14:53 -05:00
2024-09-13 21:45:59 -05:00
this.canvas = canvas;
this.ctx = this.canvas.getContext("2d");
this.lastTime = 0;
2024-09-14 17:43:53 -05:00
}
2024-09-14 17:43:53 -05:00
async load() {
await super.load();
2024-09-13 21:45:59 -05:00
clicableObjects.forEach((elementId) => {
document.getElementById(elementId).addEventListener("click", () => {
this.onMouseClick(elementId);
});
2024-09-14 17:43:53 -05:00
});
document.addEventListener("keydown", (event) => {
this.onKeyPressed(event.key);
});
document.addEventListener("keyup", (event) => {
this.onKeyReleased(event.key);
2024-09-13 21:45:59 -05:00
});
}
clear(ctx) {
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
loop(time) {
const delta = (time - this.lastTime) / 1000;
this.lastTime = time;
this.update(delta);
this.clear(this.ctx);
this.render(this.ctx);
requestAnimationFrame(this.loop.bind(this));
}
}
async function run() {
2024-09-13 21:45:59 -05:00
const game = new Game({ canvas: document.getElementById("game") });
await game.load();
game.loop(0);
}
run();