|
|
@@ -9,8 +9,8 @@ export default class TPhysics extends Trait { |
|
|
|
this.groundFriction = 6; |
|
|
|
this.airFriction = 2; |
|
|
|
|
|
|
|
this.absVelocity = new Vec2(); |
|
|
|
this.velocity = new Vec2(); |
|
|
|
this.relVelocity = new Vec2(); |
|
|
|
|
|
|
|
this.onGround = false; |
|
|
|
this.prevRelativeTo = null; |
|
|
@@ -27,18 +27,18 @@ export default class TPhysics extends Trait { |
|
|
|
if ( |
|
|
|
this.entity.has("collider") && |
|
|
|
collider.collides && |
|
|
|
this.relVelocity.y >= 0) { |
|
|
|
this.velocity.y >= 0) { |
|
|
|
|
|
|
|
// Structures are static; just teleport us to the top of them |
|
|
|
if (collider.cStructure) { |
|
|
|
this.relVelocity.y = 0; |
|
|
|
this.velocity.y = 0; |
|
|
|
this.entity.bounds.bottom = collider.cStructure.top; |
|
|
|
this.onGround = true; |
|
|
|
|
|
|
|
// If we're colliding with an entity, and that entity is |
|
|
|
// a platform, teleport us to the top of them. |
|
|
|
} else if (collider.cEntity.has("platform")) { |
|
|
|
this.relVelocity.y = 0; |
|
|
|
this.velocity.y = 0; |
|
|
|
this.entity.bounds.bottom = collider.cEntity.bounds.top; |
|
|
|
this.onGround = true; |
|
|
|
|
|
|
@@ -50,36 +50,38 @@ export default class TPhysics extends Trait { |
|
|
|
} |
|
|
|
|
|
|
|
if (!this.onGround) |
|
|
|
this.relVelocity.y += this.gravity * dt; |
|
|
|
this.velocity.y += this.gravity * dt; |
|
|
|
|
|
|
|
// If we just started riding something, adjust relative velocity |
|
|
|
// If we just started riding something, adjust relative absVelocity |
|
|
|
if (!this.prevRelativeTo && this.relativeTo) { |
|
|
|
this.relVelocity.x = this.velocity.x - this.relativeTo.velocity.x; |
|
|
|
this.relVelocity.y = this.velocity.y - this.relativeTo.velocity.y; |
|
|
|
this.velocity.x = this.absVelocity.x - this.relativeTo.absVelocity.x; |
|
|
|
this.velocity.y = this.absVelocity.y - this.relativeTo.absVelocity.y; |
|
|
|
|
|
|
|
// If we just stopped riding something, adjust relative velocity |
|
|
|
// If we just stopped riding something, adjust relative absVelocity |
|
|
|
} else if (this.prevRelativeTo && !this.relativeTo) { |
|
|
|
this.relVelocity.x = this.velocity.x + this.relativeTo.velocity.x; |
|
|
|
this.relVelocity.y = this.velocity.y + this.relativeTo.velocity.y; |
|
|
|
this.velocity.x += this.prevRelativeTo.absVelocity.x; |
|
|
|
this.velocity.y += this.prevRelativeTo.absVelocity.y; |
|
|
|
} |
|
|
|
|
|
|
|
// Apply friction |
|
|
|
var fric = this.onGround ? this.groundFriction : this.airFriction; |
|
|
|
var xRatio = 1 / (1 + (dt * fric)); |
|
|
|
this.relVelocity.x *= xRatio; |
|
|
|
this.velocity.x *= xRatio; |
|
|
|
} |
|
|
|
|
|
|
|
postUpdate(dt) { |
|
|
|
|
|
|
|
// Update velocity |
|
|
|
// Update absVelocity |
|
|
|
if (this.relativeTo) { |
|
|
|
this.velocity.x = this.relVelocity.x + this.relativeTo.velocity.x; |
|
|
|
this.velocity.y = this.relVelocity.y + this.relativeTo.velocity.y; |
|
|
|
this.absVelocity.x = this.velocity.x + this.relativeTo.absVelocity.x; |
|
|
|
this.absVelocity.y = this.velocity.y + this.relativeTo.absVelocity.y; |
|
|
|
} else { |
|
|
|
this.velocity.x = this.relVelocity.x; |
|
|
|
this.velocity.y = this.relVelocity.y; |
|
|
|
this.absVelocity.x = this.velocity.x; |
|
|
|
this.absVelocity.y = this.velocity.y; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
postUpdate(dt) { |
|
|
|
this.entity.bounds.pos.x += this.velocity.x * dt; |
|
|
|
this.entity.bounds.pos.y += this.velocity.y * dt; |
|
|
|
// Move |
|
|
|
this.entity.bounds.pos.x += this.absVelocity.x * dt; |
|
|
|
this.entity.bounds.pos.y += this.absVelocity.y * dt; |
|
|
|
} |
|
|
|
} |