From fb2d0e8a6dfae63cbc259000b0d3d7e17ed358cc Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Wed, 11 Dec 2019 16:57:30 -0500 Subject: [PATCH] add World class & refactor other things appropriately GitOrigin-RevId: 9e72fe95272da1a8e9d50ffe147ae13742547ed4 --- Jumpy.Shared/Jumpy.Shared.projitems | 3 +- Jumpy.Shared/JumpyGame.cs | 42 +++++++++----- Jumpy.Shared/Player.cs | 2 +- Jumpy.Shared/World.cs | 88 +++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 17 deletions(-) create mode 100644 Jumpy.Shared/World.cs diff --git a/Jumpy.Shared/Jumpy.Shared.projitems b/Jumpy.Shared/Jumpy.Shared.projitems index e37ecde..6af77e9 100644 --- a/Jumpy.Shared/Jumpy.Shared.projitems +++ b/Jumpy.Shared/Jumpy.Shared.projitems @@ -6,7 +6,7 @@ 2785994a-a14f-424e-8e77-2e464d28747f - Jumpy.Shared + Jumpy @@ -16,5 +16,6 @@ + \ No newline at end of file diff --git a/Jumpy.Shared/JumpyGame.cs b/Jumpy.Shared/JumpyGame.cs index 58c8e3c..eb4b9a6 100644 --- a/Jumpy.Shared/JumpyGame.cs +++ b/Jumpy.Shared/JumpyGame.cs @@ -24,10 +24,12 @@ namespace Jumpy { History gamePad = new History(2); FpsCounter fpsCounter = new FpsCounter(); - Player player; - Texture2D grassland; Texture2D grasslandBg1; Texture2D grasslandBg2; + Texture2D whiteTexture; + + Player player; + World world; public JumpyGame() { graphics = new GraphicsDeviceManager(this); @@ -54,10 +56,14 @@ namespace Jumpy { protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); font = Content.Load("font"); + // TODO: decouple things like Player and World from their textures. player = new Player(Content.Load("player_1x")); - grassland = Content.Load("grassland"); + world = new World(Content.Load("grassland")); + // 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. @@ -85,6 +91,21 @@ namespace Jumpy { 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 @@ -99,7 +120,8 @@ namespace Jumpy { spriteBatch.Begin(); // Draw background. - Rectangle bgSource = new Rectangle(0, grasslandBg1.Height - Camera.Height, Camera.Width, Camera.Height); + Rectangle bgSource = new Rectangle( + 0, grasslandBg1.Height - Camera.Height, Camera.Width, Camera.Height); Rectangle bgTarget = new Rectangle(0, 0, Camera.Width, Camera.Height); spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White); spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White); @@ -108,17 +130,7 @@ namespace Jumpy { player.Draw(gameTime, spriteBatch); // Draw foreground tiles. - int size = 16; - for (int i = 0; i < Camera.Width / size; i++) { - Rectangle source = new Rectangle(3 * size, 0 * size, size, size); - Vector2 drawPos = new Vector2(i * size, Camera.Height - size * 2); - spriteBatch.Draw(grassland, drawPos, source, Color.White); - } - for (int i = 0; i < Camera.Width / size; i++) { - Rectangle source = new Rectangle(3 * size, 1 * size, size, size); - Vector2 drawPos = new Vector2(i * size, Camera.Height - size); - spriteBatch.Draw(grassland, drawPos, source, Color.White); - } + world.Draw(spriteBatch); // Aaaaand we're done. spriteBatch.End(); diff --git a/Jumpy.Shared/Player.cs b/Jumpy.Shared/Player.cs index d44a99d..55ccec0 100644 --- a/Jumpy.Shared/Player.cs +++ b/Jumpy.Shared/Player.cs @@ -12,7 +12,7 @@ namespace Jumpy { private Texture2D texture; private const int spriteSize = 48; private const int spriteWidth = 7; - private const int bottomPadding = 5; + private const int bottomPadding = 1; private const int moveSpeed = 200; private const int jumpSpeed = 600; private const int gravity = 2000; diff --git a/Jumpy.Shared/World.cs b/Jumpy.Shared/World.cs new file mode 100644 index 0000000..9dd61f4 --- /dev/null +++ b/Jumpy.Shared/World.cs @@ -0,0 +1,88 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System.Collections.Generic; + +namespace Jumpy { + enum Terrain { + Empty, + Grass, + Rock + } + + class Tile { + Texture2D texture; + Terrain terrain; + Rectangle position; + + public Tile(Texture2D texture, Terrain terrain, Rectangle position) { + this.texture = texture; + this.terrain = terrain; + this.position = position; + } + + public Rectangle Position { get; } + public Terrain Terrain { get; } + + public void Draw(SpriteBatch spriteBatch) { + int size = World.TileSize; + switch (terrain) { + case Terrain.Grass: { + // TODO: hold these rectangles statically instead of making them anew constantly. + Rectangle source = new Rectangle(3 * size, 0 * size, size, size); + spriteBatch.Draw(texture, position, source, Color.White); + break; + } + case Terrain.Rock: { + Rectangle source = new Rectangle(3 * size, 1 * size, size, size); + spriteBatch.Draw(texture, position, source, Color.White); + break; + } + case Terrain.Empty: + default: + break; + } + } + } + + class World { + + public const int TileSize = 16; + int width; + int height; + Tile[,] tiles; + + public int Width { get; } + public int Height { get; } + + public World(Texture2D texture) { + width = Camera.Width / TileSize; + height = Camera.Height / TileSize + 1; + tiles = new Tile[width, height]; + for (int j = 0; j < height; j++) { + for (int i = 0; i < width; i++) { + Terrain terrain; + if (j < height - 3) { + terrain = Terrain.Empty; + } else if (j == height - 3) { + terrain = Terrain.Grass; + } else { + terrain = Terrain.Rock; + } + if (j == 4 && 11 < i && i < 15) { + terrain = Terrain.Grass; + } + var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize); + tiles[i, j] = new Tile(texture, terrain, position); + } + } + } + + public void Draw(SpriteBatch spriteBatch) { + for (int j = 0; j < height; j++) { + for (int i = 0; i < width; i++) { + tiles[i, j].Draw(spriteBatch); + } + } + } + } +}