Last active
February 13, 2021 01:28
-
-
Save Novout/20e97574df076a72aecc8fa228904102 to your computer and use it in GitHub Desktop.
Chunk Generator with 2D Player Coordinates JSVanilla
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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