1
\$\begingroup\$

Back Story

I'm working on a 2-Players 2D game that players play multiple rounds ,until one win.

In each round, each player choose some items (1 or more) for his/her car ( e.g. shield, anti-shield, power-up, speed, ... ) and their chosen items are hidden for opponent. when both player get ready, they just sit and see what happen to their cars which move toward each other (each round).

Problem

As I said, there are multiple items (read "magics") with different effect in my game. Each Item's effect may differ depends on item which it interact with. each item have some positive effect and some cost.

For instance, If player 1 uses "speed" item, his car may cause more damage to opponent's car ,whereas if opponent had used a "truck-bumper", this "speed" would cause player 1 to get more damage instead!

After writing all the rules and effects of each item on a paper, I need your helps to find a good and structural way to handle them in code (except many nested if-else ).

Finally

I need some kind of structure or coding style for converting these rules to code and determining the net result of items interaction in each round according to items relations.

\$\endgroup\$
  • \$\begingroup\$ Can a car have more than one item active each round? \$\endgroup\$ – user15805 Sep 1 '13 at 19:36
  • \$\begingroup\$ I edit my question. answer is yes! but I also look forward to one-to-one item solution =) \$\endgroup\$ – Emadpres Sep 1 '13 at 19:39
  • \$\begingroup\$ If both cars have Speed and Truck Bumper, what happens? \$\endgroup\$ – user15805 Sep 1 '13 at 19:41
  • \$\begingroup\$ Showing us the rules on paper will also help. \$\endgroup\$ – user15805 Sep 1 '13 at 19:43
  • \$\begingroup\$ they both get more damage. more damage than simple-hit. ( because of collision speed). Game's rules are not strange and they are designed to be understandable for players. problem is about handling them in good form in code. rules may change in future so they should be kept simple in code. \$\endgroup\$ – Emadpres Sep 1 '13 at 19:44
3
\$\begingroup\$

This is how I'd do it.

First of all, we will define methods for the interaction between cars which take into account two items only each. Note that this is not C++. All parameters are considered to be passed by reference.

void interaction(Car car1, Car car2);

A concrete example would be your Bumper vs. Speed interaction:

void bumperVsSpeedInteraction(Car car1, Car car2)
{
     if (car1.HasPowerUp(Bumper))
     {
           car2.TakeDamage(X);
     }
     else
     {
           car1.TakeDamage(X);
     }
}

Next, we're going to use an ADT like the dictionary to map <PowerUp, PowerUp> (where the first PowerUp belongs to the first car, and the 2nd to the 2nd car) pairs to pointers to interaction methods.

Example:

interactionForPair[new Pair(Bumper, Speed)] = bumperVsSpeedInteraction;
interactionForPair[new Pair(Speed, Bumper)] = bumperVsSpeedInteraction;

We notice that if we consider <Bumper, Speed> to be the same as <Speed, Bumper> and just check which car has which power up in the interaction method, we don't have to add a separate interaction method for each. Depending on how you implement the Pair class (more exactly if on equality check, Pair(a,b) equals Pair(b,a)), you can avoid adding both keys to the dictionary.

Since I'm not clear why both cars would take damage if they both had Bumper and Speed, I'm not exactly sure how it would be appropriate to continue from here. In the end, however, you should have two cars, each with a list of active power ups.

Car1
{C1PowerUp1, C2PowerUp2}

Car2
{C1PowerUp1, C2PowerUp2}

Then, you'd have to apply some rule to decide which power up gets to fight which of the others. Suppose our rule tells us C1PowerUp1 gets to fight with C2PowerUp2. You'd then do:

Pair = MakePair(C1PowerUp1, C2PowerUp2); //Make a pair with the two power ups.
interactionForPair[Pair](Car1, Car2); //Call the appropriate method for this pair.

Any of the power ups can be null or a special constant defined by you which means the power up does not exist (if both cars are fighting without power ups, both pair elements would be null).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.