Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KOTlK committed May 30, 2023
1 parent 8898dcd commit 09b228e
Show file tree
Hide file tree
Showing 46 changed files with 1,775 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Package.json
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"
}
}
7 changes: 7 additions & 0 deletions Package.json.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime/Components.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Runtime/Components/AABB.cs
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;
}
}
3 changes: 3 additions & 0 deletions Runtime/Components/AABB.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Runtime/Components/Cell.cs
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;
}
}
3 changes: 3 additions & 0 deletions Runtime/Components/Cell.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Runtime/Components/CellNeighbours.cs
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;
}
}
3 changes: 3 additions & 0 deletions Runtime/Components/CellNeighbours.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Runtime/Components/CreateCellLists.cs
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;
}
}
3 changes: 3 additions & 0 deletions Runtime/Components/CreateCellLists.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Runtime/Components/InsertInCellLists.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CellListsECS.Runtime.Components
{
public struct InsertInCellLists
{
}
}
3 changes: 3 additions & 0 deletions Runtime/Components/InsertInCellLists.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Runtime/Components/Transform.cs
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;
}
}
3 changes: 3 additions & 0 deletions Runtime/Components/Transform.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Runtime/Components/TransformContainer.cs
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;
}
}
3 changes: 3 additions & 0 deletions Runtime/Components/TransformContainer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions Runtime/Runtime.asmdef
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
}
7 changes: 7 additions & 0 deletions Runtime/Runtime.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime/Systems.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 119 additions & 0 deletions Runtime/Systems/CellListsInitSystem.cs
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);
}
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Systems/CellListsInitSystem.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 09b228e

Please sign in to comment.