-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverworldMap.js
More file actions
207 lines (193 loc) · 6.23 KB
/
OverworldMap.js
File metadata and controls
207 lines (193 loc) · 6.23 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
class OverworldMap {
constructor(config) {
// for changing maps
this.overworld = null;
this.gameObjects = config.gameObjects;
this.walls = config.walls || {};
this.cutsceneSpaces = config.cutsceneSpaces || {};
this.lowerImage = new Image();
this.lowerImage.src = config.lowerSrc;
this.upperImage = new Image();
this.upperImage.src = config.upperSrc;
this.isCutscenePlaying = false;
}
drawLowerImage(ctx, cameraPerson) {
ctx.drawImage(
this.lowerImage,
utils.withGrid(10.5) - cameraPerson.x,
utils.withGrid(6) - cameraPerson.y
);
}
drawUpperImage(ctx, cameraPerson) {
ctx.drawImage(
this.upperImage,
utils.withGrid(10.5) - cameraPerson.x,
utils.withGrid(6) - cameraPerson.y
);
}
isSpaceTaken(currentX, currentY, direction) {
const { x, y } = utils.nextPosition(currentX, currentY, direction);
return this.walls[`${x},${y}`] || false;
}
mountObjects() {
//iterate through keys to set an id
Object.keys(this.gameObjects).forEach((key) => {
let object = this.gameObjects[key];
object.id = key;
//TODO: determine if this object should actually mount
object.mount(this);
});
}
async startCutscene(events) {
//set flag to true
this.isCutscenePlaying = true;
// maps through the cutscene moves and acts them out
for (let i = 0; i < events.length; i++) {
const eventHandler = new OverworldEvent({
event: events[i],
map: this,
});
await eventHandler.init();
}
//once run set back to false
this.isCutscenePlaying = false;
//Reset NPCs to do their idle behaviour so they go back to doing their programmed behaviour;
Object.values(this.gameObjects).forEach((object) =>
object.doBehaviourEvent(this)
);
}
checkForActionCutscene() {
// where is the hero
const hero = this.gameObjects["hero"];
// uses nextPosition to find if there is anyone to talk to
const nextCoords = utils.nextPosition(hero.x, hero.y, hero.direction);
// iterate through all of our gameObjects to see if anyone is next to hero and see if they have something to say
const match = Object.values(this.gameObjects).find((object) => {
// if the nextCoords match the coords of a character it will return the coords
return `${object.x},${object.y}` === `${nextCoords.x},${nextCoords.y}`;
});
// if there is a match and the character that matches has a talking array then contains talk then start a cutscene
// at the first index
if (!this.isCutscenePlaying && match && match.talking.length) {
this.startCutscene(match.talking[0].events);
}
}
checkForFootstepCutscene() {
const hero = this.gameObjects["hero"];
const match = this.cutsceneSpaces[`${hero.x},${hero.y}`];
if (!this.isCutscenePlaying && match) {
this.startCutscene(match[0].events);
}
}
addWall(x, y) {
this.walls[`${x},${y}`] = true;
}
removeWall(x, y) {
delete this.walls[`${x},${y}`];
}
moveWall(wasX, wasY, direction) {
this.removeWall(wasX, wasY);
const { x, y } = utils.nextPosition(wasX, wasY, direction);
this.addWall(x, y);
}
}
window.OverworldMaps = {
DemoRoom: {
lowerSrc: "/images/maps/DemoLower.png",
upperSrc: "/images/maps/DemoUpper.png",
gameObjects: {
hero: new Person({
isPlayerControlled: true,
x: utils.withGrid(5),
y: utils.withGrid(6),
}),
npcA: new Person({
x: utils.withGrid(7),
y: utils.withGrid(9),
src: "/images/characters/cat1.png",
// defines the behaviour of the characters
behaviourLoop: [
{ type: "stand", direction: "left", time: 800 },
{ type: "stand", direction: "up", time: 800 },
{ type: "stand", direction: "right", time: 1200 },
{ type: "stand", direction: "up", time: 300 },
],
//
talking: [
{
events: [
{ type: "textMessage", text: "I'm busy...", faceHero: "npcA" },
{ type: "textMessage", text: "Go away!" },
{ who: "hero", type: "walk", direction: "up" },
],
},
],
}),
npcB: new Person({
x: utils.withGrid(8),
y: utils.withGrid(5),
src: "/images/characters/cat4.png",
// behaviourLoop: [
// { type: "walk", direction: "left" },
// { type: "stand", direction: "up", time: 800 },
// { type: "walk", direction: "up" },
// { type: "walk", direction: "right" },
// { type: "walk", direction: "down" },
// ],
}),
},
// setting walls that cannot be walked into
walls: {
[utils.asGridCoord(7, 6)]: true,
[utils.asGridCoord(8, 6)]: true,
[utils.asGridCoord(7, 7)]: true,
[utils.asGridCoord(8, 7)]: true,
},
cutsceneSpaces: {
[utils.asGridCoord(7, 4)]: [
{
//possible events that can happen on this square
events: [
{ who: "npcB", type: "walk", direction: "left" },
{ who: "npcB", type: "stand", direction: "up", time: 500 },
{ type: "textMessage", text: "You can't be in there!" },
{ who: "npcB", type: "walk", direction: "right" },
{ who: "hero", type: "walk", direction: "down" },
{ who: "hero", type: "walk", direction: "left" },
],
},
],
// change map event
// the space which triggers the map change
[utils.asGridCoord(5, 10)]: [
{
// this info gets passed into the changeMap function in OverworldEvent
events: [{ type: "changeMap", map: "Kitchen" }],
},
],
},
},
Kitchen: {
lowerSrc: "/images/maps/KitchenLower.png",
upperSrc: "/images/maps/KitchenUpper.png",
gameObjects: {
hero: new Person({
isPlayerControlled: true,
x: utils.withGrid(5),
y: utils.withGrid(5),
}),
npcB: new Person({
x: utils.withGrid(10),
y: utils.withGrid(8),
src: "/images/characters/cat5.png",
talking: [
{
events: [
{ type: "textMessage", text: "You made it!", faceHero: "npcB" },
],
},
],
}),
},
},
};