@@ -47,7 +47,7 @@ export default class Level { | |||
if (!bounds) | |||
return; | |||
ent.t.collider.collideStructure(bounds); | |||
ent.t.collider.collideStructure(s, bounds); | |||
}); | |||
}); | |||
@@ -20,6 +20,11 @@ export default class Rect { | |||
(this.left <= other.right && this.right >= other.left) && | |||
(this.top <= other.bottom && this.bottom >= other.top)); | |||
} | |||
bottomIntersects(other) { | |||
return ( | |||
(this.left <= other.right && this.right >= other.left) && | |||
(this.bottom <= other.bottom && this.bottom >= other.top)); | |||
} | |||
contains(other) { | |||
return ( |
@@ -32,7 +32,7 @@ export default class Texture { | |||
if (e instanceof Array) { | |||
this.fillCanvas(e); | |||
} else { | |||
assets.tiles.drawTile(this.ctx, e.tile, e.x, e.y); | |||
assets.tiles.drawTile(this.ctx, e.name, e.x, e.y); | |||
} | |||
}); | |||
} |
@@ -0,0 +1,7 @@ | |||
export default class Tile { | |||
constructor(x, y, name) { | |||
this.x = x; | |||
this.y = y; | |||
this.name = name; | |||
} | |||
} |
@@ -1,13 +1,15 @@ | |||
import Tile from "./Tile.js" | |||
export default { | |||
fromLine: function(width, tile) { | |||
fromLine: function(width, name) { | |||
if (width <= 1) { | |||
return [ { x: 0, y: 0, tile: tile+"-lr" }]; | |||
return [ new Tile(0, 0, name+"-lr") ]; | |||
} else { | |||
return [ | |||
{ x: 0, y: 0, tile: tile+"-l", }, | |||
new Tile(0, 0, name+"-l"), | |||
Array.from({ length: width - 2 }, (_, i) => | |||
({ x: i + 1, y: 0, tile: tile })), | |||
{ x: width - 1, y: 0, tile: tile+"-r" }, | |||
new Tile(i + 1, 0, name)), | |||
new Tile(width - 1, 0, name+"-r"), | |||
]; | |||
} | |||
}, |
@@ -7,16 +7,19 @@ export default class Collider extends Trait { | |||
this.collides = false; | |||
this.cEntity = null; | |||
this.cStructure = null; | |||
this.cBounds = null; | |||
} | |||
collideEntity(e) { | |||
this.cEntity = e; | |||
this.collides = true; | |||
this.cEntity = e; | |||
this.cBounds = e.bounds; | |||
} | |||
collideStructure(s) { | |||
this.cStructure = s; | |||
collideStructure(s, b) { | |||
this.collides = true; | |||
this.cStructure = s; | |||
this.cBounds = b; | |||
} | |||
postUpdate() { |
@@ -27,19 +27,20 @@ export default class TPhysics extends Trait { | |||
if ( | |||
this.entity.has("collider") && | |||
collider.collides && | |||
this.velocity.y >= 0) { | |||
this.velocity.y >= 0 && | |||
this.entity.bounds.bottomIntersects(collider.cBounds)) { | |||
// Structures are static; just teleport us to the top of them | |||
if (collider.cStructure) { | |||
this.velocity.y = 0; | |||
this.entity.bounds.bottom = collider.cStructure.top; | |||
this.entity.bounds.bottom = collider.cBounds.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.velocity.y = 0; | |||
this.entity.bounds.bottom = collider.cEntity.bounds.top; | |||
this.entity.bounds.bottom = collider.cBounds.top; | |||
this.onGround = true; | |||
if (collider.cEntity.has("physics")) { |