Skip to content

Commit

Permalink
Refactoring anomaly No. 9
Browse files Browse the repository at this point in the history
  • Loading branch information
Prown0 committed Dec 21, 2024
1 parent 676f509 commit 34e9e63
Showing 1 changed file with 80 additions and 81 deletions.
161 changes: 80 additions & 81 deletions 302/Assets/Scripts/SpecificAnomalyManager/Anomaly09Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
using System.Collections;
using System.Collections.Generic;

public class Anomaly09Controller : AbstractAnomalyComposite
public class Anomaly09Controller : AbstractAnomalyObject
{
public override string Name { get; } = "Anomaly09Controller";
/**********
* fields *
**********/

[Header("Anomaly Settings")]
public float anomalyDelay = 15f;

Expand Down Expand Up @@ -61,75 +64,69 @@ public class Anomaly09Controller : AbstractAnomalyComposite
private List<GameObject> spawnedLights = new List<GameObject>();
private Coroutine anomalyCoroutine;

protected override bool Awake_()
/**************
* properties *
**************/

public override string Name { get; } = "Anomaly09Controller";

/*************************************
* implementation: AbstractBehaviour *
*************************************/

protected override bool InitFields()
{
bool res = base.Awake_();
bool res = base.InitFields();

audioSource = gameObject.AddComponent<AudioSource>();
audioSource.clip = anomalyMusic;
audioSource.volume = musicVolume;
audioSource.loop = true;
audioSource.playOnAwake = false;
return res;
}

void Start()
{
sceneLights = FindScenePointLights();
SaveOriginalIntensities();
}

void OnEnable()
{
StartAnomaly();
Debug.Log($"이상현상 발생 - {anomalyDelay}초 후 나타납니다.");
return res;
}

void OnDisable()
{
ResetAnomaly();
}
/*****************************************
* implementation: AbstractAnomalyObject *
*****************************************/

public override bool StartAnomaly()
{
bool res = base.StartAnomaly();
anomalyCoroutine = StartCoroutine(StartAnomalyWithDelay());
return res;
}

public override bool ResetAnomaly()
{
bool res = base.ResetAnomaly();

if (anomalyCoroutine != null)
{
StopCoroutine(anomalyCoroutine);
anomalyCoroutine = null;
}

StopAnomalyMusic();
CleanupSpawnedObjects();
RestoreOriginalLightIntensities();
anomalyCoroutine = StartCoroutine(StartAnomalyWithDelay());

return res;
}

/***************
* new methods *
***************/

IEnumerator StartAnomalyWithDelay()
{
yield return new WaitForSeconds(anomalyDelay);

DimSceneLights();
SpawnLights();
SpawnDancingGirls();
if (mainSpotlightPrefab != null)
{
spawnedSpotlight = Instantiate(mainSpotlightPrefab, spotlightPosition, Quaternion.Euler(90f, spotlightRotation, -90f));
if (mainSpotlightPrefab != null) {
spawnedSpotlight = Instantiate(
mainSpotlightPrefab, spotlightPosition,
Quaternion.Euler(90f, spotlightRotation, -90f)
);
}

if (fadeInMusic)
if (fadeInMusic) {
StartCoroutine(FadeInMusic());
else
} else {
PlayAnomalyMusic();

}

Debug.Log("이상현상 발생 완료");
}

Expand All @@ -138,10 +135,8 @@ Light[] FindScenePointLights()
Light[] allLights = FindObjectsOfType<Light>();
List<Light> pointLights = new List<Light>();

foreach (Light light in allLights)
{
if (light.type == LightType.Point)
{
foreach (Light light in allLights) {
if (light.type == LightType.Point) {
pointLights.Add(light);
Debug.Log($"Found Point Light: {light.gameObject.name}");
}
Expand All @@ -152,53 +147,53 @@ Light[] FindScenePointLights()

void SaveOriginalIntensities()
{
if (sceneLights != null)
{
if (sceneLights != null) {
originalIntensities = new float[sceneLights.Length];
for (int i = 0; i < sceneLights.Length; i++)
{
if (sceneLights[i] != null)
for (int i = 0; i < sceneLights.Length; i++) {
if (sceneLights[i] != null) {
originalIntensities[i] = sceneLights[i].intensity;
}
}
}
}

void DimSceneLights()
{
if (sceneLights != null)
{
foreach (Light light in sceneLights)
{
if (light != null)
if (sceneLights != null) {
foreach (Light light in sceneLights) {
if (light != null) {
light.intensity = dimmedIntensity;
}
}
}
}

void RestoreOriginalLightIntensities()
{
if (sceneLights != null && originalIntensities != null)
{
for (int i = 0; i < sceneLights.Length; i++)
{
if (sceneLights[i] != null)
if (sceneLights != null && originalIntensities != null) {
for (int i = 0; i < sceneLights.Length; i++) {
if (sceneLights[i] != null) {
sceneLights[i].intensity = originalIntensities[i];
}
}
}
}

void SpawnLights()
{
if (lightPrefab == null) return;
if (lightPrefab == null) {
return;
}

foreach (GameObject light in spawnedLights)
{
if (light != null) Destroy(light);
foreach (GameObject light in spawnedLights) {
if (light != null) {
Destroy(light);
}
}

spawnedLights.Clear();

for (int i = 0; i < numberOfLights; i++)
{
for (int i = 0; i < numberOfLights; i++) {
float angle = i * (360f / numberOfLights);
float x = radius * Mathf.Cos(angle * Mathf.Deg2Rad);
float z = radius * Mathf.Sin(angle * Mathf.Deg2Rad);
Expand All @@ -215,12 +210,16 @@ void SpawnLights()

void SpawnDancingGirls()
{
if (dancingGirlPrefab == null) return;
if (dancingGirlPrefab == null) {
return;
}

foreach (GameObject girl in spawnedGirls)
{
if (girl != null) Destroy(girl);
foreach (GameObject girl in spawnedGirls) {
if (girl != null) {
Destroy(girl);
}
}

spawnedGirls.Clear();

SpawnGirl(position1, rotation1);
Expand All @@ -237,30 +236,29 @@ void SpawnDancingGirls()

void SpawnGirl(Vector3 position, float yRotation)
{
GameObject girl = Instantiate(dancingGirlPrefab, position,
Quaternion.Euler(0, yRotation, 0));
GameObject girl = Instantiate(
dancingGirlPrefab, position, Quaternion.Euler(0, yRotation, 0)
);

spawnedGirls.Add(girl);
}

void PlayAnomalyMusic()
{
if (anomalyMusic != null && audioSource != null)
{
if (anomalyMusic != null && audioSource != null) {
audioSource.volume = musicVolume;
audioSource.Play();
}
}

IEnumerator FadeInMusic()
{
if (anomalyMusic != null && audioSource != null)
{
if (anomalyMusic != null && audioSource != null) {
audioSource.volume = 0f;
audioSource.Play();

float currentTime = 0;
while (currentTime < fadeInDuration)
{
while (currentTime < fadeInDuration) {
currentTime += Time.deltaTime;
audioSource.volume = Mathf.Lerp(0, musicVolume, currentTime / fadeInDuration);
yield return null;
Expand All @@ -271,22 +269,23 @@ IEnumerator FadeInMusic()

void StopAnomalyMusic()
{
if (audioSource != null && audioSource.isPlaying)
if (audioSource != null && audioSource.isPlaying) {
audioSource.Stop();
}
}

void CleanupSpawnedObjects()
{
foreach (GameObject girl in spawnedGirls)
{
foreach (GameObject girl in spawnedGirls) {
if (girl != null) Destroy(girl);
}

spawnedGirls.Clear();

foreach (GameObject light in spawnedLights)
{
foreach (GameObject light in spawnedLights) {
if (light != null) Destroy(light);
}

spawnedLights.Clear();
}
}
Expand Down

0 comments on commit 34e9e63

Please sign in to comment.