Notice
Recent Posts
Recent Comments
Link
«   2024/11   »
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
Archives
Today
Total
관리 메뉴

codingfarm

df 본문

카테고리 없음

df

scarecrow1992 2022. 7. 12. 00:52

Unit

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Unit : LivingEntity {
    public float orgMana;
    public float cMana;
    public float rMana;
 
    public int castingIndex;
    public int castEndIndex;
    public int lastCastingIndex;
 
    Vector3 velocity;
    //public Camera viewCamera;
    //Rigidbody myRigidbody;
    protected CharacterController myCharacterController;
    [HideInInspector]
    public Vector3 goal;
    private float RotationSpeed = 20.0f;
    public Animator myAnimator;
 
    [SerializeField]
    TestIdleState testIdleState;
 
    public bool isMove;
    public float speed;
 
 
    //public NavMeshAgent pathFinder;
    public float refreshRate;
 
    public string idleStateName, moveStateName;
    int idleStateHash, moveStateHash;
 
    // 버프들
    // 디버프들
 
    public enum state { Idle, Casting, Death };
    public state unitState;
 
    public GameObject firePoint;
    
    public SkillManager mySkillManager;
    //public Navigator myNavigator;
 
    //Vector3 moveVector;
 
    float castintStartElapsedTime;
 
    public enum Team {player, enemy };
    public Team myTeam;
 
    bool isOnCasting;
    // Start is called before the first frame update
 
    public CapsuleCollider[] myCollider;
 
    //public MeshFilter myMeshFilter;
    //public SkinnedMeshRenderer mySkinnedMeshRenderer;
    public Transform corpseTransform;
 
 
    public Dictionary<int, Unit> friendList, targetList;
 
    public enum UnitSize {small, Big, Huge};
    public UnitSize unitSize;
 
    public enum UnitType {minion, elite, champion, player }
    public UnitType unitType;
 
    public Transform statusBarTransform;
 
 
    public Transform leftGrab, rightGrab;
 
    string[] weaponAnimation;   // WeaponManager.WeaponIndex
 
    void OnDrawGizmosSelected() {
        //Gizmos.color = Color.red;
 
        /*
        Gizmos.DrawSphere(transform.position + delta, 0.1f);
 
        tmpX = delta.x; tmpZ = delta.z;
 
        delta.x = tmpX * Mathf.Cos(Mathf.Deg2Rad * 90) - tmpZ * Mathf.Sin(Mathf.Deg2Rad * 90);
        delta.z = tmpX * Mathf.Sin(Mathf.Deg2Rad * 90) + tmpZ * Mathf.Cos(Mathf.Deg2Rad * 90);
 
        Gizmos.DrawSphere(transform.position + delta, 0.1f);
        */
 
        Vector3 delta = new Vector3(010);
 
        Gizmos.color = Color.yellow;
        //Gizmos.DrawSphere(offset, 1.1f);
    }
 
    
    protected virtual void Awake() {
        //myRigidbody = GetComponent<Rigidbody>();
        //pathFinder = GetComponent<NavMeshAgent>();
        myCharacterController = GetComponent<CharacterController>();
 
        //speed = 7.5f;
        castingIndex = -1;
        castEndIndex = -1;
        //skills = new Skill[6];
        unitState = state.Idle;
 
        idleStateHash = Animator.StringToHash(idleStateName);
        moveStateHash = Animator.StringToHash(moveStateName);
 
 
        base.Awake();
    }
 
    protected virtual void Start() {
        castintStartElapsedTime = -1f;
        isOnCasting = false;
        cMana = orgMana;
 
        OnDeath += new System.Action(SwitchColliderLayer);
        OnDeath += new System.Action(DeleteUnit);
        //if (gameObject.name == "Enemy0")
        //Debug.Log("Enemy : Unit Start");
 
        //StartCoroutine(UpdatePath());
 
        //Debug.Log(gameObject.GetInstanceID());
 
        testIdleState = myAnimator.GetBehaviour<TestIdleState>();
        //testIdleState.OnStateMachineEnter(myAnimator, 0);
        testIdleState.myUnit = this;
 
        base.Start();
 
    }
 
    // Update is called once per frame
    protected virtual void Update()
    {
        cMana = Mathf.Min(orgMana, cMana + rMana * Time.deltaTime);
        if (castintStartElapsedTime >= 0f)
            castintStartElapsedTime -= Time.deltaTime;
 
        CheckAnimationState();
 
        
        
        // UnitMove는 최종적으로 유닛이 이동해야하는 방향을 전달해야한다.
        UnitMove();
 
 
        if (unitState == state.Death) {
            // 로컬(MeshFilter 기준) -> 월드 -> 로컬(Unit 기준) 순으로 바꾼다.
            /*
            Vector3 offset = myMeshFilter.transform.TransformPoint(myMeshFilter.mesh.bounds.center);
            offset = transform.InverseTransformPoint(offset);
            offset.z = 0;
 
            for (int i = 0; i < myCollider.GetLength(0); i++)
                myCollider[i].transform.localPosition = offset;
            */
 
            /*
            Vector3 offset = mySkinnedMeshRenderer.bounds.center;
            offset = transform.InverseTransformPoint(offset);
            offset.z = 0;
 
            for (int i = 0; i < myCollider.GetLength(0); i++)
                myCollider[i].transform.localPosition = offset;
            */
 
            if (corpseTransform != null) {
                //Vector3 offset = transform.TransformPoint(corpseTransform.position);
                //offset = transform.InverseTransformPoint(offset);
                //offset.z = 0;
                Vector3 offset = corpseTransform.position;
                offset.y = 0;
                for (int i = 0; i < myCollider.GetLength(0); i++)
                    myCollider[i].transform.position = offset;
            }
        }
 
    }
 
    protected virtual void FixedUpdate() {
        
    }
 
    private void LateUpdate() {
 
        //viewCamera.transform.position = new Vector3(transform.position.x - 9.64f, transform.position.y + 11.43f, transform.position.z - 9.64f);
    }
 
 
 
    //goal을 향해 유닛을 이동시킨다.
    public void UnitMove() {
 
        if (unitState == state.Idle) {
            float moveToDistance; // 플레이어 위치 ~ 목적지
            float frameDistance;    // 이번 프레임의 이동거리
            velocity = goal - transform.position;
            velocity = velocity.normalized * speed;
            Vector3 tmpvec = goal - transform.position;
            moveToDistance = tmpvec.magnitude;
            frameDistance = speed * Time.deltaTime;
            
            if (moveToDistance > frameDistance) {
 
                //Quaternion wantedRotation = Quaternion.LookRotation(moveVector);
                Quaternion wantedRotation = Quaternion.LookRotation(goal - transform.position);
                transform.rotation = Quaternion.Lerp(transform.rotation, wantedRotation, Time.deltaTime * RotationSpeed);
 
                //myRigidbody.MovePosition(transform.position + velocity * Time.fixedDeltaTime);
                //myCharacterController.Move(transform.position + velocity * Time.fixedDeltaTime);
                myCharacterController.SimpleMove(velocity);
 
                if (isMove == false) {
                    //Debug.Log(unitState + " : Are you?");
                    MoveOn();
                    isMove = true;
                    myAnimator.SetBool("isMove", isMove);
                }
 
            }
            else if (moveToDistance <= frameDistance) {
                //myRigidbody.MovePosition(goal);
                //myCharacterController.Move(goal);
                if (isMove == true) {
                    goal = transform.position;
                    MoveOff();
                    isMove = false;
                    myAnimator.SetBool("isMove", isMove);
                }
            }
 
        }
    }
 
    public void MoveOn() {
 
        //pathFinder.enabled = true;
    }
 
 
    public void MoveOff() {
 
        //parentUnit.pathFinder.enabled = false;
    }
 
 
    /*
    IEnumerator UpdatePath() {
 
        while (true) {
            //if(pathFinder.isActiveAndEnabled)
                //pathFinder.SetDestination(goal);
 
            yield return new WaitForSeconds(refreshRate);
        }
    }
    */
    public void SetGoal(Vector3 goal_) {
        goal = goal_;
    
    }
 
    public void GetInputUp(int index, Vector3 mousePosition) {
        castEndIndex = index;
        //CastingEnd(index);
        //castingIndex = -1;
        CastingEnd(index, mousePosition);
    }
 
    void CheckAnimationState() {
        if (myAnimator.GetCurrentAnimatorStateInfo(0).IsName(idleStateName) || myAnimator.GetCurrentAnimatorStateInfo(0).IsName(moveStateName)) {
            if (unitState == state.Casting) {
                unitState = state.Idle;
            }
 
            if (castEndIndex != -1 && castintStartElapsedTime <= 0f && isOnCasting == false) {
                castingIndex = -1;
                castEndIndex = -1;
                //Debug.Log("Reset CastingIndex");
            }
        }
        else {
            if (unitState == state.Idle) {
                unitState = state.Casting;
            }
        }
    }
 
 
    public void GetInput(int index, Vector3 mousePosition) {
        // key down 감지
        if(castingIndex == -1 && castEndIndex == -1 && unitState == state.Idle) {
            //castingIndex = index;
            InputManager.instance.inputUpCheckDelay = .15f;
            //Debug.Log("Get Input : " + index);
            CastingStart(index, mousePosition);
        }
 
        // key run 감지
        if(castingIndex == index && castEndIndex == -1 /*&& unitState == state.Casting*/) {
            CastingRun(index, mousePosition);
 
        }
 
 
 
        /*
        // key up 감지
        if (castingIndex != index && playerState == state.Casting) {
            CastingEnd(index);
            castingIndex = -1;
        }
        */
    }
 
    
   
 
 
    void CastingStart(int index, Vector3 mousePosition) {
        castintStartElapsedTime = 0.32f;
        mySkillManager.CastingStart(index, mousePosition);
        isOnCasting = true;
    }
 
 
    void CastingRun(int index, Vector3 mousePosition) {
        castintStartElapsedTime = 0.32f;
        mySkillManager.CastingRun(index, mousePosition);
    }
 
    void CastingEnd(int index, Vector3 mousePosition) {
        mySkillManager.CastingEnd(index, mousePosition);
        isOnCasting = false;
    }
 
    
    void SwitchColliderLayer() {
        gameObject.layer = LayerMask.NameToLayer("Nothing");
        for (int i=0; i<myCollider.GetLength(0); i++)
            myCollider[i].gameObject.layer = LayerMask.NameToLayer("Corpse");
        //myCollider.gameObject.layer = LayerMask.NameToLayer("Corpse");
    }
 
    void DeleteUnit() {
        Debug.Log(gameObject.name + " : Call Delete Unit");
        BattleManager.instance.DeleteUnit(this);
    }
 
 
}
 
