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

ctx.drawImage(this.can, x, y, this.width, this.height); 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



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


export default class Platform extends Entity { export default class Platform extends Entity {
constructor(level) { constructor(level) {
this.bounds.size.set(5, 1); this.bounds.size.set(5, 1);
this.addTrait(new TCollider(this)); this.addTrait(new TCollider(this));
this.addTrait(new TPlatform(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) { draw(ctx) {

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

let speed = phys.onGround ? this.speed : this.speedAir; let speed = phys.onGround ? this.speed : this.speedAir;


if (this.pressed.left) if (this.pressed.left)
phys.relVelocity.x -= speed * dt;
phys.velocity.x -= speed * dt;
if (this.pressed.right) if (this.pressed.right)
phys.relVelocity.x += speed * dt;
phys.velocity.x += speed * dt;
if (phys.onGround && this.pressed.jump) { 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.jumpTime = this.jumpTimeMax;
this.jumping = true; this.jumping = true;
phys.onGround = false; phys.onGround = false;
this.jumping = false; this.jumping = false;


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

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

this.groundFriction = 6; this.groundFriction = 6;
this.airFriction = 2; this.airFriction = 2;


this.absVelocity = new Vec2();
this.velocity = new Vec2(); this.velocity = new Vec2();
this.relVelocity = new Vec2();


this.onGround = false; this.onGround = false;
this.prevRelativeTo = null; this.prevRelativeTo = null;
if ( if (
this.entity.has("collider") && this.entity.has("collider") &&
collider.collides && collider.collides &&
this.relVelocity.y >= 0) {
this.velocity.y >= 0) {


// Structures are static; just teleport us to the top of them // Structures are static; just teleport us to the top of them
if (collider.cStructure) { if (collider.cStructure) {
this.relVelocity.y = 0;
this.velocity.y = 0;
this.entity.bounds.bottom = collider.cStructure.top; this.entity.bounds.bottom = collider.cStructure.top;
this.onGround = true; this.onGround = true;


// If we're colliding with an entity, and that entity is // If we're colliding with an entity, and that entity is
// a platform, teleport us to the top of them. // a platform, teleport us to the top of them.
} else if (collider.cEntity.has("platform")) { } else if (collider.cEntity.has("platform")) {
this.relVelocity.y = 0;
this.velocity.y = 0;
this.entity.bounds.bottom = collider.cEntity.bounds.top; this.entity.bounds.bottom = collider.cEntity.bounds.top;
this.onGround = true; this.onGround = true;


} }


if (!this.onGround) 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) { 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) { } 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 // Apply friction
var fric = this.onGround ? this.groundFriction : this.airFriction; var fric = this.onGround ? this.groundFriction : this.airFriction;
var xRatio = 1 / (1 + (dt * fric)); var xRatio = 1 / (1 + (dt * fric));
this.relVelocity.x *= xRatio;
this.velocity.x *= xRatio;
}

postUpdate(dt) {


// Update velocity
// Update absVelocity
if (this.relativeTo) { 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 { } 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