Skip to content

Commit 1993ebd

Browse files
committed
feat(core): add tutorial 38 code
1 parent 7df31c9 commit 1993ebd

6 files changed

Lines changed: 59 additions & 28 deletions

File tree

Assets/Scripts/DebugConsole/DebugConsole.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ private void Awake()
8686
EventManager.TriggerEvent("UpdateUnitFormationType");
8787
});
8888

89-
new DebugCommand<float>("set_construction_ratio", "Sets the selected unit construction ratio.", "set_construction_ratio <ratio>", (x) =>
89+
new DebugCommand<int>("set_construction_hp", "Sets the selected unit construction HP.", "set_construction_hp <hp>", (x) =>
9090
{
9191
if (Globals.SELECTED_UNITS.Count == 0) return;
9292
Building b = (Building) Globals.SELECTED_UNITS[0].GetComponent<BuildingManager>().Unit;
9393
if (b == null) return;
94-
b.SetConstructionRatio(x);
94+
b.SetConstructionHP(x);
9595
});
9696

9797
_displayType = DisplayType.None;

Assets/Scripts/Managers/GameManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class GameManager : MonoBehaviour
1111

1212
public GameGlobalParameters gameGlobalParameters;
1313
public GamePlayersParameters gamePlayersParameters;
14+
public GameSoundParameters gameSoundParameters;
1415
public GameInputParameters gameInputParameters;
1516
public GameObject fov;
1617

Assets/Scripts/ScriptableObjects/Parameters/GameSoundParameters.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public class GameSoundParameters : GameParameters
1414
[Header("Ambient sounds")]
1515
public AudioClip onDayStartSound;
1616
public AudioClip onNightStartSound;
17+
public AudioClip constructionSiteSound;
1718
public AudioClip onBuildingPlacedSound;
1819
}

Assets/Scripts/Units/Buildings/Building.cs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using UnityEngine;
33

44
public 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; }

Assets/Scripts/Units/Buildings/BuildingManager.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public bool HasValidPlacement()
7272

7373
public bool Build(int buildPower)
7474
{
75-
_building.SetConstructionRatio(_building.ConstructionRatio + buildPower / 10f);
75+
_building.SetConstructionHP(_building.ConstructionHP + buildPower);
7676
UpdateHealthbar();
7777
return _building.IsAlive;
7878
}
@@ -92,11 +92,11 @@ protected override void UpdateHealthbar()
9292
if (!healthbar) return;
9393
Transform fill = healthbar.transform.Find("Fill");
9494

95-
// if in construction: show current construction ratio
96-
if (IsActive() && !_building.IsAlive)
97-
fill.GetComponent<UnityEngine.UI.Image>().fillAmount = _building.ConstructionRatio;
98-
// else show health ratio a usual
99-
else
100-
fill.GetComponent<UnityEngine.UI.Image>().fillAmount = Unit.HP / (float)Unit.MaxHP;
95+
// if in construction: show current construction HP
96+
// else, show current health
97+
int hp = (IsActive() && !_building.IsAlive)
98+
? _building.ConstructionHP
99+
: Unit.HP;
100+
fill.GetComponent<UnityEngine.UI.Image>().fillAmount = hp / (float)Unit.MaxHP;
101101
}
102102
}

Assets/Scripts/Units/Buildings/BuildingPlacer.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ private void OnDisable()
9292

9393
private void _OnBuildInput(object data)
9494
{
95+
// check to see if there is at least one selected unit
96+
// that can build -> we arbitrarily choose the first one
97+
if (Globals.SELECTED_UNITS.Count == 0) return;
98+
99+
UnitManager um = null;
100+
foreach (UnitManager selected in Globals.SELECTED_UNITS)
101+
{
102+
if (selected is CharacterManager cm && ((CharacterData) cm.Unit.Data).buildPower > 0)
103+
{
104+
um = cm;
105+
break;
106+
}
107+
}
108+
if (um == null) return;
109+
_builderManager = um;
110+
111+
95112
string buildingCode = (string)data;
96113
for (int i = 0; i < Globals.BUILDING_DATA.Length; i++)
97114
{
@@ -117,7 +134,7 @@ public void SpawnBuilding(BuildingData data, int owner, Vector3 position, List<R
117134
_placedBuilding.SetPosition(position);
118135
// finish up the placement
119136
_PlaceBuilding(false);
120-
_placedBuilding.SetConstructionRatio(1);
137+
_placedBuilding.SetConstructionHP(_placedBuilding.MaxHP);
121138

122139
// restore the previous state
123140
_placedBuilding = prevPlacedBuilding;

0 commit comments

Comments
 (0)