From 2fe7da029009d777a15fb7c94f9d8a83ad31f77d Mon Sep 17 00:00:00 2001 From: LeoTheLegion Date: Tue, 27 Sep 2022 13:41:52 -0400 Subject: [PATCH] Some code cleanup --- Core/Button.cs | 3 +- Core/Entity.cs | 6 ++- Core/EntityGroup.cs | 49 +++++++++++++++++++++++ Core/Text.cs | 1 + Game1.cs | 27 +++++++------ GameManager.cs | 94 +++++++++++++++++++++++++++++++++------------ 6 files changed, 142 insertions(+), 38 deletions(-) create mode 100644 Core/EntityGroup.cs diff --git a/Core/Button.cs b/Core/Button.cs index 8327d1c..8511a71 100644 --- a/Core/Button.cs +++ b/Core/Button.cs @@ -32,8 +32,9 @@ public Button(string normal, string hover, string font, Vector2 pos, string text this._ishovering = false; } - public void SetOnPress(onPress d){ + public Button SetOnPress(onPress d){ this._onPress = d; + return this; } public override void Update(ref GameTime gameTime) diff --git a/Core/Entity.cs b/Core/Entity.cs index 819ad91..3d18179 100644 --- a/Core/Entity.cs +++ b/Core/Entity.cs @@ -29,7 +29,11 @@ public virtual void Destroy() EntityManagementSystem.Unregister(this); } - public virtual void SetSort(int x) => sort = x; + public virtual Entity SetSort(int x) { + sort = x; + return this; + } + public virtual int GetSort() { return sort; } public virtual void SetActive(bool active) => _active = active; public virtual bool GetActive() { return _active; } diff --git a/Core/EntityGroup.cs b/Core/EntityGroup.cs new file mode 100644 index 0000000..059fcc5 --- /dev/null +++ b/Core/EntityGroup.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ShootingGallery.Core +{ + public class EntityGroup + { + private List _entities; + + public EntityGroup(List entities) + { + _entities = entities; + } + + public EntityGroup(Entity[] entities) + { + this._entities = new List(entities); + } + + public void Add(Entity e) + { + this._entities.Add(e); + } + + public void Remove(Entity e) + { + this._entities.Remove(e); + } + + public void SetActive(bool active) + { + foreach (var e in this._entities) + { + e.SetActive(active); + } + } + + public void Destroy() + { + foreach (var e in this._entities) + { + e.Destroy(); + } + } + } +} diff --git a/Core/Text.cs b/Core/Text.cs index 4387816..fd3d167 100644 --- a/Core/Text.cs +++ b/Core/Text.cs @@ -38,5 +38,6 @@ public override void Render(ref SpriteBatch _spriteBatch) public virtual void SetText(string x) => this._text = x; + } } diff --git a/Game1.cs b/Game1.cs index ff42f6b..afff5f7 100644 --- a/Game1.cs +++ b/Game1.cs @@ -4,6 +4,7 @@ using ShootingGallery.Core; using System; using System.Collections.Generic; +using static System.Formats.Asn1.AsnWriter; namespace ShootingGallery { @@ -37,16 +38,17 @@ protected override void Initialize() // TODO: Add your initialization logic here Entity _target, _crossHair, _background; - _target = new Target(new Vector2(300, 300)); - _target.SetSort(1); + _target = new Target(new Vector2(300, 300)) + .SetSort(1); - _crossHair = new Crosshair(); - _crossHair.SetSort(3); + _crossHair = new Crosshair() + .SetSort(3); - _background = new Decorative("sky", new Vector2(0, 0)); - _background.SetSort(-1); + _background = new Decorative("sky", new Vector2(0, 0)) + .SetSort(-1); - Text _score, _timer , _gameoverMessage; + Text _score, _timer, _gameoverMessage,_startMessage; + Button _restartButton, _startButton; _score = new Text("galleryFont", "Score: Null", new Vector2(3, 3)); _score.SetSort(0); @@ -55,12 +57,15 @@ protected override void Initialize() _timer.SetSort(0); _gameoverMessage = new Text("galleryFont", "Game Over - Press Button To Play Again", new Vector2(120, 150)); + _startMessage = new Text("galleryFont", "Welcome to Michael's Shooting Gallery!", new Vector2(120, 150)); - Button _restartButton = new Button("button", "button_hover", "galleryFont", new Vector2(300, 250), "Restart"); + _restartButton = new Button("button", "button_hover", "galleryFont", new Vector2(300, 250), "Restart"); + _startButton = new Button("button", "button_hover", "galleryFont", new Vector2(300, 250), "Play"); - _restartButton.SetOnPress(() => GameManager.RestartRound()); - - GameManager.Init(ref _score, ref _timer, ref _gameoverMessage,ref _restartButton, ref _target); + GameManager.Init(ref _score, ref _timer, + ref _gameoverMessage,ref _restartButton, + ref _startMessage, ref _startButton, + ref _target); base.Initialize(); } diff --git a/GameManager.cs b/GameManager.cs index 57d58ce..d719122 100644 --- a/GameManager.cs +++ b/GameManager.cs @@ -16,10 +16,10 @@ public static class GameManager private const double ROUNDTIME = 10; private static double timer; private static int score = 0; - private static Text _scoreUI, _timerUI, _gameOverMessageUI; // should be events handled - private static Button _restartButton; + private static Text _scoreUI, _timerUI, _gameOverMessageUI, _startMessageUI; // should be events handled + private static Button _restartButton, _startButtonUI; private static Entity _target; - + private static bool started; public static bool isGameOver => timer <= 0; @@ -28,50 +28,94 @@ public static class GameManager public static int GetScore() => score; public static void AddScore(int x) => score += x; - public static void Init(ref Text scoreUI, ref Text timerUI, ref Text gameOverMessageUI, ref Button restartButton , ref Entity target) + internal static void Init(ref Text scoreUI, ref Text timerUI, + ref Text gameoverMessageUI, ref Button restartButtonUI, + ref Text startMessageUI, ref Button startButtonUI, + ref Entity target) { _scoreUI = scoreUI; _timerUI = timerUI; _target = target; - _gameOverMessageUI = gameOverMessageUI; - _restartButton = restartButton; - RestartRound(); + _gameOverMessageUI = gameoverMessageUI; + _restartButton = restartButtonUI.SetOnPress(() => GameManager.RestartRound()); + + _startMessageUI = startMessageUI; + _startButtonUI = startButtonUI.SetOnPress(() => GameManager.StartGame()); + + started = false; + } - timer = 0; + public static void StartGame() + { + started = true; } public static void Update(ref GameTime gameTime) { + if (!started) + ProcessStartMenu(); + else if(isGameOver) + ProcessGameOver(); + else + ProcessGameplay(gameTime); + } + + private static void ProcessGameOver() + { + _startMessageUI.SetActive(false); + _startButtonUI.SetActive(false); + _scoreUI.SetActive(true); + _timerUI.SetActive(true); + _scoreUI.SetText("Score: " + score.ToString()); _timerUI.SetText("Time: " + Math.Ceiling(timer).ToString()); - if (!isGameOver) - { - _target.SetActive(true); - _gameOverMessageUI.SetActive(false); - _restartButton.SetActive(false); + _target.SetActive(false); + _gameOverMessageUI.SetActive(true); + _restartButton.SetActive(true); - timer -= gameTime.ElapsedGameTime.TotalSeconds; + if (Keyboard.GetState().IsKeyDown(Keys.Space)) + RestartRound(); + } - if (timer < 0) - timer = 0; - } - else - { - _target.SetActive(false); - _gameOverMessageUI.SetActive(true); - _restartButton.SetActive(true); + private static void ProcessGameplay(GameTime gameTime) + { + _startMessageUI.SetActive(false); + _startButtonUI.SetActive(false); + _scoreUI.SetActive(true); + _timerUI.SetActive(true); + + _scoreUI.SetText("Score: " + score.ToString()); + _timerUI.SetText("Time: " + Math.Ceiling(timer).ToString()); + + _target.SetActive(true); + _gameOverMessageUI.SetActive(false); + _restartButton.SetActive(false); - if (Keyboard.GetState().IsKeyDown(Keys.Space)) - RestartRound(); + timer -= gameTime.ElapsedGameTime.TotalSeconds; - } + if (timer < 0) + timer = 0; } + + private static void ProcessStartMenu() + { + _target.SetActive(false); + _gameOverMessageUI.SetActive(false); + _restartButton.SetActive(false); + _scoreUI.SetActive(false); + _timerUI.SetActive(false); + + _startMessageUI.SetActive(true); + _startButtonUI.SetActive(true); + } + public static void RestartRound() { timer = ROUNDTIME; score = 0; } + } }