Browse Source

fixed stuff

master
mortie 6 years ago
parent
commit
f11bd60152
4 changed files with 35 additions and 28 deletions
  1. 0
    3
      js/Texture.js
  2. 7
    0
      js/entities/Platform.js
  3. 5
    4
      js/traits/TKeyboardController.js
  4. 23
    21
      js/traits/TPhysics.js

+ 0
- 3
js/Texture.js View File

@@ -48,6 +48,3 @@ export default class Texture {
ctx.drawImage(this.can, x, y, this.width, this.height);
}
}

Texture.fromLine = function(l, m, r, lr) {
}

+ 7
- 0
js/entities/Platform.js View File

@@ -2,6 +2,7 @@ import Entity from "../Entity.js";

import TPlatform from "../traits/TPlatform.js";
import TCollider from "../traits/TCollider.js";
import TPhysics from "../traits/TPhysics.js";

export default class Platform extends Entity {
constructor(level) {
@@ -10,6 +11,12 @@ export default class Platform extends Entity {
this.bounds.size.set(5, 1);
this.addTrait(new TCollider(this));
this.addTrait(new TPlatform(this));
this.addTrait(new TPhysics(this));
}

update(dt) {
let phys = this.t.physics;
phys.velocity.y -= phys.gravity * dt;
}

draw(ctx) {

+ 5
- 4
js/traits/TKeyboardController.js View File

@@ -41,11 +41,12 @@ export default class TKeyboardController extends Trait {
let speed = phys.onGround ? this.speed : this.speedAir;

if (this.pressed.left)
phys.relVelocity.x -= speed * dt;
phys.velocity.x -= speed * dt;
if (this.pressed.right)
phys.relVelocity.x += speed * dt;
phys.velocity.x += speed * dt;
if (phys.onGround && this.pressed.jump) {
phys.relVelocity.y -= this.jump;
phys.velocity.y = -this.jump;
console.log("jumping to", phys.velocity.y);
this.jumpTime = this.jumpTimeMax;
this.jumping = true;
phys.onGround = false;
@@ -55,7 +56,7 @@ export default class TKeyboardController extends Trait {
this.jumping = false;

if (this.jumping) {
phys.relVelocity.y -= this.updrift * this.jumpTime * dt;
phys.velocity.y -= this.updrift * this.jumpTime * dt;
this.jumpTime -= dt;
}
}

+ 23
- 21
js/traits/TPhysics.js View File

@@ -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;
}
}

Loading…
Cancel
Save