This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
GameObjectCreationHelper.cs
45 lines (39 loc) · 1.99 KB
/
GameObjectCreationHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using Improbable.Gdk.Core;
using Improbable.Gdk.Core.Representation;
using Unity.Entities;
using UnityEngine;
namespace Improbable.Gdk.GameObjectCreation
{
public static class GameObjectCreationHelper
{
private static readonly string WorkerNotCreatedErrorMessage = $"{nameof(EnableStandardGameObjectCreation)} should be called only after a worker has been initialised for the world.";
public static void EnableStandardGameObjectCreation(World world, EntityRepresentationMapping entityRepresentationMapping, GameObject workerGameObject = null)
{
var workerSystem = world.GetExistingSystem<WorkerSystem>();
if (workerSystem == null)
{
throw new InvalidOperationException(WorkerNotCreatedErrorMessage);
}
var creator = new GameObjectCreatorFromMetadata(workerSystem.WorkerType, workerSystem.Origin);
EnableStandardGameObjectCreation(world, creator, entityRepresentationMapping, workerGameObject);
}
public static void EnableStandardGameObjectCreation(World world, IEntityGameObjectCreator creator, EntityRepresentationMapping entityRepresentationMapping,
GameObject workerGameObject = null)
{
var workerSystem = world.GetExistingSystem<WorkerSystem>();
if (workerSystem == null)
{
throw new InvalidOperationException(WorkerNotCreatedErrorMessage);
}
if (world.GetExistingSystem<GameObjectInitializationSystem>() != null)
{
workerSystem.LogDispatcher.HandleLog(LogType.Error, new LogEvent(
"You should only call EnableStandardGameobjectCreation() once on worker setup")
.WithField(LoggingUtils.LoggerName, nameof(GameObjectCreationHelper)));
return;
}
world.AddSystem(new GameObjectInitializationSystem(creator, entityRepresentationMapping, workerGameObject));
}
}
}