Unity 내일배움캠프 TIL 0830 | 스킬 클래스

cheeseonrose·2023년 8월 30일
1

Unity 내일배움캠프

목록 보기
23/89
post-thumbnail

팀 프로젝트

스킬 클래스 생성

  • 스킬 부분을 클래스로 따로 만들면 좋을 것 같아서 ISkill 인터페이스와 이걸 상속받는 AlphaSkill, DoubleSkill 클래스를 만들어줬다.
  • 기존에 Player에게 있었던 SkillType enum 클래스도 따로 스크립트를 생성해줬다.
public enum SkillType
{
	ALPHA = 10,
    DOUBLE = 15
}
internal interface ISkill
{
	SkillType SkillType { get; }
    
    string Description { get; }
    
    void Use(Player player, List<ICharacter> monsterList);
}
internal class AlphaSkill : ISkill
{
	public SkillType SkillType { get; }
    
    public string Description { get; }
    
    public AlphaSkill()
    {
    	SkillType = SkillType.ALPHA;
        Description = "알파 스트라이크 - MP 10\n   공격력 * 2 로 하나의 적을 공격합니다.\n";
    }
    
    public void Use(Player player, List<ICharacter> monsterList)
    {
    	Console.Clear();
        
        Extension.TypeWriting("Battle!!\n");
        Extension.TypeWriting($"{player.Name}의 알파 스트라이크 공격!");
        Console.WriteLine();
        
        Monster curMonster = (Monster)monsterList[0];
        int monsterPrevHP = curMonster.CurrentHealth;
        
        int damage = (player.Attack + player.AddAtk) * 2;
        curMonster.TakeDamage(damage);  // 공격력 * 2 로 하나의 적 공격
        
        // 알파 스트라이크 결과 출력
        Console.WriteLine($"Lv.{curMonster.Level} {curMonster.Name}");
        Console.WriteLine($"HP {monsterPrevHP} -> {curMonster.CurrentHealth}\n");
        Console.WriteLine();
    }
}
internal class DoubleSkill: ISkill
{
	public SkillType SkillType { get; }
    
    public string Description { get; }
    
    public DoubleSkill()
    {
    	SkillType = SkillType.DOUBLE;
        Description = "더블 스트라이크 - MP 15\n   공격력 * 1.5 로 2명의 적을 랜덤으로 공격합니다.\n";
    }
    
    public void Use(Player player, List<ICharacter> monsterList)
    {
    	Console.Clear();
        
        Extension.TypeWriting("Battle!!\n");
        Extension.TypeWriting($"{player.Name}의 더블 스트라이크 공격!");
        Console.WriteLine();
        
        int damage = (int)((player.Attack + player.AddAtk) * 1.5); // 플레이어 공격력 * 1.5
        
        List<int> targetMonsterIdxList = GetRandomAliveMonsterIdx(monsterList); // 공격할 몬스터 인덱스가 담긴 리스트
        List<int> targetMonsterPrevHP = new List<int>();    // 공격할 몬스터의 이전 체력을 담을 배열
        
        foreach (int idx in targetMonsterIdxList)
        {
        	Monster curMonster = (Monster)monsterList[idx];
            targetMonsterPrevHP.Add(curMonster.CurrentHealth);
            curMonster.TakeDamage(damage);
        }
        
        Console.WriteLine();
        
        int prevHpIdx = 0;
        // 더블 스트라이크 결과 출력
        foreach (int idx in targetMonsterIdxList)
        {
        	Console.WriteLine($"Lv.{monsterList[idx].Level} {monsterList[idx].Name}");
            Console.WriteLine($"HP {targetMonsterPrevHP[prevHpIdx++]} -> {monsterList[idx].CurrentHealth}\n");
        }
    }
    
    // Monster 배열을 받아서 살아있는 몬스터 중 랜덤으로 2마리를 뽑고, 그 몬스터의 인덱스를 리스트에 담아 반환
    // 리스트 크기가 1일 때는 1마리만 뽑아서 리턴
    private List<int> GetRandomAliveMonsterIdx(List<ICharacter> monsterList)
    {
    	int maxLength = 2;
        List<int> randomIdx = new List<int>();
        
        while (randomIdx.Count < maxLength)
        {
        	if (monsterList.Where(x => !x.IsDead).Count() == 1)
            {
            	int aliveMonsterIdx = monsterList.FindIndex(x => !x.IsDead);
                randomIdx.Add(aliveMonsterIdx);
                break;
            }
            
            int idx = new Random().Next(0, monsterList.Count);
            
            if (!((Monster)monsterList[idx]).IsDead && !randomIdx.Contains(idx)) randomIdx.Add(idx);
        }
        
        return randomIdx;
    }
}
  • Player는 SkillList를 갖고, 스킬 사용시 Player에게 스킬 타입을 넘겨주어 처리하도록 했다.
  • ICharacter로 몬스터 리스트를 받아오는 이유는 Monster 폴더 안에 Monster.cs를 만들어서 Player 클래스에서 Monster 클래스를 인식하지를 못했다 ;ㅅ; 이거슨 실수..
    암튼 그래서 ICharacter로 받아오고 Monster로 타입캐스팅을 해줬다!!
    이건 추후에 코드 리팩토링 할 때 수정하기로 함
public List<ISkill> SkillList { get; }
// Player가 스킬을 사용할 때 실행
public void UseSkill(SkillType type, List<ICharacter> monsterList)
{
	if (type == SkillType.ALPHA)
    {
    	CurrentMP -= (int)SkillType.ALPHA;
        int skillIdx = SkillList.FindIndex(x => x.SkillType == SkillType.ALPHA);
        SkillList[skillIdx].Use(this, monsterList);
    }
    else
    {
    	CurrentMP -= (int)SkillType.DOUBLE;
        int skillIdx = SkillList.FindIndex(x => x.SkillType == SkillType.DOUBLE);
        SkillList[skillIdx].Use(this, monsterList);
    }
}

보상 아이템 반영

  • Player에게 보상 아이템을 넘겨주고 처리하게 했다.
public void GetReward(IItem newItem)
{
	IItem item = PlayerInventory.InventoryItems.Find(x => x.Name == newItem.Name);
    if (item == null) PlayerInventory.InventoryItems.Add(newItem);
    else
    {
    	if (item.Type == ItemType.HealthPotion || item.Type == ItemType.StrengthPotion)
        {
        	((IConsumable)item).Quantity++;
        }
    }
}

그 외에 자잘한 버그들이랑 로직들 수정 (끊임없이) 하고
마참내 UI 작업을 했다
노동의 시작 ...

오늘은 야근 .. ^.^
TIL은 여기까지
끗 ~

0개의 댓글