Commit ee661f12 authored by Jens-Christian Fischer's avatar Jens-Christian Fischer
Browse files

Added Master objects for boids to gather around

parent c1aad677
......@@ -5,17 +5,20 @@ import {Socket, Presence} from "phoenix";
// Boid class
// Methods for Separation, Cohesion, Alignment added
function Boid(p, x, y) {
function Boid(p, x, y, master) {
this.p = p;
this.acceleration = this.p.createVector(0, 0);
this.velocity = p5.Vector.random2D();
this.position = this.p.createVector(x, y);
this.r = 3.0;
this.master = master; // which master does this boid belong to?
this.radius = 16;
this.maxspeed = 3; // Maximum speed
this.maxforce = 0.05; // Maximum steering force
};
Boid.prototype.set_velocity = function(dx, dy) {
// this.velocity = this.p.createVector(dx, dy);
let acc = this.p.createVector(dx, dy);
......@@ -181,6 +184,95 @@ Boid.prototype.cohesion = function(boids) {
}
};
Boid.prototype.star = function (x, y, radius1, radius2, npoints) {
var angle = this.p.TWO_PI / npoints;
var halfAngle = angle/2.0;
this.p.beginShape();
for (var a = 0; a < this.p.TWO_PI; a += angle) {
var sx = x + this.p.cos(a) * radius2;
var sy = y + this.p.sin(a) * radius2;
this.p.vertex(sx, sy);
sx = x + this.p.cos(a+halfAngle) * radius1;
sy = y + this.p.sin(a+halfAngle) * radius1;
this.p.vertex(sx, sy);
}
this.p.endShape(this.p.CLOSE);
}
Boid.prototype.polygon = function(x, y, radius, npoints) {
var angle = this.p.TWO_PI / npoints;
this.p.beginShape();
for (var a = 0; a < this.p.TWO_PI; a += angle) {
var sx = x + this.p.cos(a) * radius;
var sy = y + this.p.sin(a) * radius;
this.p.vertex(sx, sy);
}
this.p.endShape(this.p.CLOSE);
}
Boid.prototype.render_circle = function() {
this.p.ellipse(0, 0, this.radius, this.radius);
}
Boid.prototype.render_square = function() {
this.polygon(0, 0, this.radius, 4);
}
Boid.prototype.render_triangle = function() {
this.polygon(0, 0, this.radius, 3);
}
Boid.prototype.render_star = function() {
this.star(0, 0, this.radius, this.radius / 2, 7);
}
// For each instrument, there is a master class that has a number of boids
// associated with it
// The following types are supported:
// - circle
// - square
// - triangle
// - star
function Master(p, x, y, type) {
Boid.call(this, p, x, y, null);
this.radius = 32;
this.type = type;
}
Master.prototype = Object.create(Boid.prototype);
Master.prototype.constructor = Master;
Master.prototype.render = function() {
this.p.fill((this.radius));
this.p.stroke(200);
this.p.push();
this.p.translate(this.position.x, this.position.y);
this.p.rotate(this.p.frameCount / 50.0);
switch (this.type) {
case 'square': this.render_square();
break;
case 'circle': this.render_circle();
break;
case 'triangle': this.render_triangle();
break;
case 'star': this.render_star();
break;
default: this.render_circle();
}
this.p.pop();
}
let PresenceSketch = {
width: 720,
......@@ -189,6 +281,8 @@ let PresenceSketch = {
p5: null,
presences: {},
masters: {}, // holds the four main instruments
set_velocity(name, dx, dy) {
PresenceSketch.boids[name].set_velocity(dx, dy);
......@@ -203,42 +297,37 @@ let PresenceSketch = {
_.each(joins, function(val, name) {
if (name) {
PresenceSketch.boids[name] = new Boid(PresenceSketch.p5,
Math.random() * PresenceSketch.width,
Math.random() * PresenceSketch.height);
Math.random() * PresenceSketch.height);
}
});
},
removeBoids(leaves) {
if (!PresenceSketch.p5) { return; };
_.each(leaves, function(val, name) {
delete PresenceSketch.boids[name];
});
},
syncBoids(currentActive) {
if (!PresenceSketch.p5) { return; };
console.log("sync");
_.each(currentActive, function(p) {
console.log(p.user);
if (PresenceSketch.boids[p.user]) {
console.log("have boid");
// console.log("have boid");
} else
{
if (p.user) {
PresenceSketch.boids[p.user] = new Boid(PresenceSketch.p5,
Math.random() * PresenceSketch.width,
Math.random() * PresenceSketch.height);
Math.random() * PresenceSketch.width,
Math.random() * PresenceSketch.height);
}
}
});
},
......@@ -248,6 +337,21 @@ let PresenceSketch = {
s.createCanvas(PresenceSketch.width, PresenceSketch.height);
s.background(150, 60, 70);
PresenceSketch.masters['square'] = new Master(PresenceSketch.p5,
100, 100, 'square');
PresenceSketch.masters['circle'] = new Master(PresenceSketch.p5,
PresenceSketch.width - 100,
100, 'circle');
PresenceSketch.masters['triangle'] = new Master(PresenceSketch.p5,
100,
PresenceSketch.height - 100, 'triangle');
PresenceSketch.masters['star'] = new Master(PresenceSketch.p5,
PresenceSketch.width - 100,
PresenceSketch.height - 100, 'star');
};
s.draw = function() {
......@@ -257,9 +361,10 @@ let PresenceSketch = {
_.each(PresenceSketch.boids, function(boid, key, list) {
boid.run(list);
});
// for (var i = 0; i < PresenceSketch.boids.length; i++) {
// PresenceSketch.boids[i].run(PresenceSketch.boids);
// }
_.each(PresenceSketch.masters, function(boid, key, list) {
boid.render();
});
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment