Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (currentHealth == 0)
        {
            if (Input.GetKey(KeyCode.R) & level == 1)
            {
                SceneManager.LoadScene("Stage1");
            }

            if (Input.GetKey(KeyCode.R) & level == 2)
            {
                SceneManager.LoadScene("Stage2");
            }
        }

        Scoretext();
        Cointext();

        horizontal = Input.GetAxis("Horizontal");
        vertical   = Input.GetAxis("Vertical");

        Vector2 move = new Vector2(horizontal, vertical);

        if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
        {
            lookDirection.Set(move.x, move.y);
            lookDirection.Normalize();
        }

        animator.SetFloat("Look X", lookDirection.x);
        animator.SetFloat("Look Y", lookDirection.y);
        animator.SetFloat("Speed", move.magnitude);

        if (isInvincible)
        {
            invincibleTimer -= Time.deltaTime;
            if (invincibleTimer < 0)
            {
                isInvincible = false;
            }
        }

        if (Input.GetKeyDown(KeyCode.C))
        {
            if (cogs > 0)
            {
                Launch();
                cogs          = cogs - 1;
                cogstext.text = "cogs:" + cogs.ToString();
            }
        }

        if (score == 4 & level == 1)
        {
            conditionaltext.text = "Talk to Jambi to visit stage 2";

            RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, 1.5f, LayerMask.GetMask("NPC"));
            if (hit.collider != null)
            {
                if (Input.GetKeyDown(KeyCode.X))
                {
                    SceneManager.LoadScene("Stage2");
                    level++;
                }
            }
        }

        if (Input.GetKeyDown(KeyCode.X))
        {
            RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, 1.5f, LayerMask.GetMask("NPC"));

            if (hit.collider != null)
            {
                NonPlayerCharacter character = hit.collider.GetComponent <NonPlayerCharacter>();

                if (character != null)
                {
                    if (score < 4 | coins < 4)
                    {
                        PlaySound(npcinteraction);
                        character.DisplayDialog();
                    }

                    else if (score == 4 & level == 1)
                    {
                        character.DisplayDialog3();
                    }

                    else if (coins == 4 & level == 2)
                    {
                        character.DisplayDialog2();
                        conditionaltext.text = "You Win! Game Created by Matthew Falconett. Press R to restart";
                        musicsource.clip     = victorymusic;
                        musicsource.Play();
                        speed = 0;
                        coins = 5;
                        level = 3;
                    }
                }
            }
        }

        if (Input.GetKeyDown(KeyCode.R) && level == 3)  //Restart for winning
        {
            SceneManager.LoadScene("Stage2");
            level = 2;
        }

        if (Input.GetKeyDown(KeyCode.R))
        {
            RaycastHit2D hit = Physics2D.Raycast(rigidbody2d.position + Vector2.up * 0.2f, lookDirection, 1.5f, LayerMask.GetMask("NPC"));
            if (hit.collider != null & coins == 4)
            {
                SceneManager.LoadScene("Stage2");
            }
        }


        if (Input.GetKey("escape"))
        {
            Application.Quit();
        }


        if (powerup)
        {
            poweruptimer += Time.deltaTime;

            if (poweruptimer >= 2)
            {
                speed        = 3;
                poweruptimer = 0;
                powerup      = false;
            }
        }
    }