C#/수업내용

0707 유니티

버스창문 2022. 7. 7. 14:43

public GameObject -> gameobject 면 다 가능
public PlayerControler -> "PlayerControler"가 붙은 게임 오브젝트만 가능

 

 

 

 

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

public class GameMain : MonoBehaviour
{
    public PlayerControler player;
    public UIGame uiGame;
    public ArrowGenerator arrowGenerator;
    private List<GameObject> arrows;


    public bool m_isLbuttonDowning;
    public bool m_isRbuttonDowning;

    // Start is called before the first frame update
    void Start()
    {
        this.arrows = new List<GameObject>();
        this.arrowGenerator.onCreateArrowAction += (arrowGo) =>
        {
            var arrowController = arrowGo.GetComponent<ArrowControler>();
            arrowController.OnCollitionAction = () =>
            {
                this.player.hp -= arrowController.damage;
                this.uiGame.hpGuage.fillAmount = this.player.hp / this.player.maxHp;
                Destroy(arrowController.gameObject);
                Debug.Log(this.uiGame.hpGuage.fillAmount);
                if(this.player.hp <= 0)
                {
                    Destroy(this.arrowGenerator.gameObject);
                }
            };

            this.arrows.Add(arrowGo);

        };
        this.InitUIGame();
    }
    private void InitUIGame()
    {
        /*this.uiGame.btnLeft.onClick.AddListener(() =>
        {
            //Debug.Log("l");
            this.player.MoveLeft();
        });
        this.uiGame.btnRight.onClick.AddListener(() =>
        {
            //Debug.Log("R ");
            this.player.MoveRight();
        });*/
    }

    // Update is called once per frame
    void Update()
    {
        if (m_isLbuttonDowning)
        {
            this.player.MoveLeft();
        }
        if (m_isRbuttonDowning)
        {
            this.player.MoveRight();
        }
    }
    public void LBottonDown()
    {
        this.m_isLbuttonDowning = true;
    }
    public void RBottonDown()
    {
        this.m_isRbuttonDowning = true;
    }
    public void LBottonUp()
    {
        this.m_isLbuttonDowning = false;
    }
    public void RBottonUp()
    {
        this.m_isRbuttonDowning = false;
    }
}

player hp 가 0이되면 generator를 제거해버리는 극단적인 방법을 쓰긴했는데
응용해서 generator에 bool값 하나 넣어놓고 그걸 변화시키는 방법으로도 이용할수 있을것같음

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

public class ArrowGenerator : MonoBehaviour
{
    private float delta = 0;
    [SerializeField]
    private float span = 1.0f;
    [SerializeField]
    private GameObject ArrowPrefab;

    [SerializeField]
    private PlayerControler player;

    public UnityAction<GameObject> onCreateArrowAction;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
            this.delta += Time.deltaTime;

            if (this.delta > this.span)
            {
                this.delta = 0;
                GameObject Go = Instantiate<GameObject>(this.ArrowPrefab);
                this.onCreateArrowAction(Go);
                float px = Random.Range(-6, 7);
                Go.transform.position = new Vector3(px, 7, 0);
            }
        
    }
}

'C# > 수업내용' 카테고리의 다른 글

0708 오브젝트 풀링  (0) 2022.07.08
0708 슈팅게임  (0) 2022.07.08
0707 표창날리기  (0) 2022.07.07
0707 swipeCar  (0) 2022.07.07
0704 자료구조(트리)  (0) 2022.07.04