From 66ce866b12f6c33ec49736baee447efcfd068160 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Mon, 9 Mar 2020 12:19:19 -0400 Subject: [PATCH] move LinesOfSight into World --- Shared/Camera.cs | 2 +- Shared/LinesOfSight.cs | 2 +- Shared/Scene.cs | 6 +++--- Shared/SneakGame.cs | 9 +++------ Shared/World.cs | 21 +++++++++++++++++---- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Shared/Camera.cs b/Shared/Camera.cs index db7bb43..839cb42 100644 --- a/Shared/Camera.cs +++ b/Shared/Camera.cs @@ -4,7 +4,7 @@ using System; // Good background reading, eventually: // https://gamasutra.com/blogs/ItayKeren/20150511/243083/Scroll_Back_The_Theory_and_Practice_of_Cameras_in_SideScrollers.php namespace SemiColinGames { - class Camera { + public class Camera { // Screen size in pixels is 1920x1080 divided by 4. private Rectangle bbox = new Rectangle(0, 0, 480, 270); public int Width { get => bbox.Width; } diff --git a/Shared/LinesOfSight.cs b/Shared/LinesOfSight.cs index f861fbe..3f5131f 100644 --- a/Shared/LinesOfSight.cs +++ b/Shared/LinesOfSight.cs @@ -3,7 +3,7 @@ using Microsoft.Xna.Framework.Graphics; using System; namespace SemiColinGames { - class LinesOfSight : IDisposable { + public sealed class LinesOfSight : IDisposable { const int NUM_EDGE_VERTICES = 60; diff --git a/Shared/Scene.cs b/Shared/Scene.cs index 109764b..393ca52 100644 --- a/Shared/Scene.cs +++ b/Shared/Scene.cs @@ -4,7 +4,7 @@ using Microsoft.Xna.Framework.Graphics; using System; namespace SemiColinGames { - class Scene : IDisposable { + public sealed class Scene : IDisposable { const float DESIRED_ASPECT_RATIO = 1920.0f / 1080.0f; Color backgroundColor = Color.CornflowerBlue; @@ -56,7 +56,7 @@ namespace SemiColinGames { GC.SuppressFinalize(this); } - public void Draw(bool isRunningSlowly, World world, LinesOfSight linesOfSight, bool paused) { + public void Draw(bool isRunningSlowly, World world, bool paused) { graphics.SetRenderTarget(null); graphics.Clear(backgroundColor); @@ -87,7 +87,7 @@ namespace SemiColinGames { // Draw lines of sight. basicEffect.Projection = camera.Projection; if (Debug.Enabled) { - linesOfSight.Draw(graphics, basicEffect); + world.LinesOfSight.Draw(graphics, basicEffect); } // Set up transformation matrix for drawing world objects. diff --git a/Shared/SneakGame.cs b/Shared/SneakGame.cs index 5a909d8..b3656b2 100644 --- a/Shared/SneakGame.cs +++ b/Shared/SneakGame.cs @@ -23,7 +23,6 @@ namespace SemiColinGames { Scene scene; World world; - LinesOfSight linesOfSight; Camera camera = new Camera(); public SneakGame() { @@ -61,14 +60,13 @@ namespace SemiColinGames { SoundEffects.Load(Content); Textures.Load(Content); Sprites.Load(Content); - // TODO: move this into World. - linesOfSight = new LinesOfSight(GraphicsDevice); LoadLevel(); } private void LoadLevel() { camera = new Camera(); - world = new World(Content.LoadString("levels/demo.json")); + world?.Dispose(); + world = new World(GraphicsDevice, Content.LoadString("levels/demo.json")); scene?.Dispose(); scene = new Scene(GraphicsDevice, camera); @@ -114,7 +112,6 @@ namespace SemiColinGames { float modelTime = (float) gameTime.ElapsedGameTime.TotalSeconds; Clock.AddModelTime(modelTime); world.Update(modelTime, input); - linesOfSight.Update(world.Player, world.CollisionTargets); camera.Update(world.Player.Position, world.Width); } @@ -137,7 +134,7 @@ namespace SemiColinGames { Debug.SetFpsText(fpsText); - scene.Draw(gameTime.IsRunningSlowly, world, linesOfSight, paused); + scene.Draw(gameTime.IsRunningSlowly, world, paused); base.Draw(gameTime); drawTimer.Stop(); diff --git a/Shared/World.cs b/Shared/World.cs index e03c708..e73e9ea 100644 --- a/Shared/World.cs +++ b/Shared/World.cs @@ -6,7 +6,7 @@ using System.Collections.Generic; namespace SemiColinGames { - public class World { + public sealed class World : IDisposable { public const int TileSize = 16; @@ -21,6 +21,7 @@ namespace SemiColinGames { public Player Player { get; private set; } public AABB[] CollisionTargets { get; } + public LinesOfSight LinesOfSight { get; private set; } // Size of World in pixels. public int Width { @@ -31,7 +32,9 @@ namespace SemiColinGames { get { return gridHeight * TileSize; } } - public World(string json) { + public World(GraphicsDevice graphics, string json) { + LinesOfSight = new LinesOfSight(graphics); + JObject root = JObject.Parse(json); List hazardTiles = new List(); @@ -86,6 +89,15 @@ namespace SemiColinGames { new Vector2(Width + 1, 0), new Vector2(1, float.MaxValue)); } + ~World() { + Dispose(); + } + + public void Dispose() { + LinesOfSight.Dispose(); + GC.SuppressFinalize(this); + } + private List ParseLayer(JToken layer) { string layerName = layer.SelectToken("name").Value(); @@ -149,6 +161,7 @@ namespace SemiColinGames { if (Player.Health <= 0) { Reset(); } + LinesOfSight.Update(Player, CollisionTargets); } // Draws everything that's behind the player, from back to front. @@ -174,8 +187,8 @@ namespace SemiColinGames { return t1.Position.X.CompareTo(t2.Position.X); } - private TextureRef texture; - private Rectangle textureSource; + private readonly TextureRef texture; + private readonly Rectangle textureSource; public Tile(TextureRef texture, Rectangle textureSource, Rectangle position, bool isHazard) { Position = position;