Path: blob/main/public/games/files/garbage-collector/js/effects/trail.js
1036 views
/*globals define*/1define([2'base-object',3'utils',4'color',5'config/settings'6], function( BaseObject, Utils, Color, Settings ) {7'use strict';89var PI2 = Utils.PI2;1011var defaults = {12shrink: 0.95,13radius: 1.5,14};1516function Trail( options ) {17BaseObject.call( this );1819this.target = null;20this.particles = [];21this.fill = new Color();22this.period = 1 / 4;2324Utils.defaults( this, options, defaults );2526this.time = 0;27this.game = null;28}2930Trail.prototype = new BaseObject();31Trail.prototype.constructor = Trail;3233Trail.prototype.update = function( dt ) {34if ( !Settings.trail ) {35return;36}3738this.time += dt;3940if ( this.time > this.period ) {41this.time = 0;4243// If the target is moving, add a trail particle.44if ( this.target && ( this.target.vx || this.target.vy ) ) {45var x = this.target.x,46y = this.target.y;4748this.particles.push({49x: x,50y: y,51radius: this.radius52});53}54}5556var removed = [];57this.particles.forEach(function( particle, index ) {58particle.radius *= this.shrink;59if ( particle.radius < this.minRadius ) {60removed.push( index );61}62}.bind( this ));6364Utils.removeIndices( this.particles, removed );65};6667Trail.prototype.draw = function( ctx ) {68if ( !this.particles.length || !Settings.trail ) {69return;70}7172if ( Settings.glow ) {73ctx.globalCompositeOperation = 'lighter';74}7576ctx.beginPath();7778this.particles.forEach(function( particle ) {79ctx.moveTo( particle.x, particle.y );80ctx.arc( particle.x, particle.y, particle.radius, 0, PI2 );81}.bind( this ));8283ctx.fillStyle = this.fill.rgba();84ctx.fill();8586if ( Settings.glow ) {87ctx.globalCompositeOperation = 'source-over';88}89};9091Trail.prototype.aabb = function() {92return null;93};9495Object.defineProperty( Trail.prototype, 'frequency', {96get: function() {97return 1 / this.period;98},99100set: function( frequency ) {101this.period = 1 / frequency;102}103});104105return Trail;106});107108109