Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (!go)
        {
            Points.GetComponent <Text>().text = points.ToString();
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                if (ExitBut.activeSelf)
                {
                    Time.timeScale = Time.timeScale == 0 ? 1 : 0;
                    AudioSource.PlayClipAtPoint(Resources.Load("Sound Effects/Unpause") as AudioClip, Camera.main.transform.position, 1);
                    ExitBut.SetActive(false);
                }
                else
                {
                    AudioSource.PlayClipAtPoint(Resources.Load("Sound Effects/Pause") as AudioClip, Camera.main.transform.position, 1);
                    Time.timeScale = Time.timeScale == 0 ? 1 : 0;
                    ExitBut.SetActive(true);
                }
            }
            if (Time.timeScale != 0)
            {
                if ((isGrounded && Mathf.Abs(rb.velocity.x) > 0.05f) || (animator.enabled && animator.GetCurrentAnimatorStateInfo(0).IsName("CH_Throw")))
                {
                    animator.enabled = true;
                }
                else if (!isGrounded || (isGrounded && Mathf.Abs(rb.velocity.x) < 0.05f))
                {
                    animator.enabled = false;
                }
                if (PlayerLives.activeSelf)
                {
                    PlayerLives.transform.position = transform.GetChild(1).position;
                }
                if (currenthealth <= 0)
                {
                    points -= 100;
                    if (lives > 1)
                    {
                        AudioSource.PlayClipAtPoint(deathsounds[Random.Range(0, deathsounds.Length - 1)], Camera.main.transform.position, 1);
                        lives--;
                        currenthealth      = maxhealth;
                        transform.position = spawnpos;
                        PlayerLives.GetComponent <Text>().text = lives.ToString();
                        StartCoroutine(Buffer("invincible", 0.5f));
                    }
                    else
                    {
                        StartCoroutine(GameOver());
                    }
                }
                if (!isGrounded && jumpbuffered) //has the player jumped recently?
                {
                    hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y - col.bounds.extents.y * 1.25f), transform.up, col.bounds.extents.y);
                    if (hit.transform.gameObject != null && hit.transform.gameObject.CompareTag("Floor")) //floor is beneath player
                    {
                        isGrounded = true;
                    }
                }
                if (!(SceneManager.GetActiveScene().name.Contains("Boss Fight") && (transform.position.x > 1.85f || transform.position.x < -2.2f)))
                {
                    if (Input.GetKey(KeyCode.A))
                    {
                        facingRight = false;
                        rb.velocity = new Vector2(Mathf.Clamp(rb.velocity.x - acceleration, -maxspeed, maxspeed), rb.velocity.y);
                    }
                    else if (Input.GetKey(KeyCode.D))
                    {
                        facingRight = true;
                        rb.velocity = new Vector2(Mathf.Clamp(rb.velocity.x + acceleration, -maxspeed, maxspeed), rb.velocity.y);
                    }
                }
                else if ((transform.position.x > 1.85f))
                {
                    transform.position = (Vector2)(transform.position) + -Vector2.right * 0.025f;
                }
                else
                {
                    transform.position = (Vector2)(transform.position) + Vector2.right * 0.025f;
                }
                if (Input.GetKeyDown(KeyCode.Space) && isGrounded && jumpbuffered) //player wants to jump, is grounded and hasnt just jumped (buffer stops jumping during jump takeoff)
                {
                    AudioSource.PlayClipAtPoint(Resources.Load("Sound Effects/Player Jump") as AudioClip, Camera.main.transform.position, 1);

                    rb.AddForce(new Vector2(Mathf.Clamp(rb.velocity.x, -0.5f, 0.5f), 1) * (Mathf.Sqrt(2 * Physics2D.gravity.magnitude * jumppower)) * rb.mass, ForceMode2D.Impulse); // jump up but also in the x direction travelling clamped at half max speed
                    rb.AddForce(new Vector2(Mathf.Clamp(rb.velocity.x, -0.5f, 0.5f), 1) * (Mathf.Sqrt(2 * Physics2D.gravity.magnitude * jumppower)) * rb.mass, ForceMode2D.Impulse); // jump up but also in the x direction travelling clamped at half max speed
                    isGrounded            = false;
                    spriterenderer.sprite = jump[1];                                                                                                                                 //jump launch sprite
                    StartCoroutine(Buffer("jump", jumpbuffer));
                    StartCoroutine(Buffer("jumpanim", 0.05f));
                }
                else if (Input.GetKeyUp(KeyCode.Space) && !isGrounded) //shorten jump if space key not held
                {
                    if (Vector2.Dot(rb.velocity, Vector2.up) > 0)
                    {
                        rb.AddForce(-Vector2.up * Physics2D.gravity.magnitude * 5 * rb.mass);
                    }
                }
                if (Input.GetKeyDown(KeyCode.Mouse0) && firebuffered)
                {
                    animator.enabled = true;
                    animator.SetTrigger("Throw");
                    StartCoroutine(Buffer("fire", firecd));
                    GameObject newbrick = Instantiate(Brick, new Vector3(transform.position.x, transform.position.y, 0), Quaternion.identity);
                    LastBrick = newbrick;
                    Physics2D.IgnoreCollision(newbrick.GetComponent <BoxCollider2D>(), col);
                    newbrick.GetComponent <Rigidbody2D>().velocity        = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - newbrick.transform.position) * projspeed;
                    newbrick.GetComponent <Rigidbody2D>().angularVelocity = Random.Range(-1080f, 1080f);
                }
                spriterenderer.flipX = facingRight ? false : true;
                if (lasthealth != currenthealth)
                {
                    StopCoroutine(DisplayHPBar());
                    foreach (Transform t in transform.GetChild(0))
                    {
                        t.gameObject.SetActive(false);
                    }
                    StartCoroutine(DisplayHPBar());
                }
                lasthealth = currenthealth;
            }
        }
    }