20
loading...
This website collects cookies to deliver better user experience
const X = { walk: false, action: false,};
const O = { walk: true, action: false,};
const AO = { walk: true, action: true,};
const AX = { walk: false, action: true,};
const BEDROOM_MAP = [ //each elem in the nested array equals a tile on the x-axis
[X, X, X, X, X, X, X, X, X, X, X, X], // y = 0
[X, X, X, X, X, X, X, X, X, X, X, X], // y = 1
[X, X, X, X, X, X, X, X, X, X, X, X], // y= 2
[X, X, X, X, X, X, X, X, X, X, X, X], // y = 3
[X, X, AX, AX, X, AO, AO, X, AO, AO, X, X], // y = 4
[X, X, AO, AO, O, O, O, O, AO, AO, X, X], // y = 5
[X, X, O, O, O, O, O, O, O, O, X, X], // y = 6
[X, X, O, O, O, O, O, O, O, O, O, O], // y = 7
[X, X, X, O, O, O, O, O, O, O, O, O], // y = 8
[X, X, X, O, O, O, O, O, O, O, X, X], // y = 9
[X, X, X, X, X, X, X, X, X, X, X, X], // y = 10
[X, X, X, X, X, X, X, X, X, X, X, X], // y = 11
]
function getNextTile(direction, position) {
let newPos;
let X;
let Y;
switch (direction) {
case 'up':
newPos = position.top - 2
X = ((position.left + 192) - (position.left % 32)) / 32
Y = (newPos - (newPos % 32)) / 32
return MAP_TABLE[Y][X][key];
position
by 32 (the pixel size of my grid), and passing them as the indices to MAP_TABLE
, which held the 2D arrays for each map area, we are able to return the boolean for 'walk' held on the next tile. The return of this boolean value determines if the reducer that moves the character runs or not, thus restricting her to my map.X
coordinate calculation: Like an old Pokémon game, CodeCamp Quest uses a viewport, allowing the entire map to be rendered behind the viewport container. When the player walks up or down, the character sprite moves, but when walking left or right, the sprite is stationary and the map image moves instead. 192px renders the character in the center of the viewport container on the x-axis. The getNextTile
function needs to account for that fixed position.