- 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

View File

@@ -133,7 +133,11 @@
<Propulsion force="80" usablein="water" particles="bubbles">
<RequiredItems name="Battery Cell,Fulgurium Battery Cell" type="Contained" msg="Battery Cell required"/>
<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>

View File

@@ -295,7 +295,7 @@
<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"/>
</LightComponent>

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

View File

@@ -11,6 +11,8 @@ namespace Barotrauma.Items.Components
private string particles;
private float useState;
private ParticlePrefab.DrawTargetType usableIn;
[HasDefaultValue(0.0f, false)]
@@ -50,6 +52,10 @@ namespace Barotrauma.Items.Components
if (character == null) return false;
if (!character.IsKeyDown(InputType.Aim) || character.Stun>0.0f) return false;
IsActive = true;
useState = 0.1f;
if (character.AnimController.InWater)
{
if (usableIn == ParticlePrefab.DrawTargetType.Air) return true;
@@ -91,12 +97,13 @@ namespace Barotrauma.Items.Components
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;
}
}
}

View File

@@ -18,6 +18,8 @@ namespace Barotrauma.Items.Components
private float flicker;
private bool castShadows;
[Editable, HasDefaultValue(100.0f, true)]
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)]
public bool IsOn
{
@@ -87,17 +100,20 @@ namespace Barotrauma.Items.Components
public LightComponent(Item item, XElement 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;
foreach (XElement subElement in element.Elements())
{
if (subElement.Name.ToString().ToLowerInvariant() != "sprite") continue;
light.LightSprite = new Sprite(subElement);
light.LightSprite.Origin = light.LightSprite.size / 2.0f;
break;
}
//foreach (XElement subElement in element.Elements())
//{
// if (subElement.Name.ToString().ToLowerInvariant() != "sprite") continue;
// light.LightSprite = new Sprite(subElement);
// break;
//}
}
public override void Update(float deltaTime, Camera cam)
@@ -115,6 +131,7 @@ namespace Barotrauma.Items.Components
if (item.body != null)
{
light.Position = item.Position;
light.Rotation = item.body.Dir > 0.0f ? item.body.Rotation : item.body.Rotation - MathHelper.Pi;
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)
{
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);
}
}

View File

@@ -229,17 +229,15 @@ namespace Barotrauma
ImpactTolerance = ToolBox.GetAttributeFloat(element, "impacttolerance", 0.0f);
string categoriesStr = ToolBox.GetAttributeString(element, "category", "Misc");
string[] categories = categoriesStr.Split(',');
MapEntityCategory category;
for (int i = 0; i < categories.Length; i++)
if (!Enum.TryParse(ToolBox.GetAttributeString(element, "category", "Misc"), true, out category))
{
MapEntityCategory category;
if (Enum.TryParse(ToolBox.GetAttributeString(element, "category", "Misc"), out category))
{
Category = i == 0 ? category : Category | category;
}
category = MapEntityCategory.Misc;
}
Category = category;
string spriteColorStr = ToolBox.GetAttributeString(element, "spritecolor", "1.0,1.0,1.0,1.0");
SpriteColor = new Color(ToolBox.ParseToVector4(spriteColorStr));

View File

