Yang.공부방



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CharacterInfo = yangjaejoon.CharacterInfo;
 
public class Character : MonoBehaviour
{
    public CharacterInfo info;
    private GameObject model;
    private Character target;
    private Animation anim;
    private Action OnCompletd;
    private Coroutine routine;
 
    private bool isMove = false;
    private bool isAttack = false;
    private bool isMoveCompleted = false;
 
    private int damage = 10;
    private int speed = 1;
 
    public void Awake()
    {
        this.anim = this.gameObject.GetComponent<Animation>();
    }
 
    public virtual void Init(CharacterInfo info, GameObject model)
    {
        this.info = info;
        this.model = model;
        this.model.transform.SetParent(this.transform);
        this.model.transform.localPosition = Vector3.zero;
    }
 
    public virtual void SetInitPosition(Vector3 initPosition)
    {
        this.transform.position = initPosition;
    }
 
    public void Move(Character target)
    {
        if (routine != null)
        {
            StopCoroutine(this.routine);
        }
        this.routine = StartCoroutine(this.MoveImpl(target));
    }
 
    private IEnumerator MoveImpl(Character target)
    {
        this.transform.LookAt(target.transform);
 
        var data = DataManager.GetInstance();
        var characterId = DataManager.GetInstance().GetCharacterData(this.info.Id);
 
        var characterData = data.dicCharacterData[characterId.id];
 
        while (true)
        {
            this.anim.Play(data.dicCharacterAnimData[characterId.anim_id].run_name);
 
            var dir = (target.transform.position - this.transform.position).normalized;
            var distance = Vector3.Distance(target.transform.position, this.transform.position);
 
            this.transform.position += dir * this.speed * Time.deltaTime;
 
            if (characterData.attack_range > distance)
            {
                this.Attack(target);
                break;
            }
            if (distance <= 0.3f)
            {
                this.anim.Play(data.dicCharacterAnimData[characterId.anim_id].idle_name);
                Debug.Log("이동완료");
            }
            yield return null;
        }
    }
 
    public void Attack(Character target)
    {
        if (routine != null)
        {
            StopCoroutine(this.routine);
        }
        this.routine = StartCoroutine(this.AttackImpl(target));
    }
 
    private IEnumerator AttackImpl(Character target)
    {
        var data = DataManager.GetInstance();
        var characterId = DataManager.GetInstance().GetCharacterData(this.info.Id);
 
        var length = this.anim[data.dicCharacterAnimData[characterId.anim_id].attack_name].length;
        var clip = this.anim[data.dicCharacterAnimData[characterId.anim_id].attack_name].clip;
        var totalFrames = length * clip.frameRate;
        var attackFrame = 9;
 
        this.anim.Play(data.dicCharacterAnimData[characterId.anim_id].attack_name);
 
        var attackTime = attackFrame * length / totalFrames;
 
        yield return new WaitForSeconds(attackTime);
 
        StartCoroutine(target.TakeDamege(20));
        
        yield return new WaitForSeconds(length - attackTime);
        Debug.Log("공격완료");
        
        if (target.info.hp > 0)
        {
            this.Attack(target);
        }
    }
 
    public IEnumerator TakeDamege(int damage)
    {
        var data = DataManager.GetInstance();
        var characterId = DataManager.GetInstance().GetCharacterData(this.info.Id);
 
        this.info.hp -= damage;
        if (this.info.hp <= 0)
        {
            this.info.hp = 0;
            this.anim.Play(data.dicCharacterAnimData[characterId.anim_id].idle_name);
 
            anim.Play("Anim_Death");
 
            yield return new WaitForSeconds(anim["Anim_Death"].length);
 
            GameObject.Destroy(this.gameObject);
        }
        else
        {
            this.Attack(this.target);
        }
    }
}
 
cs


객체참조가 일어나지 않았다고 하는데.. 좀 더 알아봐야 할 것