How do you connect Collision layer number with table(coding)

Discuss any general programming issues here
Post Reply
Bugala
Posts: 757
Joined: Sun Feb 14, 2010 7:11 pm

How do you connect Collision layer number with table(coding)

Post by Bugala » Thu Dec 06, 2012 12:19 pm

This question is bit different. I am asking in this one about how you solve this kind of problem.

Typically I tend to have something like:

Code: Select all

Enemy = {}
...
Enemy[x] = {x=100, y=100...  ...enemynumber=x}
InsertLayer(1, #brush...   ..., {name="enemylayer"..enemy[x].enemynumber})
...
if Collision (#LAYER, player, enemy[x]
   removelayer("enemylayer"..enemy[x].number 
  enemy[x] = {}
by words: i create a table containing enemies, which might be generated more during the game. Meaning that the table might contain enemies enemy[1], enemy[2], enemy[3]...
I also name their layers accordingly, that they are "enemylayer1", "enemylayer2", "enemylayer3"...

Then at some point in game there comes collison detection between these enemy layers and player layer.

After which layer and the table item (ie. enemylayer[2]), needs to be destroyed.

When having one dimensional table, this is of course very easy. just remove layer "Enemylayer2" and table item "Enemy[2]".


But here comes the real question.
Suppose you have multidimensional table instead. Say only 2 dimensional table with Enemies[wave][enemy].

And then during the game it can happen that next wave comes already before the first wave have been destroyed.

This creates situation where enemy numbers arent direct to the tables anymore.

As example, situation could be this:

Code: Select all

enemies = {
            [1] = {   (this is the wave)
                [1] = { x=100, y=100, enemynumber = 1 }    (these are the individual enemies)
                [2] = { x=200, y=200, enemynumber = 2 }
                    }
           [2] =  {    (this is second wave already on screen too)
                [1] = {x=300, y=300, enemynumber = 3}
                [2] = {x=400, y=400, enemynumber = 4}
                   }
               }


and in worse case these enemynumber variables can be mixed in completely random order due to how the game progresses.

Since these enemynumber and layernumbers (ie. "enemylayer4") are always the same to make it easier to track them, I however have been having one difficulty.

For it works nice when you work with the table and decide due to table stuff that you need to destroy the layer, just pick the "enemynumber" and destroy same named layer (ie. "enemylayer"..currenttableitem.enemynumber).

But when it happens the other way round, it is bit difficult.

For when I have piece of code that say:

Code: Select all

for n=1 to number of enemies
   if collision(#LAYER, "player", "enemylayer"..n) = true
        remove("enemylayer"..n
        removeitem(enemies[?][?]

The problem is, that I do get the number which layer hits, ie "enemylayer4", which means that item containing the enemynumber=4 needs to be removed, but question is how to find out which one is the enemynumber=4.


I have figured out two ways to do this, but neither of them is perfect. Therefore I thought I ask if some of you have the similar problem as I often stumble, and how have you solved this problem?




Here are my two ways:

1. I create separate table for example called Enemynumberstable = {}-

Each time new enemy is created, I add its table location to this table.

As example:

Enemynumberstable[1] = { wave=2, enemy=3 }

This way when there comes collision between "player" and "enemylayer1", i can look at this enemynumberstable at position 1 and know in which location at "enemies" table the enemynumber=1 is located at.


2. I use Foreach command to go through every single item of the multidimensional table to find out the one table item that has enemynumber = 1. That way I dont need to keep another table like in number 1 example, but bad side is that this will go through every item in the multidimensional table.



But these both have problems, especially the problem of when i remove one of the numbers Basically all the numbers would need to be changed to make it unbroken list of 1 to n.

For normally i keep it so that there is for example: nextfreelayer and everytime i make one, its nextfreelayer=nextfreelayer+1, or when one os removed its nextfreelayer=nextfreelayer-1.

So if i have 5 enemies, and enemy number 1 is removed, then nextfreelayer would go to be 5, but in reality, bumber 5 is still occupied, since it was number 1 that was removed, not the 5.

Post Reply