Skip to content

Commit

Permalink
Sync with published result of episode 18.
Browse files Browse the repository at this point in the history
  • Loading branch information
pomle committed Mar 13, 2020
1 parent d47a899 commit 10158e1
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 27 deletions.
36 changes: 21 additions & 15 deletions public/js/TileCollider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
});
}
}
Expand All @@ -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);
}
}
3 changes: 2 additions & 1 deletion public/js/loaders/level.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down Expand Up @@ -53,6 +53,7 @@ function createGrid(tiles, patterns) {
return grid;
}


function* expandSpan(xStart, xLen, yStart, yLen) {
const xEnd = xStart + xLen;
const yEnd = yStart + yLen;
Expand Down
17 changes: 9 additions & 8 deletions public/js/tiles/brick.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions public/js/tiles/ground.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion public/js/traits/Physics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 10158e1

Please sign in to comment.