|
| 1 | +// Phaser.Physics.Arcade.PhysicsGroup |
| 2 | + |
| 3 | +var ArcadeSprite = require('./ArcadeSprite'); |
| 4 | +var Class = require('../../utils/Class'); |
| 5 | +var CONST = require('./const'); |
| 6 | +var GetFastValue = require('../../utils/object/GetFastValue'); |
| 7 | +var Group = require('../../gameobjects/group/Group'); |
| 8 | +var PhysicsEvent = require('./events'); |
| 9 | + |
| 10 | +var PhysicsGroup = new Class({ |
| 11 | + |
| 12 | + Extends: Group, |
| 13 | + |
| 14 | + initialize: |
| 15 | + |
| 16 | + function PhysicsGroup (world, scene, children, config) |
| 17 | + { |
| 18 | + if (config === undefined && !Array.isArray(children) && typeof children === 'object') |
| 19 | + { |
| 20 | + config = children; |
| 21 | + children = null; |
| 22 | + } |
| 23 | + else if (config === undefined) |
| 24 | + { |
| 25 | + config = {}; |
| 26 | + } |
| 27 | + |
| 28 | + this.world = world; |
| 29 | + |
| 30 | + config.createCallback = this.createCallback; |
| 31 | + config.removeCallback = this.removeCallback; |
| 32 | + config.classType = ArcadeSprite; |
| 33 | + |
| 34 | + this.defaults = { |
| 35 | + setCollideWorldBounds: GetFastValue(config, 'collideWorldBounds', false), |
| 36 | + setAccelerationX: GetFastValue(config, 'accelerationX', 0), |
| 37 | + setAccelerationY: GetFastValue(config, 'accelerationY', 0), |
| 38 | + setBounceX: GetFastValue(config, 'bounceX', 0), |
| 39 | + setBounceY: GetFastValue(config, 'bounceY', 0), |
| 40 | + setDragX: GetFastValue(config, 'dragX', 0), |
| 41 | + setDragY: GetFastValue(config, 'dragY', 0), |
| 42 | + setGravityX: GetFastValue(config, 'gravityX', 0), |
| 43 | + setGravityY: GetFastValue(config, 'gravityY', 0), |
| 44 | + setFrictionX: GetFastValue(config, 'frictionX', 0), |
| 45 | + setFrictionY: GetFastValue(config, 'frictionY', 0), |
| 46 | + setVelocityX: GetFastValue(config, 'velocityX', 0), |
| 47 | + setVelocityY: GetFastValue(config, 'velocityY', 0), |
| 48 | + setAngularVelocity: GetFastValue(config, 'angularVelocity', 0), |
| 49 | + setAngularAcceleration: GetFastValue(config, 'angularAcceleration', 0), |
| 50 | + setAngularDrag: GetFastValue(config, 'angularDrag', 0), |
| 51 | + setMass: GetFastValue(config, 'mass', 1), |
| 52 | + setImmovable: GetFastValue(config, 'immovable', false), |
| 53 | + }; |
| 54 | + |
| 55 | + Group.call(this, scene, children, config); |
| 56 | + }, |
| 57 | + |
| 58 | + createCallback: function (child) |
| 59 | + { |
| 60 | + if (!child.body) |
| 61 | + { |
| 62 | + this.world.enableBody(child); |
| 63 | + } |
| 64 | + |
| 65 | + var body = child.body; |
| 66 | + |
| 67 | + for (var key in this.defaults) |
| 68 | + { |
| 69 | + body[key](this.defaults[key]); |
| 70 | + } |
| 71 | + }, |
| 72 | + |
| 73 | + removeCallback: function (child) |
| 74 | + { |
| 75 | + if (child.body) |
| 76 | + { |
| 77 | + this.world.disableBody(child); |
| 78 | + } |
| 79 | + }, |
| 80 | + |
| 81 | + setVelocity: function (x, y, step) |
| 82 | + { |
| 83 | + if (step === undefined) { step = 0; } |
| 84 | + |
| 85 | + var items = this.getChildren(); |
| 86 | + |
| 87 | + for (var i = 0; i < items.length; i++) |
| 88 | + { |
| 89 | + items[i].body.velocity.set(x + (i * step), y + (i * step)); |
| 90 | + } |
| 91 | + |
| 92 | + return this; |
| 93 | + }, |
| 94 | + |
| 95 | + setVelocityX: function (value, step) |
| 96 | + { |
| 97 | + if (step === undefined) { step = 0; } |
| 98 | + |
| 99 | + var items = this.getChildren(); |
| 100 | + |
| 101 | + for (var i = 0; i < items.length; i++) |
| 102 | + { |
| 103 | + items[i].body.velocity.x = value + (i * step); |
| 104 | + } |
| 105 | + |
| 106 | + return this; |
| 107 | + }, |
| 108 | + |
| 109 | + setVelocityY: function (value, step) |
| 110 | + { |
| 111 | + if (step === undefined) { step = 0; } |
| 112 | + |
| 113 | + var items = this.getChildren(); |
| 114 | + |
| 115 | + for (var i = 0; i < items.length; i++) |
| 116 | + { |
| 117 | + items[i].body.velocity.y = value + (i * step); |
| 118 | + } |
| 119 | + |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | +}); |
| 124 | + |
| 125 | +module.exports = PhysicsGroup; |
0 commit comments