| | 1 | | using DCL.Helpers; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | public class StickersController : MonoBehaviour |
| | 8 | | { |
| | 9 | | private const int POOL_PREWARM_COUNT = 10; |
| 657 | 10 | | private Dictionary<string, Pool> pools = new Dictionary<string, Pool>(); |
| | 11 | | private StickersFactory stickersFactory; |
| | 12 | | private bool isInHideArea; |
| | 13 | |
|
| | 14 | | private void Awake() |
| | 15 | | { |
| 634 | 16 | | stickersFactory = Resources.Load<StickersFactory>("StickersFactory"); |
| | 17 | |
|
| 634 | 18 | | ConfigurePools(); |
| 634 | 19 | | } |
| | 20 | |
|
| | 21 | | public void PlaySticker(string id) |
| | 22 | | { |
| 0 | 23 | | PlaySticker(id, transform.position, Vector3.zero, true); |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Play a sticker |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="id"></param> |
| | 30 | | /// <param name="position"> if following transform, position must be an offset from the target. Otherwise, it's |
| | 31 | | /// <param name="direction"></param> |
| | 32 | | /// <param name="followTransform"></param> |
| | 33 | | public void PlaySticker(string id, Vector3 position, Vector3 direction, bool followTransform, int renderingLayer |
| | 34 | | { |
| 22 | 35 | | if (stickersFactory == null || !stickersFactory.TryGet(id, out GameObject prefab) || isInHideArea) |
| 0 | 36 | | return; |
| | 37 | |
|
| | 38 | | // TODO(Brian): Mock this system properly through our service locators or plugin system |
| 22 | 39 | | if (DCL.Configuration.EnvironmentSettings.RUNNING_TESTS) |
| 22 | 40 | | return; |
| | 41 | |
|
| 0 | 42 | | PoolableObject sticker = pools[id].Get(); |
| 0 | 43 | | GameObject stickerGo = sticker.gameObject; |
| 0 | 44 | | sticker.gameObject.layer = renderingLayer; |
| 0 | 45 | | stickerGo.transform.position = position; |
| 0 | 46 | | stickerGo.transform.rotation = Quaternion.Euler(prefab.transform.rotation.eulerAngles + direction); |
| | 47 | |
|
| 0 | 48 | | ReleaseParticlesOnFinish releaser = stickerGo.GetOrCreateComponent<ReleaseParticlesOnFinish>(); |
| 0 | 49 | | if (releaser != null) |
| 0 | 50 | | releaser.Initialize(sticker); |
| | 51 | |
|
| 0 | 52 | | if (followTransform) |
| | 53 | | { |
| 0 | 54 | | FollowObject stickerFollow = stickerGo.GetOrCreateComponent<FollowObject>(); |
| 0 | 55 | | stickerFollow.target = transform; |
| 0 | 56 | | stickerFollow.offset = stickerGo.transform.position - transform.position; |
| | 57 | | } |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | public void ToggleHideArea(bool entered) |
| | 61 | | { |
| 0 | 62 | | isInHideArea = entered; |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | internal void ConfigurePools() |
| | 66 | | { |
| 634 | 67 | | List<StickersFactory.StickerFactoryEntry> stickers = stickersFactory.GetStickersList(); |
| | 68 | |
|
| 13948 | 69 | | foreach (StickersFactory.StickerFactoryEntry stricker in stickers) |
| | 70 | | { |
| 6340 | 71 | | string nameID = $"Sticker {stricker.id}"; |
| 6340 | 72 | | Pool pool = PoolManager.i.GetPool(nameID); |
| 6340 | 73 | | if (pool == null) |
| | 74 | | { |
| 4000 | 75 | | pool = PoolManager.i.AddPool( |
| | 76 | | nameID, |
| | 77 | | Instantiate(stricker.stickerPrefab), |
| | 78 | | maxPrewarmCount: POOL_PREWARM_COUNT, |
| | 79 | | isPersistent: true); |
| | 80 | |
|
| 4000 | 81 | | pool.ForcePrewarm(); |
| | 82 | | } |
| | 83 | |
|
| 6340 | 84 | | pools.Add(stricker.id, pool); |
| | 85 | | } |
| 634 | 86 | | } |
| | 87 | | } |
| | 88 | | } |