Make TextureRef class for holding textures.

Use it in World so that all Terrain-specific configuration can be specified in
one place.

GitOrigin-RevId: 31acf292ae6e438f609a7396e78555e1db9b744e
This commit is contained in:
Colin McMillen 2020-02-20 16:36:54 -05:00
parent 0e164f7b75
commit e72b8999e4
4 changed files with 95 additions and 137 deletions

View File

@ -237,7 +237,7 @@ namespace SemiColinGames {
Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset); Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset);
SpriteEffects effect = Facing == 1 ? SpriteEffects effect = Facing == 1 ?
SpriteEffects.FlipHorizontally : SpriteEffects.None; SpriteEffects.FlipHorizontally : SpriteEffects.None;
spriteBatch.Draw(Textures.Player, position.ToVector2(), textureSource, Color.White, 0f, spriteBatch.Draw(Textures.Player.Get(), position.ToVector2(), textureSource, Color.White, 0f,
spriteCenter, Vector2.One, effect, 0f); spriteCenter, Vector2.One, effect, 0f);
} }
} }

View File

@ -60,10 +60,10 @@ namespace SemiColinGames {
float xScale = 1f / 16; float xScale = 1f / 16;
for (int i = 0; i < Textures.Backgrounds.Length; i++) { for (int i = 0; i < Textures.Backgrounds.Length; i++) {
int yOffset = Textures.Backgrounds[i].Height - camera.Height - 24; int yOffset = Textures.Backgrounds[i].Get().Height - camera.Height - 24;
Rectangle bgSource = new Rectangle( Rectangle bgSource = new Rectangle(
(int) (camera.Left * xScale), yOffset, camera.Width, camera.Height); (int) (camera.Left * xScale), yOffset, camera.Width, camera.Height);
spriteBatch.Draw(Textures.Backgrounds[i], bgTarget, bgSource, Color.White); spriteBatch.Draw(Textures.Backgrounds[i].Get(), bgTarget, bgSource, Color.White);
xScale *= 2; xScale *= 2;
} }
spriteBatch.End(); spriteBatch.End();

View File

@ -1,47 +1,64 @@
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
namespace SemiColinGames { namespace SemiColinGames {
class TextureRef {
private static readonly List<TextureRef> allTextures = new List<TextureRef>();
public static void LoadAll(ContentManager content) {
foreach (TextureRef texture in allTextures) {
texture.Load(content);
}
}
private readonly string contentPath;
private Texture2D texture;
public TextureRef(string contentPath) {
allTextures.Add(this);
this.contentPath = contentPath;
}
public Texture2D Get() {
return texture;
}
private void Load(ContentManager content) {
texture = content.Load<Texture2D>(contentPath);
}
}
class Textures { class Textures {
public static Texture2D Player;
// Backgrounds are indexed by draw order; the first element should be drawn furthest back.
public static Texture2D[] Backgrounds = new Texture2D[4];
public static Texture2D Cemetery;
public static Texture2D Crypt;
public static Texture2D Dungeon;
public static Texture2D Forest;
public static Texture2D Garden;
public static Texture2D Grassland;
public static Texture2D Ruins;
public static Texture2D Sewer;
public static Texture2D Temple;
public static Texture2D Village;
public static SpriteFont DebugFont; public static SpriteFont DebugFont;
public static TextureRef Player = new TextureRef("sprites/ccg/ninja_female");
// Backgrounds are indexed by draw order; the first element should be drawn furthest back.
public static TextureRef[] Backgrounds = new TextureRef[] {
new TextureRef("backgrounds/szadiart/pf4/background1_day"),
new TextureRef("backgrounds/szadiart/pf4/background2a_day"),
new TextureRef("backgrounds/szadiart/pf4/background3_day"),
new TextureRef("backgrounds/szadiart/pf4/background4_day"),
};
public static TextureRef Cemetery = new TextureRef("tiles/anokolisa/cemetery");
public static TextureRef Crypt = new TextureRef("tiles/anokolisa/crypt");
public static TextureRef Dungeon = new TextureRef("tiles/anokolisa/dungeon");
public static TextureRef Forest = new TextureRef("tiles/anokolisa/forest");
public static TextureRef Garden = new TextureRef("tiles/anokolisa/garden");
public static TextureRef Grassland = new TextureRef("tiles/anokolisa/grassland");
public static TextureRef Ruins = new TextureRef("tiles/anokolisa/ruins");
public static TextureRef Sewer = new TextureRef("tiles/anokolisa/sewer");
public static TextureRef Temple = new TextureRef("tiles/anokolisa/temple");
public static TextureRef Village = new TextureRef("tiles/anokolisa/village");
public static void Load(ContentManager content) { public static void Load(ContentManager content) {
Player = content.Load<Texture2D>("sprites/ccg/ninja_female");
Backgrounds[0] = content.Load<Texture2D>("backgrounds/szadiart/pf4/background1_day");
Backgrounds[1] = content.Load<Texture2D>("backgrounds/szadiart/pf4/background2a_day");
Backgrounds[2] = content.Load<Texture2D>("backgrounds/szadiart/pf4/background3_day");
Backgrounds[3] = content.Load<Texture2D>("backgrounds/szadiart/pf4/background4_day");
Cemetery = content.Load<Texture2D>("tiles/anokolisa/cemetery");
Crypt = content.Load<Texture2D>("tiles/anokolisa/crypt");
Dungeon = content.Load<Texture2D>("tiles/anokolisa/dungeon");
Forest = content.Load<Texture2D>("tiles/anokolisa/forest");
Garden = content.Load<Texture2D>("tiles/anokolisa/garden");
Grassland = content.Load<Texture2D>("tiles/anokolisa/grassland");
Ruins = content.Load<Texture2D>("tiles/anokolisa/ruins");
Sewer = content.Load<Texture2D>("tiles/anokolisa/sewer");
Temple = content.Load<Texture2D>("tiles/anokolisa/temple");
Village = content.Load<Texture2D>("tiles/anokolisa/village");
DebugFont = content.Load<SpriteFont>("font"); DebugFont = content.Load<SpriteFont>("font");
TextureRef.LoadAll(content);
} }
} }
} }

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace SemiColinGames { namespace SemiColinGames {
class Terrain { class Terrain {
public static Terrain FromSymbol(char symbol) { public static Terrain FromSymbol(char symbol) {
@ -17,123 +18,64 @@ namespace SemiColinGames {
private readonly static Dictionary<char, Terrain> mapping = new Dictionary<char, Terrain>(); private readonly static Dictionary<char, Terrain> mapping = new Dictionary<char, Terrain>();
public static Terrain Grass = new Terrain('=', true); public static Terrain Grass = new Terrain('=', true, Textures.Grassland, 3, 0);
public static Terrain GrassL = new Terrain('<', true); public static Terrain GrassL = new Terrain('<', true, Textures.Grassland, 2, 0);
public static Terrain GrassR = new Terrain('>', true); public static Terrain GrassR = new Terrain('>', true, Textures.Grassland, 4, 0);
public static Terrain Rock = new Terrain('.', true); public static Terrain Rock = new Terrain('.', true, Textures.Grassland, 3, 1);
public static Terrain RockL = new Terrain('[', true); public static Terrain RockL = new Terrain('[', true, Textures.Grassland, 1, 2);
public static Terrain RockR = new Terrain(']', true); public static Terrain RockR = new Terrain(']', true, Textures.Grassland, 5, 2);
public static Terrain WaterL = new Terrain('~', false); public static Terrain WaterL = new Terrain('~', false, Textures.Grassland, 9, 2);
public static Terrain WaterR = new Terrain('`', false); public static Terrain WaterR = new Terrain('`', false, Textures.Grassland, 10, 2);
public static Terrain Block = new Terrain('X', true); public static Terrain Block = new Terrain('X', true, Textures.Ruins, 2, 0);
public static Terrain Spike = new Terrain('^', true); public static Terrain Spike = new Terrain('^', true, Textures.Grassland, 11, 8);
public static Terrain Wood = new Terrain('_', true); public static Terrain Wood = new Terrain('_', true, Textures.Grassland, 10, 3);
public static Terrain WoodL = new Terrain('(', true); public static Terrain WoodL = new Terrain('(', true, Textures.Grassland, 9, 3);
public static Terrain WoodR = new Terrain(')', true); public static Terrain WoodR = new Terrain(')', true, Textures.Grassland, 12, 3);
public static Terrain WoodVert = new Terrain('|', false); public static Terrain WoodVert = new Terrain('|', false, Textures.Grassland, 9, 5);
public static Terrain WoodVertL = new Terrain('/', false); public static Terrain WoodVertL = new Terrain('/', false, Textures.Grassland, 9, 4);
public static Terrain WoodVertR = new Terrain('\\', false); public static Terrain WoodVertR = new Terrain('\\', false, Textures.Grassland, 12, 4);
public static Terrain WoodBottom = new Terrain('v', false); public static Terrain WoodBottom = new Terrain('v', false, Textures.Grassland, 10, 5);
public static Terrain FenceL = new Terrain('d', false); public static Terrain FenceL = new Terrain('d', false, Textures.Grassland, 5, 4);
public static Terrain Fence = new Terrain('f', false); public static Terrain Fence = new Terrain('f', false, Textures.Grassland, 6, 4);
public static Terrain FencePost = new Terrain('x', false); public static Terrain FencePost = new Terrain('x', false, Textures.Grassland, 7, 4);
public static Terrain FenceR = new Terrain('b', false); public static Terrain FenceR = new Terrain('b', false, Textures.Grassland, 8, 4);
public static Terrain VineTop = new Terrain('{', false); public static Terrain VineTop = new Terrain('{', false, Textures.Ruins, 12, 5);
public static Terrain VineMid = new Terrain(';', false); public static Terrain VineMid = new Terrain(';', false, Textures.Ruins, 12, 6);
public static Terrain VineBottom = new Terrain('}', false); public static Terrain VineBottom = new Terrain('}', false, Textures.Ruins, 12, 7);
public static Terrain GrassTall = new Terrain('q', false); public static Terrain GrassTall = new Terrain('q', false, Textures.Grassland, 13, 0);
public static Terrain GrassShort = new Terrain('w', false); public static Terrain GrassShort = new Terrain('w', false, Textures.Grassland, 14, 0);
public static Terrain Shoots = new Terrain('e', false); public static Terrain Shoots = new Terrain('e', false, Textures.Grassland, 15, 0);
public static Terrain Bush = new Terrain('r', false); public static Terrain Bush = new Terrain('r', false, Textures.Grassland, 13, 2);
public static Terrain Mushroom = new Terrain('t', false); public static Terrain Mushroom = new Terrain('t', false, Textures.Grassland, 17, 2);
public bool IsObstacle { get; private set; } public bool IsObstacle { get; private set; }
public TextureRef Texture { get; private set; }
public Rectangle TextureSource { get; private set; }
private Terrain(char symbol, bool isObstacle) { private Terrain(char symbol, bool isObstacle, TextureRef texture, int x, int y) {
if (mapping.ContainsKey(symbol)) { if (mapping.ContainsKey(symbol)) {
throw new ArgumentException("already have a terrain with symbol " + symbol); throw new ArgumentException("already have a terrain with symbol " + symbol);
} }
IsObstacle = isObstacle;
mapping[symbol] = this; mapping[symbol] = this;
} IsObstacle = isObstacle;
} Texture = texture;
class TileFactory {
struct TextureSource {
public Texture2D texture;
public Rectangle source;
public TextureSource(Texture2D texture, Rectangle source) {
this.texture = texture;
this.source = source;
}
}
readonly Dictionary<Terrain, TextureSource> terrainToTexture;
public TileFactory() {
terrainToTexture = new Dictionary<Terrain, TextureSource>() {
{ Terrain.GrassL, GetTextureSource(Textures.Grassland, 2, 0) },
{ Terrain.Grass, GetTextureSource(Textures.Grassland, 3, 0) },
{ Terrain.GrassR, GetTextureSource(Textures.Grassland, 4, 0) },
{ Terrain.Rock, GetTextureSource(Textures.Grassland, 3, 1) },
{ Terrain.RockL, GetTextureSource(Textures.Grassland, 1, 2) },
{ Terrain.RockR, GetTextureSource(Textures.Grassland, 5, 2) },
{ Terrain.WaterL, GetTextureSource(Textures.Grassland, 9, 2) },
{ Terrain.WaterR, GetTextureSource(Textures.Grassland, 10, 2) },
{ Terrain.Block, GetTextureSource(Textures.Ruins, 2, 0) },
{ Terrain.Spike, GetTextureSource(Textures.Grassland, 11, 8) },
{ Terrain.Wood, GetTextureSource(Textures.Grassland, 10, 3) },
{ Terrain.WoodL, GetTextureSource(Textures.Grassland, 9, 3) },
{ Terrain.WoodR, GetTextureSource(Textures.Grassland, 12, 3) },
{ Terrain.WoodVert, GetTextureSource(Textures.Grassland, 9, 5) },
{ Terrain.WoodVertL, GetTextureSource(Textures.Grassland, 9, 4) },
{ Terrain.WoodVertR, GetTextureSource(Textures.Grassland, 12, 4) },
{ Terrain.WoodBottom, GetTextureSource(Textures.Grassland, 10, 5) },
{ Terrain.FenceL, GetTextureSource(Textures.Grassland, 5, 4) },
{ Terrain.Fence, GetTextureSource(Textures.Grassland, 6, 4) },
{ Terrain.FencePost, GetTextureSource(Textures.Grassland, 7, 4) },
{ Terrain.FenceR, GetTextureSource(Textures.Grassland, 8, 4) },
{ Terrain.VineTop, GetTextureSource(Textures.Ruins, 12, 5) },
{ Terrain.VineMid, GetTextureSource(Textures.Ruins, 12, 6) },
{ Terrain.VineBottom, GetTextureSource(Textures.Ruins, 12, 7) },
{ Terrain.GrassTall, GetTextureSource(Textures.Grassland, 13, 0) },
{ Terrain.GrassShort, GetTextureSource(Textures.Grassland, 14, 0) },
{ Terrain.Shoots, GetTextureSource(Textures.Grassland, 15, 0) },
{ Terrain.Bush, GetTextureSource(Textures.Grassland, 13, 2) },
{ Terrain.Mushroom, GetTextureSource(Textures.Grassland, 17, 2) },
};
}
public Tile MakeTile(Terrain terrain, Rectangle position) {
TextureSource textureSource = terrainToTexture[terrain];
return new Tile(terrain, position, textureSource.texture, textureSource.source);
}
private TextureSource GetTextureSource(Texture2D texture, int x, int y) {
int size = World.TileSize; int size = World.TileSize;
Rectangle position = new Rectangle(x * size, y * size, size, size); TextureSource = new Rectangle(x * size, y * size, size, size);
return new TextureSource(texture, position);
} }
} }
class Tile { class Tile {
readonly Texture2D texture; public Tile(Terrain terrain, Rectangle position) {
readonly Rectangle textureSource;
public Tile(Terrain terrain, Rectangle position, Texture2D texture, Rectangle textureSource) {
Terrain = terrain; Terrain = terrain;
Position = position; Position = position;
this.texture = texture;
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) { public void Draw(SpriteBatch spriteBatch) {
spriteBatch.Draw(texture, Position.Location.ToVector2(), textureSource, Color.White); spriteBatch.Draw(
Terrain.Texture.Get(), Position.Location.ToVector2(), Terrain.TextureSource, Color.White);
} }
} }
@ -157,7 +99,6 @@ namespace SemiColinGames {
} }
public World(string levelSpecification) { public World(string levelSpecification) {
TileFactory factory = new TileFactory();
var tilesList = new List<Tile>(); var tilesList = new List<Tile>();
var decorationsList = new List<Tile>(); var decorationsList = new List<Tile>();
string[] worldDesc = levelSpecification.Substring(1).Split('\n'); string[] worldDesc = levelSpecification.Substring(1).Split('\n');
@ -171,7 +112,7 @@ namespace SemiColinGames {
Terrain terrain = Terrain.FromSymbol(key); Terrain terrain = Terrain.FromSymbol(key);
if (terrain != null) { if (terrain != null) {
var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize); var position = new Rectangle(i * TileSize, j * TileSize, TileSize, TileSize);
Tile tile = factory.MakeTile(terrain, position); Tile tile = new Tile(terrain, position);
if (tile.Terrain.IsObstacle) { if (tile.Terrain.IsObstacle) {
tilesList.Add(tile); tilesList.Add(tile);
} else { } else {