void Awake()
 {
     bullets = new Stack <projectile>(maxCapacity);
     for (int i = 0; i < 24; i++)
     {
         projectile pr = Instantiate(projectilePrefab);
         pr.gameObject.SetActive(false);
         bullets.Push(pr);
         pr.GetComponent <projectile>().Pool = this;
     }
 }
    // Update is called once per frame
    void Update()
    {
        for (int it = 0; it < obstacles.Length; it++)
        {
            obstacles[it].transform.position   = obstacles_Position[it] + transform.position;
            obstacles[it].transform.localScale = obstacles_scale[it];
        }
        target.transform.position = Target_position + transform.position;
        if (ball.GetComponent <Rigidbody>().velocity.magnitude < 1 && (ball.transform.position != ball.inicial_position || ball.collision_position != Vector3.zero) || ball.transform.position.y < 0)
        {
            if (ball.GetComponent <TrailRenderer>())
            {
                ball.GetComponent <TrailRenderer>().enabled = false;
            }
            if (ball.collision_position != Vector3.zero)
            {
                distance = target.transform.position - ball.collision_position;
            }
            else
            {
                distance = target.transform.position - ball.actual_position;
            }
            if (ball.GetComponent <TrailRenderer>().enabled == false)
            {
                ball.transform.position = ball.inicial_position;
                ball.GetComponent <TrailRenderer>().enabled = true;
            }

            Rigidbody rb = ball.GetComponent <Rigidbody>();
            rb.velocity       = Vector3.zero;
            rb.freezeRotation = true;
        }
    }
    void fireProjectile()
    {
        Debug.Log("pew");
        projectileForce = 2.5f;
        projectile temp = Instantiate(projectile, ProjectileSpawnPoint.position, ProjectileSpawnPoint.rotation) as projectile;

        if (isFacingLeft == false)
        {
            temp.GetComponent <Rigidbody2D>().AddForce(Vector2.right * projectileForce, ForceMode2D.Impulse);
            if (gunPowerUp == true)
            {
                temp.anim.SetBool("Strong", true);
            }
        }
        else
        {
            temp.GetComponent <Rigidbody2D>().AddForce(Vector2.left * projectileForce, ForceMode2D.Impulse);
            if (gunPowerUp == true)
            {
                temp.anim.SetBool("Strong2", true);
            }
        }
    }
