From 82dcbc3ae85b13617aba559ca387964af804d3b5 Mon Sep 17 00:00:00 2001 From: juanjp600 Date: Wed, 7 Mar 2018 22:53:28 -0300 Subject: [PATCH] Light sources now follow the direction of parent Turrets + changed Controller's output signal and Turret's input signal to an angle --- .../Source/Items/Components/Turret.cs | 20 +++++++ .../Items/Components/Machines/Controller.cs | 28 ++++++++- .../Items/Components/Signal/LightComponent.cs | 8 +++ .../Source/Items/Components/Turret.cs | 57 ++++++++++--------- 4 files changed, 84 insertions(+), 29 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Items/Components/Turret.cs b/Barotrauma/BarotraumaClient/Source/Items/Components/Turret.cs index 3af4b474c..3141ee613 100644 --- a/Barotrauma/BarotraumaClient/Source/Items/Components/Turret.cs +++ b/Barotrauma/BarotraumaClient/Source/Items/Components/Turret.cs @@ -37,6 +37,26 @@ namespace Barotrauma.Items.Components private set; } + partial void InitProjSpecific(XElement element) + { + foreach (XElement subElement in element.Elements()) + { + string texturePath = subElement.GetAttributeString("texture", ""); + switch (subElement.Name.ToString().ToLowerInvariant()) + { + case "crosshair": + crosshairSprite = new Sprite(subElement, texturePath.Contains("/") ? "" : Path.GetDirectoryName(item.Prefab.ConfigFile)); + break; + case "disabledcrosshair": + disabledCrossHairSprite = new Sprite(subElement, texturePath.Contains("/") ? "" : Path.GetDirectoryName(item.Prefab.ConfigFile)); + break; + } + } + + int barWidth = 200; + powerIndicator = new GUIProgressBar(new Rectangle(GameMain.GraphicsWidth / 2 - barWidth / 2, 20, barWidth, 30), Color.White, 0.0f); + } + public void Draw(SpriteBatch spriteBatch, bool editing = false) { Vector2 drawPos = new Vector2(item.Rect.X, item.Rect.Y); diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/Machines/Controller.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/Machines/Controller.cs index a463a688b..0cf2ede66 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/Machines/Controller.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/Machines/Controller.cs @@ -185,7 +185,14 @@ namespace Barotrauma.Items.Components Entity focusTarget = GetFocusTarget(); if (focusTarget == null) { - item.SendSignal(0, XMLExtensions.Vector2ToString(character.CursorWorldPosition), "position_out", character); + Vector2 centerPos = new Vector2(item.WorldRect.Center.X, item.WorldRect.Center.Y); + + Vector2 offset = character.CursorWorldPosition - centerPos; + offset.Y = -offset.Y; + + float targetRotation = MathUtils.WrapAngleTwoPi(MathUtils.VectorToAngle(offset)); + + item.SendSignal(0, targetRotation.ToString(), "position_out", character); return false; } @@ -202,7 +209,24 @@ namespace Barotrauma.Items.Components if (!character.IsRemotePlayer || character.ViewTarget == focusTarget) { - item.SendSignal(0, XMLExtensions.Vector2ToString(character.CursorWorldPosition), "position_out", character); + Vector2 centerPos = new Vector2(item.WorldRect.Center.X, item.WorldRect.Center.Y); + + Item targetItem = focusTarget as Item; + if (targetItem != null) + { + Turret turret = targetItem.GetComponent(); + if (turret != null) + { + centerPos = new Vector2(targetItem.WorldRect.X + turret.BarrelPos.X, targetItem.WorldRect.Y - turret.BarrelPos.Y); + } + } + + Vector2 offset = character.CursorWorldPosition - centerPos; + offset.Y = -offset.Y; + + float targetRotation = MathUtils.WrapAngleTwoPi(MathUtils.VectorToAngle(offset)); + + item.SendSignal(0, targetRotation.ToString(), "position_out", character); } return true; diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/Signal/LightComponent.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/Signal/LightComponent.cs index 44544993a..2a7081e61 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/Signal/LightComponent.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/Signal/LightComponent.cs @@ -37,6 +37,8 @@ namespace Barotrauma.Items.Components } } + public float Rotation; + [Editable(ToolTip = "Should structures cast shadows when light from this light source hits them. "+ "Disabling shadows increases the performance of the game, and is recommended for lights with a short range."), Serialize(true, true)] public bool CastShadows @@ -155,6 +157,12 @@ namespace Barotrauma.Items.Components return; } } + else + { +#if CLIENT + light.Rotation = -Rotation; +#endif + } if (powerConsumption == 0.0f) { diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/Turret.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/Turret.cs index 5af91073e..7ad0320a4 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/Turret.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/Turret.cs @@ -16,6 +16,9 @@ namespace Barotrauma.Items.Components Vector2 barrelPos; + bool? hasLight; + LightComponent lightComponent; + float rotation, targetRotation; float reload, reloadTime; @@ -62,8 +65,8 @@ namespace Barotrauma.Items.Components } set { - minRotation = MathHelper.ToRadians(value.X); - maxRotation = MathHelper.ToRadians(value.Y); + minRotation = MathHelper.ToRadians(Math.Min(value.X, value.Y)); + maxRotation = MathHelper.ToRadians(Math.Max(value.X, value.Y)); rotation = (minRotation + maxRotation) / 2; } @@ -88,28 +91,30 @@ namespace Barotrauma.Items.Components element.GetAttributeVector2("origin", Vector2.Zero)); } -#if CLIENT - foreach (XElement subElement in element.Elements()) - { - string texturePath = subElement.GetAttributeString("texture", ""); - switch (subElement.Name.ToString().ToLowerInvariant()) - { - case "crosshair": - crosshairSprite = new Sprite(subElement, texturePath.Contains("/") ? "" : Path.GetDirectoryName(item.Prefab.ConfigFile)); - break; - case "disabledcrosshair": - disabledCrossHairSprite = new Sprite(subElement, texturePath.Contains("/") ? "" : Path.GetDirectoryName(item.Prefab.ConfigFile)); - break; - } - } + hasLight = null; - int barWidth = 200; - powerIndicator = new GUIProgressBar(new Rectangle(GameMain.GraphicsWidth / 2 - barWidth / 2, 20, barWidth, 30 ), Color.White, 0.0f); -#endif + InitProjSpecific(element); } + partial void InitProjSpecific(XElement element); + public override void Update(float deltaTime, Camera cam) { + if (hasLight == null) + { + List lightComponents = item.GetComponents(); + + if (lightComponents != null && lightComponents.Count>0) + { + lightComponent = lightComponents.Find(lc => lc.Parent == this); + hasLight = (lightComponent != null); + } + else + { + hasLight = false; + } + } + this.cam = cam; if (reload > 0.0f) reload -= deltaTime; @@ -142,6 +147,11 @@ namespace Barotrauma.Items.Components { rotation = maxRotation; } + + if ((bool)hasLight) + { + lightComponent.Rotation = rotation; + } } public override bool Use(float deltaTime, Character character = null) @@ -396,14 +406,7 @@ namespace Barotrauma.Items.Components switch (connection.Name) { case "position_in": - Vector2 receivedPos = XMLExtensions.ParseVector2(signal, false); - - Vector2 centerPos = new Vector2(item.WorldRect.X + barrelPos.X, item.WorldRect.Y - barrelPos.Y); - - Vector2 offset = receivedPos - centerPos; - offset.Y = -offset.Y; - - targetRotation = MathUtils.WrapAngleTwoPi(MathUtils.VectorToAngle(offset)); + float.TryParse(signal, out targetRotation); IsActive = true;