My Full Example of GeonBit & Classes

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

Game1.cs enum

I have created the following enum within the Game1 class:

        public enum GameState
        {
            Welcome,
            Screen1,
            Screen2
        }

        private GameState state=GameState.Welcome;

        public GameState State
        {
            get { return state; }
            set
            {
                state = value;
                SwitchScreen();
            }
        }

wheneever the enum is changed it will run my SwitchScreen() method. I will show this method later. Before this we will need to create the classes for our base screen and for the actual screens.

Screen base class

    class UIScreen
    {
        public Panel window;
        public int width, height;
        public Game1 parentGame;
      
        public UIScreen(PanelSkin p, Anchor a, int w, int h)
        {
            width = w;
            height = h;
            window = new Panel(new Vector2(w, h), p,a);
            UserInterface.Active.AddEntity(window);
        }

        public void show(bool b)
        {
            window.Visible = b;
        }

        public void drag(bool b)
        {
            window.Draggable = b;
        }
    }

The variables will allow you to set and remember the height & width, the parentGame will be used in any subclass to allow the screen to access and change the enum we have created. I have added a methods to allow you to set the Visible & Draggable setting of the screen. I have also created a constructor which will create the actual window using the settings passed.

Individual Screen classes

class Screen1:UIScreen
    {
        public Screen1(PanelSkin p, Anchor a, int w, int h):base(p,a,w,h)
        {
          
        }

        public void Init(Game1 game)
        {
            parentGame = game;

            Header title = new Header("Instructions");
            window.AddChild(title);

            Paragraph test = new Paragraph("How to play the game info");
            window.AddChild(test);

            Button button1 = new Button("Back");
            window.AddChild(button1);

            button1.OnClick = (Entity btn) =>
            {
                //test.Text = "button clicked";
                base.show(false);
                parentGame.State = Game1.GameState.Welcome;
                window.ClearChildren();
            };
        }
    }

class Welcome:UIScreen
    {
        public Welcome(PanelSkin p, Anchor a, int w, int h) : base(p, a, w, h)
        {

        }

        public void Init(Game1 game)
        {
            parentGame = game;

            Header title = new Header("My Game Title");
            window.AddChild(title);

            Paragraph test = new Paragraph("Select the options for you game");
            window.AddChild(test);

            Button button = new Button("Instructions");
            window.AddChild(button);

            button.OnClick = (Entity btn) =>
            {
            //test.Text = "button clicked";
            base.show(false);
                parentGame.State = Game1.GameState.Screen1;
                window.ClearChildren();
            };

            Button button1 = new Button("Play");
            window.AddChild(button1);

            button1.OnClick = (Entity btn) =>
            {
                //test.Text = "button clicked";
                base.show(false);
                parentGame.State = Game1.GameState.Screen2;
                window.ClearChildren();
            };
        }
    }

class Screen2 : UIScreen
    {
        public Screen2(PanelSkin p, Anchor a, int w, int h) : base(p, a, w, h)
        {

        }

        public void Init(Game1 game)
        {
            parentGame = game;

            Header title = new Header("Play Options");
            window.AddChild(title);

            Paragraph test = new Paragraph("Select the options for you game");
            window.AddChild(test);

            Button button1 = new Button("Close");
            window.AddChild(button1);

            button1.OnClick = (Entity btn) =>
            {
                //test.Text = "button clicked";
                base.show(false);
                parentGame.State = Game1.GameState.Welcome;
                window.ClearChildren();
            };
        }
    }

the constructor class requires you to specify parameters for each of the items within the base screen constructor. The screen constructor just passes the values into the base constructor.

The Init method requires a parameter for the game. This means the later button1.OnClick can access the enum to switch screens. This code creates a header, paragraph, a button to carry out an action on the paragraph, and a button to access the enum and switch screens.

Note you need to do this for each screen I have created a screen for Welcome which is the game title and buttons to instructions and play. You could also add images etc to make a better title screen. I have created a Screen1 for instructions, and a Screen2 for the play options.

Creating screens in Game1.cs

You will need to declare variables for your screens

        Welcome title;
        Screen1 instructions;
        Screen2 playOptions;

in the Initialize method add the following:

            // TODO: Add your initialization logic here
            UserInterface.Initialize(Content, BuiltinThemes.editor);

            title = new Welcome(PanelSkin.Fancy, Anchor.Center, 300, 300);
            instructions = new Screen1(PanelSkin.Fancy, Anchor.Center, 300, 300);
            playOptions = new Screen2(PanelSkin.Fancy, Anchor.Center, 300, 300);

            title.Init(this);
            instructions.Init(this);
            playOptions.Init(this);

Make sure the Update method includes the line:

UserInterface.Active.Update(gameTime);

And Draw includes this line:

UserInterface.Active.Draw(spriteBatch);

Finally create the SwitchScreen method, this is called when the enum is changed:

        public void SwitchScreen()
        {
            switch (State)
            {
                case GameState.Welcome:
                    title.show(true);
                    instructions.show(false);
                    playOptions.show(false);
                    break;
                case GameState.Screen1:
                    title.show(false);
                    instructions.show(true);
                    playOptions.show(false);
                    break;
                case GameState.Screen2:
                    title.show(false);
                    instructions.show(false);
                    playOptions.show(true);
                    break;
            }
        }

You will need a case for each possible enum value. State is the property created at the begining with the get/set method.