diff --git a/src/components/physics/body.ts b/src/components/physics/body.ts index 64737018..6c3a2dc9 100644 --- a/src/components/physics/body.ts +++ b/src/components/physics/body.ts @@ -193,17 +193,17 @@ export function body(opt: BodyCompOpt = {}): BodyComp { }, isFalling(): boolean { - return this.vel.dot(internal.game.gravity) > 0; + return this.vel.dot(k.getGravityDirection()) > 0; }, isJumping(): boolean { - return this.vel.dot(internal.game.gravity) < 0; + return this.vel.dot(k.getGravityDirection()) < 0; }, jump(force: number) { curPlatform = null; lastPlatformPos = null; - this.vel = internal.game.gravity.unit().scale( + this.vel = k.getGravityDirection().scale( -force || -this.jumpForce, ); }, diff --git a/src/kaboom.ts b/src/kaboom.ts index 99b7014a..662ca7dd 100644 --- a/src/kaboom.ts +++ b/src/kaboom.ts @@ -672,7 +672,7 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { root: make([]), // misc - gravity: vec2(0, 1), + gravity: null, scenes: {}, currentScene: null, @@ -3677,19 +3677,25 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { } function setGravity(g: number) { - game.gravity = game.gravity.unit().scale(g); + // If g > 0 use either the current direction or use (0, 1) + // Else null + game.gravity = g ? (game.gravity || vec2(0, 1)).unit().scale(g) : null; } function getGravity() { - return game.gravity.len(); + // If gravity > 0 return magnitude + // Else 0 + return game.gravity ? game.gravity.len() : 0; } function setGravityDirection(d: Vec2) { - game.gravity = d.unit().scale(game.gravity.len()); + // If gravity > 0 keep magnitude, otherwise use 1 + game.gravity = d.unit().scale(game.gravity ? game.gravity.len() : 1); } function getGravityDirection() { - return game.gravity.unit(); + // If gravity > 0 return magnitude, otherwise return (0, 1) + return game.gravity ? game.gravity.unit() : vec2(0, 1); } function setBackground(...args) { @@ -4564,16 +4570,16 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => { return !this.displacement.isZero(); } isLeft() { - return this.displacement.cross(game.gravity) > 0; + return this.displacement.cross(game.gravity || vec2(0, 1)) > 0; } isRight() { - return this.displacement.cross(game.gravity) < 0; + return this.displacement.cross(game.gravity || vec2(0, 1)) < 0; } isTop() { - return this.displacement.dot(game.gravity) > 0; + return this.displacement.dot(game.gravity || vec2(0, 1)) > 0; } isBottom() { - return this.displacement.dot(game.gravity) < 0; + return this.displacement.dot(game.gravity || vec2(0, 1)) < 0; } preventResolution() { this.resolved = true;