add Debug class for displaying rects on-screen
use it to display bounding boxes of player & obstacles GitOrigin-RevId: 1354637c8ad88e953b44cb3bf0e250aae0546b81
This commit is contained in:
parent
9c8f8b70df
commit
6c5c7d4992
58
Jumpy.Shared/Debug.cs
Normal file
58
Jumpy.Shared/Debug.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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" />
|
||||
|
@ -26,7 +26,6 @@ namespace Jumpy {
|
||||
FpsCounter fpsCounter = new FpsCounter();
|
||||
Texture2D grasslandBg1;
|
||||
Texture2D grasslandBg2;
|
||||
Texture2D whiteTexture;
|
||||
|
||||
Player player;
|
||||
World world;
|
||||
@ -43,6 +42,8 @@ namespace Jumpy {
|
||||
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,25 +88,15 @@ namespace Jumpy {
|
||||
display.SetFullScreen(fullScreen);
|
||||
}
|
||||
|
||||
List<Rectangle> collisionTargets = world.CollisionTargets();
|
||||
player.Update(gameTime, gamePad);
|
||||
|
||||
base.Update(gameTime);
|
||||
if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) &&
|
||||
gamePad[1].IsButtonUp(Buttons.LeftShoulder)) {
|
||||
Debug.Enabled = !Debug.Enabled;
|
||||
}
|
||||
|
||||
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);
|
||||
List<Rectangle> collisionTargets = world.CollisionTargets();
|
||||
player.Update(gameTime, gamePad, collisionTargets);
|
||||
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
// Called when the game should draw itself.
|
||||
@ -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();
|
||||
|
@ -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…
Reference in New Issue
Block a user