コード例 #1
0
 void OnGUI()
 {
     // Draw the menu
     // Restart game
     if (GUI.Button(new Rect(10, 10, 100, 20), "Restart game"))
     {
         Application.LoadLevel(1);
     }
     // Quit the current game and get back to the start menu
     if (GUI.Button(new Rect(10, 40, 100, 20), "End game"))
     {
         if (state_ == States.ONGAMING)
         {
             foreach (GameObject a in GameObject.FindGameObjectsWithTag("quartz"))
             {
                 Quartz quartz_script = a.GetComponent <Quartz>();
                 int    level         = quartz_script.GetLevel();
                 // Do NOT count the prefab
                 if (level > 0)
                 {
                     Quartz.Colors color = quartz_script.GetColor();
                     points_     += LEVEL_POINTS[level];
                     got_quartz_ += "Level " + level + " " + color.ToString() + "\n";
                     Destroy(a);
                 }
             }
             state_ = States.ENDED;
         }
     }
     if (GUI.Button(new Rect(10, 70, 100, 20), "Quit"))
     {
         Application.LoadLevel(0);
     }
     if (state_ == States.ENDED)
     {
         string table_head;
         GUI.TextArea(
             new Rect(200, 100, 500, 500),
             got_quartz_ + "\nYou've Got " + points_.ToString() + " points");
     }
 }
コード例 #2
0
    IEnumerator  OnCheckQuartz(GameObject quartz)
    {
        if (first_quartz_ == null)
        {
            first_quartz_ = quartz;
            first_quartz_.SendMessage("Toggle");
        }
        else if (second_quartz_ == null)
        {
            second_quartz_ = quartz;
            second_quartz_.SendMessage("Toggle");
            Quartz first_quartz_script  = first_quartz_.GetComponent <Quartz>();
            Quartz second_quartz_script = second_quartz_.GetComponent <Quartz>();

            int first_quartz_level  = first_quartz_script.GetLevel();
            int second_quartz_level = second_quartz_script.GetLevel();
            if (first_quartz_level != second_quartz_level)
            {
                // level doesn't match, destory the two quartz
                Destroy(first_quartz_);
                Destroy(second_quartz_);
            }
            else
            {
                // TODO: use animation to replace the wait
                yield return(new WaitForSeconds(0.5f));

                Vector3 new_position = first_quartz_.transform.position;


                Quartz.Colors first_color  = first_quartz_script.GetColor();
                Quartz.Colors second_color = second_quartz_script.GetColor();

                int new_level = first_quartz_level;
                Destroy(first_quartz_);
                Destroy(second_quartz_);
                first_quartz_  = null;
                second_quartz_ = null;
                Quartz new_quartz_script = ((GameObject)Instantiate(
                                                quartz_prefab_,
                                                new_position,
                                                Quaternion.identity)).GetComponent <Quartz>();
                if (first_color == second_color)
                {
                    new_quartz_script.SetColor(first_color);
                    new_quartz_script.SetLevel(new_level + 1);
                }
                else if (first_color != Quartz.Colors.BLUE &&
                         second_color != Quartz.Colors.BLUE)
                {
                    new_quartz_script.SetColor(Quartz.Colors.BLUE);
                    new_quartz_script.SetLevel(new_level);
                }
                else if (first_color != Quartz.Colors.YELLOW &&
                         second_color != Quartz.Colors.YELLOW)
                {
                    new_quartz_script.SetColor(Quartz.Colors.YELLOW);
                    new_quartz_script.SetLevel(new_level);
                }
                else if (first_color != Quartz.Colors.RED &&
                         second_color != Quartz.Colors.RED)
                {
                    new_quartz_script.SetColor(Quartz.Colors.RED);
                    new_quartz_script.SetLevel(new_level);
                }

                new_quartz_script.Toggle();
                // TODO: use animation to replace the wait
                yield return(new WaitForSeconds(0.5f));

                new_quartz_script.Toggle();
            }
        }
        else
        {
            print("You shoud not open a box with two boxes already opened");
        }
    }