I'm working on a tile-based engine. It was working fine with code like this:
var room1 = [ [1,1,1,1,1],
[1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,0] ];
var room2 = [ [1,1,1,1,1],
[0,0,0,0,1],
[0,0,0,0,1],
[0,0,0,0,1],
[0,0,0,0,1] ];
var room3 = [ [1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,0],
[1,1,1,1,1] ];
var room4 = [ [0,0,0,0,1],
[0,0,0,0,1],
[0,0,0,0,1],
[0,0,0,0,1],
[1,1,1,1,1] ];
var rooms = [ [ room1, room2],
[ room3, room4] ];
1 = barrier
0 = moveable area
I have it coded so that when the player reaches the edge of the current room, it checks the "rooms" array to see if there's another room to go to (for example, if a player is in room 4 and on the left edge, it checks for a room to the left in the rooms array, i.e. index - 1).
This worked well until I realized it's no good if the rooms are not the same size. Here's an example of the rooms I want to implement now:
var room1 = [ [1,1,1,1,1],
[1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,1],
[1,0,0,0,1],
[1,0,0,0,1],
[1,0,0,0,1],
[1,0,0,0,1],
[1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,0],
[1,1,1,1,1] ];
var room2 = [ [1,1,1,1,1],
[0,0,0,0,1],
[0,0,0,0,1],
[0,0,0,0,1],
[1,1,1,1,1] ];
var room3 = [ [1,1,1,1,1],
[0,0,0,0,1],
[0,0,0,0,1],
[0,0,0,0,1],
[1,1,1,1,1] ];
Room 1 is on the left, rooms 2 and 3 are on the right (two towards the top, 3 towards the bottom). But this doesn't work with the "rooms" array from above. How would you suggest I organize the rooms so that the code can recognize what room to go to based on where in the current room the character is?