整理EntityDatabase代码
This commit is contained in:
1 parent
6c8108e3ea
commit
d9b66edd89
18 files changed
+221
-58
No files matched your search
@@ -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
|
||||
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
File renamed without changes.
@@ -1,9 +0,0 @@
|
||||
using GameCore.TinyECS;
|
||||
|
||||
namespace ECSTest.Server
|
||||
{
|
||||
public struct WorldManager
|
||||
{
|
||||
public EntityDatabase<EntityType, EntityDataBlockChain> bulletDatabase;
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ namespace ECSTest
|
||||
using var blockChain = new EntityDataBlockChain(meta, 1000);
|
||||
|
||||
blockChain.Add();
|
||||
ref var stateData = ref blockChain.GetData<StateData>(0);
|
||||
ref var stateData = ref blockChain.GetDataRef<StateData>(0);
|
||||
stateData.hp = 1000;
|
||||
|
||||
var view = blockChain.AsBlockView(0);
|
||||
|
||||
Reference in new issue
Block a user