Client LuaCsForBarotrauma
BarotraumaShared/SharedSource/Decals/Decal.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 
5 namespace Barotrauma
6 {
7  partial class Decal
8  {
9  public readonly DecalPrefab Prefab;
10  private Vector2 position;
11 
12  private float fadeTimer;
13 
14  public readonly Sprite Sprite;
15 
16  public float FadeTimer
17  {
18  get { return fadeTimer; }
19  set { fadeTimer = MathHelper.Clamp(value, 0.0f, LifeTime); }
20  }
21 
22  public float FadeInTime
23  {
24  get { return Prefab.FadeInTime; }
25  }
26 
27  public float FadeOutTime
28  {
29  get { return Prefab.FadeOutTime; }
30  }
31 
32  public float LifeTime
33  {
34  get { return Prefab.LifeTime; }
35  }
36 
37  public float BaseAlpha
38  {
39  get;
40  set;
41  } = 1.0f;
42 
43  public Color Color
44  {
45  get;
46  set;
47  }
48 
49  public Vector2 WorldPosition
50  {
51  get
52  {
53  Vector2 worldPos = position
54  + clippedSourceRect.Size.ToVector2() / 2 * Scale
55  + hull.Rect.Location.ToVector2();
56  if (hull.Submarine != null) { worldPos += hull.Submarine.DrawPosition; }
57  return worldPos;
58  }
59  }
60 
61  public Vector2 CenterPosition
62  {
63  get;
64  private set;
65  }
66 
67  public Vector2 NonClampedPosition
68  {
69  get;
70  private set;
71  }
72 
73  public int SpriteIndex
74  {
75  get;
76  private set;
77  }
78 
79  private readonly HashSet<BackgroundSection> affectedSections;
80 
81  private readonly Hull hull;
82 
83  public readonly float Scale;
84 
85  private Rectangle clippedSourceRect;
86 
87  private bool cleaned = false;
88 
89  public Decal(DecalPrefab prefab, float scale, Vector2 worldPosition, Hull hull, int? spriteIndex = null)
90  {
91  Prefab = prefab;
92 
93  this.hull = hull;
94 
95  //transform to hull-relative coordinates so we don't have to worry about the hull moving
96  NonClampedPosition = position = worldPosition - hull.WorldRect.Location.ToVector2();
97 
98  Vector2 drawPos = position + hull.Rect.Location.ToVector2();
99 
100  SpriteIndex = spriteIndex ?? Rand.Range(0, prefab.Sprites.Count, Rand.RandSync.Unsynced);
101  Sprite = prefab.Sprites[SpriteIndex];
102  Color = prefab.Color;
103 
104  Rectangle drawRect = new Rectangle(
105  (int)(drawPos.X - Sprite.size.X / 2 * scale),
106  (int)(drawPos.Y + Sprite.size.Y / 2 * scale),
107  (int)(Sprite.size.X * scale),
108  (int)(Sprite.size.Y * scale));
109 
110  Rectangle overFlowAmount = new Rectangle(
111  (int)Math.Max(hull.Rect.X - drawRect.X, 0.0f),
112  (int)Math.Max(drawRect.Y - hull.Rect.Y, 0.0f),
113  (int)Math.Max(drawRect.Right - hull.Rect.Right, 0.0f),
114  (int)Math.Max((hull.Rect.Y - hull.Rect.Height) - (drawRect.Y - drawRect.Height), 0.0f));
115 
116  clippedSourceRect = new Rectangle(
117  Sprite.SourceRect.X + (int)(overFlowAmount.X / scale),
118  Sprite.SourceRect.Y + (int)(overFlowAmount.Y / scale),
119  Sprite.SourceRect.Width - (int)((overFlowAmount.X + overFlowAmount.Width) / scale),
120  Sprite.SourceRect.Height - (int)((overFlowAmount.Y + overFlowAmount.Height) / scale));
121 
122  CenterPosition = position;
123 
124  position -= new Vector2(Sprite.size.X / 2 * scale - overFlowAmount.X, -Sprite.size.Y / 2 * scale + overFlowAmount.Y);
125 
126  this.Scale = scale;
127 
128  foreach (BackgroundSection section in hull.GetBackgroundSectionsViaContaining(new Rectangle((int)position.X, (int)position.Y - drawRect.Height, drawRect.Width, drawRect.Height)))
129  {
130  affectedSections ??= new HashSet<BackgroundSection>();
131  affectedSections.Add(section);
132  }
133  }
134 
135  public void Update(float deltaTime)
136  {
137  fadeTimer += deltaTime;
138  }
139 
140  public void ForceRefreshFadeTimer(float val)
141  {
142  cleaned = false;
143  fadeTimer = val;
144  }
145 
146  public void StopFadeIn()
147  {
148  Color *= GetAlpha();
149  fadeTimer = Prefab.FadeInTime;
150  }
151 
152  public bool AffectsSection(BackgroundSection section)
153  {
154  return affectedSections != null && affectedSections.Contains(section);
155  }
156 
157  public void Clean(float val)
158  {
159  cleaned = true;
160  float sizeModifier = MathHelper.Clamp(Sprite.size.X * Sprite.size.Y * Scale / 10000, 1.0f, 25.0f);
161  BaseAlpha -= val * -1 / sizeModifier;
162  }
163 
164  private float GetAlpha()
165  {
166  if (fadeTimer < Prefab.FadeInTime && !cleaned)
167  {
168  return BaseAlpha * fadeTimer / Prefab.FadeInTime;
169  }
170  else if (cleaned || fadeTimer > Prefab.LifeTime - Prefab.FadeOutTime)
171  {
172  return BaseAlpha * Math.Min((Prefab.LifeTime - fadeTimer) / Prefab.FadeOutTime, 1.0f);
173  }
174  return BaseAlpha;
175  }
176  }
177 }
bool AffectsSection(BackgroundSection section)
Decal(DecalPrefab prefab, float scale, Vector2 worldPosition, Hull hull, int? spriteIndex=null)
readonly Color Color
Definition: DecalPrefab.cs:23
readonly float FadeOutTime
Definition: DecalPrefab.cs:26
readonly List< Sprite > Sprites
Definition: DecalPrefab.cs:21
readonly float LifeTime
Definition: DecalPrefab.cs:25
Submarine Submarine
Definition: Entity.cs:53