Client LuaCsForBarotrauma
BarotraumaClient/ClientSource/Map/Levels/Level.cs
2 using FarseerPhysics;
3 using Microsoft.Xna.Framework;
4 using Microsoft.Xna.Framework.Graphics;
5 using System;
6 using System.Collections.Generic;
7 
8 namespace Barotrauma
9 {
10  partial class Level
11  {
12  private LevelRenderer renderer;
13 
14  private BackgroundCreatureManager backgroundCreatureManager;
15 
16  public BackgroundCreatureManager BackgroundCreatureManager => backgroundCreatureManager;
17 
18  public LevelRenderer Renderer => renderer;
19 
20  public void ReloadTextures()
21  {
22  renderer.ReloadTextures();
23 
24  HashSet<Texture2D> uniqueTextures = new HashSet<Texture2D>();
25  HashSet<Sprite> uniqueSprites = new HashSet<Sprite>();
26  var allLevelObjects = LevelObjectManager.GetAllObjects();
27  foreach (var levelObj in allLevelObjects)
28  {
29  foreach (Sprite sprite in levelObj.Prefab.Sprites)
30  {
31  if (!uniqueTextures.Contains(sprite.Texture))
32  {
33  uniqueTextures.Add(sprite.Texture);
34  uniqueSprites.Add(sprite);
35  }
36  }
37  }
38 
39  foreach (Sprite sprite in uniqueSprites)
40  {
41  sprite.ReloadTexture();
42  }
43  }
44 
45  public void DrawDebugOverlay(SpriteBatch spriteBatch, Camera cam)
46  {
47  if (renderer == null) { return; }
48  renderer.DrawDebugOverlay(spriteBatch, cam);
49 
50  if (GameMain.DebugDraw && Screen.Selected.Cam.Zoom > 0.1f)
51  {
53  {
54  Color color = Color.Yellow;
55  if (pos.PositionType == PositionType.Cave || pos.PositionType == PositionType.AbyssCave)
56  {
57  color = Color.DarkOrange;
58  }
59  else if (pos.PositionType == PositionType.Ruin)
60  {
61  color = Color.LightGray;
62  }
63  if (!pos.IsValid)
64  {
65  color = Color.Red;
66  }
67 
68  GUI.DrawRectangle(spriteBatch, new Vector2(pos.Position.X - 15.0f, -pos.Position.Y - 15.0f), new Vector2(30.0f, 30.0f), color, true);
69  }
70 
71  foreach (RuinGeneration.Ruin ruin in Ruins)
72  {
73  Rectangle ruinArea = ruin.Area;
74  ruinArea.Y = -ruinArea.Y - ruinArea.Height;
75 
76  GUI.DrawRectangle(spriteBatch, ruinArea, Color.DarkSlateBlue, false, 0, 5);
77  }
78 
79  foreach (var positions in wreckPositions.Values)
80  {
81  for (int i = 0; i < positions.Count; i++)
82  {
83  float t = (i + 1) / (float)positions.Count;
84  float multiplier = MathHelper.Lerp(0, 1, t);
85  Color color = Color.Red * multiplier;
86  var pos = positions[i];
87  pos.Y = -pos.Y;
88  var size = new Vector2(100);
89  GUI.DrawRectangle(spriteBatch, pos - size / 2, size, color, thickness: 10);
90  if (i < positions.Count - 1)
91  {
92  var nextPos = positions[i + 1];
93  nextPos.Y = -nextPos.Y;
94  GUI.DrawLine(spriteBatch, pos, nextPos, color, width: 10);
95  }
96  }
97  }
98 
99  foreach (var rects in blockedRects.Values)
100  {
101  foreach (var rect in rects)
102  {
103  Rectangle newRect = rect;
104  newRect.Y = -newRect.Y;
105  GUI.DrawRectangle(spriteBatch, newRect, Color.Red, thickness: 5);
106  }
107  }
108  }
109  }
110 
111  public void DrawBack(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam)
112  {
113  float brightness = MathHelper.Clamp(1.1f + (cam.Position.Y - Size.Y) / 100000.0f, 0.1f, 1.0f);
114  var lightColorHLS = GenerationParams.AmbientLightColor.RgbToHLS();
115  lightColorHLS.Y *= brightness;
116 
117  GameMain.LightManager.AmbientLight = ToolBox.HLSToRGB(lightColorHLS);
118 
119  graphics.Clear(BackgroundColor);
120 
121  if (renderer != null)
122  {
123  GameMain.LightManager.AmbientLight = GameMain.LightManager.AmbientLight.Add(renderer.FlashColor);
124  renderer?.DrawBackground(spriteBatch, cam, LevelObjectManager, backgroundCreatureManager);
125  }
126  }
127 
128  public void DrawFront(SpriteBatch spriteBatch, Camera cam)
129  {
130  renderer?.DrawForeground(spriteBatch, cam, LevelObjectManager);
131  }
132  public void ClientEventRead(IReadMessage msg, float sendingTime)
133  {
134  EventType eventType = (EventType)msg.ReadByte();
135 
136  switch (eventType)
137  {
138  case EventType.GlobalDestructibleWall:
139  {
140  foreach (LevelWall levelWall in ExtraWalls)
141  {
142  if (levelWall.Body.BodyType == BodyType.Static) { continue; }
143 
144  Vector2 bodyPos = new Vector2(
145  msg.ReadSingle(),
146  msg.ReadSingle());
147  levelWall.MoveState = msg.ReadRangedSingle(0.0f, MathHelper.TwoPi, 16);
148  if (Vector2.DistanceSquared(bodyPos, levelWall.Body.Position) > 0.5f
149  && !(levelWall is DestructibleLevelWall { Destroyed: true }))
150  {
151  levelWall.Body.SetTransformIgnoreContacts(ref bodyPos, levelWall.Body.Rotation);
152  }
153  }
154  }
155  break;
156  case EventType.SingleDestructibleWall:
157  {
158  int index = msg.ReadUInt16();
159  float damageByte = msg.ReadByte();
160  if (index < ExtraWalls.Count && ExtraWalls[index] is DestructibleLevelWall destructibleWall)
161  {
162  destructibleWall.SetDamage(destructibleWall.MaxHealth * damageByte / 255.0f);
163  }
164  }
165  break;
166  default:
167  throw new Exception($"Malformed incoming level event: {eventType} is not a supported event type");
168  }
169  }
170  }
171 }
float? Zoom
Definition: Camera.cs:78
Vector2 Position
Definition: Camera.cs:398
static Lights.LightManager LightManager
Definition: GameMain.cs:78
static bool DebugDraw
Definition: GameMain.cs:29
void DrawBack(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam)
BackgroundCreatureManager BackgroundCreatureManager
void DrawDebugOverlay(SpriteBatch spriteBatch, Camera cam)
void ClientEventRead(IReadMessage msg, float sendingTime)
void DrawFront(SpriteBatch spriteBatch, Camera cam)
Single ReadRangedSingle(Single min, Single max, int bitCount)