-
Notifications
You must be signed in to change notification settings - Fork 1
/
AliensSpawnDirectNoRPC.cs
44 lines (32 loc) · 1.62 KB
/
AliensSpawnDirectNoRPC.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
public class AliensSpawner : NetworkBehaviour {
[SerializeField] private DeadPool deadPool;
[SerializeField] private Transform alienSpawnTransform;
public override void OnNetworkSpawn() {
if (IsServer) InvokeRepeating(nameof(SpawnAlien), 2, 5);
}
public void SpawnAlien() {
var alienInstance = deadPool.GetAlienFromPool(alienSpawnTransform.position, alienSpawnTransform.rotation);
if (!alienInstance.IsSpawned) alienInstance.Spawn(true);
}
public void DespawnAlien(NetworkObject networkObject) {
networkObject.Despawn();
}
}
public class DeadPool : NetworkObjectPool {
[SerializeField] private GameObject bulletPrefab;
[SerializeField] private GameObject alienPrefab;
public NetworkObject GetBulletFromPool(BulletType bulletType, Vector3 position, Quaternion rotation) {
var bulletInstance = GetNetworkObject(bulletPrefab, position, rotation);
bulletInstance.Spawn(true);
bulletInstance.GetComponent<Bullet>().bulletType = bulletType;
return bulletInstance;
}
public NetworkObject GetAlienFromPool(Vector3 position, Quaternion rotation) {
var alienInstance = GetNetworkObject(alienPrefab, position, rotation);
alienInstance.Spawn(true);
return alienInstance;
}
public void ReturnAlienToPool(NetworkObject networkObject) {
ReturnNetworkObject(networkObject, alienPrefab);
}
}