-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathinit.js
253 lines (219 loc) · 6.1 KB
/
init.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
var context;
var queue;
var WIDTH = 1024;
var HEIGHT = 768;
var mouseXPosition;
var mouseYPosition;
var batImage;
var stage;
var animation;
var deathAnimation;
var spriteSheet;
var enemyXPos=100;
var enemyYPos=100;
var enemyXSpeed = 1.5;
var enemyYSpeed = 1.75;
var score = 0;
var scoreText;
var gameTimer;
var gameTime = 0;
var timerText;
window.onload = function()
{
/*
* Set up the Canvas with Size and height
*
*/
var canvas = document.getElementById('myCanvas');
context = canvas.getContext('2d');
context.canvas.width = WIDTH;
context.canvas.height = HEIGHT;
stage = new createjs.Stage("myCanvas");
/*
* Set up the Asset Queue and load sounds
*
*/
queue = new createjs.LoadQueue(false);
queue.installPlugin(createjs.Sound);
queue.on("complete", queueLoaded, this);
createjs.Sound.alternateExtensions = ["ogg"];
/*
* Create a load manifest for all assets
*
*/
queue.loadManifest([
{id: 'backgroundImage', src: 'assets/background.png'},
{id: 'crossHair', src: 'assets/crosshair.png'},
{id: 'shot', src: 'assets/shot.mp3'},
{id: 'background', src: 'assets/countryside.mp3'},
{id: 'gameOverSound', src: 'assets/gameOver.mp3'},
{id: 'tick', src: 'assets/tick.mp3'},
{id: 'deathSound', src: 'assets/die.mp3'},
{id: 'batSpritesheet', src: 'assets/batSpritesheet.png'},
{id: 'batDeath', src: 'assets/batDeath.png'},
]);
queue.load();
/*
* Create a timer that updates once per second
*
*/
gameTimer = setInterval(updateTime, 1000);
}
function queueLoaded(event)
{
// Add background image
var backgroundImage = new createjs.Bitmap(queue.getResult("backgroundImage"))
stage.addChild(backgroundImage);
//Add Score
scoreText = new createjs.Text("1UP: " + score.toString(), "36px Arial", "#FFF");
scoreText.x = 10;
scoreText.y = 10;
stage.addChild(scoreText);
//Ad Timer
timerText = new createjs.Text("Time: " + gameTime.toString(), "36px Arial", "#FFF");
timerText.x = 800;
timerText.y = 10;
stage.addChild(timerText);
// Play background sound
createjs.Sound.play("background", {loop: -1});
// Create bat spritesheet
spriteSheet = new createjs.SpriteSheet({
"images": [queue.getResult('batSpritesheet')],
"frames": {"width": 198, "height": 117},
"animations": { "flap": [0,4] }
});
// Create bat death spritesheet
batDeathSpriteSheet = new createjs.SpriteSheet({
"images": [queue.getResult('batDeath')],
"frames": {"width": 198, "height" : 148},
"animations": {"die": [0,7, false,1 ] }
});
// Create bat sprite
createEnemy();
/*
// Create crosshair
crossHair = new createjs.Bitmap(queue.getResult("crossHair"));
crossHair.x = WIDTH/2;
crossHair.y = HEIGHT/2;
stage.addChild(crossHair);
*/
// Add ticker
createjs.Ticker.setFPS(15);
createjs.Ticker.addEventListener('tick', stage);
createjs.Ticker.addEventListener('tick', tickEvent);
// Set up events AFTER the game is loaded
// window.onmousemove = handleMouseMove;
window.onmousedown = handleMouseDown;
}
function createEnemy()
{
animation = new createjs.Sprite(spriteSheet, "flap");
animation.regX = 99;
animation.regY = 58;
animation.x = enemyXPos;
animation.y = enemyYPos;
animation.gotoAndPlay("flap");
stage.addChildAt(animation,1);
}
function batDeath()
{
deathAnimation = new createjs.Sprite(batDeathSpriteSheet, "die");
deathAnimation.regX = 99;
deathAnimation.regY = 58;
deathAnimation.x = enemyXPos;
deathAnimation.y = enemyYPos;
deathAnimation.gotoAndPlay("die");
stage.addChild(deathAnimation);
}
function tickEvent()
{
//Make sure enemy bat is within game boundaries and move enemy Bat
if(enemyXPos < WIDTH && enemyXPos > 0)
{
enemyXPos += enemyXSpeed;
} else
{
enemyXSpeed = enemyXSpeed * (-1);
enemyXPos += enemyXSpeed;
}
if(enemyYPos < HEIGHT && enemyYPos > 0)
{
enemyYPos += enemyYSpeed;
} else
{
enemyYSpeed = enemyYSpeed * (-1);
enemyYPos += enemyYSpeed;
}
animation.x = enemyXPos;
animation.y = enemyYPos;
}
/*
function handleMouseMove(event)
{
//Offset the position by 45 pixels so mouse is in center of crosshair
crossHair.x = event.clientX-45;
crossHair.y = event.clientY-45;
}
*/
function handleMouseDown(event)
{
//Display CrossHair
crossHair = new createjs.Bitmap(queue.getResult("crossHair"));
crossHair.x = event.clientX-45;
crossHair.y = event.clientY-45;
stage.addChild(crossHair);
createjs.Tween.get(crossHair).to({alpha: 0},1000);
//Play Gunshot sound
createjs.Sound.play("shot");
//Increase speed of enemy slightly
enemyXSpeed *= 1.05;
enemyYSpeed *= 1.06;
//Obtain Shot position
var shotX = Math.round(event.clientX);
var shotY = Math.round(event.clientY);
var spriteX = Math.round(animation.x);
var spriteY = Math.round(animation.y);
// Compute the X and Y distance using absolte value
var distX = Math.abs(shotX - spriteX);
var distY = Math.abs(shotY - spriteY);
// Anywhere in the body or head is a hit - but not the wings
if(distX < 30 && distY < 59 )
{
//Hit
stage.removeChild(animation);
batDeath();
score += 100;
scoreText.text = "1UP: " + score.toString();
createjs.Sound.play("deathSound");
//Make it harder next time
enemyYSpeed *= 1.25;
enemyXSpeed *= 1.3;
//Create new enemy
var timeToCreate = Math.floor((Math.random()*3500)+1);
setTimeout(createEnemy,timeToCreate);
} else
{
//Miss
score -= 10;
scoreText.text = "1UP: " + score.toString();
}
}
function updateTime()
{
gameTime += 1;
if(gameTime > 60)
{
//End Game and Clean up
timerText.text = "GAME OVER";
stage.removeChild(animation);
stage.removeChild(crossHair);
createjs.Sound.removeSound("background");
var si =createjs.Sound.play("gameOverSound");
clearInterval(gameTimer);
}
else
{
timerText.text = "Time: " + gameTime
createjs.Sound.play("tick");
}
}