1 using Microsoft.Xna.Framework;
3 using System.Collections.Generic;
9 private List<MapEntity> allEntities;
10 private List<MapEntity>[,] entities;
14 private readonly
float cellSize;
31 limits.Width, limits.Height);
45 submarine.
Borders.Width + padding * 2,
46 submarine.
Borders.Height + padding * 2);
47 this.Submarine = submarine;
48 this.cellSize = cellSize;
54 this.limits = worldRect;
55 this.cellSize = cellSize;
59 private void InitializeGrid()
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++)
65 for (
int y = 0; y < entities.GetLength(1); y++)
67 entities[x, y] =
new List<MapEntity>();
78 if (indices.Width < 0 || indices.X >= entities.GetLength(0) ||
79 indices.Height < 0 || indices.Y >= entities.GetLength(1))
81 DebugConsole.ThrowError(
"Error in EntityGrid.InsertEntity: " + entity +
" is outside the grid");
85 for (
int x = Math.Max(indices.X, 0); x <= Math.Min(indices.Width, entities.GetLength(0) - 1); x++)
87 for (
int y = Math.Max(indices.Y, 0); y <= Math.Min(indices.Height, entities.GetLength(1) - 1); y++)
89 entities[x, y].Add(entity);
92 allEntities.Add(entity);
97 for (
int x = 0; x < entities.GetLength(0); x++)
99 for (
int y = 0; y < entities.GetLength(1); y++)
101 if (entities[x, y].Contains(entity)) entities[x, y].Remove(entity);
104 allEntities.Remove(entity);
109 for (
int x = 0; x < entities.GetLength(0); x++)
111 for (
int y = 0; y < entities.GetLength(1); y++)
113 entities[x, y].Clear();
126 if (!MathUtils.IsValid(position))
return null;
130 if (indices.X < 0 || indices.Y < 0 || indices.X >= entities.GetLength(0) || indices.Y >= entities.GetLength(1))
134 return entities[indices.X, indices.Y];
140 indices.X = (int)Math.Floor((rect.X - limits.X) / cellSize);
141 indices.Y = (int)Math.Floor((limits.Y - rect.Y) / cellSize);
143 indices.Width = (int)Math.Floor((rect.Right - limits.X) / cellSize);
144 indices.Height = (int)Math.Floor((limits.Y - (rect.Y - rect.Height)) / cellSize);
152 (
int)Math.Floor((position.X - limits.X) / cellSize),
153 (
int)Math.Floor((limits.Y - position.Y) / cellSize));
Point GetIndices(Vector2 position)
readonly Submarine Submarine
void InsertEntity(MapEntity entity)
IEnumerable< MapEntity > GetAllEntities()
List< MapEntity > GetEntities(Vector2 position)
EntityGrid(Submarine submarine, float cellSize)
EntityGrid(Rectangle worldRect, float cellSize)
void RemoveEntity(MapEntity entity)
Rectangle GetIndices(Rectangle rect)
override Vector2? WorldPosition
Vector2 HiddenSubPosition
Rectangle? Borders
Extents of the solid items/structures (ones with a physics body) and hulls