Pre-compute texture-source Rectangles.

Fixes #14.

GitOrigin-RevId: e09d86a099ac9274a4f3b7dcac9a2222b461dcea
This commit is contained in:
Colin McMillen 2020-02-02 09:04:33 -05:00
parent 5cb3ff9fbd
commit f7f7d5076e

View File

@ -18,63 +18,53 @@ namespace SemiColinGames {
class Tile {
readonly Texture2D texture;
readonly Rectangle textureSource;
public Tile(Texture2D texture, Terrain terrain, Rectangle position) {
this.texture = texture;
Terrain = terrain;
Position = position;
this.textureSource = TextureSource();
}
public Rectangle Position { get; private set; }
public Terrain Terrain { get; private set; }
public void Draw(SpriteBatch spriteBatch, Camera camera) {
int size = World.TileSize;
Vector2 drawPos = new Vector2(Position.Left - camera.Left, Position.Top);
spriteBatch.Draw(texture, drawPos, textureSource, Color.White);
}
private Rectangle TextureSource() {
int size = World.TileSize;
switch (Terrain) {
case Terrain.Grass: {
Rectangle source = new Rectangle(3 * size, 0 * size, size, size);
spriteBatch.Draw(texture, drawPos, source, Color.White);
break;
}
return new Rectangle(3 * size, 0 * size, size, size);
}
case Terrain.GrassL: {
Rectangle source = new Rectangle(2 * size, 0 * size, size, size);
spriteBatch.Draw(texture, drawPos, source, Color.White);
break;
}
return new Rectangle(2 * size, 0 * size, size, size);
}
case Terrain.GrassR: {
Rectangle source = new Rectangle(4 * size, 0 * size, size, size);
spriteBatch.Draw(texture, drawPos, source, Color.White);
break;
}
return new Rectangle(4 * size, 0 * size, size, size);
}
case Terrain.Rock: {
Rectangle source = new Rectangle(3 * size, 1 * size, size, size);
spriteBatch.Draw(texture, drawPos, source, Color.White);
break;
}
return new Rectangle(3 * size, 1 * size, size, size);
}
case Terrain.RockL: {
Rectangle source = new Rectangle(1 * size, 2 * size, size, size);
spriteBatch.Draw(texture, drawPos, source, Color.White);
break;
}
return new Rectangle(1 * size, 2 * size, size, size);
}
case Terrain.RockR: {
Rectangle source = new Rectangle(5 * size, 2 * size, size, size);
spriteBatch.Draw(texture, drawPos, source, Color.White);
break;
}
return new Rectangle(5 * size, 2 * size, size, size);
}
case Terrain.Water: {
Rectangle source = new Rectangle(9 * size, 2 * size, size, size);
spriteBatch.Draw(texture, drawPos, source, Color.White);
break;
}
return new Rectangle(9 * size, 2 * size, size, size);
}
case Terrain.Block: {
Rectangle source = new Rectangle(6 * size, 3 * size, size, size);
spriteBatch.Draw(texture, drawPos, source, Color.White);
break;
}
return new Rectangle(6 * size, 3 * size, size, size);
}
case Terrain.Empty:
default:
break;
return new Rectangle();
}
}
}