Hey everyone, I'm a new object developer looking to create an animation of falling cubes. I've been following a guide on Falling animation to fill a webpage
Currently, I am facing some algorithmic challenges. I'm trying to implement a similar structure to a tetris game but with the ability for multiple pixels to fall simultaneously. I have constructed a player object with various methods to control pixel movement.
My current approach involves creating an array of objects like this:
var a_player = [];
function addPlayer(pos){
var player = new Player(pos);
a_player.push(player);
}
addPlayer({x: 3, y: 3});
addPlayer({x: 0, y: 0});
I want to incorporate public methods such as a collide() function:
function collide(arena, player) {
const [m, o] = [player.matrix, player.pos];
for (let y = 0; y < m.length; ++y) {
for (let x = 0; x < m[y].length; ++x) {
if (m[y][x] !== 00 &&
(arena[y + o.y] &&
arena[y + o.y][x + o.x]) !== 0) {
return true;
}
}
}
return false;
}
However, I am unsure of the best approach to utilize this method. Should I loop through each player in the array using a "for" loop like this?
for (i = 0; i < a_player.length; i++){
console.log(a_player[i].pos);
}
I am concerned about efficiency since I plan to have more than 20k players eventually. Any suggestions or advice would be greatly appreciated!