-1

Ok So I am revisiting a prototype game I made and never finished, When prototyping you dont always come up with the best ideas haha. This is hard for me to explain but basically there are game objects on screen that can be "Eaten" then I make it basically re-spawn with a new points value and texture.

The next phase method is called when the game object is "Eaten" just when the texture intersects the mouth on screen

Hide Spoiler

Code:

void NextPhase(int food)
    {

        switch (foodNumber)
        {
                //Detects what food object has been eaten
            case 1:
                {
                    food1Phase++;
                    break;
                }
            case 2:
                {
                    food2Phase++
                    break;
                }
            case 3:
                {
                    food3Phase++;
                    break;
                }
                //
        }
        switch (foodNumber)
        {
            case 1:
                {

                    switch (food1Phase)
                    {
                    //Each phase is a new set of Textures, Points Values and specific spawn delay.
                    //Multiplier sotrage tells another method
                    //what food has been eaten and decides whether to increase the score mulitplier
                    //the position properties reset it to a location off screen and then it flies on screen
                    //for the player to collect
                    //delayCompleted false activates another method that starts timing the spawn
                    //delay
                case 1:
                    {


                        multiplierStorage("Gay Bacon");
                        food1Position.X = -200;
                        food1Position.Y = 60;
                        food1 = contentManager.Load<Texture2D>("Food2Img");
                                food1Height = food1.Bounds.Height + 20;
                                food1Width = food1.Bounds.Width + 30;
                                food1Speed.X = 0;
                                food1Speed.Y = 0;
                        delayCompleted = false;
                        if (gameOverallTimer < 30.0)
                        {

                            timeWanted = gameOverallTimer + 4.0;
                        }
                        break;
                    }
                case 2:
                    {
                        food1 = contentManager.Load<Texture2D>("Food6");
                        food1Height = food1.Bounds.Height + 20;
                        food1Width = food1.Bounds.Width + 30;
                        food1Position.X = -100;
                        food1Position.Y = 60;
                        food1Speed.X = 0;
                        food1Speed.Y = 0;
                        multiplierStorage("Bacon");




                        break;
                    }
                case 3:
                    {
                        food1 = contentManager.Load<Texture2D>("Food1Img");
                        food1Height = food1.Bounds.Height + 20;
                        food1Width = food1.Bounds.Width + 30;
                        food1Position.X = -100;
                        food1Position.Y = 60;
                        food1Speed.X = 0;
                        food1Speed.Y = 0;
                        vegetableEatenMultiplier();
                        lives = lives - 1;
                        UpdateLives();
                        food1Phase = 0;
                        break;
                    }
                }
            }
        }

            case 2:
                {

                    switch (food2Phase)
                    {
                        food2 = contentManager.Load<Texture2D>("Food4");
                        food2Height = food2.Bounds.Height + 30;
                        food2Width = food2.Bounds.Width + 30;
                        food2Position.X = 850;
                        food2Position.Y = 200;
                        food2Speed.X = 0;
                        food2Speed.Y = 0;

                        multiplierStorage("Bacon");
                        break;
                    }
                case 2:
                    {


                        food2 = contentManager.Load<Texture2D>("Food5");
                        food2Height = food2.Bounds.Height + 30;
                        food2Width = food2.Bounds.Width + 30;
                        food2Position.X = 850;
                        food2Position.Y = 200;
                        food2Speed.X = 0;
                        food2Speed.Y = 0;
                        vegetableEatenMultiplier();
                        lives = lives - 1;
                        UpdateLives();
                        break;
                    }
                case 3:
                    {
                        food2 = contentManager.Load<Texture2D>("Food2Img");
                        food2Height = food2.Bounds.Height + 30;
                        food2Width = food2.Bounds.Width + 30;
                        food2Position.X = 850;
                        food2Position.Y = 200;
                        food2Speed.X = 0;
                        food2Speed.Y = 0;
                        food2Phase = 0;
                        multiplierStorage("Steak");
                        break;
                    }
                }
            }



            case 3:
                {

                    switch (food3Phase)
                    {

                        food3Position.X = 600;
                        food3Position.Y = -100;
                        food3Speed.X = 0;
                        food3Speed.Y = 0;
                        food3Phase = 0;
                        multiplierStorage("Gay Bacon");
                        break;
                    }
                }
            }
        }

    }      

