- Fixed shadows not being cast if the lightsource is outside the sub and the convexhull is not

- Option to change the texture of the "light sphere"
- Added "headlights" to the scooters
This commit is contained in:
Regalis
2016-05-03 00:27:48 +03:00
parent 26608d3f82
commit 03626c997b
9 changed files with 122 additions and 47 deletions
@@ -133,7 +133,11 @@
<Propulsion force="80" usablein="water" particles="bubbles"> <Propulsion force="80" usablein="water" particles="bubbles">
<RequiredItems name="Battery Cell,Fulgurium Battery Cell" type="Contained" msg="Battery Cell required"/> <RequiredItems name="Battery Cell,Fulgurium Battery Cell" type="Contained" msg="Battery Cell required"/>
<StatusEffect type="OnUse" target="Contained" targetnames="Battery Cell" Condition="-1.0"/> <StatusEffect type="OnUse" target="Contained" targetnames="Battery Cell" Condition="-1.0"/>
<sound file="scooter.ogg" type="OnUse" range="500.0" loop="true"/> <sound file="scooter.ogg" type="OnUse" range="500.0" loop="true"/>
<LightComponent LightColor="1.0,1.0,1.0,1.0" Flicker="0.2" range="600">
<LightTexture texture="Content/Lights/lightcone.png" origin="0.1, 0.5" size="2.0,1.0"/>
</LightComponent>
</Propulsion> </Propulsion>
@@ -295,7 +295,7 @@
<Sprite texture="signalcomp.png" depth="0.8" sourcerect="48,0,16,16"/> <Sprite texture="signalcomp.png" depth="0.8" sourcerect="48,0,16,16"/>
<LightComponent canbeselected = "true" color="1.0,0.0,0.0,1.0"> <LightComponent canbeselected="true" color="1.0,0.0,0.0,1.0" castshadows="false">
<sprite texture="Content/Items/Electricity/signalcomp.png" sourcerect="0,96,31,32"/> <sprite texture="Content/Items/Electricity/signalcomp.png" sourcerect="0,96,31,32"/>
</LightComponent> </LightComponent>
Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

