Client LuaCsForBarotrauma
EntityGrid.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 
5 namespace Barotrauma
6 {
7  class EntityGrid
8  {
9  private List<MapEntity> allEntities;
10  private List<MapEntity>[,] entities;
11 
12  private readonly Rectangle limits;
13 
14  private readonly float cellSize;
15 
16  public readonly Submarine Submarine;
17 
19  {
20  get
21  {
22  if (Submarine == null)
23  {
24  return limits;
25  }
26  else
27  {
28  return new Rectangle(
29  (int)(limits.X + Submarine.WorldPosition.X),
30  (int)(limits.Y + Submarine.WorldPosition.Y),
31  limits.Width, limits.Height);
32  }
33  }
34  }
35 
36  public EntityGrid(Submarine submarine, float cellSize)
37  {
38  //make the grid slightly larger than the borders of the submarine,
39  //because docking ports may create gaps and hulls outside the borders
40  int padding = 128;
41 
42  this.limits = new Rectangle(
43  submarine.Borders.X - padding,
44  submarine.Borders.Y + padding,
45  submarine.Borders.Width + padding * 2,
46  submarine.Borders.Height + padding * 2);
47  this.Submarine = submarine;
48  this.cellSize = cellSize;
49  InitializeGrid();
50  }
51 
52  public EntityGrid(Rectangle worldRect, float cellSize)
53  {
54  this.limits = worldRect;
55  this.cellSize = cellSize;
56  InitializeGrid();
57  }
58 
59  private void InitializeGrid()
60  {
61  allEntities = new List<MapEntity>();
62  entities = new List<MapEntity>[(int)Math.Ceiling(limits.Width / cellSize), (int)Math.Ceiling(limits.Height / cellSize)];
63  for (int x = 0; x < entities.GetLength(0); x++)
64  {
65  for (int y = 0; y < entities.GetLength(1); y++)
66  {
67  entities[x, y] = new List<MapEntity>();
68  }
69  }
70  }
71 
72  public void InsertEntity(MapEntity entity)
73  {
74  Rectangle rect = entity.Rect;
75  //if (Submarine.Loaded != null) rect.Offset(-Submarine.HiddenSubPosition);
76  Rectangle indices = GetIndices(rect);
77 
78  if (indices.Width < 0 || indices.X >= entities.GetLength(0) ||
79  indices.Height < 0 || indices.Y >= entities.GetLength(1))
80  {
81  DebugConsole.ThrowError("Error in EntityGrid.InsertEntity: " + entity + " is outside the grid");
82  return;
83  }
84 
85  for (int x = Math.Max(indices.X, 0); x <= Math.Min(indices.Width, entities.GetLength(0) - 1); x++)
86  {
87  for (int y = Math.Max(indices.Y, 0); y <= Math.Min(indices.Height, entities.GetLength(1) - 1); y++)
88  {
89  entities[x, y].Add(entity);
90  }
91  }
92  allEntities.Add(entity);
93  }
94 
95  public void RemoveEntity(MapEntity entity)
96  {
97  for (int x = 0; x < entities.GetLength(0); x++)
98  {
99  for (int y = 0; y < entities.GetLength(1); y++)
100  {
101  if (entities[x, y].Contains(entity)) entities[x, y].Remove(entity);
102  }
103  }
104  allEntities.Remove(entity);
105  }
106 
107  public void Clear()
108  {
109  for (int x = 0; x < entities.GetLength(0); x++)
110  {
111  for (int y = 0; y < entities.GetLength(1); y++)
112  {
113  entities[x, y].Clear();
114  }
115  }
116  allEntities.Clear();
117  }
118 
119  public IEnumerable<MapEntity> GetAllEntities()
120  {
121  return allEntities;
122  }
123 
124  public List<MapEntity> GetEntities(Vector2 position)
125  {
126  if (!MathUtils.IsValid(position)) return null;
127 
128  if (Submarine != null) position -= Submarine.HiddenSubPosition;
129  Point indices = GetIndices(position);
130  if (indices.X < 0 || indices.Y < 0 || indices.X >= entities.GetLength(0) || indices.Y >= entities.GetLength(1))
131  {
132  return null;
133  }
134  return entities[indices.X, indices.Y];
135  }
136 
137  public Rectangle GetIndices(Rectangle rect)
138  {
139  Rectangle indices = Rectangle.Empty;
140  indices.X = (int)Math.Floor((rect.X - limits.X) / cellSize);
141  indices.Y = (int)Math.Floor((limits.Y - rect.Y) / cellSize);
142 
143  indices.Width = (int)Math.Floor((rect.Right - limits.X) / cellSize);
144  indices.Height = (int)Math.Floor((limits.Y - (rect.Y - rect.Height)) / cellSize);
145 
146  return indices;
147  }
148 
149  public Point GetIndices(Vector2 position)
150  {
151  return new Point(
152  (int)Math.Floor((position.X - limits.X) / cellSize),
153  (int)Math.Floor((limits.Y - position.Y) / cellSize));
154  }
155  }
156 }
Point GetIndices(Vector2 position)
Definition: EntityGrid.cs:149
readonly Submarine Submarine
Definition: EntityGrid.cs:16
void InsertEntity(MapEntity entity)
Definition: EntityGrid.cs:72
IEnumerable< MapEntity > GetAllEntities()
Definition: EntityGrid.cs:119
List< MapEntity > GetEntities(Vector2 position)
Definition: EntityGrid.cs:124
EntityGrid(Submarine submarine, float cellSize)
Definition: EntityGrid.cs:36
EntityGrid(Rectangle worldRect, float cellSize)
Definition: EntityGrid.cs:52
void RemoveEntity(MapEntity entity)
Definition: EntityGrid.cs:95
Rectangle GetIndices(Rectangle rect)
Definition: EntityGrid.cs:137
Rectangle? Borders
Extents of the solid items/structures (ones with a physics body) and hulls