hanui1210 2024. 8. 20. 19:59

구조 바꾸기
App
-사용자 인풋을 받는다
GameDirector
-Ui를 갱신한다
CarController
-이동한다

<추가구현>
깃발을 넘어가면 GameOver라고 UI출력
깃발을 넘지 않을경우 깃발과의 거리를 정수로 출력

<업그레이드> = Mathf.Clamp
자동차가 화면밖으로 벗어나지 않게 

 

comment,

영상보면서 내용 다시정리...

 

아직 구현 안된

GameOver UI출력

화면밖으로 벗어나지 않게 하기

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
    // UI를 갱신
    // using UnityEngine.UI; 
    public Text text;

    public void UpdateUI(float distance)
    {
        text.text = $"{distance}m";
    }

}

 

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

public class CarController : MonoBehaviour    
{
    public enum State
    {
        Move, Stop
    }
    public GameObject textGo;
    public float moveSpeed;
    private State state;
    private bool isStop;
    public System.Action moveAction; // 대리자 변수정의

    // 이벤트는 위에 
    void Start()
    {
        state = State.Stop;
    }

    void Update()
    {
        if (state == State.Move)
        {
            this.transform.Translate(moveSpeed, 0, 0);
            moveSpeed *= 0.96f; // 앞으로가기
                                // UpdateDistanceUi();
                                // Debug.Log(moveSpeed);
            moveAction(); // 대리자 호출
        }
        if (moveSpeed != 0 && Mathf.Abs(moveSpeed) <= 0.01f)
        {
            if (isStop != false)
            {
                state = State.Stop;
                Debug.Log(state);
                isStop = true;
            }
        }
        
    }

    // 내가 만든 메서드는 아래
    public void Move(InputManager.Direction direction)
    {
        int dir = (int)direction;
        Debug.Log($"<color=yellow>Move: {direction}, {dir}</color>");
        float speed = 0.1f;
        this.moveSpeed = dir * speed; // 속도 적용
        Debug.Log($"<color=yellow>moveSpeed: {moveSpeed}</color>");
        this.state = State.Move;
        isStop = false;
    }
}

 

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

public class App2 : MonoBehaviour
{
  public InputManager inputManager;
    public CarController carController;
    public Transform flagTrans;
    public GameDirector gameDirector;

    public void Start()
    {
        this.calcDistanceAndUpdataUI();
        // public GameObject carGo; 
        //carGo.GetComponent<carController>();
        //=> public CarController carController; => 줄여서 사용가능

        // 람다식사용
        this.inputManager.swipeAction = (direction) => { 
        
            Debug.Log(direction); //=> 확인용 
            // App에서 먼저 시작하는지 Inputmanager에서 먼저 사용하는지 확인
            carController.Move(direction);

        };
        carController.moveAction = () =>
        {
            this.calcDistanceAndUpdataUI();
        };
    }
    // 자동차 오브젝트와 깃발 오브젝트의 거리를 계산후 gameDiractor의 UpdataUI를 호출하는 메서드
    private void calcDistanceAndUpdataUI()
    {
        Vector3 carPos = carController.gameObject.transform.position; //a
        Vector3 flagPos=this.flagTrans.position; //b

        float distanceX = flagPos.x - carPos.x;
        gameDirector.UpdateUI(distanceX);
    }
}

 

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

public class InputManager : MonoBehaviour
{
    // 열거형식정의
    public enum Direction
    {
        Left=-1, Right =1
        //CarController에서 moveSpeed + / - 적용을 열거형식에 간단히 정의
        //if (direction == InputManager.Direction.Right)
        //{
        //    moveSpeed = 0.1f;
        //}
        //else if (direction == InputManager.Direction.Left)
        //{
        //    moveSpeed = -0.1f;
        //}
    }
    // 대리자 변수 정의
    public System.Action<Direction> swipeAction;

    private Vector3 startPos;

    // Update is called once per frame
    void Update()
    {
        bool isDown = Input.GetMouseButtonDown(0);
        if (isDown)
        {
            this.startPos=Input.mousePosition;
        }
        else if(Input.GetMouseButtonUp(0))
        {
            Vector3 endPos= Input.mousePosition;
            float dirx = endPos.x - startPos.x; // 거리

            if (dirx > 0)
            {
                // swipeAction 주소 확인용
                // System.Action'1[InputManage+Diraction]
               // Debug.Log($"swipeAction: {swipeAction}");
                //대리자호출               
                this.swipeAction(Direction.Right);
            }
            else if (dirx < 0)
            {
                this.swipeAction(Direction.Left);
            }

            //오른쪽, 왼쪽 이동을 enum으로 정의 후 대리자 선언해서 간단하게 사용 
            /*
            if (dirX > 0)
            {
                Debug.Log($"오른쪽으로 {Mathf.Abs(dirX)}m이동합니다");
                moveSpeed = 0.05f;
            }
            else if (dirX < 0)
            {
                Debug.Log($"왼쪽으로 {Mathf.Abs(dirX)}m이동합니다");
                moveSpeed = -0.01f;
            }
            */

        }
    }
}