Skip to content

Commit

Permalink
sprinkle
Browse files Browse the repository at this point in the history
  • Loading branch information
mallsoft committed Oct 12, 2024
1 parent c5577da commit d3c8782
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
37 changes: 24 additions & 13 deletions src/lib/components/visuals/Bees/bee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Bee {
this.pathIndex = 0;
this.paths = [];
this.speed = 0;
this.tailLength = Math.ceil(20 * Math.random()) + 5;
this.tailLength = Math.ceil(60 * Math.random() * Math.random()) + 5;
this.pos = new Vec(0, 0);
this.dir = new Vec(0, 0);
}
Expand All @@ -23,6 +23,9 @@ export class Bee {
this.speed = Math.random() + 1;
if (Math.random() < 0.3) {
this.speed = Math.random() * 5;
if (Math.random() < 0.1) {
this.speed = Math.random() * 10;
}
}
}

Expand All @@ -39,7 +42,7 @@ export class Bee {

private genPath() {
const path = new Path2D();
const tailFactor = 5;
const tailFactor = 2;
path.moveTo(this.pos.x, this.pos.y);
path.lineTo(
this.pos.x + this.dir.x * (this.speed * tailFactor),
Expand All @@ -62,11 +65,28 @@ export class Bee {
else if (this.pos.y < 0) this.pos.y = window.innerHeight;
}

private hunt(flock: Bee[]) {
if (this.hunting) {
this.turn(this.hunting.pos, 0.09 * Math.random());

if (this.pos.dist(this.hunting.pos) < 100) {
this.hunting = null;
}
} else if (Math.random() < 0.02) {
this.randSpeed();
this.hunting = flock[Math.floor(Math.random() * flock.length)];
}
}

draw(ctx: CanvasRenderingContext2D) {
this.paths[this.pathIndex++ % this.tailLength] = this.genPath();

this.paths.forEach((p) => ctx.stroke(p));

// ctx.beginPath();
// ctx.arc(this.pos.x + this.dir.x, this.pos.y + this.dir.y, this.speed * 2, 0, Math.PI * 2);
// ctx.stroke();

return this.paths;
}

Expand All @@ -75,18 +95,9 @@ export class Bee {

this.wrap();

if (this.hunting) {
this.turn(this.hunting.pos, 0.1 * Math.random());

if (this.pos.dist(this.hunting.pos) < 100) {
this.hunting = null;
}
} else if (Math.random() < 0.04) {
this.randSpeed();
this.hunting = flock[Math.floor(Math.random() * flock.length)];
}
this.hunt(flock);

this.turn(avgHeading, 0.01).turn(avgCenter, 0.01);
this.turn(avgHeading, 0.08).turn(avgCenter, 0.08);

this.dir.normalize();

Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/visuals/Bees/flock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Vec } from './vec';
import { Bee } from './bee';

const FLOCK_SIZE = 10;
const FLOCK_SIZE = 15;
let flock: Array<Bee> = [];

const flockStep = () => {
Expand All @@ -20,9 +20,8 @@ const flockStep = () => {
};

const flockDraw = (ctx: CanvasRenderingContext2D) => {
ctx.lineWidth = 1.5;
// ctx.lineCap = 'round';
ctx.globalAlpha = 0.1;
ctx.globalAlpha = 0.5;
ctx.strokeStyle = getComputedStyle(document.documentElement).getPropertyValue('---c-c1');
ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);

Expand All @@ -33,6 +32,7 @@ const flockDraw = (ctx: CanvasRenderingContext2D) => {

const flocInit = () => {
flock = Array.from({ length: FLOCK_SIZE }, () => new Bee());

for (const b of flock) {
b.respawn();
}
Expand Down

0 comments on commit d3c8782

Please sign in to comment.