diff options
| -rw-r--r-- | DungeonSlime/Content/Fonts/Ariel.spritefont | 4 | ||||
| -rw-r--r-- | DungeonSlime/Game1.cs | 205 |
2 files changed, 131 insertions, 78 deletions
diff --git a/DungeonSlime/Content/Fonts/Ariel.spritefont b/DungeonSlime/Content/Fonts/Ariel.spritefont index bd33ecf..15333a6 100644 --- a/DungeonSlime/Content/Fonts/Ariel.spritefont +++ b/DungeonSlime/Content/Fonts/Ariel.spritefont @@ -11,13 +11,13 @@ with. <!-- Modify this string to change the font that will be imported. --> - <FontName>Arial</FontName> + <FontName>Palatino</FontName> <!-- Size is a float value, measured in points. Modify this value to change the size of the font. --> - <Size>12</Size> + <Size>24</Size> <!-- Spacing is a float value, measured in pixels. Modify this value to change diff --git a/DungeonSlime/Game1.cs b/DungeonSlime/Game1.cs index d84fe25..36d5a13 100644 --- a/DungeonSlime/Game1.cs +++ b/DungeonSlime/Game1.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Formats.Tar; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; @@ -9,130 +7,185 @@ using MonoGameLibrary; namespace DungeonSlime; -internal class Target +internal enum GameState { - //Constants - private static readonly Rectangle SourceRect = new Rectangle(0, 0, 128, 128); - private static readonly float BaseScale = 0.5f; - private static readonly float MaxScale = 1.5f; + MenuState, + PlayingState, + GameOverState +} - //Fields - private Vector2 _position = Vector2.Zero; - private float _scale = 1f; - private readonly Texture2D _logoTexture; +internal class GameStateMachine +{ + private GameState _state; - public Target(Texture2D logoTexture, Random random, int windowWidth, int windowHeight) + public GameStateMachine(GameState startState) { - _logoTexture = logoTexture; - Randomize(random, windowWidth, windowHeight); + _state = startState; } - public void Randomize(Random random, int windowWidth, int windowHeight) + public GameState State { - _position = - new Vector2((float)random.NextDouble(), (float)random.NextDouble()) * new Vector2(windowWidth, windowHeight); - _scale = BaseScale + (MaxScale - BaseScale) * random.NextSingle(); + get => _state; + + set + { + if (IsValidStateTransition(value)) + { + _state = value; + } + else + { + throw new InvalidOperationException($"Cannot go from {State} to {value}!"); + } + } } - public virtual bool IsHit(MouseState mouseState) + private bool IsValidStateTransition(GameState targetState) { - float mouseX = mouseState.Position.X; - float mouseY = mouseState.Position.Y; + switch (State) + { + case GameState.MenuState: + return targetState == GameState.PlayingState; + case GameState.PlayingState: + return targetState == GameState.GameOverState; + default: //If game is over + return targetState == GameState.MenuState || targetState == GameState.PlayingState; + } + } +} - return mouseState.LeftButton == ButtonState.Pressed && - _position.X <= mouseX && mouseX <= _position.X + SourceRect.Width * _scale && - _position.Y <= mouseY && mouseY <= _position.Y + SourceRect.Height * _scale; +internal class MouseInputManager +{ + private MouseState _currentState; + private MouseState _pastState; + public MouseState MouseState { get => _currentState; } + + public MouseInputManager(MouseState startMouseState) + { + _pastState = _currentState = startMouseState; } - public void Draw(SpriteBatch _spriteBatch) + public bool IsMouseClicked { - _spriteBatch.Draw( - _logoTexture, - _position, - SourceRect, - Color.White, - 0f, - Vector2.Zero, - _scale, - SpriteEffects.None, - 0f - ); + get + { + if (_currentState.LeftButton == ButtonState.Pressed && _pastState.LeftButton == ButtonState.Released) + { + return true; + } + + return false; + } + } + + public void Update(MouseState mouseState) + { + _pastState = _currentState; + _currentState = mouseState; } } public class Game1 : Core { - private static readonly int NumTargets = 10; + private GameStateMachine _stateMachine; + private SpriteFont _arial; + private MouseInputManager _mouseInput; - private Texture2D _logo; - private List<Target> targets = []; - private int _score = 0; - private Random _random = new Random(); - private SpriteFont _arielFont; - - public Game1(): base("Dungeon Slime", 1280, 720, false) + public Game1() : base("Reaction Test", 1280, 720, false) { - + } protected override void Initialize() { base.Initialize(); - for (var i = 0; i < NumTargets; i++) - { - targets.Add(new Target( - _logo, - _random, - Window.ClientBounds.Width, - Window.ClientBounds.Height - )); - } + _stateMachine = new GameStateMachine(GameState.MenuState); + _mouseInput = new MouseInputManager(Mouse.GetState()); } protected override void LoadContent() { - _logo = Content.Load<Texture2D>("Images/logo"); - _arielFont = Content.Load<SpriteFont>("Fonts/Ariel"); + _arial = Content.Load<SpriteFont>("Fonts/Ariel"); } protected override void Update(GameTime gameTime) { - if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) - Exit(); + base.Update(gameTime); + _mouseInput.Update(Mouse.GetState()); - foreach (var target in targets) + switch (_stateMachine.State) { - if (target.IsHit(Mouse.GetState())) - { - target.Randomize(_random, Window.ClientBounds.Width, Window.ClientBounds.Height); - _score++; + case GameState.MenuState: + UpdateMenu(); + break; + } + } - //Can't hit multiple targets at once + protected override void Draw(GameTime gameTime) + { + switch (_stateMachine.State) + { + case GameState.MenuState: + DrawMenu(); + break; + default: + GraphicsDevice.Clear(Color.CornflowerBlue); break; - } } + base.Draw(gameTime); + } - base.Update(gameTime); + private void UpdateMenu() + { + if (_mouseInput.IsMouseClicked) + { + _stateMachine.State = GameState.PlayingState; + } } - protected override void Draw(GameTime gameTime) + private void DrawMenu() { - GraphicsDevice.Clear(Color.CornflowerBlue); + GraphicsDevice.Clear(Color.Green); SpriteBatch.Begin( - sortMode: SpriteSortMode.BackToFront, samplerState: SamplerState.PointClamp ); - SpriteBatch.DrawString(_arielFont, $"{_score}", Vector2.Zero, Color.Green); - foreach (var target in targets) - { - target.Draw(SpriteBatch); - } + //Draw the menu header + string header = "Welcome to the reaction time test!"; + SpriteBatch.DrawString( + _arial, + header, + new Vector2( + Window.ClientBounds.Width, + Window.ClientBounds.Height + ) * 0.5f, + Color.White, + 0f, + _arial.MeasureString(header) * 0.5f, + 1.5f, + SpriteEffects.None, + 0f + ); - SpriteBatch.End(); + //Draw the menu subheader + string subheader = "Click to play!"; + SpriteBatch.DrawString( + _arial, + subheader, + new Vector2( + Window.ClientBounds.Width * 0.5f, + Window.ClientBounds.Height * 0.6f + ), + Color.White, + 0f, + _arial.MeasureString(subheader) * 0.5f, + 0.75f, + SpriteEffects.None, + 0f + ); - base.Draw(gameTime); + SpriteBatch.End(); } } |
