private void Awake() {
if(GameManager.gmInstance == null){
GameManager.gmInstance = this;
}
SpawnMoster();
}
게임매니저는 타 스크립트에서 수치에 쉽게 접근할 수 있도록 싱글톤화를 먼저 시켰다.
public void GetSilverCoin(){
score = score + 10;
Debug.Log("Get SilverCoin");
}
public void GetGoldCoin(){
score = score + 30;
Debug.Log("Get GoldCoin");
}
public void AtkToEnemy(float weaponDmg, Collider2D enemy){ //Player로 부터 공격력과 감지된 적의 collider를 매개변수로 받음
enemy.GetComponent<Enemy>().Damaged(weaponDmg); //공격력을 매개변수로하여 감지된 적의 Damaged를 호출
}
public float DamagedFromEnemy(Collision2D enemy){
return enemy.collider.GetComponent<Enemy>().atkPower;
}
public void SetUICont(){
SetUICont_HP();
SetUICont_MP();
SetUICont_Charging();
SetUICont_Exp();
SetUICont_Level();
}
public void SetUICont_HP(){
uiController.GetComponent<UIController>().playerMaxHP = playerMaxHP;
uiController.GetComponent<UIController>().playerCurHP = playerCurHP;
}
public void SetUICont_MP(){
uiController.GetComponent<UIController>().playerMaxMP = playerMaxMP;
uiController.GetComponent<UIController>().playerCurMP = playerCurMP;
}
public void SetUICont_Charging(){
uiController.GetComponent<UIController>().playerMaxCharging = playerMaxCharging;
uiController.GetComponent<UIController>().playerCurCharging = playerCurCharging;
}
public void SetUICont_Exp(){
uiController.GetComponent<UIController>().playerMaxExp = playerMaxExp;
uiController.GetComponent<UIController>().playerCurExp = playerCurExp;
}
public void SetUICont_Level(){
uiController.GetComponent<UIController>().playerLevel = playerLevel;
}
public void NotEnoughMP(){
uiController.GetComponent<UIController>().NotEnoughMPAni();
}
public void EnemyDeadEvent(GameObject enemy){
Debug.Log("Enemy dead envet start");
ExpToPlayer(enemy.GetComponent<Enemy>().enemyLevel);
monsterCount--; //적 개체 카운트 감소
killCount++ ;
}
public void ExpToPlayer(float exp){
Debug.Log("Exp send to player. exp : " + exp);
player.GetComponent<Player_Interaction>().GetExp(exp);
}
만약 수치 기록및 변형이 추가된다면 관리가 용이할 것이라는 판단하에 코인획득, 몬스터 공격, 피격의 이벤트 및 몬스터개체 피격 이벤트 모두 gameManager를 거치도록 구현하였다.
public void SpawnMoster(){ //몬스터 소환
if(monsterCount >= monsterMaxCount){
return;
}else{
monsterSpawn.GetComponent<MonsterSpawnPoint>().SpawnMoster();
}
Invoke("SpawnMoster",5);
}
몬스터 생성을 위한 코드
이 코드는 몬스터 생성을 호출하는 코드로 아래 스크립트와 연계된다.
public void SpawnMoster(){
int randObject = Random.Range(0, Monster.Length); //몬스터 랜덤
int randPosition = Random.Range(0, SpawnPostion.Length); //위치 랜덤
GameObject FieldMonster = Instantiate(Monster[randObject], SpawnPostion[randPosition].transform.position, transform.rotation);
manager.monsterCount++;
}

몬스터의 스폰위치는 빨간색 빈 오브젝트를 통해 좌표를 특정했다.
현재 구현되어 있는 몬스터 Prefab은 한 개이지만 이후 추가가 된다면 랜덤한 개체를 랜덤한 좌표에서 5초마다 생성되도록 했다. 한번에 최대 생성 가능한 몬스터의 개체수는 5로 제한했다.