Skip to content

Commit

Permalink
Gallery: Add blocks on flyer particle system
Browse files Browse the repository at this point in the history
flyer cohesion with blocks
  • Loading branch information
experdot committed Dec 9, 2018
1 parent 222c1a1 commit fa6bcb9
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 115 deletions.
4 changes: 2 additions & 2 deletions src/scripts/Engine/Drawing/OffscreenCanvas.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class OffscreenCanvas {
private canvas: HTMLCanvasElement;
private context: CanvasRenderingContext2D;
canvas: HTMLCanvasElement;
context: CanvasRenderingContext2D;

constructor(width: number, height: number) {
this.canvas = document.createElement("canvas");
Expand Down
2 changes: 2 additions & 0 deletions src/scripts/Engine/Game/GameObject/GameVisual.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { GeneralNode } from "../../Core/GeneralNode";
import { GameVsiualInterface } from "../GameInterface/GameInterface";
import { Events } from "../../Common/Events";
import { GameWorld } from "../GameWorld/GameWorld";

export class GameVisual extends GeneralNode<GameVsiualInterface> {
events: Events;
world: GameWorld;

constructor() {
super();
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/Engine/Game/GameWorld/GameWorld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class GameWorld extends GameVisual {

width: number;
height: number;
world: GameWorld;

inputs: Inputs;
ui: UIDescription;

Expand Down
19 changes: 6 additions & 13 deletions src/scripts/Gallery/Visuals/ParticleSystem/Flyer/ArrayGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ import {
Array2
} from "../../../../Engine/Collections/Array2";

class ArrayGrid extends Array2 {
offsetX: number[];
offsetY: number[];
export class ArrayGrid extends Array2 {
offsetX: number[] = [0, -1, 0, 1, 1, 1, 0, -1, -1];
offsetY: number[] = [0, -1, -1, -1, 0, 1, 1, 1, 0];

constructor(w, h) {
super(w, h);

this.forEach((cell, x, y) => {
this.data[x][y] = [];
});

this.offsetX = [0, -1, 0, 1, 1, 1, 0, -1, -1];
this.offsetY = [0, -1, -1, -1, 0, 1, 1, 1, 0];
}

clear() {
Expand All @@ -24,7 +21,7 @@ class ArrayGrid extends Array2 {
}

neighbours(x, y) {
let result = [];
const result = [];
for (let i = 0; i < 9; i++) {
let dx = x + this.offsetX[i];
let dy = y + this.offsetY[i];
Expand All @@ -34,8 +31,4 @@ class ArrayGrid extends Array2 {
}
return result;
}
}

export {
ArrayGrid
};
}
34 changes: 14 additions & 20 deletions src/scripts/Gallery/Visuals/ParticleSystem/Flyer/FlyerParticle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Vector2
} from "../../../../Engine/Numerics/Vector2";

class FlyerParticle extends DynamicParticle {
export class FlyerParticle extends DynamicParticle {
neighbourDistance: number;

constructor(location?: Vector2, size = 1, age = 0) {
Expand All @@ -18,7 +18,7 @@ class FlyerParticle extends DynamicParticle {
this.alignspeed(flyers);
this.seperate(flyers);
this.cohesion(flyers);
//this.seperate(blocks, 5);
this.cohesion(blocks, 6);

mouse && this.follow(mouse, 3);
//this.checkRadius(mouse, new Vector2(rect.width, rect.height).length / 3);
Expand All @@ -30,7 +30,7 @@ class FlyerParticle extends DynamicParticle {
alignspeed(flyers) {
let sum = new Vector2();
let sumCount = 0;
this._forEachDistance(flyers, (element, offset, distance) => {
this.forEachDistance(flyers, (element, offset, distance) => {
if (distance < this.neighbourDistance) {
sum = sum.add(element.velocity);
sumCount++;
Expand All @@ -48,7 +48,7 @@ class FlyerParticle extends DynamicParticle {
seperate(flyers, ratio = 1) {
let sum = new Vector2();
let sumCount = 0;
this._forEachDistance(flyers, (element, offset, distance) => {
this.forEachDistance(flyers, (element, offset, distance) => {
let minDistance = (this.size + element.size) / 2 + 5;
if (distance > 0 && distance < minDistance) {
offset.normalize();
Expand All @@ -67,25 +67,25 @@ class FlyerParticle extends DynamicParticle {
}
}

cohesion(flyers) {
cohesion(flyers, ratio = 1) {
let sum = new Vector2();
let sumCount = 0;
this._forEachDistance(flyers, (element, offset, distance) => {
this.forEachDistance(flyers, (element, offset, distance) => {
if (distance < this.neighbourDistance) {
sum = sum.add(element.location);
sumCount++;
}
});
if (sumCount > 0) {
sum = sum.divide(sumCount);
let steer = this.seek(sum);
let steer = this.seekDefault(sum);
steer.limitLength(0.1);
this.applyForce(steer);
this.applyForce(steer.multiply(ratio));
}
}

follow(target, ratio = 1) {
this.applyForce(this.seek(target).multiply(ratio));
this.applyForce(this.seekDefault(target).multiply(ratio));
}

checkRadius(mouse, maxRadius = 100) {
Expand Down Expand Up @@ -125,12 +125,11 @@ class FlyerParticle extends DynamicParticle {
}
}

seek(target) {
return this._seek(this.location, target, this.velocity);
seekDefault(target) {
return this.seek(this.location, target, this.velocity);
}


_seek(source, target, velocity) {
private seek(source, target, velocity) {
let desired = target.subtract(source);
desired.normalize();
desired.multiply(this.velocityUpon);
Expand All @@ -139,16 +138,11 @@ class FlyerParticle extends DynamicParticle {
return steer;
}

_forEachDistance(flyers, action) {
private forEachDistance(flyers, action) {
flyers.forEach(element => {
let offset = this.location.subtract(element.location);
let distance = offset.length;
action && action(element, offset, distance);
});
}

}

export {
FlyerParticle
};
}
Loading

0 comments on commit fa6bcb9

Please sign in to comment.