You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.2 KiB
80 lines
2.2 KiB
import Rx, { Observable } from 'rxjs';
|
|
import Particle from './particle';
|
|
import Grid from './grid';
|
|
import Controls from './controls';
|
|
import { CONTROLS, ENTITIES } from './enums';
|
|
|
|
function Animation2a() {
|
|
this.options = {
|
|
count: 3,
|
|
maxCount: 10,
|
|
showVisionGrid: true,
|
|
speed: 4
|
|
};
|
|
|
|
this.container = document.getElementById('animation2a');
|
|
this.particles = [];
|
|
this.grid = createGlobalGrid(this.container, this.bounds);
|
|
|
|
const controls = new Controls(
|
|
document.getElementById('controls2a'),
|
|
this.options
|
|
);
|
|
|
|
controls.mount().subscribe(this.subscriber.bind(this));
|
|
|
|
this.updateAnimating(this.options.animating);
|
|
this.updateCount(this.options.count);
|
|
};
|
|
|
|
Animation2a.prototype.subscriber = function({ key, value }) {
|
|
switch(key) {
|
|
case CONTROLS.ANIMATING: this.updateAnimating(value); break;
|
|
case CONTROLS.COUNT: this.updateCount(value); break;
|
|
case CONTROLS.SPEED: this.updateSpeed(value); break;
|
|
}
|
|
}
|
|
|
|
Animation2a.prototype.nextFrame = function() {
|
|
this.particles.forEach(p => p.nextFrame());
|
|
}
|
|
|
|
Animation2a.prototype.updateAnimating = function(isAnimating) {
|
|
this.options.animating = isAnimating;
|
|
|
|
if (isAnimating) {
|
|
const fps$ = Rx.Observable.interval(1000 / 32)
|
|
.takeWhile(_ => this.options.animating);
|
|
|
|
fps$.subscribe(this.nextFrame.bind(this));
|
|
}
|
|
}
|
|
|
|
Animation2a.prototype.updateCount = function(count) {
|
|
const bounds = this.container.getBoundingClientRect();
|
|
|
|
while (this.particles.length > count) {
|
|
delete this.particles.pop().remove();
|
|
}
|
|
|
|
while (this.particles.length < count) {
|
|
const p = new Particle(this.container, bounds, this.options, this.grid);
|
|
this.particles.push(p);
|
|
}
|
|
}
|
|
|
|
Animation2a.prototype.updateSpeed = function(value) {
|
|
this.options.speed = value;
|
|
this.particles.forEach(p => p.updateConfig({ speed: value }));
|
|
}
|
|
|
|
function createGlobalGrid(container, bounds) {
|
|
const grid = new Grid();
|
|
|
|
grid.setArea({ x: 100, y: 100, w: 200, h: 200, type: ENTITIES.HAZARD }, container);
|
|
grid.setArea({ x: 600, y: 200, w: 200, h: 200, type: ENTITIES.HAZARD }, container);
|
|
|
|
return grid;
|
|
}
|
|
|
|
export default Animation2a;
|
|
|