Pre-compute texture-source Rectangles.
Fixes #14. GitOrigin-RevId: e09d86a099ac9274a4f3b7dcac9a2222b461dcea
This commit is contained in:
parent
5cb3ff9fbd
commit
f7f7d5076e
@ -18,63 +18,53 @@ namespace SemiColinGames {
|
|||||||
|
|
||||||
class Tile {
|
class Tile {
|
||||||
readonly Texture2D texture;
|
readonly Texture2D texture;
|
||||||
|
readonly Rectangle textureSource;
|
||||||
|
|
||||||
public Tile(Texture2D texture, Terrain terrain, Rectangle position) {
|
public Tile(Texture2D texture, Terrain terrain, Rectangle position) {
|
||||||
this.texture = texture;
|
this.texture = texture;
|
||||||
Terrain = terrain;
|
Terrain = terrain;
|
||||||
Position = position;
|
Position = position;
|
||||||
|
this.textureSource = TextureSource();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Rectangle Position { get; private set; }
|
public Rectangle Position { get; private set; }
|
||||||
public Terrain Terrain { get; private set; }
|
public Terrain Terrain { get; private set; }
|
||||||
|
|
||||||
public void Draw(SpriteBatch spriteBatch, Camera camera) {
|
public void Draw(SpriteBatch spriteBatch, Camera camera) {
|
||||||
int size = World.TileSize;
|
|
||||||
Vector2 drawPos = new Vector2(Position.Left - camera.Left, Position.Top);
|
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) {
|
switch (Terrain) {
|
||||||
case Terrain.Grass: {
|
case Terrain.Grass: {
|
||||||
Rectangle source = new Rectangle(3 * size, 0 * size, size, size);
|
return new Rectangle(3 * size, 0 * size, size, size);
|
||||||
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Terrain.GrassL: {
|
case Terrain.GrassL: {
|
||||||
Rectangle source = new Rectangle(2 * size, 0 * size, size, size);
|
return new Rectangle(2 * size, 0 * size, size, size);
|
||||||
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Terrain.GrassR: {
|
case Terrain.GrassR: {
|
||||||
Rectangle source = new Rectangle(4 * size, 0 * size, size, size);
|
return new Rectangle(4 * size, 0 * size, size, size);
|
||||||
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Terrain.Rock: {
|
case Terrain.Rock: {
|
||||||
Rectangle source = new Rectangle(3 * size, 1 * size, size, size);
|
return new Rectangle(3 * size, 1 * size, size, size);
|
||||||
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Terrain.RockL: {
|
case Terrain.RockL: {
|
||||||
Rectangle source = new Rectangle(1 * size, 2 * size, size, size);
|
return new Rectangle(1 * size, 2 * size, size, size);
|
||||||
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Terrain.RockR: {
|
case Terrain.RockR: {
|
||||||
Rectangle source = new Rectangle(5 * size, 2 * size, size, size);
|
return new Rectangle(5 * size, 2 * size, size, size);
|
||||||
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Terrain.Water: {
|
case Terrain.Water: {
|
||||||
Rectangle source = new Rectangle(9 * size, 2 * size, size, size);
|
return new Rectangle(9 * size, 2 * size, size, size);
|
||||||
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Terrain.Block: {
|
case Terrain.Block: {
|
||||||
Rectangle source = new Rectangle(6 * size, 3 * size, size, size);
|
return new Rectangle(6 * size, 3 * size, size, size);
|
||||||
spriteBatch.Draw(texture, drawPos, source, Color.White);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case Terrain.Empty:
|
case Terrain.Empty:
|
||||||
default:
|
default:
|
||||||
break;
|
return new Rectangle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user