D
D
Danil Shitov2020-06-13 18:04:50
Data Structures
Danil Shitov, 2020-06-13 18:04:50

js arrays and data structures, how to work correctly?

Good afternoon!
I don’t know if I’m doing it right or I don’t have enough knowledge, and I can do something smart :)
I’m writing a game in js like “zombies against plants”, I have an array with creature objects [{...},{... },..];
{
name:monster,
x:x, y :
y,
track:3, enemy
:false,
.....
} only 5 tracks), enemy - whether this creature is an enemy or not (if the enemy then goes in the opposite direction, not enemies). Well, other options.
To properly display the heroes on the canvas, you need to draw them from the smallest y to the largest, which means we need to go through the array to find the smallest y and draw, and then go through the array again and find the smallest y, not including the previous one, and so on until everything is over.
And I have option #2:
Create a separate array sorted by y .
I also need to check if the creature collided with its enemy, the creature can collide only on one track (total tracks 5) and only with the enemy. So I need to go through the array and for each creature again go through the array and find the creature with the same path and which is the enemy. And also option number 2, you can create two arrays for each track with ordinary and enemies, so you will have to compare less.

It seems to me that both options are strange. Are there adequate ways to solve such problems?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question