|
| 1 | +// Phaser.Physics.Arcade.World |
| 2 | + |
| 3 | +var Class = require('../../utils/Class'); |
| 4 | +var Rectangle = require('../../geom/rectangle/Rectangle'); |
| 5 | +var Vector2 = require('../../math/Vector2'); |
| 6 | +var CONST = require('./const'); |
| 7 | + |
| 8 | +var World = new Class({ |
| 9 | + |
| 10 | + initialize: |
| 11 | + |
| 12 | + function World (width, height) |
| 13 | + { |
| 14 | + this.gravity = new Vector2(); |
| 15 | + |
| 16 | + this.bounds = new Rectangle(0, 0, width, height); |
| 17 | + |
| 18 | + this.checkCollision = { up: true, down: true, left: true, right: true }; |
| 19 | + |
| 20 | + this.maxObjects = 10; |
| 21 | + |
| 22 | + this.maxLevels = 4; |
| 23 | + |
| 24 | + this.OVERLAP_BIAS = 4; |
| 25 | + |
| 26 | + this.forceX = false; |
| 27 | + |
| 28 | + this.sortDirection = CONST.LEFT_RIGHT; |
| 29 | + |
| 30 | + this.skipQuadTree = true; |
| 31 | + |
| 32 | + this.isPaused = false; |
| 33 | + |
| 34 | + // this.quadTree = new Phaser.QuadTree(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels); |
| 35 | + |
| 36 | + this._total = 0; |
| 37 | + |
| 38 | + // this.setBoundsToWorld(); |
| 39 | + }, |
| 40 | + |
| 41 | + setBounds: function (x, y, width, height) |
| 42 | + { |
| 43 | + this.bounds.setTo(x, y, width, height); |
| 44 | + |
| 45 | + return this; |
| 46 | + }, |
| 47 | + |
| 48 | + updateMotion: function (body) |
| 49 | + { |
| 50 | + if (body.allowRotation) |
| 51 | + { |
| 52 | + var velocityDelta = this.computeVelocity(0, body, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity; |
| 53 | + |
| 54 | + body.angularVelocity += velocityDelta; |
| 55 | + body.rotation += (body.angularVelocity * this.game.time.physicsElapsed); |
| 56 | + } |
| 57 | + |
| 58 | + body.velocity.x = this.computeVelocity(1, body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x); |
| 59 | + body.velocity.y = this.computeVelocity(2, body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y); |
| 60 | + }, |
| 61 | + |
| 62 | + computeVelocity: function (axis, body, velocity, acceleration, drag, max) |
| 63 | + { |
| 64 | + if (max === undefined) { max = 10000; } |
| 65 | + |
| 66 | + if (axis === 1 && body.allowGravity) |
| 67 | + { |
| 68 | + velocity += (this.gravity.x + body.gravity.x) * this.game.time.physicsElapsed; |
| 69 | + } |
| 70 | + else if (axis === 2 && body.allowGravity) |
| 71 | + { |
| 72 | + velocity += (this.gravity.y + body.gravity.y) * this.game.time.physicsElapsed; |
| 73 | + } |
| 74 | + |
| 75 | + if (acceleration) |
| 76 | + { |
| 77 | + velocity += acceleration * this.game.time.physicsElapsed; |
| 78 | + } |
| 79 | + else if (drag && body.allowDrag) |
| 80 | + { |
| 81 | + drag *= this.game.time.physicsElapsed; |
| 82 | + |
| 83 | + if (velocity - drag > 0) |
| 84 | + { |
| 85 | + velocity -= drag; |
| 86 | + } |
| 87 | + else if (velocity + drag < 0) |
| 88 | + { |
| 89 | + velocity += drag; |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + velocity = 0; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if (velocity > max) |
| 98 | + { |
| 99 | + velocity = max; |
| 100 | + } |
| 101 | + else if (velocity < -max) |
| 102 | + { |
| 103 | + velocity = -max; |
| 104 | + } |
| 105 | + |
| 106 | + return velocity; |
| 107 | + }, |
| 108 | + |
| 109 | + overlap: function (object1, object2, overlapCallback, processCallback, callbackContext) |
| 110 | + { |
| 111 | + overlapCallback = overlapCallback || null; |
| 112 | + processCallback = processCallback || null; |
| 113 | + callbackContext = callbackContext || overlapCallback; |
| 114 | + |
| 115 | + this._total = 0; |
| 116 | + |
| 117 | + this.collideObjects(object1, object2, overlapCallback, processCallback, callbackContext, true); |
| 118 | + |
| 119 | + return (this._total > 0); |
| 120 | + }, |
| 121 | + |
| 122 | + collide: function (object1, object2, collideCallback, processCallback, callbackContext) |
| 123 | + { |
| 124 | + collideCallback = collideCallback || null; |
| 125 | + processCallback = processCallback || null; |
| 126 | + callbackContext = callbackContext || collideCallback; |
| 127 | + |
| 128 | + this._total = 0; |
| 129 | + |
| 130 | + this.collideObjects(object1, object2, collideCallback, processCallback, callbackContext, false); |
| 131 | + |
| 132 | + return (this._total > 0); |
| 133 | + }, |
| 134 | + |
| 135 | + sortLeftRight: function (a, b) |
| 136 | + { |
| 137 | + if (!a.body || !b.body) |
| 138 | + { |
| 139 | + return 0; |
| 140 | + } |
| 141 | + |
| 142 | + return a.body.x - b.body.x; |
| 143 | + }, |
| 144 | + |
| 145 | + sortRightLeft: function (a, b) |
| 146 | + { |
| 147 | + if (!a.body || !b.body) |
| 148 | + { |
| 149 | + return 0; |
| 150 | + } |
| 151 | + |
| 152 | + return b.body.x - a.body.x; |
| 153 | + }, |
| 154 | + |
| 155 | + sortTopBottom: function (a, b) |
| 156 | + { |
| 157 | + if (!a.body || !b.body) |
| 158 | + { |
| 159 | + return 0; |
| 160 | + } |
| 161 | + |
| 162 | + return a.body.y - b.body.y; |
| 163 | + }, |
| 164 | + |
| 165 | + sortBottomTop: function (a, b) |
| 166 | + { |
| 167 | + if (!a.body || !b.body) |
| 168 | + { |
| 169 | + return 0; |
| 170 | + } |
| 171 | + |
| 172 | + return b.body.y - a.body.y; |
| 173 | + }, |
| 174 | + |
| 175 | + /* |
| 176 | + sort: function (group, sortDirection) |
| 177 | + { |
| 178 | + if (group.physicsSortDirection !== null) |
| 179 | + { |
| 180 | + sortDirection = group.physicsSortDirection; |
| 181 | + } |
| 182 | + else |
| 183 | + { |
| 184 | + if (sortDirection === undefined) { sortDirection = this.sortDirection; } |
| 185 | + } |
| 186 | +
|
| 187 | + if (sortDirection === Phaser.Physics.Arcade.LEFT_RIGHT) |
| 188 | + { |
| 189 | + // Game world is say 2000x600 and you start at 0 |
| 190 | + group.hash.sort(this.sortLeftRight); |
| 191 | + } |
| 192 | + else if (sortDirection === Phaser.Physics.Arcade.RIGHT_LEFT) |
| 193 | + { |
| 194 | + // Game world is say 2000x600 and you start at 2000 |
| 195 | + group.hash.sort(this.sortRightLeft); |
| 196 | + } |
| 197 | + else if (sortDirection === Phaser.Physics.Arcade.TOP_BOTTOM) |
| 198 | + { |
| 199 | + // Game world is say 800x2000 and you start at 0 |
| 200 | + group.hash.sort(this.sortTopBottom); |
| 201 | + } |
| 202 | + else if (sortDirection === Phaser.Physics.Arcade.BOTTOM_TOP) |
| 203 | + { |
| 204 | + // Game world is say 800x2000 and you start at 2000 |
| 205 | + group.hash.sort(this.sortBottomTop); |
| 206 | + } |
| 207 | + }, |
| 208 | + */ |
| 209 | + |
| 210 | + collideObjects: function (object1, object2, collideCallback, processCallback, callbackContext, overlapOnly) |
| 211 | + { |
| 212 | + if (!Array.isArray(object1) && Array.isArray(object2)) |
| 213 | + { |
| 214 | + for (var i = 0; i < object2.length; i++) |
| 215 | + { |
| 216 | + if (!object2[i]) { continue; } |
| 217 | + |
| 218 | + this.collideHandler(object1, object2[i], collideCallback, processCallback, callbackContext, overlapOnly); |
| 219 | + } |
| 220 | + } |
| 221 | + else if (Array.isArray(object1) && !Array.isArray(object2)) |
| 222 | + { |
| 223 | + for (var i = 0; i < object1.length; i++) |
| 224 | + { |
| 225 | + if (!object1[i]) { continue; } |
| 226 | + |
| 227 | + this.collideHandler(object1[i], object2, collideCallback, processCallback, callbackContext, overlapOnly); |
| 228 | + } |
| 229 | + } |
| 230 | + else if (Array.isArray(object1) && Array.isArray(object2)) |
| 231 | + { |
| 232 | + for (var i = 0; i < object1.length; i++) |
| 233 | + { |
| 234 | + if (!object1[i]) { continue; } |
| 235 | + |
| 236 | + for (var j = 0; j < object2.length; j++) |
| 237 | + { |
| 238 | + if (!object2[j]) { continue; } |
| 239 | + |
| 240 | + this.collideHandler(object1[i], object2[j], collideCallback, processCallback, callbackContext, overlapOnly); |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + else |
| 245 | + { |
| 246 | + this.collideHandler(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly); |
| 247 | + } |
| 248 | + }, |
| 249 | + |
| 250 | +}); |
| 251 | + |
| 252 | +module.exports = World; |
0 commit comments