summaryrefslogtreecommitdiff
path: root/resources/js
diff options
context:
space:
mode:
authorFbenas <philbeansburton@gmail.com>2020-10-13 19:46:05 +0100
committerFbenas <philbeansburton@gmail.com>2020-10-13 19:46:05 +0100
commita4908f150053316383fbfaee0477e7b4123ce3ca (patch)
treecb9eb8c8f9a9b142b5a4c19213d6e0ca376d17d4 /resources/js
parent0b89973e8e1b2251545def7796c246d68ad8e061 (diff)
Refactor
Diffstat (limited to 'resources/js')
-rw-r--r--resources/js/app.js2
-rw-r--r--resources/js/boid.js (renamed from resources/js/component.js)24
-rw-r--r--resources/js/flock.js15
-rw-r--r--resources/js/scene.js33
4 files changed, 47 insertions, 27 deletions
diff --git a/resources/js/app.js b/resources/js/app.js
index 47cabd6..b107a71 100644
--- a/resources/js/app.js
+++ b/resources/js/app.js
@@ -1,4 +1,4 @@
-let scene = new Scene(5, 20);
+let scene = new Scene(5, 100);
function stop() {
scene.stop();
diff --git a/resources/js/component.js b/resources/js/boid.js
index 373b66a..6075cc2 100644
--- a/resources/js/component.js
+++ b/resources/js/boid.js
@@ -1,15 +1,15 @@
-class Component {
+class Boid {
constructor(context, radius, color, x, y, direction, id) {
this.rayLength = 40;
- this.turnStepAmount = 5;
+ this.turnStepAmount = 20;
this.stepAmount = 4;
this.radius = radius;
this.x = x;
this.y = y;
this.direction = direction;
this.color = color;
- this.fieldOfView = 180;
+ this.fieldOfView = 270;
this.id = id;
this.boidBuffer = 20;
@@ -21,7 +21,7 @@ class Component {
this.direction = this.findNextRay(context, boids);
- var vector = this.detectionPoint(this.x, this.y, this.stepAmount, this.direction);
+ var vector = this.findPoint(this.x, this.y, this.stepAmount, this.direction);
this.x = vector.x;
this.y = vector.y;
@@ -49,7 +49,7 @@ class Component {
for (let i = 0; i < rays.length; i++) {
let tweakAngle = 0;
// if (Math.random() > 0.95) {
- // tweakAngle = this.turnStepAmount * Math.random() * 5;
+ // tweakAngle = this.turnStepAmount * Math.random() - (this.turnStepAmount / 2);
// }
let rayAngle = tweakAngle + this.direction + rays[i];
@@ -75,7 +75,7 @@ class Component {
continue;
}
- let thisFututrePosition = this.detectionPoint(this.x, this.y, this.boidBuffer, direction);
+ let thisFututrePosition = this.findPoint(this.x, this.y, this.boidBuffer, direction);
let thisPath = {
x1: this.x,
y1: this.y,
@@ -83,7 +83,7 @@ class Component {
y2: thisFututrePosition.y
};
- let boidFuturePosition = this.detectionPoint(boids[i].x, boids[i].y, boids[i].boidBuffer, boids[i].direction);
+ let boidFuturePosition = this.findPoint(boids[i].x, boids[i].y, boids[i].boidBuffer, boids[i].direction);
let boidPath = {
x1: boids[i].x,
y1: boids[i].y,
@@ -135,7 +135,7 @@ class Component {
}
detectBox(context, width, height, direction) {
- let perceptionVector = this.detectionPoint(this.x, this.y, this.rayLength, direction);
+ let perceptionVector = this.findPoint(this.x, this.y, this.rayLength, direction);
if (perceptionVector.x - this.radius < 0 ||
perceptionVector.y - this.radius < 0 ||
@@ -162,17 +162,17 @@ class Component {
}
drawRay(context, x, y, perceptionDistance, direction) {
+ let point = this.findPoint(x, y, perceptionDistance, direction);
context.lineWidth = 1;
-
context.beginPath();
- this.lineToAngle(context, x, y, perceptionDistance, direction);
+ context.moveTo(x, y);
+ context.lineTo(point.x, point.y);
context.stroke();
context.restore();
}
lineToAngle(context, x1, y1, length, angle) {
-
angle *= Math.PI / 180;
var x2 = x1 + length * Math.cos(angle),
@@ -187,7 +187,7 @@ class Component {
};
}
- detectionPoint(x1, y1, length, angle) {
+ findPoint(x1, y1, length, angle) {
angle *= Math.PI / 180;
var x2 = x1 + length * Math.cos(angle),
diff --git a/resources/js/flock.js b/resources/js/flock.js
new file mode 100644
index 0000000..952aecd
--- /dev/null
+++ b/resources/js/flock.js
@@ -0,0 +1,15 @@
+class Flock {
+ constructor() {
+ this.boids = [];
+ }
+
+ run() {
+ for (let i = 0; i < this.boids.length; i++) {
+ this.boids[i].run(boids);
+ }
+ }
+
+ addBoid(boid) {
+ this.boids.push(boid);
+ }
+}
diff --git a/resources/js/scene.js b/resources/js/scene.js
index 7e7e895..d035827 100644
--- a/resources/js/scene.js
+++ b/resources/js/scene.js
@@ -1,5 +1,9 @@
class Scene {
+ // boid radius
+ // no of boids
+ // scene width
+ // scene height
constructor(component_size, no_of_components) {
this.component_size = component_size;
this.no_of_components = no_of_components;
@@ -13,25 +17,26 @@ class Scene {
initComponents() {
let components = [];
for (let i = 0; i < this.no_of_components; i++) {
- let x, y, z;
- if (i == 0) {
- x = 100;
- y = 100;
- z = 0;
- } else {
- x = 105;
- y = 100;
- z = 180;
- }
+ // let x, y, z;
+ // if (i == 0) {
+ // x = 100;
+ // y = 100;
+ // z = 0;
+ // } else {
+ // x = 105;
+ // y = 100;
+ // z = 180;
+ // }
- components.push(new Component(
+ components.push(new Boid(
this.gameArea.context,
this.component_size,
"black",
+ 300, 300, Math.random() * 360,
// x, y, z,
- Math.random() * (this.gameArea.canvas.width - 100) + 50,
- Math.random() * (this.gameArea.canvas.height - 100) + 50,
- Math.random() * 360,
+ // Math.random() * (this.gameArea.canvas.width - 100) + 50,
+ // Math.random() * (this.gameArea.canvas.height - 100) + 50,
+ // Math.random() * 360,
i
));
}