-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathp5beh.ts
More file actions
69 lines (61 loc) · 2.04 KB
/
p5beh.ts
File metadata and controls
69 lines (61 loc) · 2.04 KB
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
/* MoMath Math Square P5 interface
* Provides shims for using a P5 drawing as a behavior.
* Note that this replaces the P5 main rendering loop with normal Math Square one.
*/
import * as Display from 'display'
import * as Sensor from 'sensors'
import Floor from 'floor'
import {User} from 'floor'
import p5 from 'p5'
export default class P5Behavior {
public p5: p5
public preload?: (this: p5, p5: p5) => void
public setup?: (this: p5, p5: p5) => void
public draw?: (this: p5, floor: Floor, p5: p5) => void
public renderer?: 'p2d'|'webgl'
constructor(public sketch?: (p5: p5) => void) {
}
/* Should be called from/as Behavior.init */
init(container: HTMLDivElement) {
this.p5 = new p5((p: p5) => {
this.p5 = p;
if (this.preload)
p.preload = this.preload.bind(p, p);
p.setup = () => {
p.createCanvas(Display.width, Display.height, this.renderer);
if (this.setup)
this.setup.call(p, p);
};
if (this.sketch)
this.sketch(p);
if (!p.draw) {
/* don't draw anything yet */
p.draw = function () {};
p.noLoop();
}
}, container, true);
}
/* Should be called from/as Behavior.render */
render(floor: Floor) {
if (this.draw)
this.p5.draw = this.draw.bind(this.p5, floor, this.p5);
this.p5._draw(); // this.p5.redraw();
}
/* Draw the sphere for a user, just like THREEContext.updateUsers does */
drawUser(user: User) {
this.p5.fill(user.id >= 0 ? Display.teamColors[user.id%Display.teamColors.length] : 255);
this.p5.noStroke();
this.p5.ellipse(user.x, user.y, 24);
this.p5.noFill();
this.p5.stroke(68);
this.p5.strokeWeight(1);
this.p5.ellipse(user.x, user.y, 31);
}
/* Draw a grid of sensors, using the current drawing style */
drawSensors(sensors: Sensor.Grid) {
this.p5.applyMatrix(Display.sensorWidth, 0, 0, Display.sensorHeight, 0, 0);
for (let i: Sensor.Index|undefined = new Sensor.Index(); i; i = i.incr())
if (sensors.get(i))
this.p5.rect(i.x+0.05, i.y+0.05, 0.9, 0.9);
}
}