cs

 

 

 

 

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
 
 
// InputManager는 Unit의 상태를 의식하지 않는다.
// 지금은 InputManager가 플레이어 유닛의 오브젝트에 속해있지만 추후 별개로 나눌예정이다.
public class InputManager : MonoBehaviour {
    //public Transform tmpBox;
 
    static public InputManager instance;
 
    int inputQueue;
    enum inputCode { Q_DOWN, Q_RUN, Q_UP };
 
    public Unit playerUnit;
    public Camera playerCamera;
 
    Vector3 mousePosition;
    Vector3 lastLeftClickPosition;
    public float inputUpCheckDelay;
 
    Vector3 navFrom, navTo;
    NavMeshPath navMeshPath;
 
    [HideInInspector]
    public Transform cursoredUnit;
 
    [SerializeField]
    Transform lastClickedUnit;      // 좌클릭 되기 직전 마지막으로 선택된 유닛
 
    void OnDrawGizmosSelected() {
        Gizmos.color = Color.yellow;
        Gizmos.DrawSphere(lastLeftClickPosition, 0.3f);
 
 
        Gizmos.color = Color.black;
        Vector3 pathFrom, pathTo;
        pathFrom = navFrom;
 
        if (navMeshPath != null && navMeshPath.corners != null && navMeshPath.corners.Length != 0) {
            for (int i = 0; i < navMeshPath.corners.Length; i++) {
                pathTo = navMeshPath.corners[i];
                Gizmos.DrawLine(pathFrom, pathTo);
                pathFrom = pathTo;
            }
        }
        pathTo = navTo;
        Gizmos.DrawLine(pathFrom, pathTo);
 
    }
 
 
    // Start is called before the first frame update
    void Start() {
        instance = this;
        lastLeftClickPosition = playerUnit.transform.position;
        inputUpCheckDelay = -0.1f;
 
        navMeshPath = new NavMeshPath();
    }
 
