Browse Source

add Debug class for displaying rects on-screen

use it to display bounding boxes of player & obstacles

GitOrigin-RevId: 1354637c8a
master
Colin McMillen 4 years ago
parent
commit
6c5c7d4992
  1. 58
      Jumpy.Shared/Debug.cs
  2. 1
      Jumpy.Shared/Jumpy.Shared.projitems
  3. 35
      Jumpy.Shared/JumpyGame.cs
  4. 19
      Jumpy.Shared/Player.cs

58
Jumpy.Shared/Debug.cs

@ -0,0 +1,58 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
namespace Jumpy {
// TODO: add a WriteLine sort of functionality.
static class Debug {
struct DebugRect {
public Rectangle rect;
public Color color;
public DebugRect(Rectangle rect, Color color) {
this.rect = rect;
this.color = color;
}
}
public static bool Enabled;
static List<DebugRect> rects = new List<DebugRect>();
static Texture2D whiteTexture;
public static void Initialize(GraphicsDevice graphics) {
whiteTexture = new Texture2D(graphics, 1, 1);
whiteTexture.SetData(new Color[] { Color.White });
}
public static void Clear() {
rects.Clear();
}
public static void AddRect(Rectangle rect, Color color) {
rects.Add(new DebugRect(rect, color));
}
public static void Draw(SpriteBatch spriteBatch) {
if (!Enabled) {
return;
}
foreach (var debugRect in rects) {
var rect = debugRect.rect;
var color = debugRect.color;
// top side
spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Left, rect.Top, rect.Width, 1), color);
// bottom side
spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Left, rect.Bottom - 1, rect.Width, 1), color);
// left side
spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Left, rect.Top, 1, rect.Height), color);
// right side
spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Right - 1, rect.Top, 1, rect.Height), color);
}
}
}
}

1
Jumpy.Shared/Jumpy.Shared.projitems

@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Camera.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Debug.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FpsCounter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)History.cs" />
<Compile Include="$(MSBuildThisFileDirectory)IDisplay.cs" />

35
Jumpy.Shared/JumpyGame.cs

@ -26,7 +26,6 @@ namespace Jumpy {
FpsCounter fpsCounter = new FpsCounter();
Texture2D grasslandBg1;
Texture2D grasslandBg2;
Texture2D whiteTexture;
Player player;
World world;
@ -42,7 +41,9 @@ namespace Jumpy {
display = (IDisplay) Services.GetService(typeof(IDisplay));
display.Initialize(Window, graphics);
display.SetFullScreen(fullScreen);
Debug.Initialize(GraphicsDevice);
for (int i = 0; i < renderTargets.Length; i++) {
renderTargets[i] = new RenderTarget2D(
GraphicsDevice, Camera.Width, Camera.Height, false /* mipmap */,
@ -63,8 +64,6 @@ namespace Jumpy {
// TODO: move backgrounds into World.
grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
whiteTexture = new Texture2D(GraphicsDevice, 1, 1);
whiteTexture.SetData(new Color[] { Color.White });
}
// Called once per game. Unloads all game content.
@ -74,6 +73,8 @@ namespace Jumpy {
// Updates the game world.
protected override void Update(GameTime gameTime) {
Debug.Clear();
gamePad.Add(GamePad.GetState(PlayerIndex.One));
keyboard.Add(Keyboard.GetState());
@ -87,27 +88,17 @@ namespace Jumpy {
display.SetFullScreen(fullScreen);
}
if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) &&
gamePad[1].IsButtonUp(Buttons.LeftShoulder)) {
Debug.Enabled = !Debug.Enabled;
}
List<Rectangle> collisionTargets = world.CollisionTargets();
player.Update(gameTime, gamePad);
player.Update(gameTime, gamePad, collisionTargets);
base.Update(gameTime);
}
private void DrawRect(SpriteBatch spriteBatch, Rectangle rect, Color color) {
// top side
spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Left, rect.Top, rect.Width, 1), color);
// bottom side
spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Left, rect.Bottom - 1, rect.Width, 1), color);
// left side
spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Left, rect.Top, 1, rect.Height), color);
// right side
spriteBatch.Draw(
whiteTexture, new Rectangle(rect.Right - 1, rect.Top, 1, rect.Height), color);
}
// Called when the game should draw itself.
protected override void Draw(GameTime gameTime) {
// We need to update the FPS counter in Draw() since Update() might get called more
@ -135,9 +126,7 @@ namespace Jumpy {
world.Draw(spriteBatch);
// Draw debug rects.
foreach (var rect in world.CollisionTargets()) {
DrawRect(spriteBatch, rect, Color.Yellow);
}
Debug.Draw(spriteBatch);
// Aaaaand we're done.
spriteBatch.End();

19
Jumpy.Shared/Player.cs

@ -2,6 +2,7 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
namespace Jumpy {
class Player {
@ -32,7 +33,23 @@ namespace Jumpy {
// TODO: refactor input to have a virtual "which directions & buttons were being pressed"
// instead of complicated if-statements in this function.
public void Update(GameTime time, History<GamePadState> gamePad) {
public void Update(
GameTime time, History<GamePadState> gamePad, List<Rectangle> collisionTargets) {
UpdateFromGamePad(time, gamePad);
Rectangle playerBbox =
new Rectangle(position.X - spriteWidth, position.Y - 9, spriteWidth * 2, 28);
Debug.AddRect(playerBbox, Color.Red);
foreach (var rect in collisionTargets) {
if (playerBbox.Intersects(rect)) {
Debug.AddRect(rect, Color.Yellow);
} else {
Debug.AddRect(rect, Color.Green);
}
}
}
void UpdateFromGamePad(GameTime time, History<GamePadState> gamePad) {
if (gamePad[0].IsButtonDown(Buttons.A) && airState == AirState.Ground) {
pose = Pose.Jumping;
airState = AirState.Jumping;

Loading…
Cancel
Save