Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DoorOpeningHelper.cs
Go to the documentation of this file.
1using System;
5
7
8public class DoorOpeningHelper
9{
16
24
31
42
47
56
58 {
60 {
61 Tile tile = Main.tile[tileCoords.X, tileCoords.Y];
62 Point tileCoordsForToggling = tileCoords;
63 tileCoordsForToggling.Y -= tile.frameY % 54 / 18;
65 result.handler = this;
66 result.tileCoordsForToggling = tileCoordsForToggling;
67 return result;
68 }
69
71 {
72 Point tileCoordsForToggling = doorInfo.tileCoordsForToggling;
73 int intendedOpeningDirection = playerInfo.intendedOpeningDirection;
74 Rectangle rectangle = new Rectangle(doorInfo.tileCoordsForToggling.X * 16, doorInfo.tileCoordsForToggling.Y * 16, 16, 48);
75 switch (playerInfo.playerGravityDirection)
76 {
77 case 1:
78 rectangle.Height += 16;
79 break;
80 case -1:
81 rectangle.Y -= 16;
82 rectangle.Height += 16;
83 break;
84 }
85 if (!rectangle.Intersects(playerInfo.hitboxToOpenDoor))
86 {
87 return false;
88 }
89 if (playerInfo.hitboxToOpenDoor.Top < rectangle.Top || playerInfo.hitboxToOpenDoor.Bottom > rectangle.Bottom)
90 {
91 return false;
92 }
93 WorldGen.OpenDoor(tileCoordsForToggling.X, tileCoordsForToggling.Y, intendedOpeningDirection);
94 if (Main.tile[tileCoordsForToggling.X, tileCoordsForToggling.Y].type != 10)
95 {
96 NetMessage.SendData(19, -1, -1, null, 0, tileCoordsForToggling.X, tileCoordsForToggling.Y, intendedOpeningDirection);
97 return true;
98 }
99 WorldGen.OpenDoor(tileCoordsForToggling.X, tileCoordsForToggling.Y, -intendedOpeningDirection);
100 if (Main.tile[tileCoordsForToggling.X, tileCoordsForToggling.Y].type != 10)
101 {
102 NetMessage.SendData(19, -1, -1, null, 0, tileCoordsForToggling.X, tileCoordsForToggling.Y, -intendedOpeningDirection);
103 return true;
104 }
105 return false;
106 }
107
109 {
110 Point tileCoordsForToggling = info.tileCoordsForToggling;
111 Tile tile = Main.tile[tileCoordsForToggling.X, tileCoordsForToggling.Y];
112 if (!tile.active() || tile.type != 11)
113 {
114 return DoorCloseAttemptResult.DoorIsInvalidated;
115 }
116 int num = tile.frameX % 72 / 18;
117 Rectangle value = new Rectangle(tileCoordsForToggling.X * 16, tileCoordsForToggling.Y * 16, 16, 48);
118 switch (num)
119 {
120 case 1:
121 value.X -= 16;
122 break;
123 case 2:
124 value.X += 16;
125 break;
126 }
127 value.Inflate(1, 0);
128 Rectangle rectangle = Rectangle.Intersect(value, playerInfo.hitboxToNotCloseDoor);
129 if (rectangle.Width > 0 || rectangle.Height > 0)
130 {
131 return DoorCloseAttemptResult.StillInDoorArea;
132 }
133 if (WorldGen.CloseDoor(tileCoordsForToggling.X, tileCoordsForToggling.Y))
134 {
135 NetMessage.SendData(13, -1, -1, null, Main.myPlayer);
136 NetMessage.SendData(19, -1, -1, null, 1, tileCoordsForToggling.X, tileCoordsForToggling.Y, 1f);
137 return DoorCloseAttemptResult.ClosedDoor;
138 }
139 return DoorCloseAttemptResult.FailedToCloseDoor;
140 }
141 }
142
144 {
146 {
147 Tile tile = Main.tile[tileCoords.X, tileCoords.Y];
148 Point tileCoordsForToggling = tileCoords;
149 tileCoordsForToggling.Y -= tile.frameY % 90 / 18;
151 result.handler = this;
152 result.tileCoordsForToggling = tileCoordsForToggling;
153 return result;
154 }
155
157 {
158 Point tileCoordsForToggling = doorInfo.tileCoordsForToggling;
159 Rectangle rectangle = new Rectangle(doorInfo.tileCoordsForToggling.X * 16, doorInfo.tileCoordsForToggling.Y * 16, 16, 80);
160 switch (playerInfo.playerGravityDirection)
161 {
162 case 1:
163 rectangle.Height += 16;
164 break;
165 case -1:
166 rectangle.Y -= 16;
167 rectangle.Height += 16;
168 break;
169 }
170 if (!rectangle.Intersects(playerInfo.hitboxToOpenDoor))
171 {
172 return false;
173 }
174 if (playerInfo.hitboxToOpenDoor.Top < rectangle.Top || playerInfo.hitboxToOpenDoor.Bottom > rectangle.Bottom)
175 {
176 return false;
177 }
178 bool flag = false;
179 if (WorldGen.ShiftTallGate(tileCoordsForToggling.X, tileCoordsForToggling.Y, flag))
180 {
181 NetMessage.SendData(19, -1, -1, null, 4 + flag.ToInt(), tileCoordsForToggling.X, tileCoordsForToggling.Y);
182 return true;
183 }
184 return false;
185 }
186
188 {
189 Point tileCoordsForToggling = info.tileCoordsForToggling;
190 Tile tile = Main.tile[tileCoordsForToggling.X, tileCoordsForToggling.Y];
191 if (!tile.active() || tile.type != 389)
192 {
193 return DoorCloseAttemptResult.DoorIsInvalidated;
194 }
195 _ = tile.frameY % 90 / 18;
196 Rectangle value = new Rectangle(tileCoordsForToggling.X * 16, tileCoordsForToggling.Y * 16, 16, 80);
197 value.Inflate(1, 0);
198 Rectangle rectangle = Rectangle.Intersect(value, playerInfo.hitboxToNotCloseDoor);
199 if (rectangle.Width > 0 || rectangle.Height > 0)
200 {
201 return DoorCloseAttemptResult.StillInDoorArea;
202 }
203 bool flag = true;
204 if (WorldGen.ShiftTallGate(tileCoordsForToggling.X, tileCoordsForToggling.Y, flag))
205 {
206 NetMessage.SendData(13, -1, -1, null, Main.myPlayer);
207 NetMessage.SendData(19, -1, -1, null, 4 + flag.ToInt(), tileCoordsForToggling.X, tileCoordsForToggling.Y);
208 return DoorCloseAttemptResult.ClosedDoor;
209 }
210 return DoorCloseAttemptResult.FailedToCloseDoor;
211 }
212 }
213
215
217 {
218 {
219 10,
221 },
222 {
223 388,
225 }
226 };
227
229
231
236
237 public void Update(Player player)
238 {
239 LookForDoorsToClose(player);
241 {
242 LookForDoorsToOpen(player);
243 }
245 {
247 }
248 }
249
251 {
252 return PreferenceSettings switch
253 {
254 DoorAutoOpeningPreference.EnabledForEverything => true,
255 DoorAutoOpeningPreference.EnabledForGamepadOnly => PlayerInput.UsingGamepad,
256 _ => false,
257 };
258 }
259
260 public static void CyclePreferences()
261 {
262 switch (PreferenceSettings)
263 {
264 case DoorAutoOpeningPreference.Disabled:
265 PreferenceSettings = DoorAutoOpeningPreference.EnabledForEverything;
266 break;
267 case DoorAutoOpeningPreference.EnabledForEverything:
268 PreferenceSettings = DoorAutoOpeningPreference.EnabledForGamepadOnly;
269 break;
270 case DoorAutoOpeningPreference.EnabledForGamepadOnly:
272 break;
273 }
274 }
275
276 public void LookForDoorsToClose(Player player)
277 {
279 for (int num = _ongoingOpenDoors.Count - 1; num >= 0; num--)
280 {
282 if (info.handler.TryCloseDoor(info, playerInfoForClosingDoor) != 0)
283 {
284 _ongoingOpenDoors.RemoveAt(num);
285 }
286 }
287 }
288
290 {
292 result.hitboxToNotCloseDoor = player.Hitbox;
293 return result;
294 }
295
296 public void LookForDoorsToOpen(Player player)
297 {
299 if (playerInfoForOpeningDoor.intendedOpeningDirection == 0 && player.velocity.X == 0f)
300 {
301 return;
302 }
303 Point tileCoords = default(Point);
304 for (int i = playerInfoForOpeningDoor.tileCoordSpaceForCheckingForDoors.Left; i <= playerInfoForOpeningDoor.tileCoordSpaceForCheckingForDoors.Right; i++)
305 {
306 for (int j = playerInfoForOpeningDoor.tileCoordSpaceForCheckingForDoors.Top; j <= playerInfoForOpeningDoor.tileCoordSpaceForCheckingForDoors.Bottom; j++)
307 {
308 tileCoords.X = i;
309 tileCoords.Y = j;
311 }
312 }
313 }
314
316 {
317 int num = player.controlRight.ToInt() - player.controlLeft.ToInt();
318 int playerGravityDirection = (int)player.gravDir;
319 Rectangle hitbox = player.Hitbox;
320 hitbox.Y -= -1;
321 hitbox.Height += -2;
322 float num2 = player.velocity.X;
323 if (num == 0 && _timeWeCanOpenDoorsUsingVelocityAlone == 0)
324 {
325 num2 = 0f;
326 }
327 float value = (float)num + num2;
328 int num3 = Math.Sign(value) * (int)Math.Ceiling(Math.Abs(value));
329 hitbox.X += num3;
330 if (num == 0)
331 {
332 num = Math.Sign(value);
333 }
335 Rectangle value2 = (hitbox2 = player.Hitbox);
336 hitbox2.X += num3;
338 Point point = r.TopLeft().ToTileCoordinates();
339 Point point2 = r.BottomRight().ToTileCoordinates();
340 Rectangle tileCoordSpaceForCheckingForDoors = new Rectangle(point.X, point.Y, point2.X - point.X, point2.Y - point.Y);
342 result.hitboxToOpenDoor = hitbox;
343 result.intendedOpeningDirection = num;
344 result.playerGravityDirection = playerGravityDirection;
345 result.tileCoordSpaceForCheckingForDoors = tileCoordSpaceForCheckingForDoors;
346 return result;
347 }
348
360
362 {
363 infoProvider = null;
365 {
366 return false;
367 }
368 Tile tile = Main.tile[tileCoords.X, tileCoords.Y];
369 if (tile == null)
370 {
371 return false;
372 }
374 {
375 return false;
376 }
377 return true;
378 }
379}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
void Add(TKey key, TValue value)
static double Ceiling(double a)
static double Abs(double value)
static int Sign(decimal value)
Definition Math.cs:1202
Vector2 velocity
Definition Entity.cs:16
Rectangle Hitbox
Definition Entity.cs:164
DoorCloseAttemptResult TryCloseDoor(DoorOpenCloseTogglingInfo info, PlayerInfoForClosingDoors playerInfo)
bool TryOpenDoor(DoorOpenCloseTogglingInfo doorInfo, PlayerInfoForOpeningDoors playerInfo)
bool TryOpenDoor(DoorOpenCloseTogglingInfo doorInfo, PlayerInfoForOpeningDoors playerInfo)
DoorCloseAttemptResult TryCloseDoor(DoorOpenCloseTogglingInfo info, PlayerInfoForClosingDoors playerInfo)
static DoorAutoOpeningPreference PreferenceSettings
PlayerInfoForOpeningDoors GetPlayerInfoForOpeningDoor(Player player)
void AllowOpeningDoorsByVelocityAloneForATime(int timeInFramesToAllow)
void TryAutoOpeningDoor(Point tileCoords, PlayerInfoForOpeningDoors playerInfo)
Dictionary< int, DoorAutoHandler > _handlerByTileType
bool TryGetHandler(Point tileCoords, out DoorAutoHandler infoProvider)
List< DoorOpenCloseTogglingInfo > _ongoingOpenDoors
PlayerInfoForClosingDoors GetPlayerInfoForClosingDoor(Player player)
static int myPlayer
Definition Main.cs:1801
static Tile[,] tile
Definition Main.cs:1675
static void SendData(int msgType, int remoteClient=-1, int ignoreClient=-1, NetworkText text=null, int number=0, float number2=0f, float number3=0f, float number4=0f, int number5=0, int number6=0, int number7=0)
Definition NetMessage.cs:88
bool controlRight
Definition Player.cs:1383
ushort type
Definition Tile.cs:8
bool active()
Definition Tile.cs:565
static bool OpenDoor(int i, int j, int direction)
static bool CloseDoor(int i, int j, bool forced=false)
static bool ShiftTallGate(int x, int y, bool closing, bool forced=false)
static bool InWorld(int x, int y, int fluff=0)
Definition WorldGen.cs:5816
bool TryOpenDoor(DoorOpenCloseTogglingInfo info, PlayerInfoForOpeningDoors playerInfo)
DoorCloseAttemptResult TryCloseDoor(DoorOpenCloseTogglingInfo info, PlayerInfoForClosingDoors playerInfo)
DoorOpenCloseTogglingInfo ProvideInfo(Point tileCoords)
static Rectangle Intersect(Rectangle value1, Rectangle value2)
Definition Rectangle.cs:143
static Rectangle Union(Rectangle value1, Rectangle value2)
Definition Rectangle.cs:197