-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhigh-noon.js
158 lines (136 loc) · 3.74 KB
/
high-noon.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 shooterPositions = [];
let shootingIndex = [];
let moveIndex = -1;
let numberOfShooters = 7;
let alive;
let dead;
const container = 'high-noon-container';
function setup() {
let canvas = createCanvas(1200, 756);
canvas.parent(container);
for (let i = 0; i < numberOfShooters; i++) {
shooterPositions[i] = new p5.Vector(random(width), random(height));
}
calcShooting();
let incrementButton = createButton('+1');
incrementButton.position(-60, 0);
incrementButton.size(50, 50);
incrementButton.style('font-weight', 'bold');
incrementButton.parent(container);
incrementButton.mousePressed(incrementNumberOfShooters);
let decrementButton = createButton('-1');
decrementButton.position(-60, 75);
decrementButton.size(50, 50);
decrementButton.style('font-weight', 'bold');
decrementButton.parent(container);
decrementButton.mousePressed(decrementNumberOfShooters);
noLoop();
}
function draw() {
background(255);
// draw arrows
stroke(0, 0, 0, 150);
fill(0);
for (let i = 0; i < numberOfShooters; i++) {
let shooterPosition = shooterPositions[i];
let targetPosition = shooterPositions[shootingIndex[i]];
let shootDirection = p5.Vector.sub(targetPosition, shooterPosition);
push();
translate(shooterPosition.x, shooterPosition.y);
// draw arrow
line(0, 0, shootDirection.x, shootDirection.y);
arrowHead(createVector(0, 0), shootDirection);
pop();
}
// draw points
stroke(0);
fill(175);
for (let i = 0; i < numberOfShooters; i++) {
let shooterPosition = shooterPositions[i];
circle(shooterPosition.x, shooterPosition.y, 15);
}
// draw crosses
for (let i = 0; i < numberOfShooters; i++) {
let targetPosition = shooterPositions[shootingIndex[i]];
push();
translate(targetPosition.x, targetPosition.y);
line(-5, -5, 5, 5);
line(-5, 5, 5, -5);
pop();
}
// draw text
stroke(255);
fill(0);
text("total: " + numberOfShooters, 50, 15);
text("alive: " + alive, 200, 15);
text("dead: " + dead, 350, 15);
}
// credit: https://editor.p5js.org/kevinhb92/sketches/zLDDLrg_
function arrowHead(start, vector) {
push();
let norm = createVector(vector.x, vector.y);
norm.normalize();
applyMatrix(norm.x, norm.y, -norm.y, norm.x, vector.x - start.x, vector.y - start.y);
triangle(-16, 8, -8, 0, -16, -8);
pop();
}
function incrementNumberOfShooters() {
if (numberOfShooters >= 100) {
return;
}
numberOfShooters++;
shooterPositions.push(new p5.Vector(random(width), random(height)));
shootingIndex.push(-1);
calcShooting();
redraw();
}
function decrementNumberOfShooters() {
if (numberOfShooters <= 2) {
return;
}
numberOfShooters--;
shooterPositions.pop();
shootingIndex.pop();
calcShooting();
redraw();
}
function calcShooting() {
for (let i = 0; i < numberOfShooters; i++) {
let minDistance = width + height;
let index = -1;
for (let j = 0; j < numberOfShooters; j++) {
if (i == j) {
continue;
}
let distance = shooterPositions[i].dist(shooterPositions[j]);
if (distance < minDistance) {
minDistance = distance;
index = j;
}
}
shootingIndex[i] = index;
}
dead = [... new Set(shootingIndex)].length;
alive = numberOfShooters - dead;
}
function mousePressed() {
let mouseVec = createVector(mouseX, mouseY);
for (let i = 0; i < numberOfShooters; i++) {
if (shooterPositions[i].dist(mouseVec) < 15) {
moveIndex = i;
return;
}
}
}
function mouseDragged() {
if (moveIndex == -1) {
return;
}
shooterPositions[moveIndex].x = constrain(mouseX, 0, width);
shooterPositions[moveIndex].y = constrain(mouseY, 0, height);
calcShooting();
redraw();
}
function mouseReleased() {
moveIndex = -1;
}