게임 클라이언트 프로그래밍

carSwipe

park-gom 2024. 8. 20. 16:41

 

using UnityEngine;

public class App : MonoBehaviour
{
    Vector2 startPos;
    Vector2 endPos;
    CarController car;

    // Start is called before the first frame update
    void Start()
    {
        GameObject carGo = GameObject.Find("car");
        GameObject dirGo = GameObject.Find("GameDirector");
        car = carGo.GetComponent<CarController>();

        car.onMoveComplete = () =>{
            Debug.Log("자동차 이동완료");
        };
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            // 마우스를 클릭한 좌표
            this.startPos = Input.mousePosition;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            // 마우스 버튼에서 손가락을 떼었을 때 좌표
            endPos = Input.mousePosition;
            float swipeLength = endPos.x - this.startPos.x;

            // 스와이프 길이를 처음 속도로 변환한다
            car.Move(swipeLength / 1000.0f);
        }
    }

    void ClearGame()
    {
        Debug.Log("게임 클리어!");
    }
}
using System;
using UnityEngine;


public class CarController : MonoBehaviour
{
    public Action onMoveComplete;
    bool isMove = false;
    float speed = 0;

    void Update()
    {

        this.transform.position = new Vector3(Mathf.Clamp(this.transform.position.x, -8, 8), -3, 0);
        transform.Translate(this.speed, 0, 0);
        this.speed *= 0.98f;
        if (this.speed < 0.0001 && isMove == true)
        {
            this.onMoveComplete();
            isMove = false;
        }

    }

    public void Move(float speed)
    {
        this.speed = speed;
        isMove = true;
    }
}
using System;
using UnityEngine;
using UnityEngine.UI;

public class GameDirector : MonoBehaviour
{
    GameObject flagGo;
    GameObject carGo;
    GameObject textGo;
    Text text;

    // Start is called before the first frame update
    void Start()
    {
        flagGo = GameObject.Find("flag");
        carGo = GameObject.Find("car");
        textGo = GameObject.Find("text");
        text = textGo.GetComponent<Text>();
        
    }

    // Update is called once per frame
    void Update()
    {
        //거리를 출력한다
        double distance = flagGo.transform.position.x - carGo.transform.position.x;
        distance = Math.Round(distance);

        if (distance == 0)
        {
            text.text = "CLEAR!";
        }
        else if (distance < 0)
        {
            text.text = "Game Over";
        }
        else
        {
            text.text = $"거리: {distance}m";
        }
    }
}

'게임 클라이언트 프로그래밍' 카테고리의 다른 글

프로세스 스레드 코루틴  (0) 2024.08.24
표창 던지기  (0) 2024.08.20