diff --git a/Jumpy.Shared/Debug.cs b/Jumpy.Shared/Debug.cs new file mode 100644 index 0000000..79b4401 --- /dev/null +++ b/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 rects = new List(); + 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); + } + } + } +} diff --git a/Jumpy.Shared/Jumpy.Shared.projitems b/Jumpy.Shared/Jumpy.Shared.projitems index 6af77e9..b84ae77 100644 --- a/Jumpy.Shared/Jumpy.Shared.projitems +++ b/Jumpy.Shared/Jumpy.Shared.projitems @@ -10,6 +10,7 @@ + diff --git a/Jumpy.Shared/JumpyGame.cs b/Jumpy.Shared/JumpyGame.cs index 36d3474..8ddf9ae 100644 --- a/Jumpy.Shared/JumpyGame.cs +++ b/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("grassland_bg1"); grasslandBg2 = Content.Load("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 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(); diff --git a/Jumpy.Shared/Player.cs b/Jumpy.Shared/Player.cs index 55ccec0..383e605 100644 --- a/Jumpy.Shared/Player.cs +++ b/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 gamePad) { + public void Update( + GameTime time, History gamePad, List 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 gamePad) { if (gamePad[0].IsButtonDown(Buttons.A) && airState == AirState.Ground) { pose = Pose.Jumping; airState = AirState.Jumping;