0
\$\begingroup\$

I have asked How camera works in 2d platformer in level transitions about camera transitions, in the process I am stuck somewhere else.

I have a targetRoom that is 32 by 32 tiles:

// [x, y, w, h]
let mb = [0, 0, 32, 32];

// bounds
let targetRoom = {
  minX: mb[0] * bs.pxTileSize,
  maxX: (mb[0] + mb[2]) * bs.pxTileSize,
  minY: mb[1] * bs.pxTileSize,
  maxY: (mb[1] + mb[3]) * bs.pxTileSize
};

I have a player object:

let p = {
    x: 0,
    y: 0,
  onEdgeX: (p, bx) => {
    p.x = bx;
  },
};

I allow the player to move within these bounds:

this.checkEdgeX = (p) => {
  let { minX, maxX } = targetRoom;
  let cbox = absCBox(p);

  if (cbox[0] < minX) {
    p.onEdgeX(p, minX - p.cbox[0]);
  }
  if (cbox[0] + cbox[2] > maxX) {
    p.onEdgeX(p, maxX - p.cbox[0] - p.cbox[2]);
  }
};

p.cbox is players local collision box [x,y,w,h]. eg: [-6, -6, 12, 12]; absCbox returns the collision box in world coordinates:

const absCbox = p => {
  let cbox = p.cbox;
  return [
    p.x + cbox[0],
    p.y + cbox[1],
    cbox[2],
    cbox[3]];
};

In checkEdgeX when the collision box exceeds the limit, p.onEdgeX is called which puts the player inside the bounds again.

The problem is, when I want player to go beyond the edge to the other level.

How do I check if the player is allowed to pass beyond the edge to the next level? When player goes to the next level, how do I make sure it is within the bounds again? Because player might go just a little bit to stand in between two levels, never meeting inside the bounds of either level.

I want the transition to be in the image in my linked question above.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.