Question

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.

Was it helpful?

Solution

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):
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top