forked from kshen0/beatdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
328 lines (279 loc) · 7.2 KB
/
main.js
File metadata and controls
328 lines (279 loc) · 7.2 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
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
327
328
// game resources
var g_resources = [
// our level tileset
{
name: "area01_level_tiles",
type: "image",
src: "data/area01_tileset/area01_level_tiles.png"
},
// our level
{
name: "area01",
type: "tmx",
src: "data/area01.tmx"
},
// the main player spritesheet
{
name: "hero",
type: "image",
src: "data/sprite/hero.png"
},
// the parallax background
{
name: "area01_bkg0",
type: "image",
src: "data/area01_parallax/area01_bkg0.png"
}, {
name: "area01_bkg1",
type: "image",
src: "data/area01_parallax/area01_bkg1.png"
},
// the spinning coin spritesheet
{
name: "powerups",
type: "image",
src: "data/sprite/powerups.png"
},
// our enemty entity
{
name: "enemy1",
type: "image",
src: "data/sprite/enemy1.png"
},
// our LOW enemy entity
{
name: "enemy2",
type: "image",
src: "data/sprite/enemy2.png"
},
// our HIGH enemy entity
{
name: "enemy3",
type: "image",
src: "data/sprite/enemy3.png"
},
// bullet entity
{
name: "bullet_right",
type: "image",
src: "data/sprite/bullet_right.png"
},
// bullet entity
{
name: "bullet_left",
type: "image",
src: "data/sprite/bullet_left.png"
},
// bullet entity
{
name: "bullet_up",
type: "image",
src: "data/sprite/bullet_up.png"
},
// bullet entity
{
name: "bullet_down",
type: "image",
src: "data/sprite/bullet_down.png"
},
// game font
{
name: "32x32_font",
type: "image",
src: "data/sprite/bebas_font.png"
},
// title screen
{
name: "title_screen",
type: "image",
src: "data/GUI/title_screen.png"
},
// GAME OVER screen
{
name: "gameover_screen",
type: "image",
src: "data/GUI/gameover.png"
},
//cling audio
{
name: "cling",
type: "audio",
src: "data/audio/",
channel: 2
},
//BUY1
{
name: "BUY1",
type: "audio",
src: "data/audio/",
//channel: 2
},
//Power up
{
name: "power_up",
type: "audio",
src: "data/audio/"
//channel: 2
}];
var jsApp = {
/* ---
Initialize the jsApp
--- */
onload: function() {
// init the video
//if (!me.video.init('jsapp', 640, 480, false, 1.0)) {
if (!me.video.init('jsapp', 850, 550, false, 1.0)) {
alert("Sorry but your browser does not support html 5 canvas.");
return;
}
// initialize the "audio"
me.audio.init("mp3,ogg");
// set all resources to be loaded
me.loader.onload = this.loaded.bind(this);
// set all resources to be loaded
me.loader.preload(g_resources);
// load everything & display a loading screen
me.state.change(me.state.LOADING);
},
/* ---
callback when everything is loaded
--- */
loaded: function ()
{
// set the "Play/Ingame" Screen Object
me.state.set(me.state.MENU, new TitleScreen());
me.state.set(me.state.PLAY, new PlayScreen());
me.state.set(me.state.GAMEOVER, new GameOverScreen());
//transition
me.state.transition("fade", "#FFFFFF", 250);
// add our player entity in the entity pool
me.entityPool.add("mainPlayer", PlayerEntity);
me.entityPool.add("CoinEntity", CoinEntity);
me.entityPool.add("EnemyEntity", EnemyEntity);
//me.entityPool.add("BulletEntity", BulletEntity);
// enable the keyboard
me.input.bindKey(me.input.KEY.A, "left");
me.input.bindKey(me.input.KEY.D, "right");
me.input.bindKey(me.input.KEY.W, "up");
me.input.bindKey(me.input.KEY.S, "down");
// Spawn keys
//me.input.bindKey(me.input.KEY.Q, "spawn", true);
//me.input.bindKey(me.input.KEY.E, "spawnEnemy", true);
me.input.bindKey(me.input.KEY.R, "spawnMultipleEnemies", true);
// Combat keys
//me.input.bindKey(me.input.KEY.P, "shoot");
//me.input.bindKey(me.input.KEY.O, "melee");
me.input.bindKey(me.input.KEY.I, "shootup");
me.input.bindKey(me.input.KEY.J, "shootleft");
me.input.bindKey(me.input.KEY.K, "shootdown");
me.input.bindKey(me.input.KEY.L, "shootright");
//me.input.bindKey(me.input.KEY.X, "jump", true);
me.input.bindKey(me.input.KEY.M, "pulse");
//CHEATS
//me.input.bindKey(me.input.KEY.I, "i");
//me.input.bindKey(me.input.KEY.U, "u");
// start the game
me.state.change(me.state.PLAY);
//me.game.onLevelLoaded = onTimerTick;
// start the game
me.state.change(me.state.MENU);
//me.debug.renderHitBox = true;
}
};
// jsApp
/* the in game stuff*/
var PlayScreen = me.ScreenObject.extend({
onResetEvent: function() {
// stuff to reset on state change
// load a level
me.levelDirector.loadLevel("area01");
// add a default to the game mngr
me.game.addHUD(0, 430, 640, 60);
// add a new HUD item
me.game.HUD.addItem("score", new ScoreObject(620, 10));
// add a health HUD item
me.game.HUD.addItem("health", new HealthObject(300,10));
me.game.HUD.updateItemValue("health", 5);
//make sure things in right order
me.game.sort();
//me.audio.play("BUY1");
},
/* ---
action to perform when game is finished (state change)
--- */
onDestroyEvent: function() {
//remove the HUD
me.game.disableHUD();
}
});
/*
//MAIN GAME LOOP
setInterval(onTimerTick, 33); // 33 milliseconds = ~ 30 frames per sec
function onTimerTick() {
if (me.input.isKeyPressed('spawn')) {
//me.entityPool.add("CoinEntity2", CoinEntity);
//me.state.change(me.state.PAUSE);
var newEnemy = new CoinEntity(2, 2, {image: 'powerups', spritewidth: 32, spriteheight: 32});
me.game.add(newEnemy, this.z);
//me.game.add(new EnemyEntity(5, 5,{}), 3);
me.game.sort();
console.log("fuck yea");
}
}*/
/*----------------------------------------
Functions and js junk
-------------------------------------------*/
// increment score
// keep in mind, if collecting an item and updating HUD, need to
// set this.collidable = false (for hte object you're collecting)
// and also me.game.remove(this)
function incScore(numPoints) {
me.game.HUD.updateItemValue("score", numPoints);
}
function decHealth() {
console.log(isFlickering);
if (!isFlickering) {
me.game.HUD.updateItemValue("health", -1);
if (me.game.HUD.getItemValue("health") <= 0) {
me.state.change(me.state.GAMEOVER);
}
isFlickering = true;
window.setTimeout(function() {isFlickering = false}, 1000);
}
}
/*
//MAIN GAME LOOP
setInterval(onTimerTick, 33); // 33 milliseconds = ~ 30 frames per sec
function onTimerTick() {
if (true) {
// maybe check to see what type of thing to spawn based on intensity
//set bool to false and repeat
}
if (me.input.isKeyPressed('i')) {
incScore();
}
if (me.input.isKeyPressed('u')) {
decHealth();
}
if (me.input.isKeyPressed('left')) {
me.game.add(new EnemyEntity(5, 5,{}), 10);
}
}
var songList = new Array();
songList[0] = "cling";
songList[1] = "BUY1";
songNum = 0;
setInterval(switchSong, 2000);
function switchSong() {
if (songNum == 0) {
me.audio.play(songList[0]);
songNum++;
} else if (songNum == 1) {
me.audio.play(songList[1]);
songNum--;
}
} */
//bootstrap :)
window.onReady(function() {
jsApp.onload();
});