Algorithm of Habbo walls

Asked

Viewed 82 times

0

I’m developing a Javascript game and its environment is very similar to that of Habbo: A floor and walls only at the bottom of the floor.

Quarto no Habbo

The game map is dynamic and represented by a matrix of numbers where 0 indicates that there will be no floor and the others refer to floor height.

I noticed that the walls are rendered starting at the lower left corner and ending at the upper right corner.

I have this code so far, his problem is that in some cases it is not rendered the block, as the block (2,2) that should be a vertical or horizontal block.

const $floor = document.getElementById("floor");
const $wall = document.getElementById("wall");

const WH = "-"; // Parede Horizontal
const WV = "|"; // Parede Vertical
const WC = "+"; // Parece Canto
const WN = "."; // Piso (Sem parede)

// Mapa de piso
const floor = [
  [0, 0, 1, 1, 1, 0, 1, 1],
  [0, 0, 1, 1, 1, 1, 1, 1],
  [1, 1, 1, 1, 1, 1, 1, 1],
  [1, 1, 1, 1, 1, 1, 1, 1],
  [1, 1, 1, 1, 1, 1, 1, 1],
  [0, 1, 1, 1, 1, 1, 1, 1],
  [1, 1, 1, 1, 1, 1, 1, 1],
  [1, 1, 1, 1, 1, 1, 1, 1]
];

// Mapa de paredes
const walls = new Array(floor.length)
  .fill(null)
  .map((_, y) =>
    new Array(floor[0].length)
      .fill(null)
      .map((_, x) => (floor[y][x] === 0 ? " " : WN))
  );

const floorRows = floor.length;
const floorCols = floor[0].length;

let maxVX = Infinity;

// Paredes Horizontais
wallsH: for (let y = 0; y < floorRows; y++) {
  const row = floor[y];

  // Percorre às colunas até o primeiro bloco vertical
  for (let x = 0; x <= floorCols; x++) {
    // Se for 0, não inserir paredes
    if (!row[x]) continue;

    // Não enfileirar paredes horizontais verticalmente
    for (let wy = y; wy >= 0; wy--) {
      const block = walls[wy][x];
      if (block === WH || block === WC) continue wallsH;
    }

    // Renderiza
    walls[y][x] = WH;
  }
}

// Paredes Verticais
for (let y = 0; y < floorRows; y++) {
  const row = floor[y];

  for (let x = 0; x < floorCols; x++) {
    // Não testar blocos com 0
    if (!row[x]) continue;

    // Não enfileirar paredes verticais
    // horizontalmente
    if (x > maxVX) break;

    // Define a posição X máxima para o bloco atual
    maxVX = x;

    // Bloco com canto se já existir
    // um bloco horizontal nesta posição
    let conner = walls[y][x] === WH;

    // Renderiza
    walls[y][x] = conner ? WC : WV;
  }
}

$floor.textContent = floor.map(r => r.join(" ")).join("\n");
$wall.textContent = walls.map(r => r.join(" ")).join("\n");
<div id="app">
  <pre id="floor"></pre>
  <pre id="wall"></pre>
</div>

  • More, more..., what is the doubt after all?

  • I need to create this algorithm, I don’t know where to start

  • It is there, it is outside the scope... I suggest to take a study before and have a basis of what you intend to do, and when specific doubts appear you put, ok...

  • But what will I study? I need to convert one matrix into another that represents only the walls

  • You can join the chat, and talk to the staff more informally, to see if anyone has any ideas that might shed some light on you, for your start, good luck...

  • 1

    Studying what I say is in the sense of having the information necessary to organize the steps of an algorithm that achieves its goal... Probably if I use Socratic methodology to align their ideas, we will find that surely they are already there... Then, organize the information you already have in an algorithm and if you have any doubts, you edit your question, which will surely appear help. You know what I mean?

  • Your question seems to have some problems and your experience here in Stack Overflow may not be the best because of this. We want you to do well here and get what you want, but for that we need you to do your part. Here are some guidelines that will help you: Stack Overflow Survival Guide in English.

  • Just for the record, I’m pretty sure this system is called "isometric," if you look for something like this you’ll find examples: "create simple Isometric game javascript"

  • Yes the map is isometric, but I found nothing similar in the research I did

  • In the first search I found: https://github.com/nick-aschenbach/simple-isometric-tile-engine, I did not test but it is up to you to try of course, and study the code .... and I also found this http://jsiso.com/tutorials.html, which I think is great for your case

  • The rendering engine I have, what I need is an algorithm to know where the walls will be rendered

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.