7. 스크립트 정리

이때까지 플레이어의 기능을 담당하는 스크립트를 추상클래스로 작성했었다.

그 이유는 다른 스크립트 필드에 있는 변수나 메서드를 참고해야 하는 상황에서 ‘그냥 추상클래스에 변수를 선언해서 편리하게 사용하자’는 계획이었는데 이건 좀 아닌 것 같아서 추상클래스를 없앴다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerStat : MonoBehaviour
{
    public float health;
    private float maxHealth;
    public float speed;
    private float maxSpeed;
    bool isDie;

    private void Start() {
        // 임의 지정
        maxHealth = 100.0f;
        maxSpeed = 25.0f;
    }

    private void Update() {
        if(health <= 0)
        {
            Die();
        }
    }

    void Die()
    {
        isDie = true;

        //삭제 시키기
        Destroy(gameObject);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour
{
    private PlayerStat playerStat;
    public float jumpVelocity;
    public float inputX;
    public float inputZ;
    private float attackAngle;
    public Vector3 directionVec;

    private void Start() {
        playerStat = GetComponent<PlayerStat>();
    }

    private void FixedUpdate() {
        Direction();
        Move();
    }

    public void Move()
    {
        inputX = Input.GetAxisRaw("Horizontal");
        inputZ = Input.GetAxisRaw("Vertical");

        Vector3 velocity = new Vector3(inputX, jumpVelocity, inputZ);

        transform.position += velocity * playerStat.speed * Time.deltaTime;
    }

    public void Direction()
    {
        if (inputX == -1 && inputZ == 1)//UpLeft
        {
            //left = true;
            //up = true;
            directionVec = new Vector3(-1.14f, 0, 1.14f);
            attackAngle = 315.0f;

            //down = false;
            //right = false;
        }
        if (inputX == 0 && inputZ == 1)//Up
        {
            //up = true;
            directionVec = new Vector3(0, 0, 2.0f);
            attackAngle = 0.0f;

            //down = false;
            //right = false;
            //left = false;
        }
        if (inputX == 1 && inputZ == 1)//UpRight
        {
            //right = true;
            //up = true;
            directionVec = new Vector3(1.14f, 0, 1.14f);
            attackAngle = 45.0f;

            //left = false;
            //down = false;
        }
        if (inputX == -1 && inputZ == 0)//Left
        {
            //left = true;
            directionVec = new Vector3(-2.0f, 0, 0);
            attackAngle = 270.0f;

            //right = false;
            //up = false;
            //down = false;
        }
        if (inputX == 0 && inputZ == 0)//Idle
        {
            //up = false;
            //down = false;
            //right = false;
            //left = false;
            directionVec = new Vector3(0, 0, 0);
        }
        if (inputX == 1 && inputZ == 0)//Right
        {
            //right = true;
            directionVec = new Vector3(2.0f, 0, 0);
            attackAngle = 90.0f;

            //up = false;
            //down = false;
            //left = false;
        }
        if (inputX == -1 && inputZ == -1)//DownLeft
        {
            //left = true;
            //down = true;
            directionVec = new Vector3(-1.14f, 0, -1.14f);
            attackAngle = 225.0f;

            //up = false;
            //right = false;
        }
        if (inputX == 0 && inputZ == -1)//Down
        {
            //down = true;
            directionVec = new Vector3(0, 0, -2.0f);
            attackAngle = 180.0f;

            //up = false;
            //right = false;
            //left = false;
        }
        if (inputX == 1 && inputZ == -1)//DownRight
        {
            //right = true;
            //down = true;
            directionVec = new Vector3(1.14f, 0, -1.14f);
            attackAngle = 135.0f;

            //up = false;
            //left = false;
        }

        //게임매니저에 플레이어 방향을 넣자.
        if(directionVec != new Vector3(0, 0, 0))
            GameManager.GetInstance().playerDirection = directionVec;
        GameManager.GetInstance().attackAngle = attackAngle;
    }

    void OnDrawGizmos()
    {
        Gizmos.color = Color.magenta;
        Gizmos.DrawLine(transform.position, transform.position + directionVec);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAnime : MonoBehaviour
{
    public PlayerMove playerMove;
    public Animator animator;
    public Animation animation;

    public int aniDir;
    public int dir;

    void Awake()
    {
        animator = GetComponent<Animator>();
        
    }

    void Start()
    {
        playerMove = GetComponent<PlayerMove>();
    }

    private void Update()
    {
        Anime();
        //Debug.Log(animator.GetCurrentAnimatorStateInfo(0).normalizedTime);
    }
    //Debug.Log(animator.GetCurrentAnimatorStateInfo(8).normalizedTime);
    // animator Direction 값이랑
    // direction 값이랑 다르면 ready를 false로?
    public void Anime()
    {
        if(playerMove.directionVec == new Vector3(-1.14f, 0, 1.14f))//UpLeft
        {
            animator.SetInteger("Direction", 0);
            animator.SetBool("inputX", true);
            animator.SetBool("inputZ", true);
            animator.SetBool("upLeft", true);
        }
        if(playerMove.directionVec == new Vector3(0, 0, 2.0f))//Up
        {
            animator.SetInteger("Direction", 1);
            animator.SetBool("inputX", false);
            animator.SetBool("inputZ", true);
            animator.SetBool("upLeft", false);
        }
        if(playerMove.directionVec == new Vector3(1.14f, 0, 1.14f))//UpRight
        {
            animator.SetInteger("Direction", 2);
            animator.SetBool("inputX", true);
            animator.SetBool("inputZ", true);
            animator.SetBool("upLeft", false);
        }
        if(playerMove.directionVec == new Vector3(-2.0f, 0, 0))//Left
        {
            animator.SetInteger("Direction", -1);
            animator.SetBool("inputX", true);
            animator.SetBool("inputZ", false);
            animator.SetBool("upLeft", false);
        }
        if(playerMove.directionVec == new Vector3(0, 0, 0))//Idle
        {
            animator.SetInteger("Direction", 0);
            animator.SetBool("inputX", false);
            animator.SetBool("inputZ", false);
            animator.SetBool("upLeft", false);
        }
        if(playerMove.directionVec == new Vector3(2.0f, 0, 0))//Right
        {
            animator.SetInteger("Direction", 1);
            animator.SetBool("inputX", true);
            animator.SetBool("inputZ", false);
            animator.SetBool("upLeft", false);
        }
        if(playerMove.directionVec == new Vector3(-1.14f, 0, -1.14f))//DownLeft
        {
            animator.SetInteger("Direction", -2);
            animator.SetBool("inputX", true);
            animator.SetBool("inputZ", true);
            animator.SetBool("upLeft", false);
        }
        if(playerMove.directionVec == new Vector3(0, 0, -2.0f))//Down
        {
            animator.SetInteger("Direction", -1);
            animator.SetBool("inputX", false);
            animator.SetBool("inputZ", true);
            animator.SetBool("upLeft", false);
        }
        if(playerMove.directionVec == new Vector3(1.14f, 0, -1.14f))//DownRight
        {
            animator.SetInteger("Direction", 0);
            animator.SetBool("inputX", true);
            animator.SetBool("inputZ", true);
            animator.SetBool("upLeft", false);
        }

        // animator.loop 값 대입
        animator.SetFloat("loop", animator.GetCurrentAnimatorStateInfo(0).normalizedTime);
    }
}

플레이어의 능력치를 담당하는 PlayerStat.cs || 움직임을 담당하는 PlayerMove.cs || 애니메이션을 담당하는 PlayerAname.cs

이런 식으로 나눴다. 매번 몇 가지 변수 때문에 불러오는 게 짜증 나서 플레이어 기능에 관련된 스크립트는 추상 클래스로 묶으려고 했었는데 그냥 각자 제작하는 게 나은 것 같다. 제작하다가 필요할 때 추상 클래스를 사용하도록 하자.

그리고 원래 플레이어 스탯이든 몬스터 스탯이든 해당 능력치가 필요할때 선언해서 그 능력치를 적극 활용하는 스크립트에서 관리를 할려고 했었는데 생각이 바뀌었다. PlayerStat.cs를 만든 것처럼 MonsterStat.cs을 만들어서 중요스탯을 관리할려고 한다.

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다