-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissiles.js
158 lines (137 loc) · 3.46 KB
/
missiles.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
let canvas = document.querySelector('#game');
boundingRect = canvas.getBoundingClientRect();
let images = {
'plane': 'sample/plane.jpg',
'missile': 'sample/missile.png'
};
let colors = {
'red': 'red',
'black': 'black'
};
let sounds = {
'background': 'sample/8bitwin.mp3',
'explosion': 'sample/explosion.mp3'
};
let planeRadius = 20;
let missileRadius = 10;
let missileAcceleration = 0.25;
let maxMissileVelocity = 6;
let interval = 0;
let missileSpawnInterval = 150;
let planeX = 0;
let planeY = 0;
let createMissile = function() {
let mCenter = [];
if(Math.random() > 0.5) {
if(Math.random() > 0.5) {
mCenter = [Math.round(Math.random() * canvas.width), 0]
} else {
mCenter = [Math.round(Math.random() * canvas.width), canvas.height]
}
} else {
if(Math.random() > 0.5) {
mCenter = [0, Math.round(Math.random() * canvas.height)]
} else {
mCenter = [canvas.width, Math.round(Math.random() * canvas.height)]
}
}
/*
let missile = new Circle(
mCenter,
missileRadius,
[0, 0],
[0, 0],
"red",
gameEngine.images.missile
);
*/
let missile = new Rect(
mCenter,
[missileRadius*2, missileRadius],
[0, 0],
[0, 0],
0.5,
"red"
);
missile.onUpdate = function() {
this.acceleration = getMissileAcceleration(this);
this.velocity[0] = this.velocity[0] > maxMissileVelocity ? maxMissileVelocity : this.velocity[0];
this.velocity[1] = this.velocity[1] > maxMissileVelocity ? maxMissileVelocity : this.velocity[1];
}
missile.onCollide = function(object, gameEngine) {
gameEngine.deleteObject(this.id);
gameEngine.sounds.explosion.play();
}
return missile;
}
let getMissileAcceleration = function(missile) {
let dX = planeX - missile.center[0];
let dY = planeY - missile.center[1];
let mag = Math.sqrt(dX * dX + dY * dY);
return [
dX * missileAcceleration / mag,
dY * missileAcceleration / mag,
];
}
let gameFunctions = {
'onStart': function(gameEngine) {
/*
let plane = new Circle(
[0, 0],
planeRadius,
[0, 0],
[0, 0],
"black",
gameEngine.images.plane
);
*/
interval = 0;
let plane = new Rect(
[60, 60],
[planeRadius*2, planeRadius],
[0, 0],
[0, 0],
1,
"black"
);
plane.onUpdate = function(gameEngine) {
[planeX, planeY] = this.center;
}
plane.onCollide = function(object, gameEngine) {
gameEngine.gameStarted = false;
}
plane.id = "plane";
plane.center = [
Math.round(canvas.width/2),
Math.round(canvas.height/2)
];
gameEngine.objects = [plane];
},
'onStop': function(gameEngine) {
gameEngine.startGame();
},
'onUpdate': function(gameEngine) {
interval++;
if(interval == missileSpawnInterval) {
gameEngine.addObject(createMissile());
interval = 0;
}
}
}
let keyControls = {
'keydown': {},
'keyup': {}
}
let gameEngine = new GameEngine(canvas, keyControls, gameFunctions, colors, images, sounds);
document.onmousemove = function(e) {
let pX = e.clientX - boundingRect.left;
let pY = e.clientY - boundingRect.top;
pX = pX > planeRadius ? pX : planeRadius;
pY = pY > planeRadius ? pY : planeRadius;
pX = pX < canvas.width - planeRadius? pX : canvas.width - planeRadius;
pY = pY < canvas.height - planeRadius ? pY : canvas.height - planeRadius;
planeX = pX;
planeY = pY;
gameEngine.getObjectById('plane').center = [planeX, planeY];
}
gameEngine.startGame();