diff --git a/public/js/TileCollider.js b/public/js/TileCollider.js index 6da8183d..c650d47b 100644 --- a/public/js/TileCollider.js +++ b/public/js/TileCollider.js @@ -5,15 +5,15 @@ import {ground} from './tiles/ground.js'; const handlers = { brick, ground, -}; +} export default class TileCollider { constructor() { this.resolvers = []; } - addGrid(matrix) { - this.resolvers.push(new TileResolver(matrix)); + addGrid(tileMatrix) { + this.resolvers.push(new TileResolver(tileMatrix)); } checkX(entity, gameContext, level) { @@ -26,13 +26,13 @@ export default class TileCollider { return; } - for (const tiles of this.resolvers) { - const matches = tiles.searchByRange( + for (const resolver of this.resolvers) { + const matches = resolver.searchByRange( x, x, entity.bounds.top, entity.bounds.bottom); matches.forEach(match => { - this.handle(0, match, entity, tiles, gameContext, level); + this.handle(0, entity, match, resolver, gameContext, level); }); } } @@ -47,23 +47,29 @@ export default class TileCollider { return; } - for (const tiles of this.resolvers) { - const matches = tiles.searchByRange( + for (const resolver of this.resolvers) { + const matches = resolver.searchByRange( entity.bounds.left, entity.bounds.right, y, y); matches.forEach(match => { - this.handle(1, match, entity, tiles, gameContext, level); + this.handle(1, entity, match, resolver, gameContext, level); }); } } - handle(index, match, entity, tiles, gameContext, level) { - const type = match.tile.type; - const handler = handlers[type]; - if (!handler) { - return; + handle(index, entity, match, resolver, gameContext, level) { + const tileCollisionContext = { + entity, + match, + resolver, + gameContext, + level, + }; + + const handler = handlers[match.tile.type]; + if (handler) { + handler[index](tileCollisionContext); } - handler[index](match, entity, tiles, gameContext, level); } } diff --git a/public/js/loaders/level.js b/public/js/loaders/level.js index 74c93290..fffa9044 100644 --- a/public/js/loaders/level.js +++ b/public/js/loaders/level.js @@ -8,8 +8,8 @@ function setupBackgrounds(levelSpec, level, backgroundSprites) { levelSpec.layers.forEach(layer => { const grid = createGrid(layer.tiles, levelSpec.patterns); const backgroundLayer = createBackgroundLayer(level, grid, backgroundSprites); - level.tileCollider.addGrid(grid); level.comp.layers.push(backgroundLayer); + level.tileCollider.addGrid(grid); }); } @@ -53,6 +53,7 @@ function createGrid(tiles, patterns) { return grid; } + function* expandSpan(xStart, xLen, yStart, yLen) { const xEnd = xStart + xLen; const yEnd = yStart + yLen; diff --git a/public/js/tiles/brick.js b/public/js/tiles/brick.js index ea020ee9..48714244 100644 --- a/public/js/tiles/brick.js +++ b/public/js/tiles/brick.js @@ -1,6 +1,7 @@ import {Sides} from '../Entity.js'; -function handleX(match, entity, tiles, gameContext, level) { +function handleX({entity, match}) { + if (entity.vel.x > 0) { if (entity.bounds.right > match.x1) { entity.obstruct(Sides.RIGHT, match); @@ -12,20 +13,20 @@ function handleX(match, entity, tiles, gameContext, level) { } } -function handleY(match, entity, tiles, gameContext, level) { +function handleY({entity, match, resolver, gameContext, level}) { if (entity.vel.y > 0) { if (entity.bounds.bottom > match.y1) { entity.obstruct(Sides.BOTTOM, match); } } else if (entity.vel.y < 0) { if (entity.player) { - tiles.matrix.delete(match.indexX, match.indexY); + const grid = resolver.matrix; + grid.delete(match.indexX, match.indexY); - const koopa = gameContext.entityFactory.koopa(); - koopa.pos.copy(entity.pos); - koopa.pos.y -= 64; - koopa.vel.set(50, -400); - level.entities.add(koopa); + const goomba = gameContext.entityFactory.goomba(); + goomba.vel.set(50, -400); + goomba.pos.set(entity.pos.x, match.y1); + level.entities.add(goomba); } if (entity.bounds.top < match.y2) { diff --git a/public/js/tiles/ground.js b/public/js/tiles/ground.js index 9f114040..4195f038 100644 --- a/public/js/tiles/ground.js +++ b/public/js/tiles/ground.js @@ -1,6 +1,6 @@ import {Sides} from '../Entity.js'; -function handleX(match, entity) { +function handleX({entity, match}) { if (entity.vel.x > 0) { if (entity.bounds.right > match.x1) { entity.obstruct(Sides.RIGHT, match); @@ -12,7 +12,7 @@ function handleX(match, entity) { } } -function handleY(match, entity) { +function handleY({entity, match}) { if (entity.vel.y > 0) { if (entity.bounds.bottom > match.y1) { entity.obstruct(Sides.BOTTOM, match); diff --git a/public/js/traits/Physics.js b/public/js/traits/Physics.js index 7cc97981..2b084d81 100644 --- a/public/js/traits/Physics.js +++ b/public/js/traits/Physics.js @@ -7,7 +7,6 @@ export default class Physics extends Trait { update(entity, gameContext, level) { const {deltaTime} = gameContext; - entity.pos.x += entity.vel.x * deltaTime; level.tileCollider.checkX(entity, gameContext, level);