Skip to content

Commit

Permalink
added more neighbours to cells
Browse files Browse the repository at this point in the history
Now its full circle around cell
  • Loading branch information
KOTlK committed May 30, 2023
1 parent d6e177f commit fb19683
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.kotlk.celllistsecs",
"version": "0.1.0",
"version": "0.1.1",
"displayName": "Cell Lists",
"description": "Simple cell lists implementation for dividing game world into pieces",
"unity": "2021.3",
Expand Down
20 changes: 19 additions & 1 deletion Runtime/Systems/CellListsInitSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Text;
using CellListsECS.Runtime.Components;
using Leopotam.EcsLite;
using Leopotam.EcsLite.Di;
Expand All @@ -14,7 +15,7 @@ public class CellListsInitSystem : IEcsRunSystem
private readonly EcsPoolInject<Cell> _cells = default;
private readonly EcsPoolInject<CellNeighbours> _neighbours = default;
private readonly EcsPoolInject<TransformContainer> _entitiesLists = default;

public void Run(IEcsSystems systems)
{
foreach (var entity in _filter.Value)
Expand Down Expand Up @@ -64,16 +65,25 @@ public void Run(IEcsSystems systems)
}
}

// pure madness
int[] GetClosestIndexesFor(int i)
{
var leftIndex = i - 1;
var rightIndex = i + 1;
var upIndex = i - width;
var downIndex = i + width;
var leftUpIndex = i - width - 1;
var rightUpIndex = i - (width - 1);
var rightDownIndex = i + width + 1;
var leftDownIndex = i + (width - 1);
var leftExist = i % width != 0 && leftIndex >= 0;
var rightExist = rightIndex % width != 0 && rightIndex < length;
var upExist = upIndex >= 0;
var downExist = downIndex < length;
var leftUpExist = leftUpIndex >= 0 && i % width != 0;
var rightUpExist = rightUpIndex % width != 0 && rightUpIndex >= 0;
var rightDownExist = rightDownIndex < length && rightDownIndex % width != 0;
var leftDownExist = leftDownIndex < length && i % width != 0;
var list = new List<int>();

if (rightExist)
Expand All @@ -84,6 +94,14 @@ int[] GetClosestIndexesFor(int i)
list.Add(upIndex);
if (downExist)
list.Add(downIndex);
if (leftUpExist)
list.Add(leftUpIndex);
if (rightUpExist)
list.Add(rightUpIndex);
if (rightDownExist)
list.Add(rightDownIndex);
if (leftDownExist)
list.Add(leftDownIndex);

return list.ToArray();
}
Expand Down
1 change: 0 additions & 1 deletion Samples~/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ MonoBehaviour:
_size: {x: 100, y: 100}
_width: 10
_height: 10
_view: {fileID: 0}
_entitiesCountText: {fileID: 574717936}
_entitiesCountPerBatch: 10000
_startEntitiesCount: 100000
Expand Down

0 comments on commit fb19683

Please sign in to comment.