2

I am currently developing a game in AndEngine and I have set up my collision detection with the enemies by checking each car against every individaul index of the enemy array as for some reason, a for loop does not work. This is extremely inconvinient as not only does it make increasing and decreasing enemies a chore, but it looks awful! It looks something like this:

if (rManager.getInstance().iceArray[0].getIceSprite().collidesWith(rManager.getInstance().carArray[r].getCarSprite()))
{
    rManager.getInstance().carArray[r].setCarSpeed(1f);
} else if (rManager.getInstance().iceArray[1].getIceSprite().collidesWith(rManager.getInstance().carArray[r].getCarSprite())) {
    rManager.getInstance().carArray[r].setCarSpeed(1f);
} else if (rManager.getInstance().iceBergArray[0].getIceBergSprite().collidesWith(rManager.getInstance().carArray[r].getCarSprite())) {
    rManager.getInstance().carArray[r].setCarSpeed(0f);
} else {
    rManager.getInstance().carArray[r].setCarSpeed(0.5f);
}   

The for loop I tried was like this with [r] being every car of the car array, but it doesn't seem to do anything.

for (int h = 0; h < rManager.getInstance().snowArray.length; h++)
{
    if (rManager.getInstance().snowArray[h].getSnowSprite().collidesWith(rManager.getInstance().carArray[r].getCarSprite())) {
        String temp = rManager.getInstance().carArray[r].toString();
        Log.e("SNOW", "SNOWWWWW!" + rManager.getInstance().snowArray[h].toString());
        rManager.getInstance().carArray[r].setCarSpeed(0.2f);
    }
}

Thanks!!

Alexey
  • 714
  • 8
  • 21
opposite of you
  • 1,295
  • 1
  • 11
  • 16

1 Answers1

4

You should use factory pattern or strategy pattern to remove out the if else.

Please see the link below to take reference.

http://www.cumps.be/nl/blog/read/design-patterns-strategy-pattern

or Strategy Pattern with no 'switch' statements?

EDIT :

Base on your code, the startegy pattern should implement follow pesudo code below. I have made some of assumption base on your code already posted above.

You will create a dictionary contains logics and appropriate action as below:

 Dictionary<Func<RManager, int, bool>, Action<Car>> 
 ruleUpdates = 
 new Dictionary<Func<RManager, int, bool>, Action<Car>>();

Each Func<Ice, Car, bool> you should implement like this

private bool ZeroElementEqualIcePrite(RManager rManager, int r)
    {
        return rManager.getInstance().iceArray[0].collidesWith(rManager.getInstance().carArray[r].getCarSprite());
    }

and Action should implement as below:

public static void DoUpdateOneLevel(Car car)
{
    car.setCarSpeed(1f);
}

From there we can implement similar with all of logics following your code with the same way. You can initialize the dictionary following below

ruleUpdates.Add(FirstElementEqualIcePrite, DoUpdateOneLevel);
ruleUpdates.Add(SecondElementEqualIcePrite, DoUpdateOneLevel);
ruleUpdates.Add(FirstElementEqualIceBergPrite, DoUpdateZeroSpeed);
ruleUpdates.Add(SecondElementEqualIceBergPrite, DoUpdateZeroSpeed);

After that you just loop go through each key of executionList as below.

foreach (KeyValuePair<Func<RManager, int, bool>, Action<Car>> 
 ruleUpdate in ruleUpdates)
    {
       if (ruleUpdate.Key.Invoke(rManager, r))
       {
          ruleUpdate.Value.Invoke(rManager.getInstance().carArray[r]);
          break;
       }
    }

Hope this help. Following the strategy your code will clean and easy to changed does not care much about if / then / else logic as well. You can easy extent it in future.

Community
  • 1
  • 1
Tim Phan
  • 311
  • 1
  • 11
  • Thanks for the response. I've tried looking into both types of pattern, but I have no idea how they relate to my game, or how I could implement them to remove the if/elses's. I only need to check if a car has collided with any of the enemies – opposite of you Apr 26 '13 at 14:12
  • Please see my edit above. Hope this help for your code will readable and easy to understand. – Tim Phan Apr 26 '13 at 17:13