Changeset 41


Ignore:
Timestamp:
02/19/12 11:51:38 (3 years ago)
Author:
acarter
Message:

More comments/refactoring

Location:
Code/oMaE/oMaE
Files:
1 added
14 edited

Legend:

Unmodified
Added
Removed
  • Code/oMaE/oMaE/GameDriver.cs

    r40 r41  
    2424        ///  The device manager for the 3D graphics.
    2525        /// </summary>
    26         GraphicsDeviceManager graphics;
     26        private GraphicsDeviceManager graphics;
    2727        /// <summary>
    2828        ///  The sprite manager for the 2D graphics.
    2929        /// </summary>
    30         SpriteBatch spriteBatch;
     30        private SpriteBatch spriteBatch;
    3131        /// <summary>
    3232        /// Holds the current gamestate with respect to User Input.
    3333        /// </summary>
    34         GameState gamestate = new GameState();
     34        private GameState gamestate = new GameState();
    3535        /// <summary>
    3636        /// The display for the 3D game.
    3737        /// </summary>
    38         public Display World3D = new Display();
     38        public Display3D World3D = new Display3D();
    3939        /// <summary>
    4040        /// All of the 2D world components.
     
    4242        public GameObject World2D = new GameObject();
    4343        /// <summary>
    44         /// The Factory for making minigames.
    45         /// </summary>
    46         MinigameFactory Factory = new MinigameFactory();
     44        /// The Minigame factory for making minigames.
     45        /// </summary>
     46        public MinigameFactory Minigames = new MinigameFactory();
     47        /// <summary>
     48        /// The screen factory for the screens of the game.
     49        /// </summary>
     50        public ScreenFactory Screens = new ScreenFactory();
    4751        /// <summary>
    4852        /// Projection matrix for the game.
    4953        /// </summary>
    5054        public static Matrix ProjectionMatrix;
    51         Screen2D screen = new Screen2D(1000, 500);
    52 
     55        /// <summary>
     56        /// The display for the game.
     57        /// </summary>
     58        private Screen2D display = new Screen2D(1000, 500);
     59        /// <summary>
     60        /// The main screen when the game is loaded.
     61        /// </summary>
    5362        private GameObject mainScreen = new MainScreen();
     63        /// <summary>
     64        /// The menu for the game.
     65        /// </summary>
     66        private GameObject gameMenu = new GameMenu();
     67        /// <summary>
     68        /// The current screen that is displayed.
     69        /// </summary>
     70        private GameObject currentScreen = null;
     71        /// <summary>
     72        /// The current screen that is displayed.
     73        /// </summary>
     74        private GameObject currentMinigame = null;
    5475        /// <summary>
    5576        /// Creates a new default game driver.
     
    7394            // TODO: Add your initialization logic here
    7495            ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), GraphicsDevice.DisplayMode.AspectRatio, 1.0f, 100.0f);
    75             World2D.Add(Factory);
    76             World2D.Add(play2DGame(mainScreen));
     96            World2D.Add(Minigames);
     97            World2D.Add(Screens);
     98            World2D.Add(gameMenu);
     99            World2D.Add(Play2DGame(mainScreen));
    77100
    78101            this.IsMouseVisible = true;
     
    90113            // Create a new SpriteBatch, which can be used to draw textures.
    91114            spriteBatch = new SpriteBatch(GraphicsDevice);
    92             screen.LoadContent(spriteBatch, Content);
     115            display.LoadContent(spriteBatch, Content);
    93116            World2D.LoadAllContent(Content);
    94117            World3D.LoadAllContent(Content);
     
    130153            GraphicsDevice.Clear(Color.MediumPurple);
    131154            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
    132             World3D.DrawAll(screen, gamestate);
    133             World2D.DrawAll(screen, gamestate);
     155            World3D.DrawAll(display, gamestate);
     156            World2D.DrawAll(display, gamestate);
    134157            base.Draw(gameTime);
    135158        }
    136159
    137160        /// <summary>
    138         /// A wrapper function for Minigame Factory's getMinigame.
     161        /// A wrapper function for Minigame Minigames's getMinigame.
    139162        /// </summary>
    140163        /// <returns>Returns a new minigame.</returns>
    141         public GameObject getMinigame()
    142         {
    143             return Factory.getMiniGame();
     164        public GameObject GetMinigame()
     165        {
     166            return Minigames.getMiniGame();
    144167        }
    145168
     
    149172        /// <param name="mainMenu">The main menu.</param>
    150173        /// <returns>The input game object.</returns>
    151         public GameObject playGame(GameObject mainMenu)
     174        public GameObject PlayGame(GameObject mainMenu)
    152175        {
    153176            mainMenu.Visible = false;
     
    162185        /// <param name="game">The minigame that was started.</param>
    163186        /// <returns>The input game object.</returns>
    164         public GameObject play2DGame(GameObject game)
     187        public GameObject Play2DGame(GameObject game)
    165188        {
    166189            game.Visible = true;
     
    169192        }
    170193
    171 
     194        /// <summary>
     195        /// Displays the game menu.
     196        /// </summary>
     197        public void DisplayGameMenu()
     198        {
     199            World3D.Updating = false;
     200            gameMenu.Visible = true;
     201        }
     202        /// <summary>
     203        /// Closes the game menu.
     204        /// </summary>
     205        public void CloseGameMenu()
     206        {
     207            World3D.Visible = true;
     208            gameMenu.Visible = false;
     209        }
     210        /// <summary>
     211        /// Displays the main menu.
     212        /// </summary>
     213        public void DisplayMainMenu()
     214        {
     215            World3D.reset();
     216            World3D.Visible = false;
     217            if (currentMinigame != null)
     218                currentMinigame.Visible = false;
     219            if (currentScreen != null)
     220                currentScreen.Visible = false;
     221            gameMenu.Visible = false;
     222            mainScreen.Visible = true;
     223        }
     224
     225        /// <summary>
     226        /// Displays a screen such as the inventory, or encyclophant.
     227        /// </summary>
     228        /// <param name="screen">The screen to be displayed.</param>
     229        /// <returns>The screen that is now displayed.</returns>
     230        public GameObject DisplayScreen(GameObject screen)
     231        {
     232            if (currentScreen != null)
     233                CloseScreen();
     234            currentScreen = screen;
     235            screen.Visible = true;
     236            World3D.Visible = false;
     237            return screen;
     238        }
    172239
    173240        /// <summary>
     
    176243        /// <param name="game">The minigame that was ended.</param>
    177244        /// <returns>The input game object.</returns>
    178         public GameObject quitMinigame(GameObject game)
     245        public GameObject QuitMinigame(GameObject game)
    179246        {
    180247            game.Visible = false;
     
    188255        /// <param name="game">The minigame that was completed.</param>
    189256        /// <returns>The input game object.</returns>
    190         public GameObject completeMinigame(GameObject game)
     257        public GameObject CompleteMinigame(GameObject game)
    191258        {
    192259            game.Visible = false;
    193260            World3D.Visible = true;
    194261            return game;
     262        }
     263        /// <summary>
     264        /// Closes the current screen.
     265        /// </summary>
     266        ///
     267        /// <returns>The screen that was closed.</returns>
     268        public GameObject CloseScreen()
     269        {
     270            GameObject screen = currentScreen;
     271            currentScreen = null;
     272            if (screen != null)
     273            {
     274                screen.Visible = false;
     275                World3D.Visible = true;
     276            }
     277            return screen;
    195278        }
    196279    }
  • Code/oMaE/oMaE/GameState.cs

    r40 r41  
    3737        private KeyboardState kc, ko;
    3838        private MouseState mc, mo;
     39        private Dictionary<Keys, bool> handledPress = new Dictionary<Keys,bool>();
     40        private Dictionary<MouseButton, bool> handledClick = new Dictionary<MouseButton,bool>();
    3941        /// <summary>
    4042        /// The current game time.
     
    7779        }
    7880        /// <summary>
     81        /// Accessor for if a key is down.
    7982        /// </summary>
    8083        /// <param name="key">The key.</param>
     
    8588        }
    8689        /// <summary>
    87         ///
     90        /// Accessor for if a key is up.
    8891        /// </summary>
    8992        /// <param name="button">The mouse button.</param>
     
    105108       
    106109        /// <summary>
    107         ///
     110        /// Accessor for if a key is pressed.
    108111        /// </summary>
    109112        /// <param name="key">The key.</param>
     
    111114        public bool Pressed(Keys key)
    112115        {
    113             return CurrentKeyboardState.IsKeyDown(key) &&
     116            return !handledPress.ContainsKey(key) &&
     117                CurrentKeyboardState.IsKeyDown(key) &&
    114118                    !LastKeyboardState.IsKeyDown(key);
    115119        }
    116120        /// <summary>
    117         ///
     121        /// Accessor for if a button is pressed.
    118122        /// </summary>
    119123        /// <param name="button">The mouse button.</param>
     
    121125        public bool Pressed(MouseButton button)
    122126        {
     127            if(handledClick.ContainsKey(button))
     128                return false;
    123129            switch (button)
    124130            {
    125131                case MouseButton.LEFT:
     132                   
    126133                    return CurrentMouseState.LeftButton == ButtonState.Pressed &&
    127134                        LastMouseState.LeftButton == ButtonState.Released;
     
    135142                    return false;
    136143            }
     144        }
     145        /// <summary>
     146        /// Indicates that a particular key press has been handled.
     147        /// </summary>
     148        /// <param name="key">The key that was handled.</param>
     149        public void handle(Keys key)
     150        {
     151            handledPress.Add(key, true);
     152        }
     153        /// <summary>
     154        /// Indicates that a particular mouse click has been handled.
     155        /// </summary>
     156        /// <param name="button">The button that was handled.</param>
     157        public void handle(MouseButton button)
     158        {
     159            handledClick.Add(button, true);
    137160        }
    138161        /// <summary>
     
    149172            kc = keyboard;
    150173            mc = mouse;
     174            handledClick.Clear();
     175            handledPress.Clear();
    151176        }
    152177    }
  • Code/oMaE/oMaE/MiniGames/Match.cs

    r40 r41  
    1717    class Match : MiniGame
    1818    {
    19         // should be refactored
    2019        private enum line_t { NONE, NEW, WRONG, CORRECT };
    2120        RadioGroup radioGroup;
  • Code/oMaE/oMaE/MiniGames/MiniGame.cs

    r40 r41  
    6161                return;
    6262            if (button == Back)
    63                 driver.quitMinigame(this);
     63                driver.QuitMinigame(this);
    6464        }
    6565    }
  • Code/oMaE/oMaE/Overworld/Display.cs

    r40 r41  
    1717    /// The display for the 3D elements.
    1818    /// </summary>
    19     public class Display : GameObject
     19    public class Display3D : GameObject
    2020    {
    2121        /// <summary>
  • Code/oMaE/oMaE/Overworld/Objects/Door.cs

    r40 r41  
    3131            children.Add(door);
    3232            children.Add(wall);
    33             doorLock = driver.getMinigame();
     33            doorLock = driver.GetMinigame();
    3434            base.Initialize();
    3535        }
     
    5959        {
    6060            if (elephantNear && gameState.Down(Keys.Space))
    61                 driver.play2DGame(doorLock);
     61                driver.Play2DGame(doorLock);
    6262            base.Update(gameState);
    6363        }
  • Code/oMaE/oMaE/Overworld/OverworldUI.cs

    r40 r41  
    2020    {
    2121        /// <summary>
    22         /// The button to get to the inventory.
    23         /// </summary>
    24         private Button inventory = new Button(new Rectangle(0, 450, 200, 50), "Inventory");
    25         /// <summary>
    26         /// The button to get to the encyclophant.
    27         /// </summary>
    28         private Button encyclophant = new Button(new Rectangle(800, 450, 200, 50), "Encyclophant");
    29         /// <summary>
    3022        /// The button to get to the in-game menu.
    3123        /// </summary>
     
    3628        public override void Initialize()
    3729        {
    38             children.Add(inventory.addButtonListener(this));
    39             children.Add(encyclophant.addButtonListener(this));
     30            children.Add(driver.Screens.createScreenButton(new Rectangle(0, 450, 200, 50), "Inventory", SCREENS.INVENTORY));
     31            children.Add(driver.Screens.createScreenButton(new Rectangle(800, 450, 200, 50), "Encyclophant", SCREENS.ENCYCLOPHANT));
    4032            children.Add(menu.addButtonListener(this));
    4133            base.Initialize();
     
    4739        public void ButtonLeftClicked(Button button)
    4840        {
     41            driver.DisplayGameMenu();
    4942        }
    5043        /// <summary>
  • Code/oMaE/oMaE/Screens/Encyclophant.cs

    r40 r41  
    2525        public override void Initialize()
    2626        {
    27             children.Add(new Button(new Rectangle(0, 0, 200, 50), "Inventory").addButtonListener(this));
    28             children.Add(new Button(new Rectangle(200, 0, 200, 50), "Encyclophant"));
     27            children.Add(driver.Screens.createScreenButton(new Rectangle(0, 0, 200, 50), "Inventory", SCREENS.INVENTORY));
     28            children.Add(driver.Screens.createScreenButton(new Rectangle(200, 0, 200, 50), "Encyclophant", SCREENS.ENCYCLOPHANT));
    2929            children.Add(new Button(new Rectangle(800, 0, 200, 50), "Back").addButtonListener(this));
    3030            base.Initialize();
     
    3636        public void ButtonLeftClicked(Button button)
    3737        {
    38             /*
    39             if (button.Text == "Back")
    40                 Display.Display3D.Game2D = null;
    41             if (button.Text == "Inventory")
    42                 Display.Display3D.toInventory();
    43              */
     38            driver.CloseScreen();
    4439        }
    4540        /// <summary>
  • Code/oMaE/oMaE/Screens/GameMenu.cs

    r40 r41  
    2525        public override void Initialize()
    2626        {
     27            Visible = false;
    2728            children.Add(new Button(new Rectangle(400, 100, 200, 50), "Save Game").addButtonListener(this));
    2829            children.Add(new Button(new Rectangle(400, 175, 200, 50), "Back to Game").addButtonListener(this));
     
    3839        {
    3940            if (button.Text == "Back to Game")
    40                 driver.quitMinigame(this);
     41                driver.CloseGameMenu();
     42            if (button.Text == "Main Menu")
     43                driver.DisplayMainMenu();
    4144            if (button.Text == "Quit Game")
    4245                driver.Exit();
  • Code/oMaE/oMaE/Screens/Inventory.cs

    r40 r41  
    2525        public override void Initialize()
    2626        {
    27             children.Add(new Button(new Rectangle(0, 0, 200, 50), "Inventory"));
    28             children.Add(new Button(new Rectangle(200, 0, 200, 50), "Encyclophant").addButtonListener(this));
     27            children.Add(driver.Screens.createScreenButton(new Rectangle(0, 0, 200, 50), "Inventory", SCREENS.INVENTORY));
     28            children.Add(driver.Screens.createScreenButton(new Rectangle(200, 0, 200, 50), "Encyclophant", SCREENS.ENCYCLOPHANT));
    2929            children.Add(new Button(new Rectangle(800, 0, 200, 50), "Back").addButtonListener(this));
    3030            base.Initialize();
     
    3636        public void ButtonLeftClicked(Button button)
    3737        {
    38             /*
    39             if (button.Text == "Back")
    40                 Display.Display3D.Game2D = null;
    41             if (button.Text == "Encyclophant")
    42                 Display.Display3D.toEncyclophant();
    43              */
     38            driver.CloseScreen();
    4439        }
    4540        /// <summary>
  • Code/oMaE/oMaE/Screens/MainScreen.cs

    r40 r41  
    3434        {
    3535            if (button.Text == "New")
    36                 driver.playGame(this);
     36                driver.PlayGame(this);
    3737            else if (button.Text == "Quit")
    3838                driver.Exit();
  • Code/oMaE/oMaE/Widgets/Button.cs

    r40 r41  
    7171        }
    7272        /// <summary>
     73        /// Handles the left click of a button.
     74        /// </summary>
     75        protected virtual void handleLeftClick()
     76        {
     77            foreach (IButtonListener listener in listeners)
     78                listener.ButtonLeftClicked(this);
     79        }
     80        /// <summary>
    7381        /// Updates this button.
    7482        /// </summary>
     
    7785        {
    7886            if (gameState.Pressed(MouseButton.LEFT) && location.Contains(gameState.MousePoint))
    79                 foreach (IButtonListener listener in listeners)
    80                     listener.ButtonLeftClicked(this);
     87            {
     88                gameState.handle(MouseButton.LEFT);
     89                handleLeftClick();
     90            }
    8191            base.Update(gameState);
    8292        }
  • Code/oMaE/oMaE/Widgets/Slider.cs

    r40 r41  
    1313namespace oMaE.Widgets
    1414{
    15     interface ISliderListener<O> { void SliderUpdated(Slider<O> listener); }
    16     class Slider<O> : GameObject
     15    /// <summary>
     16    /// A listener of a slider.
     17    /// </summary>
     18    /// <typeparam name="O">The values of the slider.</typeparam>
     19    public interface ISliderListener<O>
    1720    {
    18         List<ISliderListener<O>> listeners = new List<ISliderListener<O>>();
    19         Vector2 Location;
    20         Vector2 Scale;
    21         float Orientation;
    22 
    23         O[] Options;
    24 
    25         float slide;
     21        /// <summary>
     22        /// A callback when the slider is updated.
     23        /// </summary>
     24        /// <param name="slider">The slider that changed.</param>
     25        void SliderUpdated(Slider<O> slider);
     26    }
     27    /// <summary>
     28    /// A slide widget.
     29    /// </summary>
     30    /// <typeparam name="O">The values of the slider.</typeparam>
     31    public class Slider<O> : GameObject
     32    {
     33        /// <summary>
     34        /// The listeners of this object.
     35        /// </summary>
     36        private List<ISliderListener<O>> listeners = new List<ISliderListener<O>>();
     37        /// <summary>
     38        /// The location of the slider.
     39        /// </summary>
     40        public Vector2 Location;
     41        /// <summary>
     42        /// The size of the slider.
     43        /// </summary>
     44        public Vector2 Scale;
     45        /// <summary>
     46        /// The orientation of the slider.
     47        /// </summary>
     48        public float Orientation;
     49        /// <summary>
     50        /// The values of the slider.
     51        /// </summary>
     52        public O[] Options;
     53        /// <summary>
     54        /// Private member for Slide.
     55        /// </summary>
     56        private float slide;
     57        /// <summary>
     58        /// The location of the slide.
     59        /// </summary>
    2660        public float Slide
    2761        {
     
    2963            set { setSlideLocation(value); }
    3064        }
     65        /// <summary>
     66        /// The index into the choice array currently active
     67        /// </summary>
    3168        public int Index { get { return (int)Math.Round(slide * (Options.Length - 1)); } }
    32         public O Selected { get { return Options[(int)Math.Round(slide * (Options.Length - 1))]; } }
    33         Vector2 SlideLocation;
     69        /// <summary>
     70        /// The currently selected item.
     71        /// </summary>
     72        public O Selected { get { return Options[Index]; } }
     73        /// <summary>
     74        /// The location of the slide.
     75        /// </summary>
     76        public Vector2 SlideLocation;
     77        /// <summary>
     78        /// The constructor of a slider.
     79        /// </summary>
     80        /// <param name="Location">The location of the slider.</param>
     81        /// <param name="Scale">The size of the slider.</param>
     82        /// <param name="Orientation">The orientation of the slider.</param>
     83        /// <param name="Options">The options of this slider.</param>
     84        /// <param name="slide">The starting location of the slide.</param>
    3485        public Slider(Vector2 Location, Vector2 Scale, float Orientation, O[] Options, float slide = 0.0f)
    3586        {
     
    3788            this.Scale = Scale;
    3889            this.Orientation = Orientation;
    39             this.slide = slide;
     90            Slide = slide;
    4091            this.Options = Options;
    4192        }
     93        /// <summary>
     94        /// Sets the slide location.
     95        /// </summary>
     96        /// <param name="slide">The relative location of the slide along the slider.</param>
    4297        private void setSlideLocation(float slide)
    4398        {
     
    52107                listener.SliderUpdated(this);
    53108        }
    54         public override void Initialize()
    55         {
    56             setSlideLocation(this.slide);
    57             base.Initialize();
    58         }
     109        /// <summary>
     110        /// Updates the slide location based on mouse movements.
     111        /// </summary>
     112        /// <param name="gameState">The current game state.</param>
    59113        public override void Update(GameState gameState)
    60114        {
     
    78132            base.Update(gameState);
    79133        }
     134        /// <summary>
     135        /// Draws the slider to the screen.
     136        /// </summary>
     137        /// <param name="screen">The screen to be drawn to.</param>
    80138        public override void Draw(Screen2D screen)
    81139        {
     
    84142            base.Draw(screen);
    85143        }
    86 
     144        /// <summary>
     145        /// Adds a listener of this slider.
     146        /// </summary>
     147        /// <param name="listener">The listener object.</param>
     148        /// <returns>Returns itself.</returns>
    87149        public Slider<O> addListener(ISliderListener<O> listener)
    88150        {
  • Code/oMaE/oMaE/oMaE.csproj

    r40 r41  
    135135    <Compile Include="GameObject.cs" />
    136136    <Compile Include="GameState.cs" />
     137    <Compile Include="ScreenFactory.cs" />
    137138    <Compile Include="MinigameFactory.cs" />
    138139    <Compile Include="MiniGames\FractionalTraffic.cs" />
Note: See TracChangeset for help on using the changeset viewer.