Fake lights! Yay!

This commit is contained in:
Simon Holmberg
2012-10-22 02:10:19 +02:00
parent dca73efe29
commit 89376478b0
9 changed files with 193 additions and 20 deletions
+44
View File
@@ -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<ContentManager>().Load<Texture2D>("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);
}
}
}
+62 -9
View File
@@ -19,8 +19,11 @@ namespace DepthsBelow
/// </summary> /// </summary>
public class Core : Microsoft.Xna.Framework.Game public class Core : Microsoft.Xna.Framework.Game
{ {
public static GraphicsDeviceManager GraphicsDeviceManager; public static GraphicsDeviceManager GraphicsDeviceManager;
SpriteBatch spriteBatch; private SpriteBatch spriteBatch;
private Effect lightShader;
private RenderTarget2D lightRenderTarget;
private RenderTarget2D sceneRenderTarget;
public Lua Lua; public Lua Lua;
@@ -88,7 +91,10 @@ namespace DepthsBelow
protected override void LoadContent() protected override void LoadContent()
{ {
// Create a new SpriteBatch, which can be used to draw textures. // Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice); spriteBatch = new SpriteBatch(GraphicsDevice);
lightShader = Content.Load<Effect>("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<Map>("maps/Cave.Level1"); Map = Content.Load<Map>("maps/Cave.Level1");
// Load map objects // Load map objects
@@ -150,10 +156,47 @@ namespace DepthsBelow
/// This is called when the game should draw itself. /// This is called when the game should draw itself.
/// </summary> /// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param> /// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime) protected override void Draw(GameTime gameTime)
{ {
GraphicsDevice.Clear(Color.Black); 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<Vector2> vectors = new List<Vector2>();
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<Texture2D>("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<Component.Flashlight>())
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 // Start drawing using the Camera transform
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null,
Camera.Transform); Camera.Transform);
@@ -161,15 +204,25 @@ namespace DepthsBelow
// Draw the level // Draw the level
Map.Draw(spriteBatch); Map.Draw(spriteBatch);
// Draw entities // Draw entities
EntityManager.Draw(spriteBatch); foreach (var spriteRenderer in EntityManager.GetComponents<Component.SpriteRenderer>())
spriteRenderer.Draw(spriteBatch);
// Draw mouse input visuals // Draw mouse input visuals
MouseInput.Draw(spriteBatch); MouseInput.Draw(spriteBatch);
spriteBatch.End(); 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); spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.AnisotropicClamp, null, null, null, Matrix.Identity);
GUIManager.Draw(spriteBatch); GUIManager.Draw(spriteBatch);
spriteBatch.End(); spriteBatch.End();
@@ -121,6 +121,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Camera.cs" /> <Compile Include="Camera.cs" />
<Compile Include="Component\Flashlight.cs" />
<Compile Include="DynamicGroupManager.cs" /> <Compile Include="DynamicGroupManager.cs" />
<Compile Include="TurnManager.cs" /> <Compile Include="TurnManager.cs" />
<Compile Include="CustomExtensions.cs" /> <Compile Include="CustomExtensions.cs" />
+31 -10
View File
@@ -12,11 +12,11 @@ namespace DepthsBelow
/// </summary> /// </summary>
public class EntityManager : IDisposable public class EntityManager : IDisposable
{ {
private List<Entity> entities; public List<Entity> Entities;
public EntityManager() public EntityManager()
{ {
entities = new List<Entity>(); Entities = new List<Entity>();
} }
/// <summary> /// <summary>
@@ -25,10 +25,10 @@ namespace DepthsBelow
/// </summary> /// </summary>
public void Dispose() public void Dispose()
{ {
foreach (var entity in entities) foreach (var entity in Entities)
entity.Dispose(); entity.Dispose();
entities = null; Entities = null;
} }
/// <summary> /// <summary>
@@ -37,7 +37,7 @@ namespace DepthsBelow
/// <param name="entity">The entity to add.</param> /// <param name="entity">The entity to add.</param>
public void Add(Entity entity) public void Add(Entity entity)
{ {
entities.Add(entity); Entities.Add(entity);
} }
/// <summary> /// <summary>
@@ -46,7 +46,7 @@ namespace DepthsBelow
/// <param name="entity">The entity to remove.</param> /// <param name="entity">The entity to remove.</param>
public void Remove(Entity entity) public void Remove(Entity entity)
{ {
entities.Remove(entity); Entities.Remove(entity);
} }
/// <summary> /// <summary>
@@ -54,10 +54,29 @@ namespace DepthsBelow
/// </summary> /// </summary>
public void Reset() public void Reset()
{ {
foreach (var entity in entities) foreach (var entity in Entities)
entity.Dispose(); entity.Dispose();
entities.Clear(); Entities.Clear();
}
/// <summary>
/// Get the reference to a component contained in the entity.
/// </summary>
/// <typeparam name="T">The component type.</typeparam>
/// <returns>Returns the component of requested type.</returns>
public List<T> GetComponents<T>() where T : Component.Component
{
List<T> components = new List<T>();
foreach (var entity in Entities)
{
var component = entity.GetComponent<T>();
if (component != null)
components.Add(component);
}
return components;
} }
/// <summary> /// <summary>
@@ -66,7 +85,7 @@ namespace DepthsBelow
/// <param name="gameTime"></param> /// <param name="gameTime"></param>
public void Update(GameTime gameTime) public void Update(GameTime gameTime)
{ {
foreach (var entity in entities) foreach (var entity in Entities)
entity.Update(gameTime); entity.Update(gameTime);
} }
@@ -76,8 +95,10 @@ namespace DepthsBelow
/// <param name="spriteBatch"></param> /// <param name="spriteBatch"></param>
public void Draw(SpriteBatch spriteBatch) public void Draw(SpriteBatch spriteBatch)
{ {
foreach (var entity in entities) foreach (var entity in Entities)
{
entity.GetComponent<Component.SpriteRenderer>().Draw(spriteBatch); entity.GetComponent<Component.SpriteRenderer>().Draw(spriteBatch);
}
} }
} }
} }
+4 -1
View File
@@ -63,7 +63,10 @@ namespace DepthsBelow
AddComponent(cc); AddComponent(cc);
var pfc = new PathFinder(this); var pfc = new PathFinder(this);
AddComponent(pfc); AddComponent(pfc);
var flashlight = new Flashlight(this);
AddComponent(flashlight);
var stat = new Component.Stat(this) var stat = new Component.Stat(this)
{ {
@@ -183,6 +183,26 @@
<Processor>FontDescriptionProcessor</Processor> <Processor>FontDescriptionProcessor</Processor>
</Compile> </Compile>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Compile Include="shaders\LightEffect.fx">
<Name>LightEffect</Name>
<Importer>EffectImporter</Importer>
<Processor>EffectProcessor</Processor>
</Compile>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Compile Include="images\lights\point.png">
<Name>point</Name>
<Importer>TextureImporter</Importer>
<Processor>TextureProcessor</Processor>
</Compile>
<Compile Include="images\lights\spot.png">
<Name>spot</Name>
<Importer>TextureImporter</Importer>
<Processor>TextureProcessor</Processor>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" /> <Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. Other similar extension points exist, see Microsoft.Common.targets.
Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

+31
View File
@@ -0,0 +1,31 @@
texture LightsTexture;
sampler ColorSampler : register(s0);
sampler LightsSampler = sampler_state{
Texture = <LightsTexture>;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float2 tex = input.TexCoord;
float4 color = tex2D(ColorSampler, tex);
float4 alpha = tex2D(LightsSampler, tex);
return color * alpha;
}
technique Technique1
{
pass Pass1
{
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}