Example #1
0
    void Update()
    {
        // float scroll = Input.GetAxis("Mouse ScrollWheel");
        // Vector2 targetPos = Camera.main.ScreenPointToRay(Input.mousePosition);
        float recoilSuppressionNormal = (1 - recoilSuppression);
        Ray   ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (currentRandRange < 0.0f)
        {
            currentRandRange = 0.0f;
        }

        // Debug.Log(Input.mousePosition);
        transform.position = (Vector2)ray.origin;
        bool checkOptionOrEdit = (!globalControl.isOption && !globalControl.isEdit && !globalControl.isExplanation);

        if (globalControl.isEdit)
        {
            currentRandRange = 0;
        }
        if (Input.GetMouseButton(1) && checkOptionOrEdit)
        {
            Reload();
        }
        if (Input.GetMouseButton(0) && checkOptionOrEdit)
        {
            Vector2 hitpos      = transform.position;
            int     bulletCount = 0;
            if (fireRemainTime < 0 && remainAmmo > 0 && !isReload)
            {
                // TODO : use shellBulletCount
                while (bulletCount < 1)
                {
                    // TODO : change base player
                    // get random range
                    float maxSupRange = (maxRandRange * recoilSuppressionNormal);
                    currentRandRange = currentRandRange > maxSupRange ? maxSupRange : currentRandRange;

                    Vector2 randomRange = new Vector2(UnityEngine.Random.Range(-currentRandRange, currentRandRange), UnityEngine.Random.Range(-currentRandRange, currentRandRange));
                    hitpos += randomRange;
                    // ray
                    RaycastHit2D[] hitList = Physics2D.RaycastAll(hitpos, (Vector2)ray.direction, Mathf.Infinity, targetLayerMask);
                    if (hitList.Length > 0)
                    {
                        var penetrateTargetCount = hitList.Length < penetrate ? hitList.Length : penetrate;
                        var sliceHitList         = new ArraySegment <RaycastHit2D>(hitList, 0, penetrateTargetCount);
                        if (sliceHitList.Count > 0)
                        {
                            soundHit.PlayOneShot(soundHit.clip);
                        }
                        foreach (RaycastHit2D hit in sliceHitList)
                        {
                            // damage culc
                            var   hitTarget   = hit.collider.gameObject;
                            float dist        = Vector2.Distance(hitTarget.transform.position, hit.point);
                            float rad         = hitTarget.GetComponent <RectTransform>().localScale.x *hitTarget.GetComponent <CircleCollider2D>().radius;
                            float proc        = (1 - dist / rad);
                            float damagePoint = damage * proc;
                            AddScore((int)(1 + (damagePoint * damagePoint) * 10));
                            hitTarget.GetComponent <Target>().Damage(damagePoint);
                            globalControl.addFireHitCount();
                        }
                    }
                    Instantiate(bullet, hitpos, transform.rotation);
                    soundFire.PlayOneShot(soundFire.clip);

                    bulletCount++;
                }
                currentRandRange += randAccerate * recoilSuppressionNormal * adjustDeltaTime;
                fireRemainTime    = rappidRate;
                remainAmmo--;
                globalControl.addFireCount();
            }
            fireRemainTime -= Time.deltaTime;
        }

        // reload UI control
        if (currentReloadTime > 0)
        {
            currentReloadTime -= Time.deltaTime;
            currentReloadTime  = currentReloadTime < 0 ? 0 : currentReloadTime;
            reloadBar.GetComponent <RectTransform>().sizeDelta = new Vector2(200 * (1 - currentReloadTime / reloadSpeedLevelList[reloadSpeedLevel]), 10);
        }
        else if (currentReloadTime <= 0 && isReload)
        {
            remainAmmo = maxAmmoLevelList[maxAmmoLevel];
            isReload   = false;
            globalControl.addReloadCount();
            realoadUI.SetActive(false);
        }
        else if (currentReloadTime <= 0 && !isReload && remainAmmo <= 0)
        {
            emptyAmmoUI.SetActive(true);
        }
        currentRandRange -= Input.GetMouseButton(0) ? (Time.deltaTime / adjustDeltaTime) : Time.deltaTime * adjustDeltaTime;
        currentRandRange  = currentRandRange < 0 ? 0 : currentRandRange;
    }