-
Notifications
You must be signed in to change notification settings - Fork 0
/
EntityPrefabInstanceSystem.cs
104 lines (86 loc) · 3.72 KB
/
EntityPrefabInstanceSystem.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Collections.Generic;
using Gemserk.Leopotam.Ecs.Components;
using Gemserk.Utilities;
using Leopotam.EcsLite;
using UnityEngine;
namespace Gemserk.Leopotam.Ecs
{
public struct EntityPrefabComponent
{
public BaseEntityPrefabInstance prefabInstance;
}
public class EntityPrefabInstanceSystem : BaseSystem, IEcsRunSystem
{
private class GameObjectLinkParameter : IEntityInstanceParameter
{
public GameObject gameObject;
public void Apply(World world, Entity entity)
{
world.AddComponent(entity, new GameObjectComponent
{
gameObject = gameObject
});
var entityReference = gameObject.AddComponent<EntityReference>();
entityReference.entity = entity;
}
}
private readonly List<IEntityInstanceParameter> parameters = new List<IEntityInstanceParameter>();
// UPDATE: This is not done by GameObjectEntitySystem
// public void OnEntityDestroyed(World world, Entity entity)
// {
// if (world.HasComponent<GameObjectComponent>(entity))
// {
// ref var gameObjectComponent = ref world.GetComponent<GameObjectComponent>(entity);
// if (gameObjectComponent.gameObject != null)
// {
// GameObject.Destroy(gameObjectComponent.gameObject);
// }
// gameObjectComponent.gameObject = null;
// }
// }
public void Run(EcsSystems systems)
{
var entityPrefabComponents = world.GetComponents<EntityPrefabComponent>();
foreach (var entity in world.GetFilter<EntityPrefabComponent>().End())
{
var entityPrefabComponent = entityPrefabComponents.Get(entity);
var prefabInstance = entityPrefabComponent.prefabInstance;
if (prefabInstance.onInstantiateActionType == BaseEntityPrefabInstance.OnInstantiateActionType.LinkObject)
{
if (prefabInstance.instance != Entity.NullEntity)
{
continue;
}
}
parameters.Clear();
var definition = prefabInstance.GetEntityDefinition();
if (definition != null)
{
prefabInstance.GetEntityParameters(parameters);
if (prefabInstance.onInstantiateActionType == BaseEntityPrefabInstance.OnInstantiateActionType.LinkObject)
{
parameters.Add(new GameObjectLinkParameter
{
gameObject = prefabInstance.gameObject
});
}
prefabInstance.instance = world.CreateEntity(definition, parameters);
}
else
{
prefabInstance.instance = Entity.NullEntity;
}
if (prefabInstance.onInstantiateActionType == BaseEntityPrefabInstance.OnInstantiateActionType.Disable)
{
prefabInstance.gameObject.SetActive(false);
}
if (prefabInstance.onInstantiateActionType == BaseEntityPrefabInstance.OnInstantiateActionType.Destroy)
{
Object.Destroy(prefabInstance.gameObject);
}
// this is the temporary spawn entity
world.DestroyEntity(world.GetEntity(entity));
}
}
}
}