Phaser.Weapon = function (game, parent){ Phaser.Plugin.call(this, game, parent); this.bullets = null ; this.autoExpandBulletsGroup = false ; this.autofire = false ; this.shots = 0; this.fireLimit = 0; this.fireRate = 100; this.fireRateVariance = 0; this.fireFrom = new Phaser.Rectangle(0, 0, 1, 1); this.fireAngle = Phaser.ANGLE_UP; this.bulletInheritSpriteSpeed = false ; this.bulletAnimation = ''; this.bulletFrameRandom = false ; this.bulletFrameCycle = false ; this.bulletWorldWrap = false ; this.bulletWorldWrapPadding = 0; this.bulletAngleOffset = 0; this.bulletAngleVariance = 0; this.bulletSpeed = 200; this.bulletSpeedVariance = 0; this.bulletLifespan = 0; this.bulletKillDistance = 0; this.bulletGravity = new Phaser.Point(0, 0); this.bulletRotateToVelocity = false ; this.bulletKey = ''; this.bulletFrame = ''; this._bulletClass = Phaser.Bullet; this._bulletCollideWorldBounds = false ; this._bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS; this._data = { customBody: false , width: 0, height: 0, offsetX: 0, offsetY: 0} ; this.bounds = new Phaser.Rectangle(); this.bulletBounds = game.world.bounds; this.bulletFrames = [] ; this.bulletFrameIndex = 0; this.anims = { } ; this.onFire = new Phaser.Signal(); this.onKill = new Phaser.Signal(); this.onFireLimit = new Phaser.Signal(); this.trackedSprite = null ; this.trackedPointer = null ; this.trackRotation = false ; this.trackOffset = new Phaser.Point(); this._nextFire = 0; } ; Phaser.Weapon.prototype = Object.create(Phaser.Plugin.prototype); Phaser.Weapon.prototype.constructor = Phaser.Weapon; Phaser.Weapon.KILL_NEVER = 0; Phaser.Weapon.KILL_LIFESPAN = 1; Phaser.Weapon.KILL_DISTANCE = 2; Phaser.Weapon.KILL_WEAPON_BOUNDS = 3; Phaser.Weapon.KILL_CAMERA_BOUNDS = 4; Phaser.Weapon.KILL_WORLD_BOUNDS = 5; Phaser.Weapon.KILL_STATIC_BOUNDS = 6; Phaser.Weapon.prototype.createBullets = function (quantity, key, frame, group){ if (quantity === undefined) { quantity = 1; } if (group === undefined) { group = this.game.world; } if (!this.bullets) { this.bullets = this.game.add.physicsGroup(Phaser.Physics.ARCADE, group); this.bullets.classType = this._bulletClass; } if (quantity !== 0) { if (quantity === -1) { this.autoExpandBulletsGroup = true ; quantity = 1; } this.bullets.createMultiple(quantity, key, frame); this.bullets.setAll('data.bulletManager', this); this.bulletKey = key; this.bulletFrame = frame; } return this; } ; Phaser.Weapon.prototype.forEach = function (callback, callbackContext){ this.bullets.forEachExists(callback, callbackContext, arguments); return this; } ; Phaser.Weapon.prototype.pauseAll = function (){ this.bullets.setAll('body.enable', false ); return this; } ; Phaser.Weapon.prototype.resumeAll = function (){ this.bullets.setAll('body.enable', true ); return this; } ; Phaser.Weapon.prototype.killAll = function (){ this.bullets.callAllExists('kill', true ); this.bullets.setAll('body.enable', true ); return this; } ; Phaser.Weapon.prototype.resetShots = function (newLimit){ this.shots = 0; if (newLimit !== undefined) { this.fireLimit = newLimit; } return this; } ; Phaser.Weapon.prototype.destroy = function (){ this.parent.remove(this, false ); this.bullets.destroy(); this.game = null ; this.parent = null ; this.active = false ; this.visible = false ; } ; Phaser.Weapon.prototype.update = function (){ if (this._bulletKillType === Phaser.Weapon.KILL_WEAPON_BOUNDS) { if (this.trackedSprite) { this.trackedSprite.updateTransform(); this.bounds.centerOn(this.trackedSprite.worldPosition.x, this.trackedSprite.worldPosition.y); } else if (this.trackedPointer) { this.bounds.centerOn(this.trackedPointer.worldX, this.trackedPointer.worldY); } } if (this.autofire && this.game.time.now < this._nextFire) { this.fire(); } } ; Phaser.Weapon.prototype.trackSprite = function (sprite, offsetX, offsetY, trackRotation){ if (offsetX === undefined) { offsetX = 0; } if (offsetY === undefined) { offsetY = 0; } if (trackRotation === undefined) { trackRotation = false ; } this.trackedPointer = null ; this.trackedSprite = sprite; this.trackRotation = trackRotation; this.trackOffset.set(offsetX, offsetY); return this; } ; Phaser.Weapon.prototype.trackPointer = function (pointer, offsetX, offsetY){ if (pointer === undefined) { pointer = this.game.input.activePointer; } if (offsetX === undefined) { offsetX = 0; } if (offsetY === undefined) { offsetY = 0; } this.trackedPointer = pointer; this.trackedSprite = null ; this.trackRotation = false ; this.trackOffset.set(offsetX, offsetY); return this; } ; Phaser.Weapon.prototype.fire = function (from, x, y){ if (this.game.time.now < this._nextFire || (this.fireLimit > 0 && this.shots === this.fireLimit)) { return false ; } var speed = this.bulletSpeed; if (this.bulletSpeedVariance !== 0) { speed += Phaser.Math.between(- this.bulletSpeedVariance, this.bulletSpeedVariance); } if (from) { if (this.fireFrom.width > 1) { this.fireFrom.centerOn(from.x, from.y); } else { this.fireFrom.x = from.x; this.fireFrom.y = from.y; } } else if (this.trackedSprite) { if (this.fireFrom.width > 1) { this.fireFrom.centerOn(this.trackedSprite.world.x + this.trackOffset.x, this.trackedSprite.world.y + this.trackOffset.y); } else { this.fireFrom.x = this.trackedSprite.world.x + this.trackOffset.x; this.fireFrom.y = this.trackedSprite.world.y + this.trackOffset.y; } if (this.bulletInheritSpriteSpeed) { speed += this.trackedSprite.body.speed; } } else if (this.trackedPointer) { if (this.fireFrom.width > 1) { this.fireFrom.centerOn(this.trackedPointer.world.x + this.trackOffset.x, this.trackedPointer.world.y + this.trackOffset.y); } else { this.fireFrom.x = this.trackedPointer.world.x + this.trackOffset.x; this.fireFrom.y = this.trackedPointer.world.y + this.trackOffset.y; } } var fromX = (this.fireFrom.width > 1)? this.fireFrom.randomX: this.fireFrom.x; var fromY = (this.fireFrom.height > 1)? this.fireFrom.randomY: this.fireFrom.y; var angle = (this.trackRotation)? this.trackedSprite.angle: this.fireAngle; if (x !== undefined && y !== undefined) { angle = this.game.math.radToDeg(Math.atan2(y - fromY, x - fromX)); } if (this.bulletAngleVariance !== 0) { angle += Phaser.Math.between(- this.bulletAngleVariance, this.bulletAngleVariance); } var moveX = 0; var moveY = 0; if (angle === 0 || angle === 180) { moveX = Math.cos(this.game.math.degToRad(angle)) * speed; } else if (angle === 90 || angle === 270) { moveY = Math.sin(this.game.math.degToRad(angle)) * speed; } else { moveX = Math.cos(this.game.math.degToRad(angle)) * speed; moveY = Math.sin(this.game.math.degToRad(angle)) * speed; } var bullet = null ; if (this.autoExpandBulletsGroup) { bullet = this.bullets.getFirstExists(false , true , fromX, fromY, this.bulletKey, this.bulletFrame); bullet.data.bulletManager = this; } else { bullet = this.bullets.getFirstExists(false ); } if (bullet) { bullet.reset(fromX, fromY); bullet.data.fromX = fromX; bullet.data.fromY = fromY; bullet.data.killType = this.bulletKillType; bullet.data.killDistance = this.bulletKillDistance; bullet.data.rotateToVelocity = this.bulletRotateToVelocity; if (this.bulletKillType === Phaser.Weapon.KILL_LIFESPAN) { bullet.lifespan = this.bulletLifespan; } bullet.angle = angle + this.bulletAngleOffset; if (this.bulletAnimation !== '') { if (bullet.animations.getAnimation(this.bulletAnimation) === null ) { var anim = this.anims[this.bulletAnimation]; bullet.animations.add(anim.name, anim.frames, anim.frameRate, anim.loop, anim.useNumericIndex); } bullet.animations.play(this.bulletAnimation); } else { if (this.bulletFrameCycle) { bullet.frame = this.bulletFrames[this.bulletFrameIndex]; this.bulletFrameIndex++ ; if (this.bulletFrameIndex >= _AN_Read_length('length', this.bulletFrames)) { this.bulletFrameIndex = 0; } } else if (this.bulletFrameRandom) { bullet.frame = this.bulletFrames[Math.floor(Math.random() * _AN_Read_length('length', this.bulletFrames))]; } } if (bullet.data.bodyDirty) { if (this._data.customBody) { bullet.body.setSize(this._data.width, this._data.height, this._data.offsetX, this._data.offsetY); } bullet.body.collideWorldBounds = this.bulletCollideWorldBounds; bullet.data.bodyDirty = false ; } bullet.body.velocity.set(moveX, moveY); bullet.body.gravity.set(this.bulletGravity.x, this.bulletGravity.y); this._nextFire = this.game.time.now + this.fireRate; this.shots++ ; this.onFire.dispatch(bullet, this, speed); if (this.fireLimit > 0 && this.shots === this.fireLimit) { this.onFireLimit.dispatch(this, this.fireLimit); } } } ; Phaser.Weapon.prototype.fireAtPointer = function (pointer){ if (pointer === undefined) { pointer = this.game.input.activePointer; } return this.fire(null , pointer.worldX, pointer.worldY); } ; Phaser.Weapon.prototype.fireAtSprite = function (sprite){ return this.fire(null , sprite.world.x, sprite.world.y); } ; Phaser.Weapon.prototype.fireAtXY = function (x, y){ return this.fire(null , x, y); } ; Phaser.Weapon.prototype.setBulletBodyOffset = function (width, height, offsetX, offsetY){ if (offsetX === undefined) { offsetX = 0; } if (offsetY === undefined) { offsetY = 0; } this._data.customBody = true ; this._data.width = width; this._data.height = height; this._data.offsetX = offsetX; this._data.offsetY = offsetY; this.bullets.callAll('body.setSize', 'body', width, height, offsetX, offsetY); this.bullets.setAll('data.bodyDirty', false ); return this; } ; Phaser.Weapon.prototype.setBulletFrames = function (min, max, cycle, random){ if (cycle === undefined) { cycle = true ; } if (random === undefined) { random = false ; } this.bulletFrames = Phaser.ArrayUtils.numberArray(min, max); this.bulletFrameIndex = 0; this.bulletFrameCycle = cycle; this.bulletFrameRandom = random; return this; } ; Phaser.Weapon.prototype.addBulletAnimation = function (name, frames, frameRate, loop, useNumericIndex){ this.anims[name] = { name: name, frames: frames, frameRate: frameRate, loop: loop, useNumericIndex: useNumericIndex} ; this.bullets.callAll('animations.add', 'animations', name, frames, frameRate, loop, useNumericIndex); this.bulletAnimation = name; return this; } ; Phaser.Weapon.prototype.debug = function (x, y, debugBodies){ if (x === undefined) { x = 16; } if (y === undefined) { y = 32; } if (debugBodies === undefined) { debugBodies = false ; } this.game.debug.text("Weapon Plugin", x, y); this.game.debug.text("Bullets Alive: " + this.bullets.total + " - Total: " + _AN_Read_length("length", this.bullets), x, y + 24); if (debugBodies) { this.bullets.forEachExists(this.game.debug.body, this.game.debug, 'rgba(255, 0, 255, 0.8)'); } } ; Object.defineProperty(Phaser.Weapon.prototype, "bulletClass", { get: function (){ return this._bulletClass; } , set: function (classType){ this._bulletClass = classType; this.bullets.classType = this._bulletClass; } } ); Object.defineProperty(Phaser.Weapon.prototype, "bulletKillType", { get: function (){ return this._bulletKillType; } , set: function (type){ switch (type){ case Phaser.Weapon.KILL_STATIC_BOUNDS: case Phaser.Weapon.KILL_WEAPON_BOUNDS: this.bulletBounds = this.bounds; break ; case Phaser.Weapon.KILL_CAMERA_BOUNDS: this.bulletBounds = this.game.camera.view; break ; case Phaser.Weapon.KILL_WORLD_BOUNDS: this.bulletBounds = this.game.world.bounds; break ; } this._bulletKillType = type; } } ); Object.defineProperty(Phaser.Weapon.prototype, "bulletCollideWorldBounds", { get: function (){ return this._bulletCollideWorldBounds; } , set: function (value){ this._bulletCollideWorldBounds = value; this.bullets.setAll('body.collideWorldBounds', value); this.bullets.setAll('data.bodyDirty', false ); } } ); Object.defineProperty(Phaser.Weapon.prototype, "x", { get: function (){ return this.fireFrom.x; } , set: function (value){ this.fireFrom.x = value; } } ); Object.defineProperty(Phaser.Weapon.prototype, "y", { get: function (){ return this.fireFrom.y; } , set: function (value){ this.fireFrom.y = value; } } );