예제 #1
0
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.tag == "Player")
     {
         Destroy(gameObject);
         PistolAmmo Replenish = collision.GetComponentInChildren <PistolAmmo>();
         Replenish.AddPistolAmmo();
         Debug.Log("Gained 15 pistol ammo!");
     }
 }
    // Update is called once per frame
    void Update()
    {
        if (getMagnitude() <= __vitesseMax)
        {
            //Debug.Log("Magnitude = " + getMagnitude());

            Vector3 dir = new Vector3(0, 0, 0);

            if (Input.GetKey(KeyCode.D))
            {
                dir += new Vector3(1, 0, 0);
                //Debug.Log("Droite");
            }

            if (Input.GetKey(KeyCode.Q))
            {
                dir += new Vector3(-1, 0, 0);
                //Debug.Log("Gauche");
            }

            if (Input.GetKey(KeyCode.Z))
            {
                dir += new Vector3(0, 0, 1);
                //Debug.Log("Haut");
            }

            if (Input.GetKey(KeyCode.S))
            {
                dir += new Vector3(0, 0, -1);
                //Debug.Log("Bas");
            }

            dir = dir.normalized;
            this.__rigidbody.AddForce(dir * this.__forceAcceleration * Time.deltaTime);
        }

        // Clic gauche souris = tir d'eau avec fusil à eau
        if (Input.GetMouseButtonDown(0))
        {
            if (this.__weapon.getAmmo() > 0)
            {
                Vector3 mousePos = Input.mousePosition;
                mousePos.x /= Screen.width;
                mousePos.y /= Screen.height;
                mousePos.z  = Camera.mainCamera.nearClipPlane;

                Vector3 screenMousePos = Camera.mainCamera.ViewportToWorldPoint(mousePos);

                Debug.DrawLine(Camera.mainCamera.transform.position, screenMousePos);
                Vector3 vectDir = (screenMousePos - Camera.mainCamera.transform.position).normalized;

                RaycastHit hit = new RaycastHit();
                Ray        ray = new Ray(Camera.mainCamera.transform.position, vectDir);
                Physics.Raycast(ray, out hit, Mathf.Infinity);

                Vector3 posGround = this.__transform.position;
                posGround.y = hit.point.y;
                Vector3 vectDirThrow = (hit.point - posGround).normalized;

                string     classNameAmmo = this.__weapon.getClassAmmo();
                GameObject Throwable     = GameObject.Instantiate(Resources.Load(classNameAmmo, typeof(GameObject))) as GameObject;
                Throwable.transform.position = this.__transform.position + vectDirThrow;
                Throwable.rigidbody.AddForce(vectDirThrow * this.__weapon.getForce(), ForceMode.Impulse);

                string classAmmo = this.__weapon.getClassAmmo();
                if (classAmmo == "PistolAmmo")
                {
                    PistolAmmo throwableScript = Throwable.GetComponent <PistolAmmo>();
                    throwableScript.obj = Throwable;
                }
                else if (classAmmo == "SangriaBombAmmo")
                {
                    SangriaBombAmmo throwableScript = Throwable.GetComponent <SangriaBombAmmo>();
                    throwableScript.obj = Throwable;
                }

                this.__weapon.decreaseAmmo();
                //Debug.Log("Launch");
            }
            else
            {
                Debug.Log("Plus de munitions");
            }
        }

        this.decreaseBAC(this.__decreasingBACSpeed * Time.deltaTime);
    }