add World class & refactor other things appropriately

GitOrigin-RevId: 9e72fe95272da1a8e9d50ffe147ae13742547ed4
This commit is contained in:
Colin McMillen 2019-12-11 16:57:30 -05:00
parent 3af02981c4
commit fb2d0e8a6d
4 changed files with 118 additions and 17 deletions

View File

@ -6,7 +6,7 @@
<SharedGUID>2785994a-a14f-424e-8e77-2e464d28747f</SharedGUID> <SharedGUID>2785994a-a14f-424e-8e77-2e464d28747f</SharedGUID>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Label="Configuration"> <PropertyGroup Label="Configuration">
<Import_RootNamespace>Jumpy.Shared</Import_RootNamespace> <Import_RootNamespace>Jumpy</Import_RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Camera.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Camera.cs" />
@ -16,5 +16,6 @@
<Compile Include="$(MSBuildThisFileDirectory)KeyboardInput.cs" /> <Compile Include="$(MSBuildThisFileDirectory)KeyboardInput.cs" />
<Compile Include="$(MSBuildThisFileDirectory)JumpyGame.cs" /> <Compile Include="$(MSBuildThisFileDirectory)JumpyGame.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Player.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Player.cs" />
<Compile Include="$(MSBuildThisFileDirectory)World.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -24,10 +24,12 @@ namespace Jumpy {
History<GamePadState> gamePad = new History<GamePadState>(2); History<GamePadState> gamePad = new History<GamePadState>(2);
FpsCounter fpsCounter = new FpsCounter(); FpsCounter fpsCounter = new FpsCounter();
Player player;
Texture2D grassland;
Texture2D grasslandBg1; Texture2D grasslandBg1;
Texture2D grasslandBg2; Texture2D grasslandBg2;
Texture2D whiteTexture;
Player player;
World world;
public JumpyGame() { public JumpyGame() {
graphics = new GraphicsDeviceManager(this); graphics = new GraphicsDeviceManager(this);
@ -54,10 +56,14 @@ namespace Jumpy {
protected override void LoadContent() { protected override void LoadContent() {
spriteBatch = new SpriteBatch(GraphicsDevice); spriteBatch = new SpriteBatch(GraphicsDevice);
font = Content.Load<SpriteFont>("font"); font = Content.Load<SpriteFont>("font");
// TODO: decouple things like Player and World from their textures.
player = new Player(Content.Load<Texture2D>("player_1x")); player = new Player(Content.Load<Texture2D>("player_1x"));
grassland = Content.Load<Texture2D>("grassland"); world = new World(Content.Load<Texture2D>("grassland"));
// TODO: move backgrounds into World.
grasslandBg1 = Content.Load<Texture2D>("grassland_bg1"); grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
grasslandBg2 = Content.Load<Texture2D>("grassland_bg2"); 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. // Called once per game. Unloads all game content.
@ -85,6 +91,21 @@ namespace Jumpy {
base.Update(gameTime); 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. // Called when the game should draw itself.
protected override void Draw(GameTime gameTime) { protected override void Draw(GameTime gameTime) {
// We need to update the FPS counter in Draw() since Update() might get called more // We need to update the FPS counter in Draw() since Update() might get called more
@ -99,7 +120,8 @@ namespace Jumpy {
spriteBatch.Begin(); spriteBatch.Begin();
// Draw background. // 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); Rectangle bgTarget = new Rectangle(0, 0, Camera.Width, Camera.Height);
spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White); spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White); spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
@ -108,17 +130,7 @@ namespace Jumpy {
player.Draw(gameTime, spriteBatch); player.Draw(gameTime, spriteBatch);
// Draw foreground tiles. // Draw foreground tiles.
int size = 16; world.Draw(spriteBatch);
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);
}
// Aaaaand we're done. // Aaaaand we're done.
spriteBatch.End(); spriteBatch.End();

View File

@ -12,7 +12,7 @@ namespace Jumpy {
private Texture2D texture; private Texture2D texture;
private const int spriteSize = 48; private const int spriteSize = 48;
private const int spriteWidth = 7; private const int spriteWidth = 7;
private const int bottomPadding = 5; private const int bottomPadding = 1;
private const int moveSpeed = 200; private const int moveSpeed = 200;
private const int jumpSpeed = 600; private const int jumpSpeed = 600;
private const int gravity = 2000; private const int gravity = 2000;

88
Jumpy.Shared/World.cs Normal file
View File

@ -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);
}
}
}
}
}