Skip to content

Instantly share code, notes, and snippets.

@Novout
Last active February 13, 2021 01:28
Show Gist options
  • Select an option

  • Save Novout/20e97574df076a72aecc8fa228904102 to your computer and use it in GitHub Desktop.

Select an option

Save Novout/20e97574df076a72aecc8fa228904102 to your computer and use it in GitHub Desktop.
Chunk Generator with 2D Player Coordinates JSVanilla
export const setNodeChunk = () => {
const player = getPlayerCoordinates(); // x and y
const __GLOBAL__ = getStore(); // entity system
const chunk = __GLOBAL__.map.chunk; // radius
const container = getMapNode(); // reference player node
__GLOBAL__.player.coordinates = player;
setStore(__GLOBAL__);
const min_x = player.x - chunk;
const max_x = player.x + chunk;
const min_y = player.y - chunk;
const max_y = player.y + chunk;
for(let x = min_x - 1; x <= max_x + 1; ++x) {
for(let y = min_y - 1; y <= max_y + 1; ++y) {
if(x >= 0 && y >= 0 && x <= (__GLOBAL__.map.size[0] - 1) && y <= (__GLOBAL__.map.size[1] - 1)) {
const position = (__GLOBAL__.map.size[0] * x) + y;
const node = __GLOBAL__.map.nodes[position];
document.getElementById(node.id_node)?.remove();
const _node = createNodeElement(node, __GLOBAL__.options);
container.insertAdjacentElement('afterbegin', _node);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment