(61d00a474) v0.9.7.1

This commit is contained in:
Regalis
2020-03-04 13:04:10 +01:00
parent 3c50efa5c9
commit 3c09ebe02f
5086 changed files with 786063 additions and 295871 deletions
@@ -0,0 +1,296 @@
using Barotrauma.Lights;
using Barotrauma.Particles;
using Barotrauma.Sounds;
using Barotrauma.Networking;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Barotrauma.SpriteDeformations;
using System.Linq;
using FarseerPhysics.Dynamics;
namespace Barotrauma
{
partial class LevelObject
{
public float SwingTimer;
public float ScaleOscillateTimer;
public float CurrentSwingAmount;
public Vector2 CurrentScaleOscillation;
public float CurrentRotation;
private List<SpriteDeformation> spriteDeformations = new List<SpriteDeformation>();
public Vector2 CurrentScale
{
get;
private set;
} = Vector2.One;
public LightSource[] LightSources
{
get;
private set;
}
public LevelTrigger[] LightSourceTriggers
{
get;
private set;
}
public ParticleEmitter[] ParticleEmitters
{
get;
private set;
}
public LevelTrigger[] ParticleEmitterTriggers
{
get;
private set;
}
public RoundSound[] Sounds
{
get;
private set;
}
public SoundChannel[] SoundChannels
{
get;
private set;
}
public LevelTrigger[] SoundTriggers
{
get;
private set;
}
public Vector2[,] CurrentSpriteDeformation
{
get;
private set;
}
partial void InitProjSpecific()
{
Sprite?.EnsureLazyLoaded();
SpecularSprite?.EnsureLazyLoaded();
Prefab.DeformableSprite?.EnsureLazyLoaded();
CurrentSwingAmount = Prefab.SwingAmountRad;
CurrentScaleOscillation = Prefab.ScaleOscillation;
SwingTimer = Rand.Range(0.0f, MathHelper.TwoPi);
ScaleOscillateTimer = Rand.Range(0.0f, MathHelper.TwoPi);
if (Prefab.ParticleEmitterPrefabs != null)
{
ParticleEmitters = new ParticleEmitter[Prefab.ParticleEmitterPrefabs.Count];
ParticleEmitterTriggers = new LevelTrigger[Prefab.ParticleEmitterPrefabs.Count];
for (int i = 0; i < Prefab.ParticleEmitterPrefabs.Count; i++)
{
ParticleEmitters[i] = new ParticleEmitter(Prefab.ParticleEmitterPrefabs[i]);
ParticleEmitterTriggers[i] = Prefab.ParticleEmitterTriggerIndex[i] > -1 ?
Triggers[Prefab.ParticleEmitterTriggerIndex[i]] : null;
}
}
if (Prefab.LightSourceParams != null)
{
LightSources = new LightSource[Prefab.LightSourceParams.Count];
LightSourceTriggers = new LevelTrigger[Prefab.LightSourceParams.Count];
for (int i = 0; i < Prefab.LightSourceParams.Count; i++)
{
LightSources[i] = new LightSource(Prefab.LightSourceParams[i])
{
Position = new Vector2(Position.X, Position.Y),
IsBackground = true
};
LightSourceTriggers[i] = Prefab.LightSourceTriggerIndex[i] > -1 ?
Triggers[Prefab.LightSourceTriggerIndex[i]] : null;
}
}
Sounds = new RoundSound[Prefab.Sounds.Count];
SoundChannels = new SoundChannel[Prefab.Sounds.Count];
SoundTriggers = new LevelTrigger[Prefab.Sounds.Count];
for (int i = 0; i < Prefab.Sounds.Count; i++)
{
Sounds[i] = Submarine.LoadRoundSound(Prefab.Sounds[i].SoundElement, false);
SoundTriggers[i] = Prefab.Sounds[i].TriggerIndex > -1 ? Triggers[Prefab.Sounds[i].TriggerIndex] : null;
}
int j = 0;
foreach (XElement subElement in Prefab.Config.Elements())
{
if (!subElement.Name.ToString().Equals("deformablesprite", StringComparison.OrdinalIgnoreCase)) { continue; }
foreach (XElement animationElement in subElement.Elements())
{
var newDeformation = SpriteDeformation.Load(animationElement, Prefab.Name);
if (newDeformation != null)
{
newDeformation.Params = Prefab.SpriteDeformations[j].Params;
spriteDeformations.Add(newDeformation);
j++;
}
}
}
}
public void Update(float deltaTime)
{
if (ParticleEmitters != null)
{
for (int i = 0; i < ParticleEmitters.Length; i++)
{
if (ParticleEmitterTriggers[i] != null && !ParticleEmitterTriggers[i].IsTriggered) continue;
Vector2 emitterPos = LocalToWorld(Prefab.EmitterPositions[i]);
ParticleEmitters[i].Emit(deltaTime, emitterPos, hullGuess: null,
angle: ParticleEmitters[i].Prefab.CopyEntityAngle ? Rotation : 0.0f);
}
}
CurrentRotation = Rotation;
if (ActivePrefab.SwingFrequency > 0.0f)
{
SwingTimer += deltaTime * ActivePrefab.SwingFrequency;
SwingTimer = SwingTimer % MathHelper.TwoPi;
//lerp the swing amount to the correct value to prevent it from abruptly changing to a different value
//when a trigger changes the swing amoung
CurrentSwingAmount = MathHelper.Lerp(CurrentSwingAmount, ActivePrefab.SwingAmountRad, deltaTime * 10.0f);
if (ActivePrefab.SwingAmountRad > 0.0f)
{
CurrentRotation += (float)Math.Sin(SwingTimer) * CurrentSwingAmount;
}
}
CurrentScale = Vector2.One * Scale;
if (ActivePrefab.ScaleOscillationFrequency > 0.0f)
{
ScaleOscillateTimer += deltaTime * ActivePrefab.ScaleOscillationFrequency;
ScaleOscillateTimer = ScaleOscillateTimer % MathHelper.TwoPi;
CurrentScaleOscillation = Vector2.Lerp(CurrentScaleOscillation, ActivePrefab.ScaleOscillation, deltaTime * 10.0f);
float sin = (float)Math.Sin(ScaleOscillateTimer);
CurrentScale *= new Vector2(
1.0f + sin * CurrentScaleOscillation.X,
1.0f + sin * CurrentScaleOscillation.Y);
}
if (LightSources != null)
{
for (int i = 0; i < LightSources.Length; i++)
{
if (LightSourceTriggers[i] != null) LightSources[i].Enabled = LightSourceTriggers[i].IsTriggered;
LightSources[i].Rotation = -CurrentRotation;
LightSources[i].SpriteScale = CurrentScale;
}
}
if (spriteDeformations.Count > 0)
{
UpdateDeformations(deltaTime);
}
for (int i = 0; i < Sounds.Length; i++)
{
if (Sounds[i] == null) { continue; }
if (SoundTriggers[i] == null || SoundTriggers[i].IsTriggered)
{
RoundSound roundSound = Sounds[i];
Vector2 soundPos = LocalToWorld(new Vector2(Prefab.Sounds[i].Position.X, Prefab.Sounds[i].Position.Y));
if (Vector2.DistanceSquared(new Vector2(GameMain.SoundManager.ListenerPosition.X, GameMain.SoundManager.ListenerPosition.Y), soundPos) <
roundSound.Range * roundSound.Range)
{
if (SoundChannels[i] == null || !SoundChannels[i].IsPlaying)
{
SoundChannels[i] = roundSound.Sound.Play(roundSound.Volume, roundSound.Range, soundPos);
}
SoundChannels[i].Position = new Vector3(soundPos.X, soundPos.Y, 0.0f);
}
}
else if (SoundChannels[i] != null && SoundChannels[i].IsPlaying)
{
SoundChannels[i].FadeOutAndDispose();
SoundChannels[i] = null;
}
}
}
private void UpdateDeformations(float deltaTime)
{
foreach (SpriteDeformation deformation in spriteDeformations)
{
if (deformation is PositionalDeformation positionalDeformation)
{
UpdatePositionalDeformation(positionalDeformation, deltaTime);
}
deformation.Update(deltaTime);
}
CurrentSpriteDeformation = SpriteDeformation.GetDeformation(spriteDeformations, ActivePrefab.DeformableSprite.Size);
foreach (LightSource lightSource in LightSources)
{
if (lightSource?.DeformableLightSprite != null)
{
lightSource.DeformableLightSprite.Deform(CurrentSpriteDeformation);
}
}
}
private void UpdatePositionalDeformation(PositionalDeformation positionalDeformation, float deltaTime)
{
Matrix matrix = ActivePrefab.DeformableSprite.GetTransform(
Position,
ActivePrefab.DeformableSprite.Origin,
CurrentRotation,
Vector2.One * Scale);
Matrix rotationMatrix = Matrix.CreateRotationZ(CurrentRotation);
foreach (LevelTrigger trigger in Triggers)
{
foreach (Entity triggerer in trigger.Triggerers)
{
Vector2 moveAmount = triggerer.WorldPosition - trigger.TriggererPosition[triggerer];
moveAmount = Vector2.Transform(moveAmount, rotationMatrix);
moveAmount /= (ActivePrefab.DeformableSprite.Size * Scale);
moveAmount.Y = -moveAmount.Y;
positionalDeformation.Deform(trigger.WorldPosition, moveAmount, deltaTime, Matrix.Invert(matrix) *
Matrix.CreateScale(1.0f / ActivePrefab.DeformableSprite.Size.X, 1.0f / ActivePrefab.DeformableSprite.Size.Y, 1));
}
}
}
public void ClientRead(IReadMessage msg)
{
for (int i = 0; i < Triggers.Count; i++)
{
if (!Triggers[i].UseNetworkSyncing) continue;
Triggers[i].ClientRead(msg);
}
}
partial void RemoveProjSpecific()
{
for (int i = 0; i < Sounds.Length; i++)
{
SoundChannels[i]?.Dispose();
SoundChannels[i] = null;
}
if (LightSources != null)
{
for (int i = 0; i < LightSources.Length; i++)
{
LightSources[i].Remove();
}
LightSources = null;
}
}
}
}
@@ -0,0 +1,173 @@
using Barotrauma.Networking;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Barotrauma
{
partial class LevelObjectManager
{
private List<LevelObject> visibleObjectsBack = new List<LevelObject>();
private List<LevelObject> visibleObjectsFront = new List<LevelObject>();
private Rectangle currentGridIndices;
partial void UpdateProjSpecific(float deltaTime)
{
foreach (LevelObject obj in visibleObjectsBack)
{
obj.Update(deltaTime);
}
foreach (LevelObject obj in visibleObjectsFront)
{
obj.Update(deltaTime);
}
}
public IEnumerable<LevelObject> GetVisibleObjects()
{
return visibleObjectsBack.Union(visibleObjectsFront);
}
/// <summary>
/// Checks which level objects are in camera view and adds them to the visibleObjects lists
/// </summary>
private void RefreshVisibleObjects(Rectangle currentIndices)
{
visibleObjectsBack.Clear();
visibleObjectsFront.Clear();
for (int x = currentIndices.X; x <= currentIndices.Width; x++)
{
for (int y = currentIndices.Y; y <= currentIndices.Height; y++)
{
if (objectGrid[x, y] == null) continue;
foreach (LevelObject obj in objectGrid[x, y])
{
var objectList = obj.Position.Z >= 0 ? visibleObjectsBack : visibleObjectsFront;
int drawOrderIndex = 0;
for (int i = 0; i < objectList.Count; i++)
{
if (objectList[i] == obj)
{
drawOrderIndex = -1;
break;
}
if (objectList[i].Position.Z < obj.Position.Z)
{
break;
}
else
{
drawOrderIndex = i + 1;
}
}
if (drawOrderIndex >= 0)
{
objectList.Insert(drawOrderIndex, obj);
}
}
}
}
currentGridIndices = currentIndices;
}
public void DrawObjects(SpriteBatch spriteBatch, Camera cam, bool drawFront, bool specular = false)
{
Rectangle indices = Rectangle.Empty;
indices.X = (int)Math.Floor(cam.WorldView.X / (float)GridSize);
if (indices.X >= objectGrid.GetLength(0)) return;
indices.Y = (int)Math.Floor((cam.WorldView.Y - cam.WorldView.Height - Level.Loaded.BottomPos) / (float)GridSize);
if (indices.Y >= objectGrid.GetLength(1)) return;
indices.Width = (int)Math.Floor(cam.WorldView.Right / (float)GridSize) + 1;
if (indices.Width < 0) return;
indices.Height = (int)Math.Floor((cam.WorldView.Y - Level.Loaded.BottomPos) / (float)GridSize) + 1;
if (indices.Height < 0) return;
indices.X = Math.Max(indices.X, 0);
indices.Y = Math.Max(indices.Y, 0);
indices.Width = Math.Min(indices.Width, objectGrid.GetLength(0) - 1);
indices.Height = Math.Min(indices.Height, objectGrid.GetLength(1) - 1);
float z = 0.0f;
if (currentGridIndices != indices)
{
RefreshVisibleObjects(indices);
}
var objectList = drawFront ? visibleObjectsFront : visibleObjectsBack;
foreach (LevelObject obj in objectList)
{
Vector2 camDiff = new Vector2(obj.Position.X, obj.Position.Y) - cam.WorldViewCenter;
camDiff.Y = -camDiff.Y;
Sprite activeSprite = specular ? obj.SpecularSprite : obj.Sprite;
activeSprite?.Draw(
spriteBatch,
new Vector2(obj.Position.X, -obj.Position.Y) - camDiff * obj.Position.Z / 10000.0f,
Color.Lerp(Color.White, Level.Loaded.BackgroundTextureColor, obj.Position.Z / 5000.0f),
activeSprite.Origin,
obj.CurrentRotation,
obj.CurrentScale,
SpriteEffects.None,
z);
if (specular) continue;
if (obj.ActivePrefab.DeformableSprite != null)
{
if (obj.CurrentSpriteDeformation != null)
{
obj.ActivePrefab.DeformableSprite.Deform(obj.CurrentSpriteDeformation);
}
else
{
obj.ActivePrefab.DeformableSprite.Reset();
}
obj.ActivePrefab.DeformableSprite?.Draw(cam,
new Vector3(new Vector2(obj.Position.X, obj.Position.Y) - camDiff * obj.Position.Z / 10000.0f, z * 10.0f),
obj.ActivePrefab.DeformableSprite.Origin,
obj.CurrentRotation,
obj.CurrentScale,
Color.Lerp(Color.White, Level.Loaded.BackgroundTextureColor, obj.Position.Z / 5000.0f));
}
if (GameMain.DebugDraw)
{
GUI.DrawRectangle(spriteBatch, new Vector2(obj.Position.X, -obj.Position.Y), new Vector2(10.0f, 10.0f), GUI.Style.Red, true);
foreach (LevelTrigger trigger in obj.Triggers)
{
if (trigger.PhysicsBody == null) continue;
GUI.DrawLine(spriteBatch, new Vector2(obj.Position.X, -obj.Position.Y), new Vector2(trigger.WorldPosition.X, -trigger.WorldPosition.Y), Color.Cyan, 0, 3);
Vector2 flowForce = trigger.GetWaterFlowVelocity();
if (flowForce.LengthSquared() > 1)
{
flowForce.Y = -flowForce.Y;
GUI.DrawLine(spriteBatch, new Vector2(trigger.WorldPosition.X, -trigger.WorldPosition.Y), new Vector2(trigger.WorldPosition.X, -trigger.WorldPosition.Y) + flowForce * 10, GUI.Style.Orange, 0, 5);
}
trigger.PhysicsBody.UpdateDrawPosition();
trigger.PhysicsBody.DebugDraw(spriteBatch, trigger.IsTriggered ? Color.Cyan : Color.DarkCyan);
}
}
z += 0.0001f;
}
}
public void ClientRead(ServerNetObject type, IReadMessage msg, float sendingTime)
{
int objIndex = msg.ReadRangedInteger(0, objects.Count);
objects[objIndex].ClientRead(msg);
}
}
}
@@ -0,0 +1,181 @@
using Barotrauma.Lights;
using Barotrauma.Particles;
using Barotrauma.SpriteDeformations;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Xml.Linq;
namespace Barotrauma
{
partial class LevelObjectPrefab
{
public class SoundConfig
{
public readonly XElement SoundElement;
public readonly Vector2 Position;
public readonly int TriggerIndex;
public SoundConfig(XElement element, int triggerIndex)
{
SoundElement = element;
Position = element.GetAttributeVector2("position", Vector2.Zero);
TriggerIndex = triggerIndex;
}
}
public List<int> ParticleEmitterTriggerIndex
{
get;
private set;
}
public List<ParticleEmitterPrefab> ParticleEmitterPrefabs
{
get;
private set;
}
public List<Vector2> EmitterPositions
{
get;
private set;
}
public List<SoundConfig> Sounds
{
get;
private set;
} = new List<SoundConfig>();
public List<int> LightSourceTriggerIndex
{
get;
private set;
} = new List<int>();
public List<LightSourceParams> LightSourceParams
{
get;
private set;
} = new List<Lights.LightSourceParams>();
/// <summary>
/// Only used for editing sprite deformation parameters. The actual LevelObjects use separate SpriteDeformation instances.
/// </summary>
public List<SpriteDeformation> SpriteDeformations
{
get;
private set;
} = new List<SpriteDeformation>();
partial void InitProjSpecific(XElement element)
{
LoadElementsProjSpecific(element, -1);
}
private void LoadElementsProjSpecific(XElement element, int parentTriggerIndex)
{
foreach (XElement subElement in element.Elements())
{
switch (subElement.Name.ToString().ToLowerInvariant())
{
case "leveltrigger":
case "trigger":
LoadElementsProjSpecific(subElement, LevelTriggerElements.IndexOf(subElement));
break;
case "lightsource":
LightSourceTriggerIndex.Add(parentTriggerIndex);
LightSourceParams.Add(new LightSourceParams(subElement));
break;
case "particleemitter":
if (ParticleEmitterPrefabs == null)
{
ParticleEmitterPrefabs = new List<ParticleEmitterPrefab>();
EmitterPositions = new List<Vector2>();
ParticleEmitterTriggerIndex = new List<int>();
}
ParticleEmitterPrefabs.Add(new ParticleEmitterPrefab(subElement));
ParticleEmitterTriggerIndex.Add(parentTriggerIndex);
EmitterPositions.Add(subElement.GetAttributeVector2("position", Vector2.Zero));
break;
case "sound":
Sounds.Add(new SoundConfig(subElement, parentTriggerIndex));
break;
case "deformablesprite":
foreach (XElement deformElement in subElement.Elements())
{
var deformation = SpriteDeformation.Load(deformElement, Name);
if (deformation != null)
{
SpriteDeformations.Add(deformation);
}
}
break;
}
}
}
public void Save(XElement element)
{
this.Config = element;
SerializableProperty.SerializeProperties(this, element);
foreach (XElement subElement in element.Elements().ToList())
{
switch (subElement.Name.ToString().ToLowerInvariant())
{
case "childobject":
case "lightsource":
subElement.Remove();
break;
case "deformablesprite":
subElement.RemoveNodes();
foreach (SpriteDeformation deformation in SpriteDeformations)
{
var deformationElement = new XElement("SpriteDeformation");
deformation.Save(deformationElement);
subElement.Add(deformationElement);
}
break;
}
}
foreach (LightSourceParams lightSourceParams in LightSourceParams)
{
var lightElement = new XElement("LightSource");
SerializableProperty.SerializeProperties(lightSourceParams, lightElement);
element.Add(lightElement);
}
foreach (ChildObject childObj in ChildObjects)
{
element.Add(new XElement("ChildObject",
new XAttribute("names", string.Join(", ", childObj.AllowedNames)),
new XAttribute("mincount", childObj.MinCount),
new XAttribute("maxcount", childObj.MaxCount)));
}
foreach (KeyValuePair<string, float> overrideCommonness in OverrideCommonness)
{
bool elementFound = false;
foreach (XElement subElement in element.Elements())
{
if (subElement.Name.ToString().Equals("overridecommonness", System.StringComparison.OrdinalIgnoreCase)
&& subElement.GetAttributeString("leveltype", "") == overrideCommonness.Key)
{
subElement.Attribute("commonness").Value = overrideCommonness.Value.ToString("G", CultureInfo.InvariantCulture);
elementFound = true;
break;
}
}
if (!elementFound)
{
element.Add(new XElement("overridecommonness",
new XAttribute("leveltype", overrideCommonness.Key),
new XAttribute("commonness", overrideCommonness.Value.ToString("G", CultureInfo.InvariantCulture))));
}
}
}
}
}
@@ -0,0 +1,19 @@
using Barotrauma.Networking;
namespace Barotrauma
{
partial class LevelTrigger
{
public void ClientRead(IReadMessage msg)
{
if (ForceFluctuationStrength > 0.0f)
{
currentForceFluctuation = msg.ReadRangedSingle(0.0f, 1.0f, 8);
}
if (stayTriggeredDelay > 0.0f)
{
triggeredTimer = msg.ReadRangedSingle(0.0f, stayTriggeredDelay, 16);
}
}
}
}