1- using System . Collections . Generic ;
1+ using System . Collections . Generic ;
22using UnityEngine ;
33
44public enum BuildingPlacement
@@ -17,11 +17,13 @@ public class Building : Unit
1717
1818 private MeshFilter _rendererMesh ;
1919 private Mesh [ ] _constructionMeshes ;
20- private float _constructionRatio ;
20+ private int _constructionHP ;
2121 private List < CharacterManager > _constructors ;
2222 private List < Transform > _smokeVfx ;
2323 private BuildingBT _bt ;
2424
25+ private AudioClip _ambientSound ;
26+
2527 private bool _isAlive ;
2628
2729 public Building ( BuildingData data , int owner ) : this ( data , owner , new List < ResourceValue > ( ) { } ) { }
@@ -32,7 +34,7 @@ public Building(BuildingData data, int owner, List<ResourceValue> production) :
3234 _bt = _transform . GetComponent < BuildingBT > ( ) ;
3335 _bt . enabled = false ;
3436
35- _constructionRatio = 0f ;
37+ _constructionHP = 0 ;
3638 _constructors = new List < CharacterManager > ( ) ;
3739 _smokeVfx = new List < Transform > ( ) ;
3840 _isAlive = false ;
@@ -48,16 +50,14 @@ public Building(BuildingData data, int owner, List<ResourceValue> production) :
4850 _rendererMesh = mesh . GetComponent < MeshFilter > ( ) ;
4951 _constructionMeshes = data . constructionMeshes ;
5052
51- if ( data . ambientSound != null )
53+ if ( _buildingManager . ambientSource != null )
5254 {
53- if ( _buildingManager . ambientSource != null )
54- {
55- _buildingManager . ambientSource . clip = data . ambientSound ;
56- _buildingManager . ambientSource . enabled = false ;
57- }
58- else
59- Debug . LogWarning ( $ "'{ data . unitName } ' prefab is missing an ambient audio source!") ;
55+ _buildingManager . ambientSource . clip =
56+ GameManager . instance . gameSoundParameters . constructionSiteSound ;
57+ _ambientSound = data . ambientSound ;
6058 }
59+ else
60+ Debug . LogWarning ( $ "'{ data . unitName } ' prefab is missing an ambient audio source!") ;
6161 }
6262
6363 public void SetMaterials ( ) { SetMaterials ( _placement ) ; }
@@ -93,22 +93,23 @@ public override void Place()
9393 // change building materials
9494 SetMaterials ( ) ;
9595 // change building construction ratio
96- SetConstructionRatio ( 0 ) ;
96+ SetConstructionHP ( 0 ) ;
9797 }
9898
99- public void SetConstructionRatio ( float constructionRatio )
99+ public void SetConstructionHP ( int constructionHP )
100100 {
101101 if ( _isAlive ) return ;
102102
103- _constructionRatio = constructionRatio ;
103+ _constructionHP = constructionHP ;
104+ float constructionRatio = _constructionHP / ( float ) MaxHP ;
104105
105106 int meshIndex = Mathf . Max (
106107 0 ,
107108 ( int ) ( _constructionMeshes . Length * constructionRatio ) - 1 ) ;
108109 Mesh m = _constructionMeshes [ meshIndex ] ;
109110 _rendererMesh . sharedMesh = m ;
110111
111- if ( _constructionRatio >= 1 )
112+ if ( constructionRatio >= 1 )
112113 _SetAlive ( ) ;
113114 }
114115
@@ -135,6 +136,7 @@ public void AddConstructor(CharacterManager m)
135136 VfxType . Smoke ,
136137 Transform . position + new Vector3 ( offset . x , 0 , offset . y ) ) ) ;
137138
139+ _buildingManager . ambientSource . Play ( ) ;
138140 }
139141 }
140142
@@ -149,25 +151,35 @@ public void RemoveConstructor(int index)
149151 foreach ( Transform vfx in _smokeVfx )
150152 VFXManager . instance . Unspawn ( VfxType . Smoke , vfx ) ;
151153 _smokeVfx . Clear ( ) ;
154+ // stop construction sound
155+ _buildingManager . ambientSource . Pause ( ) ;
152156 }
153157
154158 private void _SetAlive ( )
155159 {
156160 _isAlive = true ;
157161 _bt . enabled = true ;
158162 ComputeProduction ( ) ;
159- _buildingManager . ambientSource . enabled = true ;
160- _buildingManager . ambientSource . Play ( ) ;
161163 EventManager . TriggerEvent ( "PlaySoundByName" , "onBuildingPlacedSound" ) ;
162164 Globals . UpdateNavMeshSurface ( ) ;
163165
164166 // when finishing construction, remove smoke VFX
165167 foreach ( Transform vfx in _smokeVfx )
166168 VFXManager . instance . Unspawn ( VfxType . Smoke , vfx ) ;
167169 _smokeVfx . Clear ( ) ;
170+ // replace construction sound
171+ if ( _ambientSound )
172+ {
173+ _buildingManager . ambientSource . clip = _ambientSound ;
174+ _buildingManager . ambientSource . Play ( ) ;
175+ }
176+ else
177+ {
178+ _buildingManager . ambientSource . Stop ( ) ;
179+ }
168180 }
169181
170- public float ConstructionRatio { get => _constructionRatio ; }
182+ public int ConstructionHP { get => _constructionHP ; }
171183 public List < CharacterManager > Constructors { get => _constructors ; }
172184 public bool HasConstructorsFull { get => _constructors . Count == 3 ; }
173185 public bool HasValidPlacement { get => _placement == BuildingPlacement . VALID ; }
0 commit comments