341 lines
12 KiB
C#
341 lines
12 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using DG.Tweening;
|
|
using TMPro;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.SceneManagement;
|
|
using DarkTonic.MasterAudio;
|
|
using System.Collections;
|
|
|
|
#if UNITY_EDITOR
|
|
// PackageManager ???? ????
|
|
#endif
|
|
|
|
public partial class BattleManager : MonoBehaviour
|
|
{
|
|
public GameObject HeroUnit;
|
|
public Transform[] MonsterPositions = new Transform[3];
|
|
|
|
public GameObject HitEffectPrefab;
|
|
public GameObject MissPrefab;
|
|
public GameObject NumberPrefab;
|
|
public GameObject CriticalPrefab;
|
|
public TextMeshProUGUI stageClearText;
|
|
public Image darkPanel; // ???? ???? ???????? ???? ????
|
|
|
|
|
|
public float CriticalRate; // ?????? ????(10000????)
|
|
public float CriticalDamage; // ?????? ????(10000????)
|
|
public int HeroDamag; // ???? ??????
|
|
|
|
private List<Transform> aliveMonsters = new List<Transform>();
|
|
private bool stageCleared = false;
|
|
private bool isHeroAttacking = false;
|
|
public string playlistName = "Bgm";
|
|
|
|
// ?????????? ?????? ???? ????
|
|
private SpriteRenderer[] spriteRenderers;
|
|
|
|
|
|
private void Start()
|
|
{
|
|
// ?????? ???? ???????? ???? ?????? ???????? ????
|
|
foreach (Transform monsterPos in MonsterPositions)
|
|
{
|
|
aliveMonsters.Add(monsterPos);
|
|
}
|
|
// NumberPrefab?? ???? ???????? ???? ??????????.
|
|
int childCount = NumberPrefab.transform.childCount;
|
|
|
|
|
|
// ?????????? ?????? ???? ??????
|
|
spriteRenderers = new SpriteRenderer[childCount];
|
|
|
|
// ?? ???? ?????????? ?????????? ?????? ?????????? ?????? ????
|
|
for (int i = 0; i < childCount; i++)
|
|
{
|
|
Transform child = NumberPrefab.transform.GetChild(i);
|
|
SpriteRenderer spriteRenderer = child.GetComponent<SpriteRenderer>();
|
|
|
|
if (spriteRenderer != null)
|
|
{
|
|
spriteRenderers[i] = spriteRenderer;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("SpriteRenderer component not found on child " + i + " of NumberPrefab.");
|
|
}
|
|
}
|
|
|
|
MasterAudio.PlaylistsMuted = true;
|
|
MasterAudio.PlaySound("BGM_DAILYDUNGEON", 0.7f);
|
|
}
|
|
|
|
public void HeroAttack()
|
|
{
|
|
if (aliveMonsters.Count > 0 && !isHeroAttacking)
|
|
{
|
|
isHeroAttacking = true;
|
|
HeroUnit.SetActive(true);
|
|
|
|
// ?????? ???? ??????????
|
|
Sequence attackSequence = DOTween.Sequence();
|
|
attackSequence.Append(HeroUnit.transform.DOLocalMoveY(-700, 0.25f).SetEase(Ease.OutCubic));
|
|
attackSequence.AppendCallback(() =>
|
|
{
|
|
// ???? ???????????? ?????? ?????????? ?????? ????????.
|
|
DealDamageToMonster();
|
|
});
|
|
attackSequence.Append(HeroUnit.transform.DOLocalMoveY(-1500, 0.15f).SetEase(Ease.OutCubic));
|
|
attackSequence.OnComplete(() =>
|
|
{
|
|
// ?????????? ???? ?? ?????? ????
|
|
isHeroAttacking = false;
|
|
HeroUnit.SetActive(false);
|
|
});
|
|
}
|
|
}
|
|
|
|
private void DealDamageToMonster()
|
|
{
|
|
if (aliveMonsters.Count > 0)
|
|
{
|
|
|
|
int randomMonsterIndex = Random.Range(0, aliveMonsters.Count);
|
|
Transform targetMonster = aliveMonsters[randomMonsterIndex];
|
|
|
|
float randomNumber = Random.Range(0, HeroDamag); // 0???? ???? ???????????? ?????? ????
|
|
|
|
// ???? ???? ???????? ???????? ?????? ????
|
|
float randomValue = Random.Range(0, 10000); // 0???? 9999?????? ???? ??
|
|
bool isCritical = randomValue<= CriticalRate;
|
|
int resultNumber = 0;
|
|
|
|
|
|
|
|
int randomSoundIndex = Random.Range(1, 4); // 1???? 3?????? ?????? ???? ????
|
|
|
|
string soundName = "Hot_Type2_" + randomSoundIndex.ToString(); // ?????? ???? ????
|
|
|
|
MasterAudio.PlaySound(soundName, 0.7f, null);
|
|
|
|
int randomVoiceIndex = Random.Range(1, 13); // 1???? 12?????? ?????? ???? ????
|
|
|
|
string voiceName = "Voice_HERO_01_Combo_1_" + randomVoiceIndex.ToString(); // ?????? ???? ????
|
|
|
|
MasterAudio.PlaySound(voiceName, 0.7f, null);
|
|
|
|
|
|
|
|
|
|
|
|
if (isCritical)
|
|
{
|
|
resultNumber = Mathf.RoundToInt((randomNumber * CriticalDamage) / 10000f);
|
|
}
|
|
else
|
|
{
|
|
resultNumber = (int)randomNumber;
|
|
}
|
|
|
|
// HitEffectPrefab ?????????? ???????? ???????? ?????? ???????? ????
|
|
GameObject hitEffectPosition = Instantiate(HitEffectPrefab, targetMonster.position, Quaternion.identity);
|
|
hitEffectPosition.SetActive(true);
|
|
Destroy(hitEffectPosition, 0.5f); // ???? ???? ???? ????
|
|
|
|
|
|
if (resultNumber == 0)
|
|
{
|
|
// ?????? ?????? ???? ?????? ????
|
|
ShowMissDamage(targetMonster.position + new Vector3(0, 1.4f, 0), targetMonster);
|
|
}
|
|
else
|
|
{
|
|
ShowNumberDamage(targetMonster.position + new Vector3(0, 1.4f, 0), targetMonster, resultNumber, isCritical);
|
|
// HitEffectPrefab ?????????? ???????? ???????? ?????? ???????? ????
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ShowMissDamage(Vector3 monsterPosition, Transform targetMonster)
|
|
{
|
|
// ?????? ?????? ???? ?????? ????
|
|
GameObject missObject = Instantiate(MissPrefab, monsterPosition, Quaternion.identity);
|
|
missObject.SetActive(true);
|
|
|
|
// ???? ??????????
|
|
missObject.transform.localScale = Vector3.one * 1.5f;
|
|
missObject.transform.DOScale(Vector3.one * 0.3f, 0.4f).SetEase(Ease.OutCubic);
|
|
missObject.transform.DOMoveY(missObject.transform.position.y + 1, 0.5f).SetEase(Ease.OutCubic).OnComplete(() =>
|
|
{
|
|
Destroy(missObject);
|
|
});
|
|
}
|
|
private void ShowCriEffect(Vector3 monsterPosition, Transform targetMonster)
|
|
{
|
|
|
|
GameObject CriObject = Instantiate(CriticalPrefab, monsterPosition, Quaternion.identity);
|
|
CriObject.SetActive(true);
|
|
|
|
// ???? ??????????
|
|
CriObject.transform.localScale = Vector3.one * 1.5f;
|
|
CriObject.transform.DOScale(Vector3.one * 0.3f, 0.4f).SetEase(Ease.OutCubic);
|
|
CriObject.transform.DOMoveY(CriObject.transform.position.y + 1, 0.5f).SetEase(Ease.OutCubic).OnComplete(() =>
|
|
{
|
|
Destroy(CriObject);
|
|
});
|
|
}
|
|
|
|
private void ShowNumberDamage(Vector3 monsterPosition, Transform targetMonster, int damage, bool isCritical)
|
|
{
|
|
// ???? ???? ????
|
|
targetMonster.DOPunchPosition(new Vector3(10, 0, 0), 0.2f);
|
|
|
|
// damage?? ???????? ????
|
|
string damageString = damage.ToString();
|
|
|
|
// ???? ?????? ???? ??????
|
|
float xOffset = 0;
|
|
|
|
if (isCritical)
|
|
{
|
|
xOffset+=2.3f;
|
|
}
|
|
else
|
|
{
|
|
xOffset += 1.1f;
|
|
}
|
|
|
|
float totalWidth = ((damageString.Length-1) * xOffset); // ???? ????
|
|
|
|
// ???? ???????? ????
|
|
GameObject digitObject = new GameObject("HitNumber");
|
|
|
|
// ???? ?????? monsterPosition?? ???????? ???? (???? ??????)
|
|
digitObject.transform.localPosition = new Vector3(monsterPosition.x, monsterPosition.y - 2f, monsterPosition.z);
|
|
|
|
// ?????????? ?? ???? ????
|
|
for (int i = 0; i < damageString.Length; i++)
|
|
{
|
|
int digit = int.Parse(damageString[i].ToString()); // ???????? ????
|
|
|
|
if (digit >= 0 && digit < spriteRenderers.Length)
|
|
{
|
|
if (isCritical)
|
|
{
|
|
digit += 10;
|
|
}
|
|
// ???? ?????????? ?????? ????
|
|
SpriteRenderer digitSpriteRenderer = spriteRenderers[digit];
|
|
|
|
// ???? ?????????? ????
|
|
GameObject digitImageObject = new GameObject("DigitImage"); // ?? ???? ???????? ????
|
|
//digitImageObject.transform.position = digitObject.transform.position;
|
|
|
|
// ?????????? ?????? ???? ?? ????
|
|
SpriteRenderer newRenderer = digitImageObject.AddComponent<SpriteRenderer>();
|
|
newRenderer.sprite = digitSpriteRenderer.sprite;
|
|
|
|
// Order ???? 300???? ????
|
|
newRenderer.sortingOrder = 300;
|
|
|
|
// x ???? ???????? ???? ?????? ???????? ????
|
|
float xPos = -totalWidth / 2 + i * xOffset;
|
|
digitImageObject.transform.localPosition = new Vector3(xPos, 0, 0);
|
|
|
|
// ?????? ???? (???? ????????)
|
|
digitImageObject.transform.localScale = Vector3.one * 1.0f;
|
|
|
|
// ???? ????
|
|
digitImageObject.transform.SetParent(digitObject.transform);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Digit out of range: " + digit);
|
|
}
|
|
}
|
|
if (isCritical)
|
|
{
|
|
ShowCriEffect(targetMonster.position + new Vector3(0, 1.8f, 0), targetMonster);
|
|
digitObject.transform.localScale = Vector3.one * 0.5f; // ???? ????
|
|
digitObject.transform.DOScale(Vector3.one * 0.2f, 0.3f).SetEase(Ease.OutCubic);
|
|
digitObject.transform.DOMoveY(digitObject.transform.position.y + 2, 0.5f).SetEase(Ease.OutCubic).OnComplete(() =>
|
|
{
|
|
Destroy(digitObject); // ?????????? ???????? ???? ???? ?????????? ??????????.
|
|
});
|
|
}
|
|
else
|
|
{
|
|
// digitObject?? ???????? ?????????? ????
|
|
digitObject.transform.localScale = Vector3.one * 0.5f; // ???? ????
|
|
digitObject.transform.DOScale(Vector3.one * 0.3f, 0.3f).SetEase(Ease.OutCubic);
|
|
digitObject.transform.DOMoveY(digitObject.transform.position.y + 2, 0.5f).SetEase(Ease.OutCubic).OnComplete(() =>
|
|
{
|
|
Destroy(digitObject); // ?????????? ???????? ???? ???? ?????????? ??????????.
|
|
});
|
|
}
|
|
|
|
|
|
|
|
// ???????? HP ?????? ???? ????
|
|
MonsterHealth monsterHealth = targetMonster.GetComponentInChildren<MonsterHealth>();
|
|
if (monsterHealth != null && !monsterHealth.IsDead)
|
|
{
|
|
monsterHealth.TakeDamage(damage); // damage ???? ????
|
|
|
|
// ???? ???????? HP?? 0 ??????
|
|
if (monsterHealth.CurrentHealth <= 0)
|
|
{
|
|
// ???????? ?????????? ????
|
|
aliveMonsters.Remove(targetMonster);
|
|
|
|
// ???? ???????? ?????? StageClear ???? ????
|
|
if (aliveMonsters.Count == 0 && !stageCleared)
|
|
{
|
|
stageCleared = true;
|
|
MasterAudio.StopAllOfSound("BGM_DAILYDUNGEON");
|
|
MasterAudio.PlaySound("BGM_VAILAGE", 0.7f);
|
|
ShowStageClearAnimation();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
private void ShowStageClearAnimation()
|
|
{
|
|
// ?????? ?????? ?? ??????
|
|
stageClearText.gameObject.SetActive(true);
|
|
HeroUnit.SetActive(false);
|
|
stageClearText.transform.localScale = Vector3.zero;
|
|
|
|
// Scale ??????????
|
|
stageClearText.transform.DOScale(Vector3.one, 0.5f).SetEase(Ease.OutBack).OnComplete(() =>
|
|
{
|
|
// ?????? ?????? ??????????
|
|
stageClearText.transform.DOShakePosition(1.0f, new Vector3(0, 10, 0), 10, 90, false, true).OnComplete(() =>
|
|
{
|
|
// ?????? ?????? ???? ??????????
|
|
CanvasGroup canvasGroup = stageClearText.GetComponent<CanvasGroup>();
|
|
canvasGroup.DOFade(0, 0.5f).OnComplete(() =>
|
|
{
|
|
// ???? ???? ?????? ??????
|
|
darkPanel.gameObject.SetActive(true);
|
|
|
|
// ?????? ???? ?????????? (???????? 0???? 1?? ????)
|
|
DOVirtual.Float(0, 1.0f, 1.0f, (fadeValue) =>
|
|
{
|
|
darkPanel.color = new Color(0, 0, 0, fadeValue);
|
|
}).OnComplete(() =>
|
|
{
|
|
// ???? ?? ????
|
|
MasterAudio.PlaySound("BGM_VAILAGE", 0);
|
|
MasterAudio.PlaylistsMuted = false;
|
|
SceneManager.LoadScene(4);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
} |