5 private const int MINIMUM_MOVE_DISTANCE_SQ = 2;
6 private const int INVALID = -1;
7 private const int LOOP_SOUND_HEIGHT_LIMIT = 25;
8 private const int LOOP_SOUND_COUNT = 9;
9 private const float BOUNDING_BOX_CROP_FACTOR = 0.3;
10 private const float BOUNDING_BOX_CROP_FACTOR_HEIGHT = 0.9;
11 private const int BUSH_LOOP_SOUND_COUNT_LIMIT = 4;
12 private const string ENTITY_SIZE_SIGNAL_NAME =
"EntitySize";
13 private const string SEED_SIGNAL_NAME =
"Seed";
15 [
Attribute(
"0.3",
desc:
"Ratio of bushes with insect sounds vs without")]
16 private float m_fBushCricketThreshold;
18 private int m_iEntitySizeSignalIdx;
19 private int m_iSeedSignalIdx;
22 private vector m_vCameraPosLooped;
25 private ref array<IEntity> m_aClosestEntityID = {};
28 private ref array<IEntity> m_aLoopedSoundID = {};
31 private ref array<AudioHandle> m_aLoopedSoundAudioHandle = {};
35 override void Update(
float worldTime, vector cameraPos)
37 if (vector.DistanceSqXZ(m_vCameraPosLooped, cameraPos) < MINIMUM_MOVE_DISTANCE_SQ)
40 m_vCameraPosLooped = cameraPos;
42 m_aClosestEntityID.Clear();
45 UpdateClosesEntitieyArray();
51 PlayLoopedSounds(cameraPos);
55 private void StopLoopedSounds()
57 for (
int i = m_aLoopedSoundID.Count() - 1; i >= 0; i--)
59 int index = m_aClosestEntityID.Find(m_aLoopedSoundID[i]);
63 m_aLoopedSoundID.Remove(i);
65 m_aLoopedSoundAudioHandle.Remove(i);
71 private void PlayLoopedSounds(vector cameraPos)
73 foreach(IEntity entity: m_aClosestEntityID)
78 int index = m_aLoopedSoundID.Find(entity);
84 ETreeSoundTypes treeSoundType
85 GetTreeProperties(entity, treeSoundType, foliageHight);
90 if (treeSoundType == ETreeSoundTypes.Leafy)
92 eventName = GetTreeSoundEventName(foliageHight, treeSoundType);
96 vector v = entity.GetOrigin();
97 int seed = v[0] * v[2];
98 RandomGenerator ranGen =
SCR_Math.GetMathRandomGenerator();
101 if (ranGen.RandFloat01() > m_fBushCricketThreshold)
103 float sampleSelector = Math.AbsInt(seed % 100) * 0.01;
115 Math3D.MatrixIdentity4(mat);
119 entity.GetWorldBounds(mins, maxs);
122 float BVHeight = (maxs[1] - mins[1] - foliageHight) * BOUNDING_BOX_CROP_FACTOR_HEIGHT;
125 float widthX = (maxs[0] - mins[0]) * 0.5;
126 float widthZ = (maxs[2] - mins[2]) * 0.5;
127 float BVRadius = 0.5 * (widthX + widthZ) * BOUNDING_BOX_CROP_FACTOR;
130 mat[3][0] = mins[0] + widthX;
131 mat[3][1] = mins[1] + foliageHight + 0.5 * BVHeight;
132 mat[3][2] = mins[2] + widthZ;
140 AudioSystem.SetBoundingVolumeParams(audioHandle, AudioSystem.BV_Cylinder, BVRadius, BVHeight, 0);
143 m_aLoopedSoundID.Insert(entity);
144 m_aLoopedSoundAudioHandle.Insert(audioHandle);
149 private void UpdateClosesEntitieyArray()
156 protected void GetTreeProperties(IEntity entity, out ETreeSoundTypes soundType, out
float foliageHeight)
159 Tree tree = Tree.Cast(entity);
164 TreeClass treeClass = TreeClass.Cast(tree.GetPrefabData());
168 foliageHeight = treeClass.m_iFoliageHeight * entity.GetScale();
169 soundType = treeClass.SoundType;
173 protected string GetTreeSoundEventName(
float foliageHeight, ETreeSoundTypes treeSoundType)
175 if (foliageHeight < 3)
177 else if (foliageHeight < 5)
179 else if (foliageHeight < 7)
189 super.OnPostInit(ambientSoundsComponent, signalsManagerComponent);
191 m_iEntitySizeSignalIdx = signalsManagerComponent.AddOrFindSignal(ENTITY_SIZE_SIGNAL_NAME);
192 m_iSeedSignalIdx = signalsManagerComponent.AddOrFindSignal(SEED_SIGNAL_NAME);