적기 데미지 교환

카테고리 없음 2020. 2. 11. 15:34

총알에 데미지, 방어구 관통력 및 여러가지 특성을 멤버 변수로 갖게하고 collider를 이용해 GetComponent<EnemyHP>()로 적기의 체력을 담당하는 컴포넌트에 접속해서 데미지를 가하게 한다.

한프레임에 여러 총알이 데미지를 가하면서 죽는 시점이 곂칠 수 있는것을 방지하기 위해 데미지는 한프레임에 한번씩만 넣어야한다. 따라서 Queue<int> damages 멤버변수를 만들고 코루틴을 돌려서 매 프레임 Dequeue 하면서 적기의 체력을 깍아 나가는 방식으로 하였다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class EnemyHP : MonoBehaviour
{
    public int hp;
    public int armor;
    public Queue<int> damages;
    Coroutine giveDamageC;
    public void Init(int hp, int armor)
    {
        this.hp = hp;
        this.armor = armor;
    }
    IEnumerator GiveDamage()
    {
        while(true)
        {
            this.hp -= damages.Dequeue();
            if(damages.Count == 0)
            {
                break;
            }
            if(this.hp <=0)
            {
                Destroy(this.gameObject);
            }
            yield return null;
        }
    }
    public void Hit(int damage, params int[] penetration) //데미지, 고정 방관, 비율 방관순
    {
        int finalDamage = 0;
        //실제 피해 = 원래 피해 x 100 / (100 + 방어력)------ - (방어력이 0 이상일 경우)
 
        //실제 피해 = 원래 피해 x(2 - 100 / (100 - 방어력))
        if (penetration.Length ==2//데미지, 고정방관, 비율방관
        {
            finalDamage = (damage * 100/ (100 + ((this.armor - penetration[0]) / penetration[1]));
            damages.Enqueue(finalDamage);
        }
        else if(penetration.Length == 1//데미지, 고정방관
        {
            finalDamage = (damage * 100/ (100 + this.armor - penetration[0]);
            damages.Enqueue(finalDamage);
        }
        else if(penetration.Length ==0)
        {
            finalDamage = (damage * 100/ (100 + this.armor);
            damages.Enqueue(finalDamage);
        }
        else
        {
            Debug.Log("Wrong penetration parameter!");
        }
        StopAllCoroutines();
        this.giveDamageC = StartCoroutine(GiveDamage());
        
    }
    
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

이런식으로 하면 특성에 있는 도트딜을 주는것도 자신의 코루틴 안에서  저 컴포넌트에 접속해서 Hit 메서드를 불러와서 데미지를 줄 수 있다.

: