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