diff --git a/DepthsBelow/DepthsBelow/Component/Flashlight.cs b/DepthsBelow/DepthsBelow/Component/Flashlight.cs new file mode 100755 index 0000000..33e2c5f --- /dev/null +++ b/DepthsBelow/DepthsBelow/Component/Flashlight.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; + +namespace DepthsBelow.Component +{ + public class Flashlight : Component + { + public float Scale = 1f; + public float Intensity = 1f; + public float Angle = 0; + public Vector2 Position { get; private set; } + public Vector2 Offset; + public Texture2D Texture; + public Color Color; + + public bool IsOn = true; + + public Flashlight(Entity parent) + : base(parent) + { + Texture = GameServices.GetService().Load("images/lights/spot"); + Color = Color.White; + Offset = new Vector2(-50, -60); + } + + public override void Update(GameTime gameTime) + { + Position = Parent.Transform.World + Parent.Transform.World.Origin + Offset; + Angle = Parent.Transform.World.Rotation; + } + + public void Draw(SpriteBatch spriteBatch) + { + if (IsOn) + spriteBatch.Draw(Texture, Position, null, Color * Intensity, 0, Vector2.Zero, Scale, SpriteEffects.None, 0); + } + + } +} diff --git a/DepthsBelow/DepthsBelow/Core.cs b/DepthsBelow/DepthsBelow/Core.cs index 2c64b2e..f0470bd 100755 --- a/DepthsBelow/DepthsBelow/Core.cs +++ b/DepthsBelow/DepthsBelow/Core.cs @@ -19,8 +19,11 @@ namespace DepthsBelow /// public class Core : Microsoft.Xna.Framework.Game { - public static GraphicsDeviceManager GraphicsDeviceManager; - SpriteBatch spriteBatch; + public static GraphicsDeviceManager GraphicsDeviceManager; + private SpriteBatch spriteBatch; + private Effect lightShader; + private RenderTarget2D lightRenderTarget; + private RenderTarget2D sceneRenderTarget; public Lua Lua; @@ -88,7 +91,10 @@ namespace DepthsBelow protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. - spriteBatch = new SpriteBatch(GraphicsDevice); + spriteBatch = new SpriteBatch(GraphicsDevice); + lightShader = Content.Load("shaders/LightEffect"); + lightRenderTarget = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents); + sceneRenderTarget = new RenderTarget2D(GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); Map = Content.Load("maps/Cave.Level1"); // Load map objects @@ -150,10 +156,47 @@ namespace DepthsBelow /// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. - protected override void Draw(GameTime gameTime) - { - GraphicsDevice.Clear(Color.Black); + protected override void Draw(GameTime gameTime) + { + MouseState ms = Mouse.GetState(); + // Draw to the lighting render target + GraphicsDevice.SetRenderTarget(lightRenderTarget); + GraphicsDevice.Clear(new Color(20, 20, 20)); + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, + Camera.Transform); + /*foreach (var @group in GroupManager.Groups) + { + List vectors = new List(); + foreach (var entity in group.Entities) + { + vectors.Add(entity.Transform.World); + } + + float xMean = 0; + float yMean = 0; + foreach (var vector2 in vectors) + { + xMean += vector2.X; + yMean += vector2.Y; + } + xMean /= vectors.Count; + yMean /= vectors.Count; + + var texture = Content.Load("images/lights/point"); + spriteBatch.Draw(texture, new Vector2(xMean, yMean) + new Vector2(Grid.TileSize /2, Grid.TileSize / 2), null, Color.White, 0, new Vector2(texture.Width / 2f, texture.Height / 2f), 0.5f + (0.5f * (group.Entities.Count / 5f)), SpriteEffects.None, 0); + }*/ + + // Draw flashlight + foreach (var light in EntityManager.GetComponents()) + light.Draw(spriteBatch); + + spriteBatch.End(); + GraphicsDevice.SetRenderTarget(null); + + // Draw the scene to a render target + GraphicsDevice.SetRenderTarget(sceneRenderTarget); + GraphicsDevice.Clear(Color.Black); // Start drawing using the Camera transform spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, Camera.Transform); @@ -161,15 +204,25 @@ namespace DepthsBelow // Draw the level Map.Draw(spriteBatch); - // Draw entities - EntityManager.Draw(spriteBatch); + // Draw entities + foreach (var spriteRenderer in EntityManager.GetComponents()) + spriteRenderer.Draw(spriteBatch); // Draw mouse input visuals MouseInput.Draw(spriteBatch); spriteBatch.End(); + GraphicsDevice.SetRenderTarget(null); - // Start drawing GUI components + // Draw the scene from the render target with the light shader + lightShader.Parameters["LightsTexture"].SetValue(lightRenderTarget); + lightShader.CurrentTechnique.Passes[0].Apply(); + spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, lightShader, + Matrix.Identity); + spriteBatch.Draw(sceneRenderTarget, Vector2.Zero, Color.White); + spriteBatch.End(); + + // Draw GUI components spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.AnisotropicClamp, null, null, null, Matrix.Identity); GUIManager.Draw(spriteBatch); spriteBatch.End(); diff --git a/DepthsBelow/DepthsBelow/DepthsBelow.csproj b/DepthsBelow/DepthsBelow/DepthsBelow.csproj index e908eb5..d6809fa 100644 --- a/DepthsBelow/DepthsBelow/DepthsBelow.csproj +++ b/DepthsBelow/DepthsBelow/DepthsBelow.csproj @@ -121,6 +121,7 @@ + diff --git a/DepthsBelow/DepthsBelow/EntityManager.cs b/DepthsBelow/DepthsBelow/EntityManager.cs index ea781da..a20ebde 100755 --- a/DepthsBelow/DepthsBelow/EntityManager.cs +++ b/DepthsBelow/DepthsBelow/EntityManager.cs @@ -12,11 +12,11 @@ namespace DepthsBelow /// public class EntityManager : IDisposable { - private List entities; + public List Entities; public EntityManager() { - entities = new List(); + Entities = new List(); } /// @@ -25,10 +25,10 @@ namespace DepthsBelow /// public void Dispose() { - foreach (var entity in entities) + foreach (var entity in Entities) entity.Dispose(); - entities = null; + Entities = null; } /// @@ -37,7 +37,7 @@ namespace DepthsBelow /// The entity to add. public void Add(Entity entity) { - entities.Add(entity); + Entities.Add(entity); } /// @@ -46,7 +46,7 @@ namespace DepthsBelow /// The entity to remove. public void Remove(Entity entity) { - entities.Remove(entity); + Entities.Remove(entity); } /// @@ -54,10 +54,29 @@ namespace DepthsBelow /// public void Reset() { - foreach (var entity in entities) + foreach (var entity in Entities) entity.Dispose(); - entities.Clear(); + Entities.Clear(); + } + + /// + /// Get the reference to a component contained in the entity. + /// + /// The component type. + /// Returns the component of requested type. + public List GetComponents() where T : Component.Component + { + List components = new List(); + + foreach (var entity in Entities) + { + var component = entity.GetComponent(); + if (component != null) + components.Add(component); + } + + return components; } /// @@ -66,7 +85,7 @@ namespace DepthsBelow /// public void Update(GameTime gameTime) { - foreach (var entity in entities) + foreach (var entity in Entities) entity.Update(gameTime); } @@ -76,8 +95,10 @@ namespace DepthsBelow /// public void Draw(SpriteBatch spriteBatch) { - foreach (var entity in entities) + foreach (var entity in Entities) + { entity.GetComponent().Draw(spriteBatch); + } } } } diff --git a/DepthsBelow/DepthsBelow/Soldier.cs b/DepthsBelow/DepthsBelow/Soldier.cs index 44883d4..c632bdf 100755 --- a/DepthsBelow/DepthsBelow/Soldier.cs +++ b/DepthsBelow/DepthsBelow/Soldier.cs @@ -63,7 +63,10 @@ namespace DepthsBelow AddComponent(cc); var pfc = new PathFinder(this); - AddComponent(pfc); + AddComponent(pfc); + + var flashlight = new Flashlight(this); + AddComponent(flashlight); var stat = new Component.Stat(this) { diff --git a/DepthsBelow/DepthsBelowContent/DepthsBelowContent.contentproj b/DepthsBelow/DepthsBelowContent/DepthsBelowContent.contentproj index 082baf3..361413e 100755 --- a/DepthsBelow/DepthsBelowContent/DepthsBelowContent.contentproj +++ b/DepthsBelow/DepthsBelowContent/DepthsBelowContent.contentproj @@ -183,6 +183,26 @@ FontDescriptionProcessor + + + LightEffect + EffectImporter + EffectProcessor + + + + + + point + TextureImporter + TextureProcessor + + + spot + TextureImporter + TextureProcessor + +