2024-07-30 18:56:24 +00:00
|
|
|
class Overworld {
|
|
|
|
constructor(config) {
|
|
|
|
this.element = config.element;
|
|
|
|
this.canvas = this.element.querySelector(".game-canvas");
|
|
|
|
this.ctx = this.canvas.getContext("2d");
|
|
|
|
this.map = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
startGameLoop() {
|
|
|
|
const step = () => {
|
|
|
|
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
|
|
|
|
|
|
const cameraPerson = this.map.gameObjects.hero;
|
|
|
|
|
|
|
|
// update all objects
|
|
|
|
Object.values(this.map.gameObjects).forEach(object => {
|
|
|
|
object.update({
|
|
|
|
arrow: this.directionInput.direction,
|
|
|
|
map: this.map,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
this.map.drawLowerImage(this.ctx, cameraPerson);
|
|
|
|
|
2024-08-02 14:01:51 +00:00
|
|
|
Object.values(this.map.gameObjects).sort((a,b) => {
|
|
|
|
return a.y - b.y;
|
|
|
|
}).forEach(object => {
|
2024-07-30 18:56:24 +00:00
|
|
|
object.sprite.draw(this.ctx, cameraPerson);
|
|
|
|
})
|
|
|
|
|
|
|
|
this.map.drawUpperImage(this.ctx, cameraPerson);
|
|
|
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
step();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
step();
|
|
|
|
}
|
|
|
|
|
2024-08-05 17:36:41 +00:00
|
|
|
bindActionInput() {
|
|
|
|
new KeyPressListener("Enter", () => {
|
|
|
|
// check if there is a person to talk to
|
|
|
|
this.map.checkForActionCutscene();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
bindHeroPositionCheck() {
|
|
|
|
document.addEventListener("PersonWalkingComplete", e => {
|
|
|
|
if (e.detail.whoId === "hero") {
|
|
|
|
// heros position changed
|
|
|
|
this.map.checkForFootstepCutscene()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
startMap(mapConfig) {
|
|
|
|
this.map = new OverworldMap(mapConfig);
|
|
|
|
this.map.overworld = this;
|
2024-07-30 18:56:24 +00:00
|
|
|
this.map.mountObjects();
|
2024-08-05 17:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
|
|
|
this.startMap(window.OverworldMaps.DemoRoom);
|
|
|
|
|
|
|
|
this.bindActionInput();
|
|
|
|
this.bindHeroPositionCheck();
|
2024-07-30 18:56:24 +00:00
|
|
|
|
|
|
|
this.directionInput = new DirectionInput();
|
|
|
|
this.directionInput.init();
|
|
|
|
|
|
|
|
this.startGameLoop();
|
2024-08-02 14:01:51 +00:00
|
|
|
|
|
|
|
this.map.startCutscene([
|
|
|
|
{ who: "hero", type: "walk", direction: "down" },
|
|
|
|
{ who: "hero", type: "walk", direction: "down" },
|
|
|
|
{ who: "hero", type: "walk", direction: "down" },
|
|
|
|
{ who: "npc1", type: "walk", direction: "right" },
|
2024-08-05 17:36:41 +00:00
|
|
|
{ who: "hero", type: "stand", direction: "left", time: 200 },
|
2024-08-06 15:55:14 +00:00
|
|
|
{ type: "textMessage", text: "Hi! Lorem ipsum" },
|
2024-08-02 14:01:51 +00:00
|
|
|
])
|
2024-07-30 18:56:24 +00:00
|
|
|
}
|
|
|
|
}
|