Browse Source

better collision detection and other stuff

master
mortie 6 years ago
parent
commit
a09bdd5f1b
4 changed files with 29 additions and 8 deletions
  1. 13
    6
      js/Rect.js
  2. 5
    0
      js/Shaker.js
  3. 8
    2
      js/traits/TKeyboardController.js
  4. 3
    0
      js/traits/TPhysics.js

+ 13
- 6
js/Rect.js View File

} }


intersectSide(other) { intersectSide(other) {
if (this.midY < other.top)
return "top";
else if (this.midY > other.bottom)
return "bottom";
else if (this.midX < other.left)
let mx = this.midX;
let my = this.midY;

let diffleft = mx - other.pos.x;
let diffright = mx - (other.pos.x + other.size.x);
let difftop = my - (other.pos.y + other.size.y);

if (diffleft <= 0)
return "left"; return "left";
else
else if (diffright >= 0)
return "right"; return "right";
else if (difftop >= 0)
return "bottom";
else
return "top";
} }


contains(other) { contains(other) {

+ 5
- 0
js/Shaker.js View File

update(dt) { update(dt) {
if (this.shakeX > 0.1) if (this.shakeX > 0.1)
this.vec.x = (Math.random() - 0.5) * this.shakeX * dt; this.vec.x = (Math.random() - 0.5) * this.shakeX * dt;
else
this.vec.x = 0;

if (this.shakeY > 0.1) if (this.shakeY > 0.1)
this.vec.y = (Math.random() - 0.5) * this.shakeY * dt; this.vec.y = (Math.random() - 0.5) * this.shakeY * dt;
else
this.vec.y = 0;


var xRatio = 1 / (1 + (dt * this.decay)); var xRatio = 1 / (1 + (dt * this.decay));
this.shakeX *= xRatio; this.shakeX *= xRatio;

+ 8
- 2
js/traits/TKeyboardController.js View File

this.speedAir = 20; this.speedAir = 20;


this.jump = 8; this.jump = 8;
this.jumpTimeMax = 0.4;
this.updrift = 87;
this.jumpTimeMax = 0.45;
this.updrift = 100;
this.jumpLeeway = 0.1; this.jumpLeeway = 0.1;


this.map = { this.map = {
KeyA: 'left', KeyA: 'left',
KeyD: 'right', KeyD: 'right',
KeyW: 'up',
KeyS: 'down',
Space: "jump", Space: "jump",
}; };


phys.velocity.x -= speed * dt; phys.velocity.x -= speed * dt;
if (this.pressed.right) if (this.pressed.right)
phys.velocity.x += speed * dt; phys.velocity.x += speed * dt;
if (this.pressed.down)
phys.velocity.y += speed * dt;
if (this.pressed.up)
phys.velocity.y -= speed * dt;


let canJump = let canJump =
!this.jumped && !this.jumped &&

+ 3
- 0
js/traits/TPhysics.js View File

} else if (side === "right" && this.velocity.x < velocity.x) { } else if (side === "right" && this.velocity.x < velocity.x) {
this.velocity.x = velocity.x; this.velocity.x = velocity.x;
this.entity.bounds.left = bounds.right; this.entity.bounds.left = bounds.right;
} else if (side === "bottom" && this.velocity.y < velocity.y) {
this.velocity.y = velocity.y;
this.entity.bounds.top = bounds.bottom;
} }
} }



Loading…
Cancel
Save