public void UpdateEntity(GridEntity2D entity) { var prevCell = lastPositions.ContainsKey(entity) ? lastPositions[entity] : Outside; var curCell = PositionInGrid(entity.transform.position); if (prevCell.Equals(curCell)) { return; } //Entity was previously inside the grid and it will move from there if (InsideGrid(prevCell)) { buckets[prevCell.x, prevCell.y].Remove(entity); } //Entity is now inside the grid, and just moved from prev cell, add it to the new cell if (InsideGrid(curCell)) { buckets[curCell.x, curCell.y].Add(entity); lastPositions[entity] = curCell; } else { lastPositions.Remove(entity); } }
public void RemoveEntity(GridEntity2D entity) { var prevCell = lastPositions.ContainsKey(entity) ? lastPositions[entity] : Outside; if (InsideGrid(prevCell)) { buckets[prevCell.x, prevCell.y].Remove(entity); } entity.OnMove -= UpdateEntity; }
public void AddEntity(GridEntity2D entity) { entity.OnMove += UpdateEntity; UpdateEntity(entity); }