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

各アイテムは、Interfaceを実装する

public interface ICalcScore
{
    int CalcScore(int score);
}
using UnityEngine;

public class AppleCalcScore : MonoBehaviour, ICalcScore
{
    [SerializeField]
    int Point;
    public int CalcScore(int score)
    {
        return score += Point;
    }
}
using UnityEngine;

public class BombCalcScore : MonoBehaviour, ICalcScore
{
    public 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<ICalcScore>().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