-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverworld.js
143 lines (115 loc) · 4.26 KB
/
Overworld.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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 = () => {
//Limpiar canvas de dibujos anteriores
this.ctx.clearRect(0,0, this.canvas.width, this.canvas.height);
//Establecer el personaje de la camara
const cameraPerson = this.map.gameObjects.hero;
//Update todos los objetos
Object.values(this.map.gameObjects).forEach(object => {
object.update({
arrow: this.directionInput.direction,
map: this.map,
})
})
//Dibuja lower layer
this.map.drawLowerImage(this.ctx, cameraPerson);
//Dibuja game objects
Object.values(this.map.gameObjects).sort((a,b) => {
return a.y - b.y;
}).forEach(object => {
object.sprite.draw(this.ctx, cameraPerson);
})
//Dibuja upper layer
this.map.drawUpperImage(this.ctx, cameraPerson);
if (!this.map.isPaused) {
setTimeout(function() {
requestAnimationFrame(() => {
step();
})},1000 / 60);
}
}
step();
}
bindActionInput() {
new KeyPressListener("Enter", () => {
//Hay un npc aca para hablar?
this.map.checkForActionCutscene()
});
new KeyPressListener("Escape", () => {
if (!this.map.isCutscenePlaying) {
this.map.startCutscene([
{type: "pause"}
])
}
});
}
bindHeroPositionCheck() {
document.addEventListener("PersonWalkingComplete", e => {
if (e.detail.whoId === "hero") {
//La posicion del heroe cambio
this.map.checkForFootstepCutscene()
}
})
}
startMap(mapConfig, heroInitialState = null){
this.map = new OverworldMap(mapConfig);
this.map.overworld = this;
this.map.mountObjects();
if (heroInitialState) {
const {hero} = this.map.gameObjects;
this.map.removeWall(hero.x, hero.y);
hero.x = heroInitialState.x;
hero.y = heroInitialState.y;
hero.direction = heroInitialState.direction;
this.map.addWall(hero.x, hero.y);
}
this.progress.mapId = mapConfig.id;
this.progress.startingHeroX = this.map.gameObjects.hero.x;
this.progress.startingHeroY = this.map.gameObjects.hero.y;
this.progress.startingHeroDirection = this.map.gameObjects.hero.direction;
}
async init () {
const container = document.querySelector(".game-container");
//Crea un nuevo Progress tracker
this.progress = new Progress();
//Muestra la Title Screen
this.titleScreen = new TitleScreen ({
progress: this.progress
})
const useSaveFile = await this.titleScreen.init(container);
//Carga el Progress guardado, si lo hay
let initialHeroState = null;
if (useSaveFile) {
this.progress.load();
initialHeroState = {
x: this.progress.startingHeroX,
y: this.progress.startingHeroY,
direction: this.progress.startingHeroDirection,
}
}
//Carga el HUD
this.hud = new Hud();
this.hud.init(container);
//Empieza el mapa
this.startMap(window.OverworldMaps[this.progress.mapId], initialHeroState );
//Crea los controles
this.bindActionInput();
this.bindHeroPositionCheck();
this.directionInput = new DirectionInput();
this.directionInput.init();
//Empieza el juego!
this.startGameLoop();
// this.map.startCutscene([
// { type: "battle", enemyId: "beth" }
// // { type: "changeMap", map: "DemoRoom"}
// // { type: "textMessage", text: "Hey, hola amigo!"}
// ])
}
}