-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
134 lines (114 loc) · 3.58 KB
/
sketch.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
var gravity;
var shipOne;
var shipTwo;
var asteroids = [];
var lasersOne = [];
var lasersTwo = [];
function setup() {
createCanvas(windowWidth, windowHeight);
gravity = createVector(0, 0.09);
shipOne = new Ship(10);
shipTwo = new Ship(70);
for (i = 0; i < 5; i++) {
asteroids.push(new Asteroid());
}
}
function draw() {
background(0);
//for (var i = 0; i < asteroids.length; i++){
// asteroids[i].render();
// asteroids[i].update();
// asteroids[i].edges();
//}
for (var i = lasersOne.length - 1; i >= 0; i--) {
lasersOne[i].render();
lasersOne[i].update();
if (lasersOne[i].offscreen()) {
lasersOne.splice(i, 1);
}
//if (lasersOne[i].hits(shipTwo)) {
// console.log("Hit!");
//}
if (lasersOne.length >= 1) {
if (pointInTriangle(lasersOne[i], shipTwo)) {
console.log("Ship Two HIT!");
shipTwo.lifebar -= 10;
lasersOne.splice(i, 1);
}
}
}
for (var i = lasersTwo.length - 1; i >= 0; i--) {
lasersTwo[i].render();
lasersTwo[i].update();
if (lasersTwo[i].offscreen()) {
lasersTwo.splice(i, 1);
}
if (lasersTwo.length >= 1) {
if (pointInTriangle(lasersTwo[i], shipOne)) {
console.log("Ship One HIT!");
shipOne.lifebar -= 10;
lasersTwo.splice(i, 1);
}
}
}
shipOne.render();
shipOne.turn();
shipOne.update();
//shipOne.applyForce(gravity);
shipOne.edges();
//ship.checkLanding();
shipTwo.render();
shipTwo.turn();
shipTwo.update();
shipTwo.edges();
//console.log(shipOne.pos);
}
function sign(p1x, p1y, p2x, p2y, p3x, p3y) {
return (p1x - p3x) * (p2y - p3y) - (p2x - p3x) * (p1y - p3y);
}
function pointInTriangle(laser, ship) {
var b1;
var b2;
var b3;
//b1 = sign(pt, v1, v2) < 0.0f;
//b2 = sign(pt, v2, v3) < 0.0f;
//b3 = sign(pt, v3, v1) < 0.0f;
//return ((b1 == b2) && (b2 == b3));
//triangle(-this.r, this.r, this.r, this.r, 0, -this.r);
b1 = Boolean(sign(laser.pos.x, laser.pos.y, ship.pos.x - 20, ship.pos.y + 20, ship.pos.x + 20, ship.pos.y + 20) < 0.0);
b2 = Boolean(sign(laser.pos.x, laser.pos.y, ship.pos.x + 20, ship.pos.y + 20, ship.pos.x, ship.pos.y - 20) < 0.0);
b3 = Boolean(sign(laser.pos.x, laser.pos.y, ship.pos.x, ship.pos.y - 20, ship.pos.x - 20, ship.pos.y + 20) < 0.0);
//console.log('B1:'+b1,'B2:'+b2,'B3:'+b3);
return ((b1 == b2) && (b2 == b3));
}
function keyReleased() {
if (keyCode == RIGHT_ARROW || keyCode == LEFT_ARROW) {
shipOne.setRotation(0);
} else if (keyCode == UP_ARROW) {
shipOne.boosting(false);
} else if (keyCode == 65 || keyCode == 68) {
shipTwo.setRotation(0);
} else if (keyCode == 87) {
shipTwo.boosting(false);
}
}
function keyPressed() {
if (key == ' ') {
lasersOne.push(new Laser(shipOne.pos, shipOne.heading));
} else if (keyCode == RIGHT_ARROW) {
shipOne.setRotation(0.1);
} else if (keyCode == LEFT_ARROW) {
shipOne.setRotation(-0.1);
} else if (keyCode == UP_ARROW) {
shipOne.boosting(true);
//shipTwo.boosting(true);
} else if (keyCode == 48) {
lasersTwo.push(new Laser(shipTwo.pos, shipTwo.heading));
} else if (keyCode == 68) {
shipTwo.setRotation(0.1);
} else if (keyCode == 65) {
shipTwo.setRotation(-0.1);
} else if (keyCode == 87) {
shipTwo.boosting(true);
}
}