From e72b8999e42ce72a1f191fa6682e6c913d9da341 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Thu, 20 Feb 2020 16:36:54 -0500 Subject: [PATCH] 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 --- Shared/Player.cs | 2 +- Shared/Scene.cs | 4 +- Shared/Textures.cs | 81 ++++++++++++++++---------- Shared/World.cs | 141 +++++++++++++-------------------------------- 4 files changed, 93 insertions(+), 135 deletions(-) diff --git a/Shared/Player.cs b/Shared/Player.cs index d621b06..4e75110 100644 --- a/Shared/Player.cs +++ b/Shared/Player.cs @@ -237,7 +237,7 @@ namespace SemiColinGames { Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset); SpriteEffects effect = Facing == 1 ? 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); } } diff --git a/Shared/Scene.cs b/Shared/Scene.cs index be1e778..4ecd1ef 100644 --- a/Shared/Scene.cs +++ b/Shared/Scene.cs @@ -60,10 +60,10 @@ namespace SemiColinGames { float xScale = 1f / 16; 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( (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; } spriteBatch.End(); diff --git a/Shared/Textures.cs b/Shared/Textures.cs index 2392271..ed4b76e 100644 --- a/Shared/Textures.cs +++ b/Shared/Textures.cs @@ -1,47 +1,64 @@ using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; +using System.Collections.Generic; + namespace SemiColinGames { + + class TextureRef { + private static readonly List allTextures = new List(); + + 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(contentPath); + } + } + class Textures { - public static Texture2D Player; + 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 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 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 SpriteFont DebugFont; + 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) { - Player = content.Load("sprites/ccg/ninja_female"); - - Backgrounds[0] = content.Load("backgrounds/szadiart/pf4/background1_day"); - Backgrounds[1] = content.Load("backgrounds/szadiart/pf4/background2a_day"); - Backgrounds[2] = content.Load("backgrounds/szadiart/pf4/background3_day"); - Backgrounds[3] = content.Load("backgrounds/szadiart/pf4/background4_day"); - - Cemetery = content.Load("tiles/anokolisa/cemetery"); - Crypt = content.Load("tiles/anokolisa/crypt"); - Dungeon = content.Load("tiles/anokolisa/dungeon"); - Forest = content.Load("tiles/anokolisa/forest"); - Garden = content.Load("tiles/anokolisa/garden"); - Grassland = content.Load("tiles/anokolisa/grassland"); - Ruins = content.Load("tiles/anokolisa/ruins"); - Sewer = content.Load("tiles/anokolisa/sewer"); - Temple = content.Load("tiles/anokolisa/temple"); - Village = content.Load("tiles/anokolisa/village"); - DebugFont = content.Load("font"); + TextureRef.LoadAll(content); } } } diff --git a/Shared/World.cs b/Shared/World.cs index 914fa2e..3e4b9a9 100644 --- a/Shared/World.cs +++ b/Shared/World.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; namespace SemiColinGames { + class Terrain { public static Terrain FromSymbol(char symbol) { @@ -17,123 +18,64 @@ namespace SemiColinGames { private readonly static Dictionary mapping = new Dictionary(); - public static Terrain Grass = new Terrain('=', true); - public static Terrain GrassL = new Terrain('<', true); - public static Terrain GrassR = new Terrain('>', true); - public static Terrain Rock = new Terrain('.', true); - public static Terrain RockL = new Terrain('[', true); - public static Terrain RockR = new Terrain(']', true); - public static Terrain WaterL = new Terrain('~', false); - public static Terrain WaterR = new Terrain('`', false); - public static Terrain Block = new Terrain('X', true); - public static Terrain Spike = new Terrain('^', true); - public static Terrain Wood = new Terrain('_', true); - public static Terrain WoodL = new Terrain('(', true); - public static Terrain WoodR = new Terrain(')', true); - public static Terrain WoodVert = new Terrain('|', false); - public static Terrain WoodVertL = new Terrain('/', false); - public static Terrain WoodVertR = new Terrain('\\', false); - public static Terrain WoodBottom = new Terrain('v', false); - public static Terrain FenceL = new Terrain('d', false); - public static Terrain Fence = new Terrain('f', false); - public static Terrain FencePost = new Terrain('x', false); - public static Terrain FenceR = new Terrain('b', false); - public static Terrain VineTop = new Terrain('{', false); - public static Terrain VineMid = new Terrain(';', false); - public static Terrain VineBottom = new Terrain('}', false); - public static Terrain GrassTall = new Terrain('q', false); - public static Terrain GrassShort = new Terrain('w', false); - public static Terrain Shoots = new Terrain('e', false); - public static Terrain Bush = new Terrain('r', false); - public static Terrain Mushroom = new Terrain('t', false); + public static Terrain Grass = new Terrain('=', true, Textures.Grassland, 3, 0); + public static Terrain GrassL = new Terrain('<', true, Textures.Grassland, 2, 0); + public static Terrain GrassR = new Terrain('>', true, Textures.Grassland, 4, 0); + public static Terrain Rock = new Terrain('.', true, Textures.Grassland, 3, 1); + public static Terrain RockL = new Terrain('[', true, Textures.Grassland, 1, 2); + public static Terrain RockR = new Terrain(']', true, Textures.Grassland, 5, 2); + public static Terrain WaterL = new Terrain('~', false, Textures.Grassland, 9, 2); + public static Terrain WaterR = new Terrain('`', false, Textures.Grassland, 10, 2); + public static Terrain Block = new Terrain('X', true, Textures.Ruins, 2, 0); + public static Terrain Spike = new Terrain('^', true, Textures.Grassland, 11, 8); + public static Terrain Wood = new Terrain('_', true, Textures.Grassland, 10, 3); + public static Terrain WoodL = new Terrain('(', true, Textures.Grassland, 9, 3); + public static Terrain WoodR = new Terrain(')', true, Textures.Grassland, 12, 3); + public static Terrain WoodVert = new Terrain('|', false, Textures.Grassland, 9, 5); + public static Terrain WoodVertL = new Terrain('/', false, Textures.Grassland, 9, 4); + public static Terrain WoodVertR = new Terrain('\\', false, Textures.Grassland, 12, 4); + public static Terrain WoodBottom = new Terrain('v', false, Textures.Grassland, 10, 5); + public static Terrain FenceL = new Terrain('d', false, Textures.Grassland, 5, 4); + public static Terrain Fence = new Terrain('f', false, Textures.Grassland, 6, 4); + public static Terrain FencePost = new Terrain('x', false, Textures.Grassland, 7, 4); + public static Terrain FenceR = new Terrain('b', false, Textures.Grassland, 8, 4); + public static Terrain VineTop = new Terrain('{', false, Textures.Ruins, 12, 5); + public static Terrain VineMid = new Terrain(';', false, Textures.Ruins, 12, 6); + public static Terrain VineBottom = new Terrain('}', false, Textures.Ruins, 12, 7); + public static Terrain GrassTall = new Terrain('q', false, Textures.Grassland, 13, 0); + public static Terrain GrassShort = new Terrain('w', false, Textures.Grassland, 14, 0); + public static Terrain Shoots = new Terrain('e', false, Textures.Grassland, 15, 0); + public static Terrain Bush = new Terrain('r', false, Textures.Grassland, 13, 2); + public static Terrain Mushroom = new Terrain('t', false, Textures.Grassland, 17, 2); 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)) { throw new ArgumentException("already have a terrain with symbol " + symbol); } - IsObstacle = isObstacle; mapping[symbol] = this; - } - } - - class TileFactory { - - struct TextureSource { - public Texture2D texture; - public Rectangle source; - - public TextureSource(Texture2D texture, Rectangle source) { - this.texture = texture; - this.source = source; - } - } - - readonly Dictionary terrainToTexture; - - public TileFactory() { - terrainToTexture = new Dictionary() { - { 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) { + IsObstacle = isObstacle; + Texture = texture; int size = World.TileSize; - Rectangle position = new Rectangle(x * size, y * size, size, size); - return new TextureSource(texture, position); + TextureSource = new Rectangle(x * size, y * size, size, size); } } - + class Tile { - readonly Texture2D texture; - readonly Rectangle textureSource; - - public Tile(Terrain terrain, Rectangle position, Texture2D texture, Rectangle textureSource) { + public Tile(Terrain terrain, Rectangle position) { Terrain = terrain; Position = position; - this.texture = texture; - this.textureSource = textureSource; } public Rectangle Position { get; private set; } public Terrain Terrain { get; private set; } 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) { - TileFactory factory = new TileFactory(); var tilesList = new List(); var decorationsList = new List(); string[] worldDesc = levelSpecification.Substring(1).Split('\n'); @@ -171,7 +112,7 @@ namespace SemiColinGames { Terrain terrain = Terrain.FromSymbol(key); if (terrain != null) { 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) { tilesList.Add(tile); } else {