Skip to content

Commit

Permalink
Some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoTheLegion committed Sep 27, 2022
1 parent 599abeb commit 2fe7da0
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 38 deletions.
3 changes: 2 additions & 1 deletion Core/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion Core/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
49 changes: 49 additions & 0 deletions Core/EntityGroup.cs
Original file line number Diff line number Diff line change
@@ -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<Entity> _entities;

public EntityGroup(List<Entity> entities)
{
_entities = entities;
}

public EntityGroup(Entity[] entities)
{
this._entities = new List<Entity>(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();
}
}
}
}
1 change: 1 addition & 0 deletions Core/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ public override void Render(ref SpriteBatch _spriteBatch)

public virtual void SetText(string x) => this._text = x;


}
}
27 changes: 16 additions & 11 deletions Game1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ShootingGallery.Core;
using System;
using System.Collections.Generic;
using static System.Formats.Asn1.AsnWriter;

namespace ShootingGallery
{
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down
94 changes: 69 additions & 25 deletions GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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;
}


}
}

0 comments on commit 2fe7da0

Please sign in to comment.