xxxxxxxxxx
64
class Word {
constructor(s) {
// Store a count for occurences in two different books
this.countDracula = 0;
this.countFranken = 0;
// Also the total count
this.totalCount = 0;
this.position = createVector(random(width), random(-height, height * 2));
// What is the String
this.word = s;
}
// We will display a word if it appears at least 5 times
// and only in one of the books
qualify() {
if ((this.countDracula == this.totalCount || this.countFranken == this.totalCount) && this.totalCount > 5) {
return true;
} else {
return false;
}
}
// Increment the count for Dracula
incrementDracula() {
this.countDracula++;
this.totalCount++;
}
// Increment the count for Frankenstein
incrementFranken() {
this.countFranken++;
this.totalCount++;
}
// The more often it appears, the faster it falls
move() {
let speed = map(this.totalCount, 5, 25, 0.1, 0.4);
speed = constrain(speed, 0, 10);
this.position.y += speed;
if (this.position.y > height * 2) {
this.position.y = -height;
}
}
// Depending on which book it gets a color
display() {
if (this.countDracula > 0) {
fill(255);
} else if (this.countFranken > 0) {
fill(0);
}
// Its size is also tied to number of occurences
let fs = map(this.totalCount, 5, 25, 2, 24);
fs = constrain(fs, 2, 48);
textSize(fs);
textAlign(CENTER);
text(this.word, this.position.x, this.position.y);
}
}