/
TwoStickBootstrap.cs
92 lines (74 loc) · 3.82 KB
/
TwoStickBootstrap.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
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using Unity.Rendering;
using Unity.Transforms;
using Unity.Transforms2D;
namespace TwoStickPureExample
{
public sealed class TwoStickBootstrap
{
public static EntityArchetype PlayerArchetype;
public static EntityArchetype BasicEnemyArchetype;
public static EntityArchetype ShotSpawnArchetype;
public static MeshInstanceRenderer PlayerLook;
public static MeshInstanceRenderer PlayerShotLook;
public static MeshInstanceRenderer EnemyShotLook;
public static MeshInstanceRenderer EnemyLook;
public static TwoStickSettings Settings;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void Initialize()
{
// This method creates archetypes for entities we will spawn frequently in this game.
// Archetypes are optional but can speed up entity spawning substantially.
var entityManager = World.Active.GetOrCreateManager<EntityManager>();
// Create player archetype
PlayerArchetype = entityManager.CreateArchetype(
typeof(Position2D), typeof(Heading2D), typeof(PlayerInput),
typeof(Health), typeof(TransformMatrix));
// Create an archetype for "shot spawn request" entities
ShotSpawnArchetype = entityManager.CreateArchetype(typeof(ShotSpawnData));
// Create an archetype for basic enemies.
BasicEnemyArchetype = entityManager.CreateArchetype(
typeof(Enemy), typeof(Health), typeof(EnemyShootState),
typeof(Position2D), typeof(Heading2D),
typeof(TransformMatrix), typeof(MoveSpeed), typeof(MoveForward));
}
// Begin a new game.
public static void NewGame()
{
// Access the ECS entity manager
var entityManager = World.Active.GetOrCreateManager<EntityManager>();
// Create an entity based on the player archetype. It will get default-constructed
// defaults for all the component types we listed.
Entity player = entityManager.CreateEntity(PlayerArchetype);
// We can tweak a few components to make more sense like this.
entityManager.SetComponentData(player, new Position2D {Value = new float2(0.0f, 0.0f)});
entityManager.SetComponentData(player, new Heading2D {Value = new float2(0.0f, 1.0f)});
entityManager.SetComponentData(player, new Health { Value = Settings.playerInitialHealth });
// Finally we add a shared component which dictates the rendered look
entityManager.AddSharedComponentData(player, PlayerLook);
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
public static void InitializeWithScene()
{
var settingsGO = GameObject.Find("Settings");
Settings = settingsGO?.GetComponent<TwoStickSettings>();
if (!Settings)
return;
PlayerLook = GetLookFromPrototype("PlayerRenderPrototype");
PlayerShotLook = GetLookFromPrototype("PlayerShotRenderPrototype");
EnemyShotLook = GetLookFromPrototype("EnemyShotRenderPrototype");
EnemyLook = GetLookFromPrototype("EnemyRenderPrototype");
EnemySpawnSystem.SetupComponentData(World.Active.GetOrCreateManager<EntityManager>());
World.Active.GetOrCreateManager<UpdatePlayerHUD>().SetupGameObjects();
}
private static MeshInstanceRenderer GetLookFromPrototype(string protoName)
{
var proto = GameObject.Find(protoName);
var result = proto.GetComponent<MeshInstanceRendererComponent>().Value;
Object.Destroy(proto);
return result;
}
}
}