Lesson11 後編

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : MonoBehaviour
{

    private AudioSource audio;

    public AudioClip sound01;
    public AudioClip sound02;
    public AudioClip sound03;

    void Start()
    {
        audio = gameObject.AddComponent<AudioSource>();
    }

    void OnCollisionEnter(Collision other)
    {
        GameObject gm = GameObject.Find("GameObject");
        if (gm.GetComponent<GameManager>().IsInGame())
        {
            if (other.gameObject.tag == "Player")
            {
                audio.PlayOneShot(sound01);
            }
            else if (other.gameObject.tag == "Target")
            {
                audio.PlayOneShot(sound02);
            }
            else audio.PlayOneShot(sound03);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bounce : MonoBehaviour
{

    public float bounce = 10.0f;
    public int scorepoint = 10;
    GameObject go;

    void Start()
    {
        go = GameObject.Find("GameObject");
    }

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Ball")
        {
            Vector3 norm = other.contacts[0].normal;
            Vector3 vel = new Vector3(-norm.x, 0f, -norm.z);
            other.rigidbody.AddForce(vel.normalized * bounce, ForceMode.VelocityChange);
            go.GetComponent<GameManager>().AddScore(scorepoint);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Broken : MonoBehaviour
{

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Ball")
        {
            Destroy(gameObject, 0.2f);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bumper : MonoBehaviour
{

    public float bounce = 10f;

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Ball")
        {
            other.rigidbody.AddForce(0f, bounce/6, bounce, ForceMode.Impulse);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{

    public int life = 3;
    public GameObject ballPrefab;
    public Text textGameOver;
    public Text textClear;
    private int score;
    private float leftTime;
    private Text textScore;
    private Text textLife;
    private Text textTimer;
    private bool inGame;
    static int highScore = 0;

    private Text textResultScore;
    private Text textResultTime;
    private Text textResultBall;
    private Text textResultTotal;
    private Text textHighScore;
    public GameObject nextSceneButton;

    private AudioSource audioSource;
    public AudioClip overSound;
    public AudioClip clearSound;

    void Start()
    {
        textGameOver.enabled = false;
        textClear.enabled = false;
        nextSceneButton.SetActive(false);

        score = 0;
        leftTime = 120f;
        audioSource = gameObject.AddComponent<AudioSource>();
        textScore = GameObject.Find("Score").GetComponent<Text>();
        textLife = GameObject.Find("BallLife").GetComponent<Text>();
        textTimer = GameObject.Find("TimeText").GetComponent<Text>();

        textResultScore = GameObject.Find("ResultScore").GetComponent<Text>();
        textResultTime = GameObject.Find("ResultTime").GetComponent<Text>();
        textResultBall = GameObject.Find("ResultBall").GetComponent<Text>();
        textResultTotal = GameObject.Find("ResultTotal").GetComponent<Text>();
        textHighScore = GameObject.Find("HighScore").GetComponent<Text>();

        SetScoreText(score);
        SetLifeText(life);
        SetHighScoreText(highScore);
        inGame = true;
    }

    private void SetScoreText(int score)
    {
        textScore.text = "Score : " + score.ToString();
    }

    private void SetLifeText(int life)
    {
        textLife.text = "Ball : " + life.ToString();
    }

    private void SetHighScoreText(int highScore)
    {
        textHighScore.text = "HighScore : " + highScore.ToString();
    }

    public void Replay()
    {
        int sceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(sceneIndex);
    }

    public void Menu()
    {
        SceneManager.LoadScene("pinball-title");
    }

    public void NextScene()
    {
        Scene scene = SceneManager.GetActiveScene();
        int i = scene.buildIndex;
        i++;
        SceneManager.LoadScene(i); 
    }

    public void AddScore(int point)
    {
        if (inGame)
        {
            score += point;
            SetScoreText(score);
        }
    }

    public bool IsInGame()
    {
        return inGame;
    }

    void Update()
    {
        if (inGame)
        {
            leftTime -= Time.deltaTime;
            textTimer.text = "Time : " + (leftTime > 0f ? leftTime.ToString("0.00") : "0.00");
            if (leftTime < 0f)
            {
                audioSource.PlayOneShot(overSound);
                textGameOver.enabled = true;
                inGame = false;
            }

            GameObject ballObj = GameObject.Find("Ball");
            if (ballObj == null)
            {
                life--;
                SetLifeText(life);
                if (life > 0)
                {
                    GameObject newBall = Instantiate(ballPrefab);
                    newBall.name = ballPrefab.name;
                }
                else
                {
                    life = 0;
                    audioSource.PlayOneShot(overSound);
                    textGameOver.enabled = true;
                    inGame = false;
                }
            }

            GameObject targetObj = GameObject.FindWithTag("Target");
            if (targetObj == null)
            {
                audioSource.PlayOneShot(clearSound);
                textClear.enabled = true;

                Scene scene = SceneManager.GetActiveScene();
                int i = scene.buildIndex;
                i++;
                int j = SceneManager.sceneCountInBuildSettings;
                if (j-i > 0)
                {
                    nextSceneButton.SetActive(true);
                }

                int scorePoint = score * 50;
                int ballPoint = life * 1000;
                int timePoint = (int)(leftTime * 100f);
                textResultScore.text = "Score * 50 = " + scorePoint.ToString();
                textResultBall.text = "Ball * 1000 = " + ballPoint.ToString();
                textResultTime.text = "Time * 100 = " + timePoint.ToString();

                int totalPoint = scorePoint + ballPoint + timePoint;
                textResultTotal.text = "Total Score : " + totalPoint.ToString();

                if (highScore < totalPoint)
                {
                    highScore = totalPoint;
                }
                inGame = false;
            }
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Kill : MonoBehaviour
{

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Ball")
        {
            Destroy(other.gameObject, 0.1f);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMove : MonoBehaviour
{

    public float speed = 15f;

    void FixedUpdate()
    {
        var velox = speed * Input.GetAxisRaw("Horizontal");
        GetComponent<Rigidbody>().velocity = new Vector3(velox, 0f, 0f);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotation : MonoBehaviour
{

    public float rotAngle = 4.0f;

    void FixedUpdate()
    {
        transform.Rotate(0f, -rotAngle, 0f);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class StartScene : MonoBehaviour
{

    public void StartGame()
    {
        SceneManager.LoadScene("pinball");
    }
}