This repository has been archived by the owner on Aug 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
142 lines (136 loc) · 3.54 KB
/
main.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
window.onload = function () {
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
width = canvas.width = window.innerWidth,
height = canvas.height = window.innerHeight;
var ship = spaceship.create(width / 2, height / 2, 0, 0),
shipMirrored = [],
numOfMirrors = 50;
ship.setAngle( Math.random() * Math.PI * 2);
for (var i = 0; i < numOfMirrors; i ++) {
shipMirrored.push(spaceship.create(Math.random() * width, Math.random() * height, 0, 0));
shipMirrored[i].setAngle(Math.random() * Math.PI * 2);
}
updateFrame(); // update calculations and render animations
function updateFrame() {
// clear context
context.clearRect(0, 0, width, height);
// update ship properties
ship.update();
for (var i = 0; i < numOfMirrors; i ++) {
shipMirrored[i].update();
}
// reder ship animation
ship.render(context, width, height, "rgba(0, 0, 0, 0.5");
for (var i = 0; i < numOfMirrors; i ++) {
shipMirrored[i].render(context, width, height, "rgba(255, 255, 255, 0.5");
}
// refresh recursive animation frame
requestAnimationFrame(updateFrame);
}
// // /* /* Event listener */ */
/* ===== ===== ===== ===== ===== ===== ===== ===== ===== */
document.body.addEventListener("keydown", function(event){
switch (event.keyCode) {
case 38: // up
ship.setThrusting(true);
// mirrored
for (var i = 0; i < numOfMirrors; i ++) {
shipMirrored[i].setThrusting(true);
}
break;
case 32: // spacebar
if (ship.isAutoThrust()) {
ship.setAutoThrust(false);
// mirrored
for (var i = 0; i < numOfMirrors; i ++) {
shipMirrored[i].setAutoThrust(false);
}
}
else {
ship.setAutoThrust(true);
// mirrored
for (var i = 0; i < numOfMirrors; i ++) {
shipMirrored[i].setAutoThrust(true);
}
}
break;
case 16: // shift
if (ship.isDrifting()) {
ship.setDrifting(false);
// mirrored
for (var i = 0; i < numOfMirrors; i ++) {
shipMirrored[i].setDrifting(false);
}
}
else {
ship.setDrifting(true);
// mirrored
for (var i = 0; i < numOfMirrors; i ++) {
shipMirrored[i].setDrifting(true);
}
}
break;
case 40: // down
ship.brake();
ship.setBrakeUsing(true);
// mirrored
for (var i = 0; i < numOfMirrors; i ++) {
shipMirrored[i].brake();
shipMirrored[i].setBrakeUsing(true);
}
break;
case 37: // left
ship.angle -= ship.turnRate;
// mirrored
for (var i = 0; i < numOfMirrors; i ++) {
if (i % 2 == 0) {
shipMirrored[i].angle += shipMirrored[i].turnRate;
}
else {
shipMirrored[i].angle -= shipMirrored[i].turnRate;
}
}
break;
case 39: //right
ship.angle += ship.turnRate;
// mirrored
for (var i = 0; i < numOfMirrors; i ++) {
if (i % 2 == 0) {
shipMirrored[i].angle -= shipMirrored[i].turnRate;
}
else {
shipMirrored[i].angle += shipMirrored[i].turnRate;
}
}
break;
default:
break;
}
console.log ("keycode = " + event.keyCode);
});
document.body.addEventListener("keyup", function(event){
switch (event.keyCode) {
case 38: // up
ship.setThrusting(false);
// mirrored
for (var i = 0; i < numOfMirrors; i ++) {
shipMirrored[i].setThrusting(false);
}
break;
case 40: // down
ship.setBrakeUsing(false);
// mirrored
for (var i = 0; i < numOfMirrors; i ++) {
shipMirrored[i].setBrakeUsing(false);
}
break;
case 37: // left
break;
case 39: //right
break;
default:
break;
}
});
}