Exemple #4
0
 // Start is called before the first frame update
 void Start()
 {
     rb2d          = GetComponent <Rigidbody2D>();
     startingScale = transform.localScale.x;
     sword.SetActive(false);
     hasShot = false;
     Physics2D.IgnoreCollision(projectile.GetComponent <Collider2D>(), GetComponent <Collider2D>());
     PlayerPrefs.SetString("lastLoadedScene", SceneManager.GetActiveScene().name);
     HF1.enabled = true;
     HF2.enabled = true;
     HF3.enabled = true;
     HH1.enabled = false;
     HH2.enabled = false;
     HH3.enabled = false;
     HE1.enabled = false;
     HE2.enabled = false;
     HE3.enabled = false;
 }
    // Update is called once per frame
    void Update()
    {
        if (Time.timeScale == 1)
        {
            // set timer to seconds
            timer += 1.0F * Time.deltaTime;

            //toggle platform detection
            if (gravityFliped == false)
            {
                foreach (GameObject platform in GameObject.FindGameObjectsWithTag("Platform"))
                {
                    platform.GetComponent <EdgeCollider2D>().enabled = true;
                }
                foreach (GameObject invertedplatform in GameObject.FindGameObjectsWithTag("InvertedPlatform"))
                {
                    invertedplatform.GetComponent <EdgeCollider2D>().enabled = false;
                }
            }

            if (gravityFliped == true)
            {
                foreach (GameObject platform in GameObject.FindGameObjectsWithTag("Platform"))
                {
                    platform.GetComponent <EdgeCollider2D>().enabled = false;
                }
                foreach (GameObject invertedplatform in GameObject.FindGameObjectsWithTag("InvertedPlatform"))
                {
                    invertedplatform.GetComponent <EdgeCollider2D>().enabled = true;
                }
            }

            // Check if left or right keys are pressed
            // - Gives decimals from -1 to +1
            //float moveValue = Input.GetAxis("Horizontal");

            // - Gives -1, 0, +1
            float moveValue = Input.GetAxis("Horizontal");

            //check if character is touching anything labeled as Ground/Platform/Jumpable
            isGrounded = Physics2D.OverlapCircle(groundcheck.position, groundcheckradius, isGroundLayer);

            //check if character is grounded
            if (isGrounded)
            {
                // Check if Jump was pressed (aka Space)
                if (Input.GetButton("Jump"))
                {
                    if (jumpForce > 3.5f)
                    {
                        return;
                    }
                    jumpForce += 0.5f;
                    //rb.AddForce(new Vector2(0, 10.0f), ForceMode2D.Impulse);

                    // Vector2.up --> new Vector2(0,1)
                    // Vector2.down --> new Vector2(0,-1)
                    // Vector2.left --> new Vector2(-1,0)
                    // Vector2.right --> new Vector2(1,0)
                    // Vector2.zero --> new Vector2(0,0)
                    // Vector2.one --> new Vector2(1,1)
                }
                if (gravityFliped == false)
                {
                    if (Input.GetButtonUp("Jump"))
                    {
                        // Applies a force in UP direction
                        rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
                        SoundManager.instance.playSingleSound(jump);
                        jumpForce = 1.5f;
                        anim.SetBool("Jumping", true);
                        StartCoroutine(StopAnimation());
                    }
                }
                if (gravityFliped == true)
                {
                    if (Input.GetButtonUp("Jump"))
                    {
                        // Applies a force in UP direction
                        rb.AddForce(Vector2.down * jumpForce, ForceMode2D.Impulse);
                        SoundManager.instance.playSingleSound(jump);
                        jumpForce = 1.5f;
                        anim.SetBool("Jumping", true);
                        StartCoroutine(StopAnimation());
                    }
                }
            }
            if (gravityFliped == false)
            {
                if (Input.GetKey("w") && Input.GetButtonDown("Fire1"))
                {
                    SoundManager.instance.playSingleSound(shoot);
                    upFire          = true;
                    projectileForce = 2.5f;
                    projectile temp = Instantiate(projectile, TopProjectileSpawnPoint.position, TopProjectileSpawnPoint.rotation) as projectile;
                    temp.GetComponent <Rigidbody2D>().AddForce(Vector2.up * projectileForce, ForceMode2D.Impulse);
                    if (gunPowerUp == true)
                    {
                        temp.anim.SetBool("Strong3", true);
                    }
                    anim.SetBool("Shooting", true);
                    StartCoroutine(StopAnimation());
                }
            }
            if (gravityFliped == true)
            {
                if (Input.GetKey("s") && Input.GetButtonDown("Fire1"))
                {
                    SoundManager.instance.playSingleSound(shoot);
                    upFire          = true;
                    projectileForce = 2.5f;
                    projectile temp = Instantiate(projectile, TopProjectileSpawnPoint.position, TopProjectileSpawnPoint.rotation) as projectile;
                    temp.GetComponent <Rigidbody2D>().AddForce(Vector2.down * projectileForce, ForceMode2D.Impulse);
                    if (gunPowerUp == true)
                    {
                        temp.anim.SetBool("Strong4", true);
                    }
                    anim.SetBool("Shooting", true);
                    StartCoroutine(StopAnimation());
                }
            }

            if (Input.GetKeyDown("g") && isFliping == false)
            {
                flipGravity();
                isFliping = true;
                anim.SetBool("GravitySwap", true);
            }

            // Make player move left or right based off moveValue
            rb.velocity = new Vector2(moveValue * speed, rb.velocity.y);
            //rb.AddForce(new Vector2(moveValue * speed, rb.velocity.y));
            if (moveValue != 0)
            {
                anim.SetBool("Moving", true);
            }
            if (moveValue == 0)
            {
                anim.SetBool("Moving", false);
            }

            if (moveValue < 0 && !isFacingLeft)
            {
                flip();
            }
            else if (moveValue > 0 && isFacingLeft)
            {
                flip();
            }
            if (upFire == false)
            {
                if (Input.GetButtonDown("Fire1"))
                {
                    SoundManager.instance.playSingleSound(shoot);
                    fireProjectile();
                }
            }

            upFire = false;
        }
    }