Tank大戰,坦克移動

public class PlayerTank : MonoBehaviour
{
    public float speed;


    public Image bg;
    public Image img;
    public Canvas cv;
    public bool isMapTank;


    public int i;
    public int j;


    public float x;
    public float y;


    public Vector2 pos;
    /// <summary>
    /// 1上 2下 3左 4右
    /// </summary>
    public int curDir;
    public static Dictionary<int, string> dirDic = new Dictionary<int, string>();


    public GameObject playerMisPre;
    public GameObject myMis1;
    public GameObject myMis2;
    public Transform[] firePointArr;


    public static PlayerTank instance;


    public GameObject blastEff;


    public int startNum;


    public TankMoveLimit tml;


    void Start()
    {
        if (instance == null && isMapTank)
        {
            instance = this;
        }
    }


    public void SetInfo(Vector2 ij)
    {
        if (dirDic.Count == 0)
        {
            dirDic.Add(1, "U");
            dirDic.Add(2, "D");
            dirDic.Add(3, "L");
            dirDic.Add(4, "R");
        }


        isMapTank = true;
        transform.parent = bg.transform;
        transform.localScale = Vector3.one;


        i = (int)ij.x;
        j = (int)ij.y;
        x = (i + 1 / 2f) * (img.rectTransform.rect.width * GameManager.instance.uiScale);
        y = (j + 1 / 2f) * (img.rectTransform.rect.height * GameManager.instance.uiScale);


        RectTransformUtility.ScreenPointToLocalPointInRectangle(bg.rectTransform, new Vector2(x, y), cv.worldCamera, out pos);
        transform.localPosition = pos;


        img.sprite = Resources.Load<Sprite>("player/" + "U");


        gameObject.SetActive(true);
        transform.SetAsFirstSibling();
    }


    void Move()
    {
        int dir = 0;


        if (Input.GetKey(KeyCode.W))
        {
            dir = 1;
        }
        if (Input.GetKey(KeyCode.S))
        {
            dir = 2;
        }
        if (Input.GetKey(KeyCode.A))
        {
            dir = 3;
        }
        if (Input.GetKey(KeyCode.D))
        {
            dir = 4;
        }


        if (dir == 0)
        {
            //Debug.LogError("方向計算出現錯誤");
            return;
        }
        else if (dir != curDir)
        {
            curDir = dir;
            img.sprite = Resources.Load<Sprite>("player/" + dirDic[curDir]);
        }


        if (dir == 1)
        {
            tml.transform.localPosition = new Vector3(0, 21f, 0);
            tml.transform.localRotation = Quaternion.Euler(0, 0, 0);
        }
        else if (dir == 2)
        {
            tml.transform.localPosition = new Vector3(0, -21f, 0);
            tml.transform.localRotation = Quaternion.Euler(0, 0, 0);
        }
        else if (dir == 3)
        {
            tml.transform.localPosition = new Vector3(-21f, 0, 0);
            tml.transform.localRotation = Quaternion.Euler(0, 0, 90);
        }
        else if (dir == 4)
        {
            tml.transform.localPosition = new Vector3(21f, 0, 0);
            tml.transform.localRotation = Quaternion.Euler(0, 0, 90);
        }




        Vector3 d = Vector3.zero;
        if (curDir == 1)
        {
            d = Vector3.up;
        }
        else if (curDir == 2)
        {
            d = Vector3.down;
        }
        else if (curDir == 3)
        {
            d = Vector3.left;
        }
        else if (curDir == 4)
        {
            d = Vector3.right;
        }


        float w = img.rectTransform.rect.width * cv.transform.localScale.x / 2;


        if (!tml.isCanMove)
        {
            return;
        }


        transform.Translate(d * speed * Time.deltaTime);
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章