Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
expose velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
slmjkdbtl committed Nov 12, 2023
1 parent d4eda8f commit a396ada
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
7 changes: 7 additions & 0 deletions examples/gravity.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ player.onGround(() => {
debug.log("ouch")
})

// Accelerate falling when player holding down arrow key
onKeyDown("down", () => {
if (player.vel.y > 0 && !player.isGrounded()) {
player.vel.y *= 1.1
}
})

// Add a platform to hold the player
add([
rect(width(), 48),
Expand Down
22 changes: 11 additions & 11 deletions src/kaboom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4180,7 +4180,6 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
// TODO: land on wall
function body(opt: BodyCompOpt = {}): BodyComp {

const vel = vec2(0)
let curPlatform: GameObj<PosComp | AreaComp | BodyComp> | null = null
let lastPlatformPos = null
let wantFall = false
Expand All @@ -4189,6 +4188,7 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {

id: "body",
require: [ "pos", "area" ],
vel: new Vec2(0),
jumpForce: opt.jumpForce ?? DEF_JUMP_FORCE,
gravityScale: opt.gravityScale ?? 1,
isStatic: opt.isStatic ?? false,
Expand Down Expand Up @@ -4247,7 +4247,7 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
this.onPhysicsResolve((col) => {
if (game.gravity) {
if (col.isBottom() && this.isFalling()) {
vel.y = 0
this.vel.y = 0
curPlatform = col.target as GameObj<PosComp | BodyComp | AreaComp>
lastPlatformPos = col.target.pos
if (wantFall) {
Expand All @@ -4256,7 +4256,7 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
this.trigger("ground", curPlatform)
}
} else if (col.isTop() && this.isJumping()) {
vel.y = 0
this.vel.y = 0
this.trigger("headbutt", col.target)
}
}
Expand Down Expand Up @@ -4301,13 +4301,13 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
}
}

const prevVelY = vel.y
vel.y += game.gravity * this.gravityScale * dt()
vel.y = Math.min(vel.y, opt.maxVelocity ?? MAX_VEL)
if (prevVelY < 0 && vel.y >= 0) {
const prevVelY = this.vel.y
this.vel.y += game.gravity * this.gravityScale * dt()
this.vel.y = Math.min(this.vel.y, opt.maxVelocity ?? MAX_VEL)
if (prevVelY < 0 && this.vel.y >= 0) {
this.trigger("fall")
}
this.move(vel)
this.move(this.vel)

},

Expand All @@ -4328,17 +4328,17 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
},

isFalling(): boolean {
return vel.y > 0
return this.vel.y > 0
},

isJumping(): boolean {
return vel.y < 0
return this.vel.y < 0
},

jump(force: number) {
curPlatform = null
lastPlatformPos = null
vel.y = -force || -this.jumpForce
this.vel.y = -force || -this.jumpForce
},

onGround(this: GameObj, action: () => void): EventController {
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4796,10 +4796,16 @@ export interface ShaderComp extends Comp {
}

export interface BodyComp extends Comp {
/**
* Object current velocity.
*
* @since v3000.2
*/
vel: Vec2,
/**
* If object is static, won't move, and all non static objects won't move past it.
*/
isStatic?: boolean,
isStatic: boolean,
/**
* Initial speed in pixels per second for jump().
*/
Expand Down

0 comments on commit a396ada

Please sign in to comment.