Browse Source

better collision detection and other stuff

master
mortie 3 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

@@ -22,14 +22,21 @@ export default class Rect {
}

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";
else
else if (diffright >= 0)
return "right";
else if (difftop >= 0)
return "bottom";
else
return "top";
}

contains(other) {

+ 5
- 0
js/Shaker.js View File

@@ -16,8 +16,13 @@ export default class Shaker {
update(dt) {
if (this.shakeX > 0.1)
this.vec.x = (Math.random() - 0.5) * this.shakeX * dt;
else
this.vec.x = 0;

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

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

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

@@ -8,13 +8,15 @@ export default class TKeyboardController extends Trait {
this.speedAir = 20;

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

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

@@ -47,6 +49,10 @@ export default class TKeyboardController extends Trait {
phys.velocity.x -= speed * dt;
if (this.pressed.right)
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 =
!this.jumped &&

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

@@ -34,6 +34,9 @@ export default class TPhysics extends Trait {
} else if (side === "right" && this.velocity.x < velocity.x) {
this.velocity.x = velocity.x;
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