The thing is this doesn't seem very efficient to me, Now yes I have thought of creating a new class with Texture and position Etc. properties then I just make new objects of that class and make it better that way but I would still have to use this phase code in that class. I want an alternative for code only.

Ricehead
  • 55
  • 1
  • 6
  • What are you looking for? Maybe write what you're trying to accomplish, and someone could suggest patterns to implement. – ZeroPhase Aug 01 '13 at 09:27
  • Hmm maybe I'm just wondering if a switch statement is really the best way to do this Phasing action? Is there a better way to do Phasing like this ? It just feels kind of clunky to me. – Ricehead Aug 01 '13 at 09:43
  • 2
    Your code basically suffers from not using classes to bundle your properties. There is nothing particularly wrong with your code, and asking for cleaner code without using classes is much of a waste of time. – Kai Hartmann Aug 01 '13 at 09:44
  • @Ricehead If you describe what the code should do in a concise paragraph it will be much easier to suggest changes. Namely, which types of patterns to use, and I could point you to some examples of said patterns. From glancing over your code it looks like you need abstract factories and concrete factories. – ZeroPhase Aug 01 '13 at 09:51
  • @Kai Hartmann If you read my paragraph at the bottom of the question I already know this but it's irrelevant because even encasing this method in a class would still result in me essentially using the same code. – Ricehead Aug 01 '13 at 10:01
  • @ZeroPhase If you look at the comments in the code have done this for you, What are these Abstract and Concrete Factories you speak of and how can i utilise them ? – Ricehead Aug 01 '13 at 10:02
  • @ZeroPhase //Each phase is a new set of Textures, Points Values and specific spawn delay. Multiplier sotrage tells another method what food has been eaten and decides whether to increase the score mulitplier. The position properties reset it to a location off screen and then it flies on screen for the player to collect. delayCompleted false activates another method that starts timing the spawn delay. This is what i put in – Ricehead Aug 01 '13 at 10:03
  • An abstract factory provides an interface for creating similar objects, without specifying an implementation of those objects. A concrete factory creates specialized objects that can accomplish a specific function. By inheriting your abstract factory in a concrete factory you can create multiple objects with different effects. When the player eats a specific object you can apply that effect to the player. – ZeroPhase Aug 01 '13 at 10:11
  • @ZeroPhase I think I kind of understand, This would help from a rendering point of view because if I understand correctly I wouldn't need to manually add the objects to the draw method I could just add the factory and its children. Is there any resources you could link me to, I would really like to have a chat with you on an IM like sype or something aswell as I havent grasped this fully and Google is not being very helpful – Ricehead Aug 01 '13 at 11:20
  • Why have the switch and the if? Drop the switch and do the Phase++ in the if. – paparazzo Aug 01 '13 at 13:24
  • @Blam Using If statements would be even more inefficient because the if decision operator line would still be executed to evaluate whether the if(Blah) is true where as in the switch it only executes the possible outcome, This is an Mobile XNA little performance tweaks like this actually matter :) – Ricehead Aug 01 '13 at 14:44
  • @Ricehead So, right now you are doing both. If you just pick one it will be more efficient. – paparazzo Aug 01 '13 at 14:55
  • @Blam Ahh I see, I have replaced the secondary If's with Switch's thank you :) – Ricehead Aug 01 '13 at 15:56

1 Answers1

0

You can probably combine some of those switches.

switch (foodNumber)
{
    case 1:
        switch (food1Phase)
        {
            case 1:

Could become:

switch (foodNumber << 8 | food1Phase)
{
    case (1 << 8 | 1):
Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
  • Ok, I thought the << operator shifts the variable bits left by the number you specify in this case 8 ? Why do I need it and its argument? switch (foodNumber | food1Phase) { case (1 | 1): couldnt I just have this ? – Ricehead Aug 01 '13 at 18:20
  • `1|3` and `3|3` both result in `3`. The shift is to make room for both values, assuming `foodNumber` needs at most 24 bits and `food1Phase` needs at most 8 bits. – Cory Nelson Aug 01 '13 at 18:27