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

Boids are now one of four possible types

parent a766ffb8
......@@ -3,7 +3,7 @@ defmodule Grains.Mixfile do
def project do
[app: :grains,
version: "0.0.57",
version: "0.0.58",
elixir: "~> 1.2",
elixirc_paths: elixirc_paths(Mix.env),
compilers: [:phoenix, :gettext] ++ Mix.compilers,
......
......@@ -72,6 +72,21 @@ defmodule Grains.Presence do
information, while maintaining the required `:metas` field from the
original presence data.
"""
@nr_types 4
def select_type(uuid) do
{i, _} = Integer.parse(uuid, 16)
Integer.mod(i, @nr_types)
end
def fetch(_topic, entries) do
for {key, %{metas: metas}} <- entries, into: %{} do
{key, %{metas: metas, type: select_type(key)}}
end
end
use Phoenix.Presence, otp_app: :grains,
pubsub_server: Grains.PubSub
end
......@@ -6,7 +6,7 @@ defmodule Grains.Token do
end
def call(conn, _opts) do
uuid = get_session(conn, :user_uuid) || UUID.uuid1()
uuid = get_session(conn, :user_uuid) || UUID.uuid4(:hex)
token = Phoenix.Token.sign(conn, "user socket", uuid )
conn
......
......@@ -5,16 +5,18 @@ import {Socket, Presence} from "phoenix";
// Boid class
// Methods for Separation, Cohesion, Alignment added
function Boid(p, x, y, master) {
function Boid(p, x, y, type) {
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.norm_radius = 24;
this.radius = 24;
this.r_rand = 1.0;
this.maxspeed = 3; // Maximum speed
this.maxforce = 0.05; // Maximum steering force
this.type = type;
};
......@@ -88,8 +90,14 @@ Boid.prototype.seek = function(target) {
Boid.prototype.render = function() {
this.p.fill((this.radius * 8) - 1);
this.p.stroke(200);
this.p.ellipse(this.position.x, this.position.y, this.radius, this.radius);
if (this.radius > 16) { this.radius = this.radius - 1; }
this.p.push();
this.p.translate(this.position.x, this.position.y);
let factor = 1 + Math.random(0.1) - 0.1;
this.p.rotate((this.p.frameCount / -175.0 )* factor);
this.p.arc(0, 0, this.radius * factor, this.radius * factor, 0, this.p.PI + this.p.QUARTER_PI,
this.p.OPEN);
this.p.pop();
if (this.radius > this.norm_radius) { this.radius = this.radius - 1; }
};
// Wraparound
......@@ -211,6 +219,8 @@ Boid.prototype.polygon = function(x, y, radius, npoints) {
}
Boid.prototype.render_circle = function() {
this.p.arc(0, 0, this.radius- 2, this.radius-2, 0, this.p.QUARTER_PI);
this.p.ellipse(0, 0, this.radius, this.radius);
}
......@@ -245,7 +255,7 @@ function Master(p, x, y, type) {
Master.prototype = Object.create(Boid.prototype);
Master.prototype.constructor = Master;
Master.prototype.render = function() {
Boid.prototype.render = function() {
this.p.fill((this.radius));
this.p.stroke(200);
......@@ -275,14 +285,15 @@ Master.prototype.render = function() {
let PresenceSketch = {
width: 720,
height: 400,
width: 960,
height: 750,
boids: {},
p5: null,
presences: {},
masters: {}, // holds the four main instruments
types: {1: 'circle', 2: 'square', 3: 'triangle', 4: 'star'},
set_velocity(name, dx, dy) {
PresenceSketch.boids[name].set_velocity(dx, dy);
......@@ -296,10 +307,12 @@ let PresenceSketch = {
if (!PresenceSketch.p5) { return; };
_.each(joins, function(val, name) {
console.log(val.type);
if (name) {
PresenceSketch.boids[name] = new Boid(PresenceSketch.p5,
Math.random() * PresenceSketch.width,
Math.random() * PresenceSketch.height);
Math.random() * PresenceSketch.height,
PresenceSketch.types[val.type]);
}
});
},
......@@ -324,7 +337,8 @@ let PresenceSketch = {
if (p.user) {
PresenceSketch.boids[p.user] = new Boid(PresenceSketch.p5,
Math.random() * PresenceSketch.width,
Math.random() * PresenceSketch.height);
Math.random() * PresenceSketch.height,
PresenceSketch.types[p.type]);
}
}
});
......@@ -335,7 +349,7 @@ let PresenceSketch = {
PresenceSketch.p5 = s;
s.setup = function() {
s.createCanvas(PresenceSketch.width, PresenceSketch.height);
s.background(150, 60, 70);
s.background(0);
PresenceSketch.masters['square'] = new Master(PresenceSketch.p5,
100, 100, 'square');
......@@ -355,8 +369,8 @@ let PresenceSketch = {
};
s.draw = function() {
s.background(51, 66, 240);
// Run all the boids
s.background(88, 131, 120);
// Run all the boids
_.each(PresenceSketch.boids, function(boid, key, list) {
boid.run(list);
......
Markdown is supported
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