@@ -11,6 +11,8 @@ namespace Barotrauma.Items.Components
private string particles; private string particles;
private float useState;
private ParticlePrefab.DrawTargetType usableIn; private ParticlePrefab.DrawTargetType usableIn;
[HasDefaultValue(0.0f, false)] [HasDefaultValue(0.0f, false)]
@@ -50,6 +52,10 @@ namespace Barotrauma.Items.Components
if (character == null) return false; if (character == null) return false;
if (!character.IsKeyDown(InputType.Aim) || character.Stun>0.0f) return false; if (!character.IsKeyDown(InputType.Aim) || character.Stun>0.0f) return false;
IsActive = true;
useState = 0.1f;
if (character.AnimController.InWater) if (character.AnimController.InWater)
{ {
if (usableIn == ParticlePrefab.DrawTargetType.Air) return true; if (usableIn == ParticlePrefab.DrawTargetType.Air) return true;
@@ -91,12 +97,13 @@ namespace Barotrauma.Items.Components
return true; return true;
} }
public override void Draw(SpriteBatch spriteBatch, bool editing = false) public override void Update(float deltaTime, Camera cam)
{ {
IsActive = false; useState -= deltaTime;
if (useState <= 0.0f) IsActive = false;
} }
} }
} }
@@ -18,6 +18,8 @@ namespace Barotrauma.Items.Components
private float flicker; private float flicker;
private bool castShadows;
[Editable, HasDefaultValue(100.0f, true)] [Editable, HasDefaultValue(100.0f, true)]
public float Range public float Range
{ {
@@ -28,6 +30,17 @@ namespace Barotrauma.Items.Components
} }
} }
[Editable, HasDefaultValue(true, true)]
public bool CastShadows
{
get { return castShadows; }
set
{
castShadows = value;
if (light != null) light.CastShadows = value;
}
}
[Editable, HasDefaultValue(false, true)] [Editable, HasDefaultValue(false, true)]
public bool IsOn public bool IsOn
{ {
@@ -87,17 +100,20 @@ namespace Barotrauma.Items.Components
public LightComponent(Item item, XElement element) public LightComponent(Item item, XElement element)
: base (item, element) : base (item, element)
{ {
light = new LightSource(item.Position, 100.0f, Color.White, item.CurrentHull == null ? null : item.CurrentHull.Submarine); light = new LightSource(element);
light.Submarine = item.CurrentHull == null ? null : item.CurrentHull.Submarine;
light.Position = item.Position;
light.CastShadows = castShadows;
IsActive = true; IsActive = true;
foreach (XElement subElement in element.Elements()) //foreach (XElement subElement in element.Elements())
{ //{
if (subElement.Name.ToString().ToLowerInvariant() != "sprite") continue; // if (subElement.Name.ToString().ToLowerInvariant() != "sprite") continue;
light.LightSprite = new Sprite(subElement);
light.LightSprite.Origin = light.LightSprite.size / 2.0f; // light.LightSprite = new Sprite(subElement);
break; // break;
} //}
} }
public override void Update(float deltaTime, Camera cam) public override void Update(float deltaTime, Camera cam)
@@ -115,6 +131,7 @@ namespace Barotrauma.Items.Components
if (item.body != null) if (item.body != null)
{ {
light.Position = item.Position; light.Position = item.Position;
light.Rotation = item.body.Dir > 0.0f ? item.body.Rotation : item.body.Rotation - MathHelper.Pi;
if (!item.body.Enabled) if (!item.body.Enabled)
{ {
@@ -150,9 +167,9 @@ namespace Barotrauma.Items.Components
public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, bool editing = false) public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch, bool editing = false)
{ {
if (light.LightSprite != null && (item.body==null || item.body.Enabled)) if (light.LightSprite != null && (item.body == null || item.body.Enabled))
{ {
light.LightSprite.Draw(spriteBatch, new Vector2(item.DrawPosition.X, -item.DrawPosition.Y), lightColor * lightBrightness, 0.0f, 1.0f, Microsoft.Xna.Framework.Graphics.SpriteEffects.None, item.Sprite.Depth-0.0001f); light.LightSprite.Draw(spriteBatch, new Vector2(item.DrawPosition.X, -item.DrawPosition.Y), lightColor * lightBrightness, 0.0f, 1.0f, Microsoft.Xna.Framework.Graphics.SpriteEffects.None, item.Sprite.Depth - 0.0001f);
} }
} }
+6 -8
View File
@@ -229,17 +229,15 @@ namespace Barotrauma
ImpactTolerance = ToolBox.GetAttributeFloat(element, "impacttolerance", 0.0f); ImpactTolerance = ToolBox.GetAttributeFloat(element, "impacttolerance", 0.0f);
string categoriesStr = ToolBox.GetAttributeString(element, "category", "Misc"); MapEntityCategory category;
string[] categories = categoriesStr.Split(',');
for (int i = 0; i < categories.Length; i++) if (!Enum.TryParse(ToolBox.GetAttributeString(element, "category", "Misc"), true, out category))
{ {
MapEntityCategory category; category = MapEntityCategory.Misc;
if (Enum.TryParse(ToolBox.GetAttributeString(element, "category", "Misc"), out category))
{
Category = i == 0 ? category : Category | category;
}
} }
Category = category;
string spriteColorStr = ToolBox.GetAttributeString(element, "spritecolor", "1.0,1.0,1.0,1.0"); string spriteColorStr = ToolBox.GetAttributeString(element, "spritecolor", "1.0,1.0,1.0,1.0");
SpriteColor = new Color(ToolBox.ParseToVector4(spriteColorStr)); SpriteColor = new Color(ToolBox.ParseToVector4(spriteColorStr));
+15 -5
View File
@@ -64,6 +64,12 @@ namespace Barotrauma.Lights
private Rectangle boundingBox; private Rectangle boundingBox;
public Entity ParentEntity
{
get { return parentEntity; }
}
public bool Enabled public bool Enabled
{ {
get; get;
@@ -302,9 +308,13 @@ namespace Barotrauma.Lights
{ {
if (!Enabled) return; if (!Enabled) return;
Vector2 lightSourcePos = light.Position;
if (light.Submarine==null && parentEntity != null && parentEntity.Submarine != null) lightSourcePos -= parentEntity.Submarine.Position;
CachedShadow cachedShadow = null; CachedShadow cachedShadow = null;
if (cachedShadows.TryGetValue(light, out cachedShadow) && if (cachedShadows.TryGetValue(light, out cachedShadow) &&
(light.Position == cachedShadow.LightPos || Vector2.DistanceSquared(light.Position, cachedShadow.LightPos) < 1.0f)) (lightSourcePos == cachedShadow.LightPos || Vector2.DistanceSquared(lightSourcePos, cachedShadow.LightPos) < 1.0f))
{ {
} }
@@ -315,17 +325,17 @@ namespace Barotrauma.Lights
// lightPos = light.Position; // lightPos = light.Position;
//} //}
CalculateShadowVertices(light.Position, los); CalculateShadowVertices(lightSourcePos, los);
if (cachedShadow != null) if (cachedShadow != null)
{ {
cachedShadow.LightPos = light.Position; cachedShadow.LightPos = lightSourcePos;
cachedShadow.ShadowBuffer.SetData(shadowVertices, 0, shadowVertices.Length); cachedShadow.ShadowBuffer.SetData(shadowVertices, 0, shadowVertices.Length);
cachedShadow.ShadowVertexCount = shadowVertexCount; cachedShadow.ShadowVertexCount = shadowVertexCount;
} }
else else
{ {
cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, light.Position, shadowVertexCount, 0); cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, lightSourcePos, shadowVertexCount, 0);
RemoveCachedShadow(light); RemoveCachedShadow(light);
cachedShadows.Add(light, cachedShadow); cachedShadows.Add(light, cachedShadow);
} }
+4 -7
View File
@@ -2,6 +2,7 @@
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
namespace Barotrauma.Lights namespace Barotrauma.Lights
{ {
@@ -122,7 +123,7 @@ namespace Barotrauma.Lights
foreach (LightSource light in lights) foreach (LightSource light in lights)
{ {
if (light.hullsInRange.Count == 0 || light.Color.A < 0.01f || light.Range < 1.0f) continue; if (!light.hullsInRange.Any() || light.Color.A < 0.01f || light.Range < 1.0f) continue;
if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue; if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue;
//clear alpha to 1 //clear alpha to 1
@@ -135,7 +136,7 @@ namespace Barotrauma.Lights
foreach (ConvexHull ch in light.hullsInRange) foreach (ConvexHull ch in light.hullsInRange)
{ {
if (!MathUtils.CircleIntersectsRectangle(light.Position, light.Range, ch.BoundingBox)) continue; //if (!MathUtils.CircleIntersectsRectangle(light.Position, light.Range, ch.BoundingBox)) continue;
//draw shadow //draw shadow
ch.DrawShadows(graphics, cam, light, shadowTransform, false); ch.DrawShadows(graphics, cam, light, shadowTransform, false);
} }
@@ -152,7 +153,7 @@ namespace Barotrauma.Lights
foreach (LightSource light in lights) foreach (LightSource light in lights)
{ {
if (light.hullsInRange.Count > 0 || light.Color.A < 0.01f) continue; if (light.hullsInRange.Any() || light.Color.A < 0.01f) continue;
//if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue; //if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue;
light.Draw(spriteBatch); light.Draw(spriteBatch);
@@ -163,10 +164,6 @@ namespace Barotrauma.Lights
//clear alpha, to avoid messing stuff up later //clear alpha, to avoid messing stuff up later
ClearAlphaToOne(graphics, spriteBatch); ClearAlphaToOne(graphics, spriteBatch);
graphics.SetRenderTarget(null); graphics.SetRenderTarget(null);
if (!ObstructVision) return;
} }
public void UpdateObstructVision(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 lookAtPosition) public void UpdateObstructVision(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 lookAtPosition)
+52 -10
View File
@@ -22,8 +22,12 @@ namespace Barotrauma.Lights
public Sprite LightSprite; public Sprite LightSprite;
private Sprite overrideLightTexture;
public Entity Submarine; public Entity Submarine;
public bool CastShadows;
//what was the range of the light when HullsInRange were last updated //what was the range of the light when HullsInRange were last updated
private float prevHullUpdateRange; private float prevHullUpdateRange;
@@ -45,6 +49,12 @@ namespace Barotrauma.Lights
} }
} }
public float Rotation
{
get;
set;
}
public Vector2 WorldPosition public Vector2 WorldPosition
{ {
get { return (Submarine == null) ? position : position + Submarine.Position; } get { return (Submarine == null) ? position : position + Submarine.Position; }
@@ -84,17 +94,24 @@ namespace Barotrauma.Lights
} }
public LightSource (XElement element) public LightSource (XElement element)
:this(Vector2.Zero, 100.0f, Color.White, null) : this(Vector2.Zero, 100.0f, Color.White, null)
{ {
float range = ToolBox.GetAttributeFloat(element, "range", 100.0f); float range = ToolBox.GetAttributeFloat(element, "range", 100.0f);
Color color = new Color(ToolBox.GetAttributeVector4(element, "color", Vector4.One)); Color color = new Color(ToolBox.GetAttributeVector4(element, "color", Vector4.One));
CastShadows = ToolBox.GetAttributeBool(element, "castshadows", true);
foreach (XElement subElement in element.Elements()) foreach (XElement subElement in element.Elements())
{ {
if (subElement.Name.ToString().ToLowerInvariant() != "sprite") continue; switch (subElement.Name.ToString().ToLowerInvariant())
{
LightSprite = new Sprite(subElement); case "sprite":
LightSprite.Origin = LightSprite.size / 2.0f; LightSprite = new Sprite(subElement);
break;
case "lighttexture":
overrideLightTexture = new Sprite(subElement);
break;
}
} }
} }
@@ -108,6 +125,8 @@ namespace Barotrauma.Lights
this.range = range; this.range = range;
this.color = color; this.color = color;
CastShadows = true;
texture = LightTexture; texture = LightTexture;
GameMain.LightManager.AddLight(this); GameMain.LightManager.AddLight(this);
@@ -115,12 +134,25 @@ namespace Barotrauma.Lights
public void UpdateHullsInRange() public void UpdateHullsInRange()
{ {
if (!CastShadows) return;
hullsInRange.Clear(); hullsInRange.Clear();
if (range < 1.0f || color.A < 0.01f) return; if (range < 1.0f || color.A < 0.01f) return;
foreach (ConvexHull ch in ConvexHull.list) foreach (ConvexHull ch in ConvexHull.list)
{ {
if (MathUtils.CircleIntersectsRectangle(position, range, ch.BoundingBox)) hullsInRange.Add(ch); if (Submarine == null && ch.ParentEntity.Submarine != null)
{
if (MathUtils.CircleIntersectsRectangle(position - ch.ParentEntity.Submarine.Position, range, ch.BoundingBox))
{
hullsInRange.Add(ch);
}
}
else if (MathUtils.CircleIntersectsRectangle(position, range, ch.BoundingBox))
{
hullsInRange.Add(ch);
}
} }
} }
@@ -128,15 +160,25 @@ namespace Barotrauma.Lights
{ {
if (range > 1.0f) if (range > 1.0f)
{ {
Vector2 center = new Vector2(LightTexture.Width / 2, LightTexture.Height / 2); if (overrideLightTexture == null)
float scale = range / (lightTexture.Width / 2.0f); {
Vector2 center = new Vector2(LightTexture.Width / 2, LightTexture.Height / 2);
float scale = range / (lightTexture.Width / 2.0f);
spriteBatch.Draw(lightTexture, new Vector2(WorldPosition.X, -WorldPosition.Y), null, color, 0, center, scale, SpriteEffects.None, 1); spriteBatch.Draw(lightTexture, new Vector2(WorldPosition.X, -WorldPosition.Y), null, color, 0, center, scale, SpriteEffects.None, 1);
}
else
{
overrideLightTexture.Draw(spriteBatch,
new Vector2(WorldPosition.X, -WorldPosition.Y), Color,
overrideLightTexture.Origin, -Rotation,
new Vector2(overrideLightTexture.size.X/overrideLightTexture.SourceRect.Width, overrideLightTexture.size.Y/overrideLightTexture.SourceRect.Height));
}
} }
if (LightSprite != null) if (LightSprite != null)
{ {
LightSprite.Draw(spriteBatch, new Vector2(WorldPosition.X, -WorldPosition.Y), Color); LightSprite.Draw(spriteBatch, new Vector2(WorldPosition.X, -WorldPosition.Y), Color, LightSprite.Origin);
} }
} }