Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Items/Components/LightComponent.cs
2 using Barotrauma.Lights;
4 using Microsoft.Xna.Framework;
5 using Microsoft.Xna.Framework.Graphics;
6 using System.Collections.Generic;
7 
9 {
10  partial class LightComponent : Powered, IServerSerializable, IDrawableComponent
11  {
12  private bool? lastReceivedState;
13 
14  private CoroutineHandle resetPredictionCoroutine;
15  private float resetPredictionTimer;
16 
22  private float lightColorMultiplier;
23 
24  [Serialize(1.0f, IsPropertySaveable.Yes, description: "The scale of the light sprite.")]
25  public float LightSpriteScale { get; set; }
26 
27  public Vector2 DrawSize
28  {
29  get { return new Vector2(Light.Range * 2, Light.Range * 2); }
30  }
31 
32  public LightSource Light { get; }
33 
34  public override void OnScaleChanged()
35  {
36  Light.SpriteScale = Vector2.One * item.Scale;
38  }
39 
40  partial void SetLightSourceState(bool enabled, float brightness)
41  {
42  if (Light == null) { return; }
43  if (item.IsHidden) { enabled = false; }
44  Light.Enabled = enabled;
45  lightColorMultiplier = brightness;
46  if (enabled)
47  {
48  Light.Color = LightColor.Multiply(lightColorMultiplier);
49  }
50  }
51 
52  partial void SetLightSourceTransformProjSpecific()
53  {
54  if (ParentBody != null)
55  {
57  }
58  else if (turret != null)
59  {
60  Light.Position = new Vector2(item.Rect.X + turret.TransformedBarrelPos.X, item.Rect.Y - turret.TransformedBarrelPos.Y);
61  }
62  else if (item.body != null)
63  {
65  }
66  else
67  {
69  }
70  PhysicsBody body = Light.ParentBody;
71  if (body != null && body.Enabled)
72  {
73  Light.Rotation = body.Dir > 0.0f ? body.DrawRotation : body.DrawRotation - MathHelper.Pi;
74  Light.LightSpriteEffect = (body.Dir > 0.0f) ? SpriteEffects.None : SpriteEffects.FlipVertically;
75  }
76  else
77  {
80  }
81  }
82 
83  public void Draw(SpriteBatch spriteBatch, bool editing = false, float itemDepth = -1, Color? overrideColor = null)
84  {
85  if (Light?.LightSprite == null) { return; }
86  if ((item.body == null || item.body.Enabled) && lightBrightness > 0.0f && IsOn && Light.Enabled)
87  {
88  Vector2 origin = Light.LightSprite.Origin;
89  if ((Light.LightSpriteEffect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally) { origin.X = Light.LightSprite.SourceRect.Width - origin.X; }
90  if ((Light.LightSpriteEffect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically) { origin.Y = Light.LightSprite.SourceRect.Height - origin.Y; }
91 
92  Vector2 drawPos = item.body?.DrawPosition ?? item.DrawPosition;
93 
94  Color color = lightColor;
95  if (Light.OverrideLightSpriteAlpha.HasValue)
96  {
97  color = new Color(lightColor, Light.OverrideLightSpriteAlpha.Value);
98  }
99  Light.LightSprite.Draw(spriteBatch,
100  new Vector2(drawPos.X, -drawPos.Y),
101  color * lightBrightness,
102  origin,
103  -Light.Rotation,
105  Light.LightSpriteEffect, itemDepth - 0.0001f);
106  }
107  }
108 
109  public override void FlipX(bool relativeToSub)
110  {
111  if (Light?.LightSprite != null && item.Prefab.CanSpriteFlipX)
112  {
113  Light.LightSpriteEffect = Light.LightSpriteEffect == SpriteEffects.None ?
114  SpriteEffects.FlipHorizontally : SpriteEffects.None;
115  }
116  SetLightSourceTransformProjSpecific();
117  }
118 
119  partial void OnStateChanged()
120  {
121  if (GameMain.Client == null || !lastReceivedState.HasValue) { return; }
122  //reset to last known server state after the state hasn't changed in 1.0 seconds client-side
123  resetPredictionTimer = 1.0f;
124  if (resetPredictionCoroutine == null || !CoroutineManager.IsCoroutineRunning(resetPredictionCoroutine))
125  {
126  resetPredictionCoroutine = CoroutineManager.StartCoroutine(ResetPredictionAfterDelay());
127  }
128  }
129 
133  private IEnumerable<CoroutineStatus> ResetPredictionAfterDelay()
134  {
135  while (resetPredictionTimer > 0.0f)
136  {
137  resetPredictionTimer -= CoroutineManager.DeltaTime;
138  yield return CoroutineStatus.Running;
139  }
140  if (lastReceivedState.HasValue) { IsActive = lastReceivedState.Value; }
141  resetPredictionCoroutine = null;
142  yield return CoroutineStatus.Success;
143  }
144 
145  public void ClientEventRead(IReadMessage msg, float sendingTime)
146  {
147  IsActive = msg.ReadBoolean();
148  lastReceivedState = IsActive;
149  }
150 
151  protected override void RemoveComponentSpecific()
152  {
153  base.RemoveComponentSpecific();
154  Light.Remove();
155  }
156  }
157 }
virtual Vector2 DrawPosition
Definition: Entity.cs:51
static GameClient Client
Definition: GameMain.cs:188
void Draw(SpriteBatch spriteBatch, bool editing=false, float itemDepth=-1, Color? overrideColor=null)
bool IsHidden
Is the entity hidden due to HiddenInGame being enabled or the layer the entity is in being hidden?
void Draw(ISpriteBatch spriteBatch, Vector2 pos, float rotate=0.0f, float scale=1.0f, SpriteEffects spriteEffect=SpriteEffects.None)
Interface for entities that the server can send events to the clients