Path: blob/main/public/games/files/garbage-collector/js/entities/explosion.js
1036 views
/*globals define*/1define([2'entities/entity',3'utils',4'config/settings'5], function( Entity, Utils, Settings ) {6'use strict';78var PI2 = Utils.PI2;910var defaults = {11particleCount: 10,1213radius: 1.0,14radiusSpread: 0.6,1516minRadius: 0.05,1718drag: 0.925,19dragSpread: 0.025,2021force: 10,22forceSpread: 5,2324spin: 1.5,25spinSpread: 0.5,2627shrink: 0.9528};2930function Explosion( x, y, options ) {31Entity.call( this, x, y );3233this.area = null;34this.particles = [];3536Utils.defaults( this, options, defaults );3738this.initialize();39}4041Explosion.prototype = new Entity();42Explosion.prototype.constructor = Explosion;4344Explosion.prototype.initialize = function() {45var particleCount = this.particleCount;4647var x, y;48var point;49var angle, force;50while ( particleCount-- ) {51x = 0;52y = 0;5354if ( this.area ) {55point = this.area.random();56x = point.x;57y = point.y;58}5960angle = Math.random() * PI2;61force = Utils.floatSpread( this.force, this.forceSpread );6263this.particles.push({64x: x,65y: y,66radius: Utils.floatSpread( this.radius, this.radiusSpread ),67angle: angle,68vx: Math.cos( angle ) * force,69vy: Math.sin( angle ) * force,70drag: Utils.floatSpread( this.drag, this.dragSpread ),71spin: Utils.floatSpread( this.spin, this.spinSpread )72});73}74};7576Explosion.prototype.update = function( dt ) {77Entity.prototype.update.call( this, dt );7879var removed = [];80this.particles.forEach(function( particle, index ) {81particle.x += particle.vx * dt;82particle.y += particle.vy * dt;8384particle.vx *= particle.drag;85particle.vy *= particle.drag;8687particle.angle += Utils.randomFloat( -0.5, 0.5 ) * particle.spin;8889particle.vx += Math.cos( particle.angle ) * 0.1;90particle.vy += Math.sin( particle.angle ) * 0.1;9192particle.radius *= this.shrink;93if ( particle.radius < this.minRadius ) {94removed.push( index );95}96}.bind( this ));9798Utils.removeIndices( this.particles, removed );99100if ( !this.particles.length ) {101this.game.removed.push( this );102}103};104105Explosion.prototype.draw = function( ctx ) {106ctx.save();107108ctx.translate( this.x, this.y );109ctx.rotate( -this.angle );110111if ( Settings.glow ) {112ctx.globalCompositeOperation = 'lighter';113}114115ctx.fillStyle = this.fill.rgba();116this.particles.forEach(function( particle ) {117ctx.beginPath();118ctx.arc( particle.x, particle.y, particle.radius, 0, PI2 );119ctx.fill();120});121122if ( Settings.glow ) {123ctx.globalCompositeOperation = 'source-over';124}125126ctx.restore();127};128129Explosion.prototype.aabb = function() {130return {131xmin: this.x - 8,132ymin: this.y - 8,133xmax: this.x + 8,134ymax: this.y + 8135};136};137138return Explosion;139});140141142