-
Notifications
You must be signed in to change notification settings - Fork 0
/
character.js
105 lines (98 loc) · 2.84 KB
/
character.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
import { TargetChaserStroke } from './strokes/target-chaser-stroke';
/* A Character variation is based off of strokes from a grid
* The character initiates a stroke by creating an agent that traverses the grid from one point to another
* In this case, the stroke created starts at a point in the grid, and steers toward anothe point in the grid in to create the stroke.
*
*/
export class Character {
constructor({ numStrokes, sizeX, sizeY, xGrid, yGrid }) {
this.numStrokes = numStrokes;
this.sizeX = sizeX;
this.sizeY = sizeY;
this.xGrid = xGrid; //int
this.yGrid = yGrid; //int
this.cellX = sizeX / xGrid;
this.cellY = sizeY / yGrid;
this.numCells = xGrid * yGrid;
this.currentStroke = this.randomStroke();
this.done = false;
this.paths = []; // array of arrays
this.boundingBox = {
minX: Number.MAX_SAFE_INTEGER,
minY: Number.MAX_SAFE_INTEGER,
maxX: Number.MIN_SAFE_INTEGER,
maxY: Number.MIN_SAFE_INTEGER,
};
}
//returns vector
getGridCenterLoc(intLoc) {
// integer index in grid
return createVector(
this.cellX / 2.0 + (intLoc % this.xGrid) * this.cellX,
this.cellY / 2.0 + Math.floor(intLoc / this.xGrid) * this.cellY
);
}
getUnitVectorFromInt(dir) {
return createVector(-1 + (dir % 3), -1 + Math.floor(dir / 3));
}
getRowCol(intLoc) {
return [intLoc / this.xGrid, intLoc % this.xGrid];
}
isOutsideLoc(intLoc) {
const [row, col] = getRowCol[intLoc];
if (row === 0 || row === this.xGrid - 1) {
return true;
}
if (col === 0 || col === this.yGrid - 1) {
return true;
}
return false;
}
getControlledUnitVectorBasedOffLoc(loc) {}
randomStroke() {
var targetProps = {
loc: this.getGridCenterLoc(Math.floor(random(this.numCells))),
};
var chaserProps = {
initLoc: this.getGridCenterLoc(Math.floor(random(this.numCells))),
initVel: this.getUnitVectorFromInt(Math.floor(random(9))).mult(8),
};
var duration = random(10, 30);
return new TargetChaserStroke({ chaserProps, targetProps, duration });
}
print() {
while (this.numStrokes > 0) {
const currStroke = this.randomStroke();
currStroke.print();
this.paths.push(currStroke.getPath());
this.numStrokes--;
}
this.done = true;
}
draw() {
if (this.currentStroke.done()) {
this.paths.push(this.currentStroke.getPath());
if (this.numStrokes !== 0) {
this.currentStroke = this.randomStroke();
this.numStrokes--;
} else {
this.done = true;
}
} else {
this.currentStroke.update();
this.currentStroke.draw();
}
}
debugLines() {
strokeWeight(1);
stroke(0);
noFill();
this.paths.forEach((path) => {
beginShape();
path.forEach((pt) => {
vertex(pt.x, pt.y);
});
endShape();
});
}
}