    // Update is called once per frame
    void Update() {
        mousePosition = GetMousePosition();
        CheckInputDown();
        CheckInputRun();
        CheckInputUp();
        Transform mousePositionTarget = DetectUnit();
 
        if (mousePositionTarget != null && mousePositionTarget.GetComponent<Unit>().myTeam == Unit.Team.enemy)
            cursoredUnit = mousePositionTarget;
        else
            cursoredUnit = null;
 
        if (inputUpCheckDelay >= 0f) {
            inputUpCheckDelay -= Time.deltaTime;
        }
 
        FollowFarUnit();
 
        if (Input.GetKeyDown(KeyCode.C))
            navFrom = GetMousePosition();
 
        if (Input.GetKeyDown(KeyCode.V))
            navTo = GetMousePosition();
 
        if (Input.GetKeyDown(KeyCode.B))
            NavMesh.CalculatePath(navFrom, navTo, NavMesh.AllAreas, navMeshPath);
 
    }
 
 
    private void LateUpdate() {
        //if (tmpBox != null)
        //    tmpBox.position = mousePosition;
    }
 
 
    // 마우스 위치에 적이 있으면 반환
    Transform DetectUnit() {
        Transform ret = null;
 
        Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition);
        int layerMask = 1 << LayerMask.NameToLayer("Hit");  // Player에게는 안 가리도록, 마우스에만 반응하는 collider를 유닛에게 씌우자.
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, 30f, layerMask))
            ret = hit.collider.transform.parent;
 
        return ret;
    }
 
    Vector3 GetMousePosition() {
        Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition);
        Plane groundPlane = new Plane(Vector3.up, transform.position);
 
        float rayDistance;
 
        if (groundPlane.Raycast(ray, out rayDistance))
            return ray.GetPoint(rayDistance);
        else
            return transform.position;
    }
 
    void CheckInputDown() {
        if (Input.GetKeyDown(KeyCode.Mouse0))
            lastClickedUnit = cursoredUnit;
 
 
    }
 
    void CheckInputRun() {
 
        if (Input.GetKey(KeyCode.Mouse0)) {
 
            lastLeftClickPosition = mousePosition;
 
            if (cursoredUnit != null) {
 
                float targetDistance = (playerUnit.transform.position - cursoredUnit.transform.position).magnitude;
                if (targetDistance > playerUnit.mySkillManager.skills[0].range) {
                    playerUnit.SetGoal(cursoredUnit.transform.position);
                    //Debug.Log("Call SetGoal1");
                }
                else {
                    if (inputUpCheckDelay <= 0f)
                        playerUnit.GetInput(0, cursoredUnit.transform.position);
                }
 
                // 이것만 달랑 있으면 해당 유닛 방향으로 무조건 스킬을 쓰게된다.
                //playerUnit.GetInput(0, cursoredUnit.transform.position);
            }
            else {
                if (inputUpCheckDelay <= 0f && !Input.GetKey(KeyCode.LeftShift)) {
                    playerUnit.SetGoal(lastLeftClickPosition);
                    //Debug.Log("Call SetGoal2");
 
                }
 
                if (Input.GetKey(KeyCode.LeftShift)) {
                    playerUnit.GetInput(0, lastLeftClickPosition);
                }
            }
        }
 
        if (Input.GetKey(KeyCode.Mouse1)) {
            playerUnit.GetInput(1, mousePosition);
        }
 
        if (Input.GetKey(KeyCode.Q)) {
            playerUnit.GetInput(2, mousePosition);
        }
 
        if (Input.GetKey(KeyCode.W)) {
            playerUnit.GetInput(3, mousePosition);
        }
 
        if (Input.GetKey(KeyCode.E)) {
            playerUnit.GetInput(4, mousePosition);
        }
 
        if (Input.GetKey(KeyCode.R)) {
            playerUnit.GetInput(5, mousePosition);
        }
 
        if (Input.GetKey(KeyCode.T)) {
            playerUnit.GetInput(6, mousePosition);
        }
    }
 
    void CheckInputUp() {
 
 
        if (inputUpCheckDelay <= 0f && playerUnit.castingIndex == 0 && playerUnit.castEndIndex == -1 && !Input.GetKey(KeyCode.Mouse0)) {
            playerUnit.GetInputUp(0, mousePosition);
        }
 
 
        if (inputUpCheckDelay <= 0f && playerUnit.castingIndex == 1 && playerUnit.castEndIndex == -1 && !Input.GetKey(KeyCode.Mouse1)) {
            playerUnit.GetInputUp(1, mousePosition);
        }
 
        if (inputUpCheckDelay <= 0f && playerUnit.castingIndex == 2 && playerUnit.castEndIndex == -1 && !Input.GetKey(KeyCode.Q)) {
            playerUnit.GetInputUp(2, mousePosition);
        }
 
        if (inputUpCheckDelay <= 0f && playerUnit.castingIndex == 3 && playerUnit.castEndIndex == -1 && !Input.GetKey(KeyCode.W)) {
            playerUnit.GetInputUp(3, mousePosition);
        }
 
        if (inputUpCheckDelay <= 0f && playerUnit.castingIndex == 4 && playerUnit.castEndIndex == -1 && !Input.GetKey(KeyCode.E)) {
            playerUnit.GetInputUp(4, mousePosition);
        }
 
 
        if (inputUpCheckDelay <= 0f && playerUnit.castingIndex == 5 && playerUnit.castEndIndex == -1 && !Input.GetKey(KeyCode.R)) {
            playerUnit.GetInputUp(5, mousePosition);
        }
 
 
        if (inputUpCheckDelay <= 0f && playerUnit.castingIndex == 6 && playerUnit.castEndIndex == -1 && !Input.GetKey(KeyCode.T)) {
            playerUnit.GetInputUp(6, mousePosition);
        }
 
 
        /*
        if (inputUpCheckDelay <= 0f && playerUnit.castingIndex == 0 && playerUnit.castEndIndex == -1 && !Input.GetKey(KeyCode.Mouse0)) {
            playerUnit.GetInputUp(0, mousePosition);
            Debug.Log("Casting End");
        }
        */
 
    }
 
    // 마지막으로 좌클릭한 유닛이 멀리 있으면, 해당 유닛을 추적하여 공격한다.
    void FollowFarUnit() {
        if (lastClickedUnit != null) {
            //playerUnit.goal = lastClickedUnit.position;
            playerUnit.SetGoal(lastClickedUnit.position);
            float distance = (lastClickedUnit.transform.position - playerUnit.transform.position).magnitude;
            if (distance < playerUnit.mySkillManager.skills[0].range) {
                if (inputUpCheckDelay <= 0f)
                    playerUnit.GetInput(0, lastClickedUnit.transform.position);
                lastClickedUnit = null;
                playerUnit.SetGoal(playerUnit.transform.position);
            }
        }
    }
 
}
 
cs

 

 

 

 

 

Comments