整理EntityDatabase代码

This commit is contained in:
sunqinzhe committed 2025-12-18 19:42:22 +08:00
1 parent 6c8108e3ea
commit d9b66edd89
18 files changed
+221 -58

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
using Unity.Mathematics;
namespace ECSTest.Server
{
public struct CannonData
{
public float2 position;
public float2 velocity;
public float direction;
public int cooldownFrameId;
public int health;
public int deathFrameId;
}
public struct BulletData
{
public float2 position;
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8e44ed253ab647b3910794e124e6402e
timeCreated: 1766030729
+52 -1
View File
@@ -1,13 +1,64 @@
using GameCore.TinyECS;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
using Random = Unity.Mathematics.Random;
namespace ECSTest.Server
{
[BurstCompile]
public struct TestWorldJob : IJob
{
public World world;
public void Execute()
{
// var componentId = ComponentId<BulletData>.Get();
// Debug.LogError($"XXXX bulletData id: {componentId}");
// var componentId2 = ComponentId<CannonData>.Get();
// Debug.LogError($"xxxx2222 CannonData id: {componentId2}");
}
}
[BurstCompile]
public struct SpawnCannonJob : IJob
{
public World world;
public NativeList<bool> deferList;
public uint seed;
public int maxCount;
public int spawnCountPerFrame;
public Rect rect;
public void Execute()
{
ref var database = ref world.cannonDatabase;
if (database.Count >= maxCount)
return;
var random = new Random(seed);
for (int i = 0; i < spawnCountPerFrame; i++)
{
var pos = random.NextFloat2(rect.min, rect.max);
var dir = random.NextFloat(math.PI2);
math.sincos(dir, out var s, out var c);
var velocity = new float2(c, s);
var entity = database.AddEntity();
//TODO
}
}
}
[BurstCompile]
public struct SpawnBulletJob : IJob
{
public WorldManager worldManager;
public World World;
public void Execute()
{
+31
View File
@@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using GameCore.Network;
using GameCore.TinyECS;
using Unity.Collections;
using Unity.Jobs;
using Unity.Networking.Transport;
@@ -21,26 +22,47 @@ namespace ECSTest.Server
private NativeList<NetworkConnection> connectionList;
private NativeList<int> disconnectIndexList;
private World world;
private JobHandle gameLogicJobHandle;
private void OnEnable()
{
driver = driverAsset.CreateServerDriver(address, out unreliablePipeline, out reliablePipeline);
connectionList = new NativeList<NetworkConnection>(16, Allocator.Persistent);
disconnectIndexList = new NativeList<int>(16, Allocator.Persistent);
var cannonMeta = EntityDataBlockMeta.Create<CannonData>();
world.cannonDatabase =
new EntityDatabase<EntityType, EntityDataBlockChain>(EntityType.Cannon,
new EntityDataBlockChain(cannonMeta));
var bulletMeta = EntityDataBlockMeta.Create<BulletData>();
world.bulletDatabase =
new EntityDatabase<EntityType, EntityDataBlockChain>(EntityType.Bullet,
new EntityDataBlockChain(bulletMeta));
}
private void OnDisable()
{
networkJobHandle.Complete();
gameLogicJobHandle.Complete();
driver.Dispose();
connectionList.Dispose();
disconnectIndexList.Dispose();
world.Dispose();
}
void Update()
{
var componentId = ComponentId<BulletData>.Get();
Debug.LogError($"AAAA bulletData id: {componentId}");
var componentId2 = ComponentId<CannonData>.Get();
Debug.LogError($"AAAA2222 CannonData id: {componentId2}");
networkJobHandle.Complete();
var concurrentDriver = driver.ToConcurrent();
@@ -60,6 +82,15 @@ namespace ECSTest.Server
disconnectIndexList = disconnectIndexList.AsParallelWriter(),
}.Schedule(connectionList, 16, networkJobHandle);
if (gameLogicJobHandle.IsCompleted)
{
gameLogicJobHandle.Complete();
gameLogicJobHandle = new TestWorldJob()
{
world = world,
}.Schedule(networkJobHandle);
}
}
}
}
+17
View File
@@ -0,0 +1,17 @@
using System;
using GameCore.TinyECS;
namespace ECSTest.Server
{
public struct World : IDisposable
{
public EntityDatabase<EntityType, EntityDataBlockChain> cannonDatabase;
public EntityDatabase<EntityType, EntityDataBlockChain> bulletDatabase;
public void Dispose()
{
cannonDatabase.Dispose();
bulletDatabase.Dispose();
}
}
}
-9
View File
@@ -1,9 +0,0 @@
using GameCore.TinyECS;
namespace ECSTest.Server
{
public struct WorldManager
{
public EntityDatabase<EntityType, EntityDataBlockChain> bulletDatabase;
}
}