diff --git a/Shared/Debug.cs b/Shared/Debug.cs index b4cc752..74d4419 100644 --- a/Shared/Debug.cs +++ b/Shared/Debug.cs @@ -96,13 +96,13 @@ namespace SemiColinGames { AddLine(start.ToPoint(), end.ToPoint(), color); } - public static void DrawToasts(SpriteBatch spriteBatch, SpriteFont font) { + public static void DrawToasts(SpriteBatch spriteBatch) { if (!Enabled) { return; } int y = 10; foreach (var toast in toasts) { - spriteBatch.DrawString(font, toast, new Vector2(10, y), Color.Teal); + spriteBatch.DrawString(Textures.DebugFont, toast, new Vector2(10, y), Color.Teal); y += 30; } } diff --git a/Shared/Player.cs b/Shared/Player.cs index 24af833..5faa608 100644 --- a/Shared/Player.cs +++ b/Shared/Player.cs @@ -18,7 +18,6 @@ namespace SemiColinGames { private const int spriteWidth = 96; private const int spriteHeight = 64; private const int spriteCenterYOffset = 1; - private readonly Texture2D texture; // Details of the actual Player model. @@ -36,8 +35,7 @@ namespace SemiColinGames { private const int swordSwingMax = 6; private float ySpeed = 0; - public Player(ContentManager content) { - this.texture = content.Load("sprites/ccg/ninja_female"); + public Player() { } public int Facing { get; private set; } = 1; @@ -234,8 +232,8 @@ namespace SemiColinGames { Vector2 spriteCenter = new Vector2(spriteWidth / 2, spriteHeight / 2 + spriteCenterYOffset); SpriteEffects effect = Facing == 1 ? SpriteEffects.FlipHorizontally : SpriteEffects.None; - spriteBatch.Draw(texture, position.ToVector2(), textureSource, Color.White, 0f, spriteCenter, - Vector2.One, effect, 0f); + spriteBatch.Draw(Textures.Player, position.ToVector2(), textureSource, Color.White, 0f, + spriteCenter, Vector2.One, effect, 0f); } } } diff --git a/Shared/Scene.cs b/Shared/Scene.cs index ed95aa2..e0ec554 100644 --- a/Shared/Scene.cs +++ b/Shared/Scene.cs @@ -4,6 +4,8 @@ using Microsoft.Xna.Framework.Graphics; namespace SemiColinGames { class Scene { + public bool Enabled = false; + Color backgroundColor = Color.CornflowerBlue; readonly GraphicsDevice graphics; @@ -14,12 +16,8 @@ namespace SemiColinGames { readonly BasicEffect lightingEffect; readonly SpriteBatch spriteBatch; - readonly SpriteFont font; - readonly Texture2D grasslandBg1; - readonly Texture2D grasslandBg2; - public Scene(ContentManager content, GraphicsDevice graphics, Camera camera) { - Enabled = false; + public Scene(GraphicsDevice graphics, Camera camera) { this.graphics = graphics; this.camera = camera; @@ -35,15 +33,9 @@ namespace SemiColinGames { lightingEffect.View = Matrix.CreateLookAt(Vector3.Backward, Vector3.Zero, Vector3.Up); lightingEffect.VertexColorEnabled = true; - // TODO: handle unloading of resources when the level is done. spriteBatch = new SpriteBatch(graphics); - font = content.Load("font"); - grasslandBg1 = content.Load("backgrounds/anokolisa/grassland_bg1"); - grasslandBg2 = content.Load("backgrounds/anokolisa/grassland_bg2"); } - public bool Enabled { get; set; } - public void Draw(World world, Player player, LinesOfSight linesOfSight) { graphics.SetRenderTarget(null); graphics.Clear(backgroundColor); @@ -62,10 +54,10 @@ namespace SemiColinGames { Rectangle bgSource = new Rectangle( (int) (camera.Left * 0.25), 0, camera.Width, camera.Height); Rectangle bgTarget = new Rectangle(0, 0, camera.Width, camera.Height); - spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, bgBlend); + spriteBatch.Draw(Textures.Background2, bgTarget, bgSource, bgBlend); bgSource = new Rectangle( (int) (camera.Left * 0.5), 0, camera.Width, camera.Height); - spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, bgBlend); + spriteBatch.Draw(Textures.Background1, bgTarget, bgSource, bgBlend); spriteBatch.End(); // Set up transformation matrix for drawing world objects. @@ -102,7 +94,7 @@ namespace SemiColinGames { spriteBatch.Draw(lightingTarget, drawRect, Color.White); // Draw debug toasts. - Debug.DrawToasts(spriteBatch, font); + Debug.DrawToasts(spriteBatch); spriteBatch.End(); } diff --git a/Shared/Shared.projitems b/Shared/Shared.projitems index cb50e14..a028aa5 100644 --- a/Shared/Shared.projitems +++ b/Shared/Shared.projitems @@ -10,6 +10,7 @@ + diff --git a/Shared/SneakGame.cs b/Shared/SneakGame.cs index a6a1ae6..3c21a8c 100644 --- a/Shared/SneakGame.cs +++ b/Shared/SneakGame.cs @@ -63,6 +63,7 @@ namespace SemiColinGames { // Called once per game. Loads all game content. protected override void LoadContent() { base.LoadContent(); + Textures.Load(Content); linesOfSight = new LinesOfSight(GraphicsDevice); LoadLevel(); } @@ -70,9 +71,9 @@ namespace SemiColinGames { private void LoadLevel() { framesToSuppress = 2; camera = new Camera(); - player = new Player(Content); - world = new World(Content, Levels.ONE_ONE); - scene = new Scene(Content, GraphicsDevice, camera); + player = new Player(); + world = new World(Levels.ONE_ONE); + scene = new Scene(GraphicsDevice, camera); } // Called once per game. Unloads all game content. diff --git a/Shared/Textures.cs b/Shared/Textures.cs new file mode 100644 index 0000000..c1f7492 --- /dev/null +++ b/Shared/Textures.cs @@ -0,0 +1,45 @@ +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; + +namespace SemiColinGames { + class Textures { + + public static Texture2D Player; + + public static Texture2D Background1; + public static Texture2D Background2; + + 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 void Load(ContentManager content) { + Player = content.Load("sprites/ccg/ninja_female"); + + Background1 = content.Load("backgrounds/anokolisa/grassland_bg1"); + Background2 = content.Load("backgrounds/anokolisa/grassland_bg2"); + + 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"); + } + } +} diff --git a/Shared/World.cs b/Shared/World.cs index 79b1524..e5af764 100644 --- a/Shared/World.cs +++ b/Shared/World.cs @@ -1,5 +1,4 @@ using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; @@ -17,19 +16,6 @@ namespace SemiColinGames { Block } - enum TileSet { - Cemetery, - Crypt, - Dungeon, - Forest, - Garden, - Grassland, - Ruins, - Sewer, - Temple, - Village, - } - class TileFactory { struct TextureSource { @@ -42,40 +28,19 @@ namespace SemiColinGames { } } - static readonly Dictionary tileSetToContentPath = - new Dictionary() { - { TileSet.Cemetery, "tiles/anokolisa/cemetery" }, - { TileSet.Crypt, "tiles/anokolisa/crypt" }, - { TileSet.Dungeon, "tiles/anokolisa/dungeon" }, - { TileSet.Forest, "tiles/anokolisa/forest" }, - { TileSet.Garden, "tiles/anokolisa/garden" }, - { TileSet.Grassland, "tiles/anokolisa/grassland" }, - { TileSet.Ruins, "tiles/anokolisa/ruins" }, - { TileSet.Sewer, "tiles/anokolisa/sewer" }, - { TileSet.Temple, "tiles/anokolisa/temple" }, - { TileSet.Village, "tiles/anokolisa/village" }, + 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.Water, GetTextureSource(Textures.Grassland, 9, 2) }, + { Terrain.Block, GetTextureSource(Textures.Grassland, 6, 3) }, }; - - readonly Dictionary terrainToTexture = - new Dictionary(); - - readonly Texture2D[] textures; - - public TileFactory(ContentManager content) { - Array tileSets = Enum.GetValues(typeof(TileSet)); - textures = new Texture2D[tileSets.Length]; - foreach (TileSet tileSet in tileSets) { - textures[(int) tileSet] = content.Load(tileSetToContentPath[tileSet]); - } - - terrainToTexture[Terrain.Grass] = GetTextureSource(TileSet.Grassland, 3, 0); - terrainToTexture[Terrain.GrassL] = GetTextureSource(TileSet.Grassland, 2, 0); - terrainToTexture[Terrain.GrassR] = GetTextureSource(TileSet.Grassland, 4, 0); - terrainToTexture[Terrain.Rock] = GetTextureSource(TileSet.Grassland, 3, 1); - terrainToTexture[Terrain.RockL] = GetTextureSource(TileSet.Grassland, 1, 2); - terrainToTexture[Terrain.RockR] = GetTextureSource(TileSet.Grassland, 5, 2); - terrainToTexture[Terrain.Water] = GetTextureSource(TileSet.Grassland, 9, 2); - terrainToTexture[Terrain.Block] = GetTextureSource(TileSet.Grassland, 6, 3); } public Tile MakeTile(Terrain terrain, Rectangle position) { @@ -83,8 +48,7 @@ namespace SemiColinGames { return new Tile(terrain, position, textureSource.texture, textureSource.source); } - private TextureSource GetTextureSource(TileSet tileSet, int x, int y) { - Texture2D texture = textures[(int) tileSet]; + private TextureSource GetTextureSource(Texture2D texture, int x, int y) { int size = World.TileSize; Rectangle position = new Rectangle(x * size, y * size, size, size); return new TextureSource(texture, position); @@ -140,8 +104,8 @@ namespace SemiColinGames { { 'X', Terrain.Block } }; - public World(ContentManager content, string levelSpecification) { - TileFactory factory = new TileFactory(content); + public World(string levelSpecification) { + TileFactory factory = new TileFactory(); var tilesList = new List(); string[] worldDesc = levelSpecification.Split('\n'); tileWidth = worldDesc.AsQueryable().Max(a => a.Length);