【Unity】接触時の処理を全て相手側にお任せ(バスケットボールの処理からアイテムの要素を取り去る)抽象クラス編

各アイテムクラスは、抽象クラスを継承する

using UnityEngine;

public abstract class ItemController : MonoBehaviour
{
    public AudioClip SE;

    public float dropSpeed = -0.03f;

    void Update()
    {
        transform.Translate(0, this.dropSpeed, 0);
        if (transform.position.y < -1.0f)
        {
            Destroy(gameObject);
        }
    }
    public void PlaySound()
    {
        AudioSource.PlayClipAtPoint(SE, transform.position);
    }

    public abstract int CalcScore(int score);
}
using UnityEngine;

public class AppleController : ItemController
{
    [SerializeField]
    int Point;

    public override int CalcScore(int score)
    {
        return score += Point;
    }
}
public class BombController : ItemController
{
    public override int CalcScore(int score)
    {
        return score /= 2;
    }
}
using UnityEngine;

public class BasketController : MonoBehaviour
{
    GameDirector director;

    void Start()
    {
        Application.targetFrameRate = 60;
        this.director = GameObject.Find("GameDirector").GetComponent<GameDirector>();
    }

    void OnTriggerEnter(Collider other)
    {
        other.GetComponent<ItemController>().PlaySound();
        director.Point = other.GetComponent<ItemController>().CalcScore(director.Point);

        Destroy(other.gameObject);
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
                float x = Mathf.RoundToInt(hit.point.x);
                float z = Mathf.RoundToInt(hit.point.z);
                transform.position = new Vector3(x, 0, z);
            }
        }
    }
}
using UnityEngine;
using TMPro; // TextMeshProを使う時は忘れないように注意!!

public class GameDirector : MonoBehaviour
{
    GameObject timerText;
    GameObject pointText;
    float time = 30.0f;
    GameObject generator;

    public int Point { get; set; }

    void Start()
    {
        this.timerText = GameObject.Find("Time");
        this.pointText = GameObject.Find("Point");
        this.generator = GameObject.Find("ItemGenerator");
    }

    void Update()
    {
        this.time -= Time.deltaTime;

        if (this.time < 0)
        {
            this.time = 0;
            this.generator.GetComponent<ItemGenerator>().SetParameter(10000.0f, 0, 0);
        }
        else if (0 <= this.time && this.time < 4)
        {
            this.generator.GetComponent<ItemGenerator>().SetParameter(0.3f, -0.06f, 0);
        }
        else if (4 <= this.time && this.time < 12)
        {
            this.generator.GetComponent<ItemGenerator>().SetParameter(0.5f, -0.05f, 6);
        }
        else if (12 <= this.time && this.time < 23)
        {
            this.generator.GetComponent<ItemGenerator>().SetParameter(0.8f, -0.05f, 4);
        }
        else if (23 <= this.time && this.time < 30)
        {
            this.generator.GetComponent<ItemGenerator>().SetParameter(1.0f, -0.03f, 2);
        }

        this.timerText.GetComponent<TextMeshProUGUI>().text = this.time.ToString("F1");
        this.pointText.GetComponent<TextMeshProUGUI>().text = this.Point.ToString() + " point";
    }
}

C#,Unity

Posted by hidepon