Skip to content

Commit

Permalink
Use ShapeBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Apostolique committed Dec 14, 2023
1 parent 766d22b commit 7aa9c14
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 109 deletions.
31 changes: 15 additions & 16 deletions Game/Board.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Apos.Shapes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;

namespace GameProject {
/// <summary>
/// Goal: Board, not actually sure what it will handle...
/// </summary>
class Board {
public Board(int iWidth, int iHeight) {
Width = iWidth;
Height = iHeight;
public Board(int width, int height) {
Width = width;
Height = height;

Paths = [];
OldPaths = [];
Expand Down Expand Up @@ -225,27 +224,27 @@ private HashSet<Tile> FindNeighbors(Tile t) {

return neighbors;
}
public void Draw(SpriteBatch s) {
s.FillRectangle(new Rectangle(0, 0, GridSize * Width, GridSize * Height), new Color(15, 15, 15));
public void Draw(ShapeBatch sb) {
sb.FillRectangle(new Vector2(0, 0), new Vector2(GridSize * Width, GridSize * Height), new Color(15, 15, 15));

for (int i = 1; i < FullWidth - 1; i++) {
for (int j = 1; j < FullHeight - 1; j++) {
_tiles[i, j].Draw(s, Color.White);
_tiles[i, j].Draw(sb, Color.White);
}
}
Board.DrawPaths(s, OldPaths);
Board.DrawPathLines(s, OldPaths);
Board.DrawPaths(s, Paths);
Board.DrawPathLines(s, Paths);
DrawPaths(sb, OldPaths);
DrawPathLines(sb, OldPaths);
DrawPaths(sb, Paths);
DrawPathLines(sb, Paths);
}
public static void DrawPaths(SpriteBatch s, List<Path> paths) {
public static void DrawPaths(ShapeBatch sb, List<Path> paths) {
foreach (Path p in paths) {
p.Draw(s);
p.Draw(sb);
}
}
public static void DrawPathLines(SpriteBatch s, List<Path> paths) {
public static void DrawPathLines(ShapeBatch sb, List<Path> paths) {
foreach (Path p in paths) {
p.DrawLine(s);
p.DrawLine(sb);
}
}

Expand Down
52 changes: 31 additions & 21 deletions Game/GameRoot.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Apos.Input;
using Apos.Shapes;
using FontStashSharp;
using GameProject.Towers;
using GameProject.UI;
Expand All @@ -11,7 +12,9 @@
namespace GameProject {
public class GameRoot : Game {
public GameRoot() {
_graphics = new GraphicsDeviceManager(this);
_graphics = new GraphicsDeviceManager(this) {
GraphicsProfile = GraphicsProfile.HiDef
};
Content.RootDirectory = "Content";
IsMouseVisible = true;
Window.AllowUserResizing = true;
Expand All @@ -34,6 +37,7 @@ protected override void Initialize() {
protected override void LoadContent() {
InputHelper.Setup(this);
_s = new SpriteBatch(GraphicsDevice);
_sb = new ShapeBatch(GraphicsDevice, Content);
_renderTarget01 = new RenderTarget2D(GraphicsDevice, Utility.Board.GridSize * Utility.Board.Width, Utility.Board.GridSize * Utility.Board.Height);
Utility.AssetManager = new AssetManager(Content);
Utility.AssetManager.PlayMusic("break_space", 0.3F);
Expand Down Expand Up @@ -79,7 +83,7 @@ protected override void Update(GameTime gameTime) {
}
protected override void Draw(GameTime gameTime) {
if (Utility.CurrentGameState == Utility.GameState.Playing) {
DrawPlayingState(_s);
DrawPlayingState();
}
else if (Utility.CurrentGameState == Utility.GameState.GameOver) {
GraphicsDevice.Clear(Color.Black);
Expand Down Expand Up @@ -141,45 +145,50 @@ private void UpdatePlayingState(GameTime gameTime) {
}
*/
}
private void DrawPlayingState(SpriteBatch spriteBatch) {
private void DrawPlayingState() {
GraphicsDevice.SetRenderTarget(_renderTarget01);
GraphicsDevice.Clear(Color.Black);
// TODO: Add your drawing code here
spriteBatch.Begin();
Utility.Board.Draw(spriteBatch);
_sb.Begin();
Utility.Board.Draw(_sb);
if (_latestHoveredOverTower != null)
HUD.DrawRangeIndicators(spriteBatch, new Point(_latestHoveredOverTower.X, _latestHoveredOverTower.Y), _latestHoveredOverTower);
HUD.DrawRangeIndicators(_sb, new Point(_latestHoveredOverTower.X, _latestHoveredOverTower.Y), _latestHoveredOverTower);

// Highlight needs to be drawn before the actual towers
if (_previewTower != null) {
if (IsWithinDimensions())
HUD.DrawPlacementIndicator(spriteBatch, _previewTower, IsValidTileForTower(Utility.MouseToGameGrid().X, Utility.MouseToGameGrid().Y));
_selectedTower!.DrawSelectionHighlight(spriteBatch);
HUD.DrawPlacementIndicator(_sb, _previewTower, IsValidTileForTower(Utility.MouseToGameGrid().X, Utility.MouseToGameGrid().Y));
_selectedTower!.DrawSelectionHighlight(_sb);
}
foreach (Tower t in Utility.TowerList) t.Draw(spriteBatch);
foreach (Tower t in Utility.TowerList) t.Draw(_sb);
_sb.End();
_s.Begin();
foreach (Tower t in Utility.TowerList) t.DrawHotkey(_s);
foreach (Path p in Utility.Board.Paths) {
p.DrawMinions(spriteBatch);
p.DrawMinions(_s);
}
_s.End();
_sb.Begin();
// Draw projectiles and fire effect
foreach(Tower t in Utility.TowerList) {
if (t is Shotgun) (t as Shotgun)!.DrawProjectiles(spriteBatch);
else if (t is FlameThrower) (t as FlameThrower)!.DrawFireEffect(spriteBatch);
if (t is Shotgun) (t as Shotgun)!.DrawProjectiles(_sb);
else if (t is FlameThrower) (t as FlameThrower)!.DrawFireEffect(_sb);
}
spriteBatch.End();
_sb.End();
GraphicsDevice.SetRenderTarget(null);

spriteBatch.Begin();
spriteBatch.Draw(_renderTarget01, new Vector2(0, 0), Color.White);
spriteBatch.End();
_s.Begin();
_s.Draw(_renderTarget01, new Vector2(0, 0), Color.White);
_s.End();
// Draw sideBar
spriteBatch.Begin();
if (_latestHoveredOverTower != null) _sidebarUI.DrawTowerInfo(spriteBatch, _latestHoveredOverTower);
_sidebarUI.Draw(spriteBatch);
spriteBatch.End();
_s.Begin();
if (_latestHoveredOverTower != null) _sidebarUI.DrawTowerInfo(_s, _latestHoveredOverTower);
_sidebarUI.Draw(_s);
_s.End();
_latestHoveredOverTower = null;
}

public bool IsWithinDimensions() {
public static bool IsWithinDimensions() {
if (Utility.MouseToGameGrid().X >= Utility.Board.FullWidth - 1 || Utility.MouseToGameGrid().X <= 0)
return false;
else if (Utility.MouseToGameGrid().Y >= Utility.Board.FullHeight - 1 || Utility.MouseToGameGrid().Y <= 0)
Expand Down Expand Up @@ -299,6 +308,7 @@ private static void AddTower(int x, int y, Utility.TowerType type, int HotKeyNum
private const int _startingLives = 10;
readonly GraphicsDeviceManager _graphics;
SpriteBatch _s = null!;
ShapeBatch _sb = null!;
RenderTarget2D _renderTarget01 = null!;
Tower? _latestHoveredOverTower;
HUD _hud = null!;
Expand Down
27 changes: 13 additions & 14 deletions Game/Path.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using Apos.Shapes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;

namespace GameProject {
/// <summary>
Expand Down Expand Up @@ -123,51 +123,50 @@ public void Update(GameTime gameTime) {
}
}

public void Draw(SpriteBatch s) {
public void Draw(ShapeBatch sb) {
if (Sequence == Animation.Spawn) {
for (int i = 0; i < _pathsShown; i++) {
DrawPathTile(s, i);
DrawPathTile(sb, i);
}
} else if (Sequence == Animation.Despawn) {
for (int i = _pathsShown; i < pathway.Count; i++) {
DrawPathTile(s, i);
DrawPathTile(sb, i);
}
} else {
for (int i = 0; i < pathway.Count; i++) {
DrawPathTile(s, i);
DrawPathTile(sb, i);
}
}
}
public void DrawLine(SpriteBatch s) {
public void DrawLine(ShapeBatch sb) {
if (Sequence == Animation.Spawn) {
for (int i = 1; i < _pathsShown; i++) {
DrawPathLine(s, i);
DrawPathLine(sb, i);
}
} else if (Sequence == Animation.Despawn) {
for (int i = _pathsShown + 1; i < pathway.Count; i++) {
DrawPathLine(s, i);
DrawPathLine(sb, i);
}
} else {
for (int i = 1; i < pathway.Count; i++) {
DrawPathLine(s, i);
DrawPathLine(sb, i);
}
}
}
public void DrawMinions(SpriteBatch s) {
foreach (Minion m in MinionList)
m.Draw(s);
}
private void DrawPathTile(SpriteBatch s, int i) {
s.FillRectangle(new Rectangle(Utility.GameToScreen(pathway[i].X), Utility.GameToScreen(pathway[i].Y), Utility.Board.GridSize, Utility.Board.GridSize), new Color(19, 59, 131));
private void DrawPathTile(ShapeBatch sb, int i) {
sb.FillRectangle(new Vector2(Utility.GameToScreen(pathway[i].X), Utility.GameToScreen(pathway[i].Y)), new Vector2(Utility.Board.GridSize, Utility.Board.GridSize), new Color(19, 59, 131));
}
private void DrawPathLine(SpriteBatch s, int i) {
private void DrawPathLine(ShapeBatch sb, int i) {
float x1 = Utility.GameToScreen(pathway[i].X + 0.5f);
float y1 = Utility.GameToScreen(pathway[i].Y + 0.5f);
float x2 = Utility.GameToScreen(pathway[i - 1].X + 0.5f);
float y2 = Utility.GameToScreen(pathway[i - 1].Y + 0.5f);

s.DrawLine(x1, y1, x2, y2, Color.Black, Utility.Board.GridSize * 0.15f);
s.DrawLine(x1, y1, x2, y2, Color.White, Utility.Board.GridSize * 0.1f);
sb.DrawLine(new Vector2(x1, y1), new Vector2(x2, y2), Utility.Board.GridSize * 0.1f, Color.White, Color.Black, Utility.Board.GridSize * 0.001f);
}

private readonly Spawner _spawner;
Expand Down
7 changes: 3 additions & 4 deletions Game/Tile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Apos.Shapes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;

namespace GameProject {
/// <summary>
Expand All @@ -10,8 +9,8 @@ class Tile(int x, int y) {
public int X = x;
public int Y = y;

public void Draw(SpriteBatch s, Color c) {
s.DrawRectangle(new RectangleF(Utility.GameToScreen(X), Utility.GameToScreen(Y), Utility.Board.GridSize, Utility.Board.GridSize), c, 1);
public void Draw(ShapeBatch sb, Color c) {
sb.BorderRectangle(new Vector2(Utility.GameToScreen(X), Utility.GameToScreen(Y)), new Vector2(Utility.Board.GridSize, Utility.Board.GridSize), c, 1f);
}
}
}
7 changes: 2 additions & 5 deletions Game/Towers/FlameThrower.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using Apos.Shapes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameProject.Towers {
class FlameThrower : Tower {
Expand Down Expand Up @@ -37,11 +37,8 @@ public override void Update(GameTime gameTime) {
f.MarkedForDeletion = true;
}
}
public override void Draw(SpriteBatch s) {
base.Draw(s);
}

public void DrawFireEffect(SpriteBatch s) {
public void DrawFireEffect(ShapeBatch s) {
foreach (Projectile f in _flameList)
f.Draw(s);
}
Expand Down
12 changes: 5 additions & 7 deletions Game/Towers/Projectile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;
using Apos.Shapes;
using Microsoft.Xna.Framework;

namespace GameProject.Towers {
class Projectile(Vector2 position, Vector2 direction, Color color, float speed) {
Expand All @@ -9,10 +8,9 @@ public void Update(GameTime gameTime) {
_distanceTraveled += _speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}

public void Draw(SpriteBatch s) {
Point2 tempP = new Point2(Utility.GameToScreen(_position.X), Utility.GameToScreen(_position.Y));
s.DrawCircle(new CircleF(tempP, Radius * Utility.Board.GridSize), 8, _color, Radius * Utility.Board.GridSize);
s.DrawCircle(new CircleF(tempP, Radius * Utility.Board.GridSize), 8, Color.Black, 1);
public void Draw(ShapeBatch sb) {
Vector2 tempP = new Vector2(Utility.GameToScreen(_position.X), Utility.GameToScreen(_position.Y));
sb.DrawCircle(tempP, Radius * Utility.Board.GridSize, _color, Color.Black, 1f);
}

public float DistanceTraveled => _distanceTraveled;
Expand Down
10 changes: 3 additions & 7 deletions Game/Towers/Shotgun.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using Apos.Shapes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameProject.Towers {
class Shotgun : Tower {
Expand Down Expand Up @@ -51,13 +51,9 @@ private void BulletCollisionChecker() {
}
}

public override void Draw(SpriteBatch s) {
base.Draw(s);
}

public void DrawProjectiles(SpriteBatch s) {
public void DrawProjectiles(ShapeBatch sb) {
foreach (Projectile b in _bulletList)
b.Draw(s);
b.Draw(sb);
}

private void ShootAtTargetedMinion() {
Expand Down
9 changes: 4 additions & 5 deletions Game/Towers/Sniper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Apos.Shapes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGame.Extended;

namespace GameProject.Towers {
class Sniper : Tower {
Expand All @@ -28,10 +27,10 @@ public override void Update(GameTime gameTime) {
}
base.Update(gameTime);
}
public override void Draw(SpriteBatch s) {
base.Draw(s);
public override void Draw(ShapeBatch sb) {
base.Draw(sb);
if (!_disabled && !(_targetedMinion == null))
s.DrawLine(Utility.GameToScreen(X) + Utility.Board.GridSize / 2, Utility.GameToScreen(Y) + Utility.Board.GridSize / 2, Utility.GameToScreen(_targetedMinion.Position.X), Utility.GameToScreen(_targetedMinion.Position.Y), Color.Red, 2f);
sb.BorderLine(new Vector2(Utility.GameToScreen(X) + Utility.Board.GridSize / 2, Utility.GameToScreen(Y) + Utility.Board.GridSize / 2), new Vector2(Utility.GameToScreen(_targetedMinion.Position.X), Utility.GameToScreen(_targetedMinion.Position.Y)), 1f, Color.Red, 2f);
}
private void TargetMinion() {
_targetedMinion = null;
Expand Down
Loading

0 comments on commit 7aa9c14

Please sign in to comment.