Lights, colored items (only used for wires atm)

This commit is contained in:
Regalis
2015-07-22 20:22:50 +03:00
parent 9a7eae8ace
commit 4820e55e05
51 changed files with 726 additions and 191 deletions
+61
View File
@@ -0,0 +1,61 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Subsurface.Lights
{
class LightSource
{
private static Texture2D lightTexture;
private Color color;
private float range;
private Texture2D texture;
public Vector2 Position;
public Color Color
{
get { return color; }
set { color = value; }
}
public float Range
{
get { return range; }
set
{
range = MathHelper.Clamp(value, 0.0f, 2048.0f);
}
}
public LightSource(Vector2 position, float range, Color color)
{
Position = position;
this.range = range;
this.color = color;
if (lightTexture == null)
{
lightTexture = Game1.TextureLoader.FromFile("Content/Lights/light.png");
}
texture = lightTexture;
Game1.LightManager.AddLight(this);
}
public void Draw(SpriteBatch spriteBatch)
{
Vector2 center = new Vector2(lightTexture.Width / 2, lightTexture.Height / 2);
float scale = range / ((float)lightTexture.Width / 2.0f);
spriteBatch.Draw(lightTexture, new Vector2(Position.X, -Position.Y), null, color, 0, center, scale, SpriteEffects.None, 1);
}
}
}
+114 -3
View File
@@ -1,5 +1,6 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
namespace Subsurface.Lights
{
@@ -7,16 +8,126 @@ namespace Subsurface.Lights
{
public static Vector2 ViewPos;
public static bool FowEnabled = true;
public Color AmbientLight;
public static void DrawFow(GraphicsDevice graphics, Camera cam)
RenderTarget2D lightMap;
private static Texture2D alphaClearTexture;
private List<LightSource> lights;
public bool FowEnabled = true;
public bool LightingEnabled = true;
public RenderTarget2D LightMap
{
get { return lightMap; }
}
public LightManager(GraphicsDevice graphics)
{
lights = new List<LightSource>();
AmbientLight = new Color(80, 80, 80, 255);
var pp = graphics.PresentationParameters;
lightMap = new RenderTarget2D(graphics, Game1.GraphicsWidth, Game1.GraphicsHeight, false,
pp.BackBufferFormat, pp.DepthStencilFormat, pp.MultiSampleCount,
RenderTargetUsage.DiscardContents);
if (alphaClearTexture==null)
{
alphaClearTexture = Game1.TextureLoader.FromFile("Content/Lights/alphaOne.png");
}
}
public void AddLight(LightSource light)
{
lights.Add(light);
}
public void RemoveLight(LightSource light)
{
lights.Remove(light);
}
public void DrawFow(GraphicsDevice graphics, Camera cam, Vector2 pos)
{
if (!FowEnabled) return;
foreach (ConvexHull convexHull in ConvexHull.list)
{
convexHull.DrawShadows(graphics, cam, ViewPos);
convexHull.DrawShadows(graphics, cam, pos);
}
}
public void DrawLightmap(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam)
{
graphics.SetRenderTarget(lightMap);
//clear to some small ambient light
graphics.Clear(AmbientLight);
foreach (LightSource light in lights)
{
//clear alpha to 1
ClearAlphaToOne(graphics, spriteBatch);
//draw all shadows
//write only to the alpha channel, which sets alpha to 0
//graphics.RasterizerState = RasterizerState.CullNone;
//graphics.BlendState = CustomBlendStates.WriteToAlpha;
//foreach (ConvexHull ch in ConvexHull.list)
//{
// //draw shadow
// ch.DrawShadows(graphics, cam, light.Position);
//}
//draw the light shape
//where Alpha is 0, nothing will be written
spriteBatch.Begin(SpriteSortMode.Immediate, CustomBlendStates.MultiplyWithAlpha, null, null, null, null, cam.Transform);
light.Draw(spriteBatch);
spriteBatch.End();
}
//clear alpha, to avoid messing stuff up later
ClearAlphaToOne(graphics, spriteBatch);
graphics.SetRenderTarget(null);
}
private void ClearAlphaToOne(GraphicsDevice graphics, SpriteBatch spriteBatch)
{
spriteBatch.Begin(SpriteSortMode.Immediate, CustomBlendStates.WriteToAlpha);
spriteBatch.Draw(alphaClearTexture, new Rectangle(0, 0,graphics.Viewport.Width, graphics.Viewport.Height), Color.White);
spriteBatch.End();
}
}
class CustomBlendStates
{
static CustomBlendStates()
{
Multiplicative = new BlendState();
Multiplicative.ColorSourceBlend = Multiplicative.AlphaSourceBlend = Blend.Zero;
Multiplicative.ColorDestinationBlend = Multiplicative.AlphaDestinationBlend = Blend.SourceColor;
Multiplicative.ColorBlendFunction = Multiplicative.AlphaBlendFunction = BlendFunction.Add;
WriteToAlpha = new BlendState();
WriteToAlpha.ColorWriteChannels = ColorWriteChannels.Alpha;
MultiplyWithAlpha = new BlendState();
MultiplyWithAlpha.ColorDestinationBlend = MultiplyWithAlpha.AlphaDestinationBlend = Blend.One;
MultiplyWithAlpha.ColorSourceBlend = MultiplyWithAlpha.AlphaSourceBlend = Blend.DestinationAlpha;
}
public static BlendState Multiplicative { get; private set; }
public static BlendState WriteToAlpha { get; private set; }
public static BlendState MultiplyWithAlpha { get; private set; }
}
}
+10 -8
View File
@@ -22,9 +22,9 @@ namespace Subsurface
private int seed;
private int size;
private Texture2D iceTexture;
private Texture2D iceCraters;
private Texture2D iceCrack;
private static Texture2D iceTexture;
private static Texture2D iceCraters;
private static Texture2D iceCrack;
private Location currentLocation;
private Location selectedLocation;
@@ -61,9 +61,9 @@ namespace Subsurface
connections = new List<LocationConnection>();
iceTexture = Game1.TextureLoader.FromFile("Content/Map/iceSurface.png");
iceCraters = Game1.TextureLoader.FromFile("Content/Map/iceCraters.png");
iceCrack = Game1.TextureLoader.FromFile("Content/Map/iceCrack.png");
if (iceTexture==null) iceTexture = Game1.TextureLoader.FromFile("Content/Map/iceSurface.png");
if (iceCraters == null) iceCraters = Game1.TextureLoader.FromFile("Content/Map/iceCraters.png");
if (iceCrack == null) iceCrack = Game1.TextureLoader.FromFile("Content/Map/iceCrack.png");
Rand.SetSyncedSeed(this.seed);
@@ -151,14 +151,16 @@ namespace Subsurface
for (int i = connections.Count - 1; i >= 0; i--)
{
i = Math.Min(i, connections.Count - 1);
LocationConnection connection = connections[i];
for (int n = i - 1; n >= 0; n--)
for (int n = Math.Min(i - 1,connections.Count - 1); n >= 0; n--)
{
if (connection.Locations.Contains(connections[n].Locations[0])
&& connection.Locations.Contains(connections[n].Locations[1]))
{
connections.RemoveAt(i);
connections.RemoveAt(n);
}
}
}
+35 -4
View File
@@ -37,6 +37,21 @@ namespace Subsurface
protected bool isSelected;
private static bool disableSelect;
public static bool DisableSelect
{
get { return disableSelect; }
set {
disableSelect = value;
if (disableSelect==true)
{
startMovingPos = Vector2.Zero;
selectionSize = Vector2.Zero;
selectionPos = Vector2.Zero;
}
}
}
public bool MoveWithLevel
{
get;
@@ -174,6 +189,12 @@ namespace Subsurface
{
if (GUIComponent.MouseOn != null) return;
if (DisableSelect)
{
DisableSelect = false;
return;
}
foreach (MapEntity e in mapEntityList)
{
e.isHighlighted = false;
@@ -189,8 +210,7 @@ namespace Subsurface
if (PlayerInput.GetKeyboardState.IsKeyDown(Keys.Delete))
{
foreach (MapEntity e in selectedList)
e.Remove();
foreach (MapEntity e in selectedList) e.Remove();
selectedList.Clear();
}
@@ -209,7 +229,7 @@ namespace Subsurface
e.isSelected = false;
}
if (highLightedEntity!=null)
if (highLightedEntity != null)
highLightedEntity.isHighlighted = true;
foreach (MapEntity e in selectedList)
@@ -269,7 +289,6 @@ namespace Subsurface
foreach (MapEntity e2 in selectedList)
{
Debug.WriteLine(e.ID+", "+e2.ID);
if (e.ID == e2.ID) alreadySelected = true;
}
@@ -359,6 +378,18 @@ namespace Subsurface
editingHUD = null;
}
}
public static void SelectEntity(MapEntity entity)
{
foreach (MapEntity e in selectedList)
{
e.isSelected = false;
}
selectedList.Clear();
entity.isSelected = true;
selectedList.Add(entity);
}
public virtual void DrawEditing(SpriteBatch spriteBatch, Camera cam) {}
+9 -3
View File
@@ -25,9 +25,7 @@ namespace Subsurface
//is it possible to stretch the entity horizontally/vertically
protected bool resizeHorizontal;
protected bool resizeVertical;
//which prefab has been selected for placing
protected static MapEntityPrefab selected;
@@ -46,7 +44,15 @@ namespace Subsurface
get { return isLinkable; }
}
public bool ResizeHorizontal
{
get { return resizeHorizontal; }
}
public bool ResizeVertical
{
get { return resizeVertical; }
}
public static void Init()
{
+1
View File
@@ -825,6 +825,7 @@ namespace Subsurface
foreach (Item item in Item.itemList)
{
System.Diagnostics.Debug.WriteLine(item.ID);
foreach (ItemComponent ic in item.components)
{
ic.OnMapLoaded();