-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "com.kotlk.celllistsecs", | ||
"version": "0.1.0", | ||
"displayName": "Cell Lists", | ||
"description": "Simple cell lists implementation for dividing game world into pieces", | ||
"unity": "2021.3", | ||
"dependencies": { | ||
"com.unity.collections": "1.4.0", | ||
"com.leopotam.ecslite": "2023.5.22", | ||
"com.leopotam.ecslite.di": "2023.4.22" | ||
}, | ||
"author": { | ||
"name": "Kotlk", | ||
"email": "rojuty@gmail.com" | ||
}, | ||
"repository" : { | ||
"type": "git", | ||
"url": "https://github.com/KOTlK/CellLists.git" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace CellListsECS.Runtime.Components | ||
{ | ||
[Serializable] | ||
public struct AABB | ||
{ | ||
public Vector2 Size; | ||
public Vector2 HalfExtents => Size * 0.5f; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace CellListsECS.Runtime.Components | ||
{ | ||
[Serializable] | ||
public struct Cell | ||
{ | ||
public AABB AABB; | ||
public Vector2 Position; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Unity.Collections; | ||
|
||
namespace CellListsECS.Runtime.Components | ||
{ | ||
public struct CellNeighbours | ||
{ | ||
public NativeArray<int> All; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace CellListsECS.Runtime.Components | ||
{ | ||
[Serializable] | ||
public struct CreateCellLists | ||
{ | ||
public Vector2 Size; | ||
public Vector2 Center; | ||
public int Width; | ||
public int Height; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace CellListsECS.Runtime.Components | ||
{ | ||
public struct InsertInCellLists | ||
{ | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace CellListsECS.Runtime.Components | ||
{ | ||
[Serializable] | ||
public struct Transform | ||
{ | ||
public Vector2 Position; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace CellListsECS.Runtime.Components | ||
{ | ||
public struct TransformContainer | ||
{ | ||
public List<int> All; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "Runtime", | ||
"rootNamespace": "", | ||
"references": [ | ||
"GUID:e59bf9fc7d5a44b18a819a92edd46163", | ||
"GUID:f5f01a091e0d746659b4380253d52f4e", | ||
"GUID:e0cd26848372d4e5c891c569017e11f1", | ||
"GUID:2665a8d13d1b3f18800f46e256720795" | ||
], | ||
"includePlatforms": [], | ||
"excludePlatforms": [], | ||
"allowUnsafeCode": false, | ||
"overrideReferences": false, | ||
"precompiledReferences": [], | ||
"autoReferenced": true, | ||
"defineConstraints": [], | ||
"versionDefines": [], | ||
"noEngineReferences": false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
using System.Collections.Generic; | ||
using CellListsECS.Runtime.Components; | ||
using Leopotam.EcsLite; | ||
using Leopotam.EcsLite.Di; | ||
using Unity.Collections; | ||
using UnityEngine; | ||
|
||
namespace CellListsECS.Runtime.Systems | ||
{ | ||
public class CellListsInitSystem : IEcsRunSystem | ||
{ | ||
private readonly EcsFilterInject<Inc<CreateCellLists>> _filter = default; | ||
private readonly EcsPoolInject<CreateCellLists> _commands = default; | ||
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) | ||
{ | ||
ref var command = ref _commands.Value.Get(entity); | ||
|
||
var length = command.Width * command.Height; | ||
var size = command.Size; | ||
var width = command.Width; | ||
var height = command.Height; | ||
var centerPosition = command.Center; | ||
var overall = new AABB | ||
{ | ||
Size = size | ||
}; | ||
var cellSize = new Vector2(size.x / width, size.y / height); | ||
var cells = new(Cell, int)[length]; // cell, entity | ||
var overallHalfExtents = overall.HalfExtents; | ||
var startPosition = new Vector2(centerPosition.x - overallHalfExtents.x, | ||
centerPosition.y + overallHalfExtents.y); | ||
var currentPosition = startPosition; | ||
var right = cellSize.x; | ||
var down = -cellSize.y; | ||
var currentColumn = 0; | ||
|
||
|
||
for (var i = 0; i < length; i++) | ||
{ | ||
var cell = new Cell() | ||
{ | ||
AABB = new AABB() | ||
{ | ||
Size = cellSize | ||
}, | ||
Position = currentPosition | ||
}; | ||
var cellEntity = systems.GetWorld().NewEntity(); | ||
cells[i] = (cell, cellEntity); | ||
|
||
currentPosition.x += right; | ||
currentColumn++; | ||
if (currentColumn == width) | ||
{ | ||
currentPosition.x = startPosition.x; | ||
currentPosition.y += down; | ||
currentColumn = 0; | ||
} | ||
} | ||
|
||
int[] GetClosestIndexesFor(int i) | ||
{ | ||
var leftIndex = i - 1; | ||
var rightIndex = i + 1; | ||
var upIndex = i - width; | ||
var downIndex = i + width; | ||
var leftExist = i % width != 0 && leftIndex >= 0; | ||
var rightExist = rightIndex % width != 0 && rightIndex < length; | ||
var upExist = upIndex >= 0; | ||
var downExist = downIndex < length; | ||
var list = new List<int>(); | ||
|
||
if (rightExist) | ||
list.Add(rightIndex); | ||
if (leftExist) | ||
list.Add(leftIndex); | ||
if (upExist) | ||
list.Add(upIndex); | ||
if (downExist) | ||
list.Add(downIndex); | ||
|
||
return list.ToArray(); | ||
} | ||
|
||
for (var i = 0; i < length; i++) | ||
{ | ||
var neighbours = GetClosestIndexesFor(i); | ||
var list = new NativeArray<int>(neighbours.Length, Allocator.Persistent); | ||
for (var j = 0; j < neighbours.Length; j++) | ||
{ | ||
var (_, neighbourEntity) = cells[neighbours[j]]; | ||
list[j] = neighbourEntity; | ||
} | ||
|
||
ref var neighboursComponent = ref _neighbours.Value.Add(cells[i].Item2); | ||
|
||
neighboursComponent.All = list; | ||
} | ||
|
||
foreach (var (cell, cellEntity) in cells) | ||
{ | ||
ref var cellComponent = ref _cells.Value.Add(cellEntity); | ||
cellComponent = cell; | ||
|
||
ref var container = ref _entitiesLists.Value.Add(cellEntity); | ||
container.All = new List<int>(); | ||
} | ||
|
||
systems.GetWorld().DelEntity(entity); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.