@@ -64,6 +64,12 @@ namespace Barotrauma.Lights
private Rectangle boundingBox;
public Entity ParentEntity
{
get { return parentEntity; }
}
public bool Enabled
{
get;
@@ -302,9 +308,13 @@ namespace Barotrauma.Lights
{
if (!Enabled) return;
Vector2 lightSourcePos = light.Position;
if (light.Submarine==null && parentEntity != null && parentEntity.Submarine != null) lightSourcePos -= parentEntity.Submarine.Position;
CachedShadow cachedShadow = null;
if (cachedShadows.TryGetValue(light, out cachedShadow) &&
(light.Position == cachedShadow.LightPos || Vector2.DistanceSquared(light.Position, cachedShadow.LightPos) < 1.0f))
if (cachedShadows.TryGetValue(light, out cachedShadow) &&
(lightSourcePos == cachedShadow.LightPos || Vector2.DistanceSquared(lightSourcePos, cachedShadow.LightPos) < 1.0f))
{
}
@@ -315,17 +325,17 @@ namespace Barotrauma.Lights
// lightPos = light.Position;
//}
CalculateShadowVertices(light.Position, los);
CalculateShadowVertices(lightSourcePos, los);
if (cachedShadow != null)
{
cachedShadow.LightPos = light.Position;
cachedShadow.LightPos = lightSourcePos;
cachedShadow.ShadowBuffer.SetData(shadowVertices, 0, shadowVertices.Length);
cachedShadow.ShadowVertexCount = shadowVertexCount;
}
else
{
cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, light.Position, shadowVertexCount, 0);
cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, lightSourcePos, shadowVertexCount, 0);
RemoveCachedShadow(light);
cachedShadows.Add(light, cachedShadow);
}

View File

@@ -2,6 +2,7 @@
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Barotrauma.Lights
{
@@ -122,7 +123,7 @@ namespace Barotrauma.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;
//clear alpha to 1
@@ -135,7 +136,7 @@ namespace Barotrauma.Lights
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
ch.DrawShadows(graphics, cam, light, shadowTransform, false);
}
@@ -152,7 +153,7 @@ namespace Barotrauma.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;
light.Draw(spriteBatch);
@@ -163,10 +164,6 @@ namespace Barotrauma.Lights
//clear alpha, to avoid messing stuff up later
ClearAlphaToOne(graphics, spriteBatch);
graphics.SetRenderTarget(null);
if (!ObstructVision) return;
}
public void UpdateObstructVision(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 lookAtPosition)

View File

@@ -22,8 +22,12 @@ namespace Barotrauma.Lights
public Sprite LightSprite;
private Sprite overrideLightTexture;
public Entity Submarine;
public bool CastShadows;
//what was the range of the light when HullsInRange were last updated
private float prevHullUpdateRange;
@@ -45,6 +49,12 @@ namespace Barotrauma.Lights
}
}
public float Rotation
{
get;
set;
}
public Vector2 WorldPosition
{
get { return (Submarine == null) ? position : position + Submarine.Position; }
@@ -84,17 +94,24 @@ namespace Barotrauma.Lights
}
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);
Color color = new Color(ToolBox.GetAttributeVector4(element, "color", Vector4.One));
CastShadows = ToolBox.GetAttributeBool(element, "castshadows", true);
foreach (XElement subElement in element.Elements())
{
if (subElement.Name.ToString().ToLowerInvariant() != "sprite") continue;
LightSprite = new Sprite(subElement);
LightSprite.Origin = LightSprite.size / 2.0f;
switch (subElement.Name.ToString().ToLowerInvariant())
{
case "sprite":
LightSprite = new Sprite(subElement);
break;
case "lighttexture":
overrideLightTexture = new Sprite(subElement);
break;
}
}
}
@@ -108,6 +125,8 @@ namespace Barotrauma.Lights
this.range = range;
this.color = color;
CastShadows = true;
texture = LightTexture;
GameMain.LightManager.AddLight(this);
@@ -115,12 +134,25 @@ namespace Barotrauma.Lights
public void UpdateHullsInRange()
{
if (!CastShadows) return;
hullsInRange.Clear();
if (range < 1.0f || color.A < 0.01f) return;
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)
{
Vector2 center = new Vector2(LightTexture.Width / 2, LightTexture.Height / 2);
float scale = range / (lightTexture.Width / 2.0f);
if (overrideLightTexture == null)
{
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)
{
LightSprite.Draw(spriteBatch, new Vector2(WorldPosition.X, -WorldPosition.Y), Color);
LightSprite.Draw(spriteBatch, new Vector2(WorldPosition.X, -WorldPosition.Y), Color, LightSprite.Origin);
}
}