Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Items/Components/Machines/Engine.cs
2 using Microsoft.Xna.Framework;
3 using Microsoft.Xna.Framework.Graphics;
4 using System;
5 using System.Xml.Linq;
6 
8 {
9  partial class Engine : Powered, IDrawableComponent
10  {
11  private float spriteIndex;
12 
13  private SpriteSheet propellerSprite;
14 
15  private GUITickBox powerIndicator;
16  private GUIScrollBar forceSlider;
17  private GUITickBox autoControlIndicator;
18 
19  private int particlesPerSec = 60;
20  private float particleTimer;
21 
22 
23  public float AnimSpeed
24  {
25  get;
26  private set;
27  }
28 
29  public Vector2 DrawSize
30  {
31  //use the extents of the item as the draw size
32  get { return Vector2.Zero; }
33  }
34 
35  partial void InitProjSpecific(ContentXElement element)
36  {
37  var paddedFrame = new GUIFrame(new RectTransform(new Vector2(0.85f, 0.65f), GuiFrame.RectTransform, Anchor.Center)
38  {
39  RelativeOffset = new Vector2(0, 0.04f)
40  }, style: null);
41 
42  var lightsArea = new GUIFrame(new RectTransform(new Vector2(1, 0.38f), paddedFrame.RectTransform, Anchor.TopLeft), style: null);
43  powerIndicator = new GUITickBox(new RectTransform(new Vector2(0.45f, 0.8f), lightsArea.RectTransform, Anchor.Center, Pivot.CenterRight)
44  {
45  RelativeOffset = new Vector2(-0.05f, 0)
46  }, TextManager.Get("EnginePowered"), font: GUIStyle.SubHeadingFont, style: "IndicatorLightGreen")
47  {
48  CanBeFocused = false
49  };
50  autoControlIndicator = new GUITickBox(new RectTransform(new Vector2(0.45f, 0.8f), lightsArea.RectTransform, Anchor.Center, Pivot.CenterLeft)
51  {
52  RelativeOffset = new Vector2(0.05f, 0)
53  }, TextManager.Get("PumpAutoControl", "ReactorAutoControl"), font: GUIStyle.SubHeadingFont, style: "IndicatorLightYellow")
54  {
55  Selected = false,
56  Enabled = false,
57  ToolTip = TextManager.Get("AutoControlTip")
58  };
59  powerIndicator.TextBlock.Wrap = autoControlIndicator.TextBlock.Wrap = true;
60  powerIndicator.TextBlock.OverrideTextColor(GUIStyle.TextColorNormal);
61  autoControlIndicator.TextBlock.OverrideTextColor(GUIStyle.TextColorNormal);
62  GUITextBlock.AutoScaleAndNormalize(powerIndicator.TextBlock, autoControlIndicator.TextBlock);
63 
64  var sliderArea = new GUIFrame(new RectTransform(new Vector2(1, 0.6f), paddedFrame.RectTransform, Anchor.BottomLeft), style: null);
65  LocalizedString powerLabel = TextManager.Get("EngineForce");
66  new GUITextBlock(new RectTransform(new Vector2(1.0f, 0.3f), sliderArea.RectTransform, Anchor.TopCenter), "", textColor: GUIStyle.TextColorNormal, font: GUIStyle.SubHeadingFont, textAlignment: Alignment.Center)
67  {
68  AutoScaleHorizontal = true,
69  TextGetter = () =>
70  {
71  return TextManager.AddPunctuation(':', powerLabel,
72  TextManager.GetWithVariable("percentageformat", "[value]", ((int)MathF.Round(targetForce)).ToString()));
73  }
74  };
75  forceSlider = new GUIScrollBar(new RectTransform(new Vector2(0.95f, 0.45f), sliderArea.RectTransform, Anchor.Center), barSize: 0.1f, style: "DeviceSlider")
76  {
77  Step = 0.05f,
78  OnMoved = (GUIScrollBar scrollBar, float barScroll) =>
79  {
80  lastReceivedTargetForce = null;
81  float newTargetForce = barScroll * 200.0f - 100.0f;
82  if (Math.Abs(newTargetForce - targetForce) < 0.01) { return false; }
83 
84  targetForce = newTargetForce;
85  User = Character.Controlled;
86 
87  if (GameMain.Client != null)
88  {
90  item.CreateClientEvent(this);
91  }
92  return true;
93  }
94  };
95 
96  var textsArea = new GUIFrame(new RectTransform(new Vector2(1, 0.25f), sliderArea.RectTransform, Anchor.BottomCenter), style: null);
97  var backwardsLabel = new GUITextBlock(new RectTransform(new Vector2(0.4f, 1.0f), textsArea.RectTransform, Anchor.CenterLeft), TextManager.Get("EngineBackwards"),
98  textColor: GUIStyle.TextColorNormal, font: GUIStyle.SubHeadingFont, textAlignment: Alignment.CenterLeft);
99  var forwardsLabel = new GUITextBlock(new RectTransform(new Vector2(0.4f, 1.0f), textsArea.RectTransform, Anchor.CenterRight), TextManager.Get("EngineForwards"),
100  textColor: GUIStyle.TextColorNormal, font: GUIStyle.SubHeadingFont, textAlignment: Alignment.CenterRight);
101  GUITextBlock.AutoScaleAndNormalize(backwardsLabel, forwardsLabel);
102 
103  foreach (var subElement in element.Elements())
104  {
105  switch (subElement.Name.ToString().ToLowerInvariant())
106  {
107  case "propellersprite":
108  propellerSprite = new SpriteSheet(subElement);
109  AnimSpeed = subElement.GetAttributeFloat("animspeed", 1.0f);
110  break;
111  }
112  }
113  }
114 
115  public override void UpdateHUDComponentSpecific(Character character, float deltaTime, Camera cam)
116  {
117  powerIndicator.Selected = hasPower && IsActive;
118  autoControlIndicator.Selected = controlLockTimer > 0.0f;
119  forceSlider.Enabled = controlLockTimer <= 0.0f;
120 
122  {
123  float newScroll = (targetForce + 100.0f) / 200.0f;
124  if (Math.Abs(newScroll - forceSlider.BarScroll) > 0.01f)
125  {
126  forceSlider.BarScroll = newScroll;
127  }
128  }
129  }
130 
131  partial void UpdateAnimation(float deltaTime)
132  {
133  if (propellerSprite == null) { return; }
134  spriteIndex += (force / 100.0f) * AnimSpeed * deltaTime;
135  if (spriteIndex < 0)
136  {
137  spriteIndex = propellerSprite.FrameCount - Math.Abs(spriteIndex) % propellerSprite.FrameCount;
138  }
139  else
140  {
141  spriteIndex = spriteIndex % propellerSprite.FrameCount;
142  }
143  }
144 
145  public void Draw(SpriteBatch spriteBatch, bool editing, float itemDepth = -1, Color? overrideColor = null)
146  {
147  if (propellerSprite != null)
148  {
149  Vector2 drawPos = item.DrawPosition;
150  drawPos += PropellerPos;
151  drawPos.Y = -drawPos.Y;
152  propellerSprite.Draw(spriteBatch, (int)Math.Floor(spriteIndex), drawPos, overrideColor ?? Color.White, propellerSprite.Origin, 0.0f, Vector2.One);
153  }
154 
155  if (editing && !DisablePropellerDamage && propellerDamage != null && !GUI.DisableHUD)
156  {
157  Vector2 drawPos = item.DrawPosition;
158  drawPos += PropellerPos * item.Scale;
159  drawPos.Y = -drawPos.Y;
160  spriteBatch.DrawCircle(drawPos, propellerDamage.DamageRange * item.Scale, 16, GUIStyle.Red, thickness: 2);
161  }
162  }
163 
164  public void ClientEventWrite(IWriteMessage msg, NetEntityEvent.IData extraData = null)
165  {
166  //targetForce can only be adjusted at 10% intervals -> no need for more accuracy than this
167  msg.WriteRangedInteger((int)(targetForce / 10.0f), -10, 10);
168  }
169 
170  public void ClientEventRead(IReadMessage msg, float sendingTime)
171  {
172  if (correctionTimer > 0.0f)
173  {
174  StartDelayedCorrection(msg.ExtractBits(5 + 16), sendingTime);
175  return;
176  }
177 
178  targetForce = msg.ReadRangedInteger(-10, 10) * 10.0f;
179  UInt16 userID = msg.ReadUInt16();
180  if (userID != Entity.NullEntityID)
181  {
182  User = Entity.FindEntityByID(userID) as Character;
183  }
184  }
185  }
186 }
IEnumerable< ContentXElement > Elements()
virtual Vector2 DrawPosition
Definition: Entity.cs:51
const ushort NullEntityID
Definition: Entity.cs:14
static Entity FindEntityByID(ushort ID)
Find an entity based on the ID
Definition: Entity.cs:204
RectTransform RectTransform
static void AutoScaleAndNormalize(params GUITextBlock[] textBlocks)
Set the text scale of the GUITextBlocks so that they all use the same scale and can fit the text with...
override void UpdateHUDComponentSpecific(Character character, float deltaTime, Camera cam)
void ClientEventWrite(IWriteMessage msg, NetEntityEvent.IData extraData=null)
void Draw(SpriteBatch spriteBatch, bool editing, float itemDepth=-1, Color? overrideColor=null)
void StartDelayedCorrection(IReadMessage buffer, float sendingTime, bool waitForMidRoundSync=false)
int ReadRangedInteger(int min, int max)
void WriteRangedInteger(int val, int min, int max)