You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.9 KiB

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;
using UnityEngine;
namespace ECSTest.Server
{
public class ServerMain : MonoBehaviour
{
public string address = "127.0.0.1";
public NetworkDriverAsset driverAsset;
private NetworkDriver driver;
private NetworkPipeline unreliablePipeline;
private NetworkPipeline reliablePipeline;
private JobHandle networkJobHandle;
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>();
var cannonDataBlockChain = new EntityDataBlockChain(cannonMeta, Allocator.Persistent);
world.cannonDatabase = cannonDataBlockChain.CreateEntityDatabase(EntityType.Cannon, 16);
var bulletMeta = EntityDataBlockMeta.Create<BulletData>();
var bulletDataBlockChain = new EntityDataBlockChain(bulletMeta, Allocator.Persistent);
world.bulletDatabase = bulletDataBlockChain.CreateEntityDatabase(EntityType.Bullet, 16);
}
private void OnDisable()
{
networkJobHandle.Complete();
gameLogicJobHandle.Complete();
driver.Dispose();
connectionList.Dispose();
disconnectIndexList.Dispose();
world.Dispose();
}
void Update()
{
networkJobHandle.Complete();
var concurrentDriver = driver.ToConcurrent();
networkJobHandle = driver.ScheduleUpdate();
networkJobHandle = new ServerConnectionJob()
{
driver = driver,
connectionList = connectionList,
disconnectIndexList = disconnectIndexList,
}.Schedule(networkJobHandle);
networkJobHandle = new ServerReceiveJob()
{
driver = concurrentDriver,
connectionList = connectionList,
disconnectIndexList = disconnectIndexList.AsParallelWriter(),
}.Schedule(connectionList, 16, networkJobHandle);
if (gameLogicJobHandle.IsCompleted)
{
gameLogicJobHandle.Complete();
gameLogicJobHandle = new TestWorldJob()
{
world = world,
}.Schedule(networkJobHandle);
}
}
}
}