|
| 1 | +// Phaser.Physics.Impact.Body |
| 2 | + |
| 3 | +var Class = require('../../utils/Class'); |
| 4 | +var UpdateVelocity = require('./UpdateVelocity'); |
| 5 | +var UpdateMotion = require('./UpdateMotion'); |
| 6 | +var Trace = require('./Trace'); |
| 7 | +var COLLIDES = require('./COLLIDES'); |
| 8 | +var TYPE = require('./TYPE'); |
| 9 | + |
| 10 | +/** |
| 11 | +* An Impact.js compatible physics body. |
| 12 | +* This re-creates the properties you'd get on an Entity and the math needed to update them. |
| 13 | +* |
| 14 | +* @class |
| 15 | +*/ |
| 16 | + |
| 17 | +var Body = new Class({ |
| 18 | + |
| 19 | + initialize: |
| 20 | + |
| 21 | + function Body (world, x, y) |
| 22 | + { |
| 23 | + this.world = world; |
| 24 | + |
| 25 | + this.enabled = true; |
| 26 | + |
| 27 | + this.size = { x: 16, y: 16 }; |
| 28 | + this.offset = { x: 0, y: 0 }; |
| 29 | + this.pos = { x: x, y: y }; |
| 30 | + this.last = { x: 0, y: 0 }; |
| 31 | + this.vel = { x: 0, y: 0 }; |
| 32 | + this.accel = { x: 0, y: 0 }; |
| 33 | + this.friction = { x: 0, y: 0 }; |
| 34 | + this.maxVel = { x: 100, y: 100 }; |
| 35 | + this.gravityFactor = 1; |
| 36 | + this.standing = false; |
| 37 | + this.bounciness = 0; |
| 38 | + this.minBounceVelocity = 40; |
| 39 | + |
| 40 | + this.type = TYPE.NONE; |
| 41 | + this.checkAgainst = TYPE.NONE; |
| 42 | + this.collides = COLLIDES.NEVER; |
| 43 | + |
| 44 | + // min 44 deg, max 136 deg |
| 45 | + this.slopeStanding = { min: 0.767944870877505, max: 2.3736477827122884 }; |
| 46 | + }, |
| 47 | + |
| 48 | + update: function (delta) |
| 49 | + { |
| 50 | + this.last.x = this.pos.x; |
| 51 | + this.last.y = this.pos.y; |
| 52 | + |
| 53 | + this.vel.y += this.world.gravity * delta * this.gravityFactor; |
| 54 | + |
| 55 | + UpdateVelocity(this, delta); |
| 56 | + |
| 57 | + var mx = this.vel.x * delta; |
| 58 | + var my = this.vel.y * delta; |
| 59 | + |
| 60 | + var res = Trace(this.pos.x, this.pos.y, mx, my, this.size.x, this.size.y); |
| 61 | + |
| 62 | + UpdateMotion(this, res); |
| 63 | + }, |
| 64 | + |
| 65 | + skipHash: function () |
| 66 | + { |
| 67 | + return (!this.enabled || (this.type === 0 && this.checkAgainst === 0 && this.collides === 0)); |
| 68 | + }, |
| 69 | + |
| 70 | + touches: function (other) |
| 71 | + { |
| 72 | + return !( |
| 73 | + this.pos.x >= other.pos.x + other.size.x || |
| 74 | + this.pos.x + this.size.x <= other.pos.x || |
| 75 | + this.pos.y >= other.pos.y + other.size.y || |
| 76 | + this.pos.y + this.size.y <= other.pos.y |
| 77 | + ); |
| 78 | + }, |
| 79 | + |
| 80 | + setVelocityX: function (x) |
| 81 | + { |
| 82 | + this.vel.x = x; |
| 83 | + |
| 84 | + return this; |
| 85 | + }, |
| 86 | + |
| 87 | + setVelocityY: function (y) |
| 88 | + { |
| 89 | + this.vel.y = y; |
| 90 | + |
| 91 | + return this; |
| 92 | + }, |
| 93 | + |
| 94 | + setVelocity: function (x, y) |
| 95 | + { |
| 96 | + this.vel.x = x; |
| 97 | + this.vel.y = y; |
| 98 | + |
| 99 | + return this; |
| 100 | + }, |
| 101 | + |
| 102 | + setMaxVelocity: function (x, y) |
| 103 | + { |
| 104 | + if (y === undefined) { y = x; } |
| 105 | + |
| 106 | + this.maxVel.x = x; |
| 107 | + this.maxVel.y = y; |
| 108 | + |
| 109 | + return this; |
| 110 | + }, |
| 111 | + |
| 112 | + setTypeNone: function () |
| 113 | + { |
| 114 | + this.type = TYPE.NONE; |
| 115 | + |
| 116 | + return this; |
| 117 | + }, |
| 118 | + |
| 119 | + setTypeA: function () |
| 120 | + { |
| 121 | + this.type = TYPE.A; |
| 122 | + |
| 123 | + return this; |
| 124 | + }, |
| 125 | + |
| 126 | + setTypeB: function () |
| 127 | + { |
| 128 | + this.type = TYPE.B; |
| 129 | + |
| 130 | + return this; |
| 131 | + }, |
| 132 | + |
| 133 | + setCheckAgainstNone: function () |
| 134 | + { |
| 135 | + this.checkAgainst = TYPE.NONE; |
| 136 | + |
| 137 | + return this; |
| 138 | + }, |
| 139 | + |
| 140 | + setCheckAgainstA: function () |
| 141 | + { |
| 142 | + this.checkAgainst = TYPE.A; |
| 143 | + |
| 144 | + return this; |
| 145 | + }, |
| 146 | + |
| 147 | + setCheckAgainstB: function () |
| 148 | + { |
| 149 | + this.checkAgainst = TYPE.B; |
| 150 | + |
| 151 | + return this; |
| 152 | + }, |
| 153 | + |
| 154 | + setCollidesNever: function () |
| 155 | + { |
| 156 | + this.collides = COLLIDES.NEVER; |
| 157 | + |
| 158 | + return this; |
| 159 | + }, |
| 160 | + |
| 161 | + setLite: function () |
| 162 | + { |
| 163 | + this.collides = COLLIDES.LITE; |
| 164 | + |
| 165 | + return this; |
| 166 | + }, |
| 167 | + |
| 168 | + setPassive: function () |
| 169 | + { |
| 170 | + this.collides = COLLIDES.PASSIVE; |
| 171 | + |
| 172 | + return this; |
| 173 | + }, |
| 174 | + |
| 175 | + setActive: function () |
| 176 | + { |
| 177 | + this.collides = COLLIDES.ACTIVE; |
| 178 | + |
| 179 | + return this; |
| 180 | + }, |
| 181 | + |
| 182 | + setFixed: function () |
| 183 | + { |
| 184 | + this.collides = COLLIDES.FIXED; |
| 185 | + |
| 186 | + return this; |
| 187 | + }, |
| 188 | + |
| 189 | + check: function( other ) {}, |
| 190 | + |
| 191 | + collideWith: function( other, axis ) {} |
| 192 | + |
| 193 | +}); |
| 194 | + |
| 195 | +module.exports = Body; |
0 commit comments