-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
326 lines (281 loc) · 8.46 KB
/
game.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Planilandia - a friend-making RPG
*/
// Loader
thingsToLoad = [
"maps/building.json",
"images/assets.json",
"images/tileset.png",
"images/buttons.json",
"images/buttons.png",
"maps/world.json",
"audio/bloop.mp3",
"audio/close.mp3",
"audio/gibberish.mp3",
"audio/lose.wav",
"audio/pop.mp3",
"audio/step.mp3",
"audio/win.wav"
];
//Create a new Hexi instance, and start it.
g = hexi(WIDTH, HEIGHT, setup, thingsToLoad);
//Start Hexi
g.start();
//The setup function to initialize the application
function setup() {
sfxBloop = g.sound("audio/bloop.mp3");
sfxClose = g.sound("audio/close.mp3");
sfxGibberish = g.sound("audio/gibberish.mp3");
sfxLose = g.sound("audio/lose.wav");
sfxPop = g.sound("audio/pop.mp3");
sfxStep = g.sound("audio/step.mp3");
sfxWin = g.sound("audio/win.wav");
world_state = "world";
// Make a map of inside a building
building_world = g.makeTiledWorld(
"maps/building.json",
"images/tileset.png"
);
outside_world = g.makeTiledWorld(
"maps/world.json",
"images/tileset.png"
);
world = outside_world;
//call setup methods
setupSprites();
setupItems();
setupDialogue();
// setup trigger NPC's
setupTriggers();
setupQuests();
setupNPCs();
setupScenes();
// Setup player sprite that is visible
let frames = ["penguin_1.png", "penguin_2.png","penguin_1.png", "penguin_3.png", "penguin_4.png", "penguin_5.png", "penguin_6.png", "penguin_7.png", "penguin_6.png", "penguin_8.png"];
player_tex = g.sprite(frames);
player_tex.show(0);
player_tex.position = player.position;
world.addChild(player_tex);
// Add a world camera to follow the player
camera = g.worldCamera(world, world.worldWidth, world.worldHeight);
camera.centerOver(player);
// Get data from the map
wallMapArray = world.getObject("wallLayer").data;
npcArray = world.getObject("npcLayer").data;
itemLayerArray = world.getObject("itemLayer").data;
doorMapArray = world.getObject("doorLayer").data;
itemMapArray = world.getObject("itemLayer").data;
//Give the `player` a `direction` property
player.direction = "";
// Set directional objects
leftArrow = g.keyboard(KEY_LEFT);
upArrow = g.keyboard(KEY_UP);
rightArrow = g.keyboard(KEY_RIGHT);
downArrow = g.keyboard(KEY_DOWN);
interact = g.keyboard(ACTION_KEY);
questkey = g.keyboard(QUEST_KEY);
menukey = g.keyboard(MENU_KEY);
inventorykey = g.keyboard(INVENTORY_KEY);
//Program the keyboard objects; moves while the key is pressed, stops when released
leftArrow.press = () => player.direction = "left";
upArrow.press = () => player.direction = "up";
rightArrow.press = () => player.direction = "right";
downArrow.press = () => player.direction = "down";
// Stop the player when the button is released
leftArrow.release = () => player.direction = "none";
upArrow.release = () => player.direction = "none";
rightArrow.release = () => player.direction = "none";
downArrow.release = () => player.direction = "none";
sceneState = "play";
// Player attempts interaction
interact.press = function() {
console.log("interaction");
checkForDoor();
};
//pull up quests page by pressing 'q'
questkey.press = function() {
if (sceneState == "play")
{
sceneState = "quests";
console.log("quests");
dispQuestList();
}
else
{
sceneState = "play";
dispGame();
}
};
inventorykey.press = function() {
if (sceneState == "inventory")
{
sceneState = "play";
dispGame();
}
else
{
console.log("inventory");
sceneState = "inventory";
dispInventory();
}
};
menukey.press = function() {
if (sceneState == "menu")
{
sceneState = "play";
dispGame();
}
else
{
sceneState = "menu";
console.log("menu");
dispMenu();
}
};
//Change the game state to `play`
g.state = dispTitle;
}
function checkForDoor()
{
let playerVsDoor = g.hitTestTile(player, doorMapArray, 9, world, "center");
if (playerVsDoor.hit) {
player.vx = 0;
player.vy = 0;
player.direction = "none";
switch(world_state) {
case "building":
player.position.y = Math.floor(player.position.y / TILE_SIZE) * TILE_SIZE;
world_state = "world";
break;
case "world":
player.position.y = Math.floor(player.position.y / TILE_SIZE) * TILE_SIZE + TILE_SIZE;
world_state = "building";
break;
}
loadMap(world_state);
}
}
function checkGameOver()
{
var i, count = 0;
for (i = 0; i < allQuestsArray.length; i++)
{
if (allQuestsArray[i].questState === QUEST_COMPLETE)
{
count++;
//console.log("completed: " + count);
}
}
if (count === allQuestsArray.length)
{
console.log("game over");
dispGameOver();
}
}
function reset()
{
questArray = [];
allQuestsArray = [];
questNPCArray = [];
regNPCArray = [];
questTextArray = [];
itemArray = [];
inventory = [];
worldTriggerArray = [];
buildingTriggerArray = [];
g.remove(gameScene);
g.remove(dialogueScene);
g.remove(menuScene);
g.remove(questListScene);
g.remove(titleScene);
g.remove(inventoryScene);
g.remove(gameOverScene);
setup();
g.state = play;
//g.resume();
}
function loadMap(MAP) {
world.visible = false;
switch(MAP) {
case "building":
world = building_world;
break;
case "world":
world = outside_world;
break;
}
world.visible = true;
// Setup the player
player = world.getObject("player");
//Give the `player` a `direction` property
player.direction = "";
world.addChild(player_tex);
player_tex.position = player.position;
updatePlayerTexture();
// Add a camera to follow the player in both outside and inside worlds
camera = g.worldCamera(world, world.worldWidth, world.worldHeight);
camera.centerOver(player);
loadWallsAndDoors(world);
}
function loadWallsAndDoors(MAP) {
wallMapArray = MAP.getObject("wallLayer").data;
doorMapArray = MAP.getObject("doorLayer").data;
npcArray = MAP.getObject("npcLayer").data;
doors = MAP.getObjects("door");
}
//The `play` function contains all the game logic and runs in a loop
function play() {
// Handle player movement
player_tex.position = player.position;
if (Math.floor(player.x) % world.tilewidth === 0 && Math.floor(player.y) % world.tileheight === 0) {
switch (player.direction) {
case "up":
player_tex.playAnimation([6, 9]);
player.vy = -MOVE_SPEED;
player.vx = 0;
break;
case "down":
player_tex.playAnimation([0, 3]);
player.vy = MOVE_SPEED;
player.vx = 0;
break;
case "left":
player_tex.playAnimation([4]);
player.vx = -MOVE_SPEED;
player.vy = 0;
break;
case "right":
player_tex.playAnimation([5]);
player.vx = MOVE_SPEED;
player.vy = 0;
break;
case "none":
player.vx = 0;
player.vy = 0;
break;
}
updatePlayerTexture();
}
//Move the player and camera
g.move(player);
camera.follow(player);
//checks for collision with wall, NPC, item, or trigger
playerVsFloor = g.hitTestTile(player, wallMapArray, 0, world, "every");
playerVsNPC = g.hitTestTile(player, npcArray, 0, world, "every");
playerVsItem = g.hitTestTile(player, itemLayerArray, 0, world, "every");
playerVsTrigger = playerTriggerHitTest();
checkForDoor();
checkForNPC();
checkForItem();
if (g.state != dispDialogue)
{
checkGameOver();
}
tempItem = null;
//console.log("" g.state);
}
function updatePlayerTexture() {
player_tex.animationSpeed = ANIM_SPEED;
player_tex.vx = player.vx;
player_tex.vy = player.vy;
}