// Space MMO
// Author: Josh Hughes
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Game.ECS.Players;
[BurstCompile]
[UpdateInGroup(typeof(SimulationSystemGroup))]
public partial struct PlayerInitialisationSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
}
[BurstCompile]
public void OnDestroy(ref SystemState state)
{
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
EntityCommandBuffer.ParallelWriter ecb = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>()
.CreateCommandBuffer(state.WorldUnmanaged).AsParallelWriter();
state.Dependency = new InitialisePlayerJob()
{
ecb = ecb
}.ScheduleParallel(state.Dependency);
}
[BurstCompile]
public partial struct InitialisePlayerJob : IJobEntity
{
public EntityCommandBuffer.ParallelWriter ecb;
[BurstCompile]
public void Execute([ChunkIndexInQuery] int sortKey, PlayerClassAspect classComponent, in PlayerInitialiseTag tag)
{
if (classComponent.CheckHasInitialised())
{
return;
}
// Spawn in a chosen prefab
// Parent prefab to the base entity (this)
Entity selectedMesh = classComponent.GetClassPrefab();
Entity shipMesh = ecb.Instantiate(sortKey, selectedMesh);
ecb.AddComponent<Parent>(sortKey, shipMesh, new Parent { Value = classComponent.self });
classComponent.SetHasInitialised();
ecb.RemoveComponent<PlayerInitialiseTag>(sortKey, classComponent.self);
}
}
}