★Kairi Ito

(2025年1月18日)

<無敵状態時に、色を変化させる>

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;

public class TankHealth : MonoBehaviour
{
    public GameObject effectPrefab1;
    public GameObject effectPrefab2;
    public int tankHP;
    public TextMeshProUGUI hpLabel;
    private int tankMaxHP = 10;
    private bool isMuteki = false;

    // ★「UnityEngine.」を追加
    public UnityEngine.Material[] mats;
    public GameObject[] parts;


    private void Start()
    {
        tankHP = tankMaxHP;
        hpLabel.text = "" + tankHP;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("EnemyShell") && isMuteki == false)
        {
            tankHP -= 1;
            hpLabel.text = "" + tankHP;
            Destroy(other.gameObject);

            if (tankHP > 0)
            {
                GameObject effect1 = Instantiate(effectPrefab1, transform.position, Quaternion.identity);
                Destroy(effect1, 1.0f);
            }
            else
            {
                GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
                Destroy(effect2, 1.0f);
                //Destroy(gameObject);
                this.gameObject.SetActive(false);
                Invoke("GoToGameOver", 1.5f);
            }
        }
    }
    void GoToGameOver()
    {
        SceneManager.LoadScene("GameOver");
    }
    public void AddHP(int amount)
    {
        tankHP += amount;
        if (tankHP > tankMaxHP)
        {
            tankHP = tankMaxHP;
        }
        hpLabel.text = "" + tankHP;
    }
     public void ReduceHP(int amount)
    {
        tankHP -= amount;
        hpLabel.text = "" + tankHP;
        if (tankHP < 0)
        {
            SceneManager.LoadScene("GameOver");
        }
    }

    public void  Muteki()
    {
        tankHP -= 0;
        isMuteki = true;
        Invoke("Super",  3);

        // ★追加
        // 色を黄金に変化
        foreach(GameObject p in parts)
        {
            p.GetComponent<MeshRenderer>().material = mats[1];
        }
    }


    void Super()
    {
        isMuteki = false;

        // ★追加
        // 色を初期に戻す
        foreach(GameObject p in parts)
        {
            p.GetComponent<MeshRenderer>().material = mats[0];
        }
    }


    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.CompareTag("Doragon")&& isMuteki == false)
        {
            tankHP -= 1;
            hpLabel.text = "" + tankHP;
            if (tankHP < 0)
            {
                SceneManager.LoadScene("GameOver");
            }
        }
    }
}
 //次回の課題
 //今、無敵状態だと分かるようにする。
 //↑タンクのマテリアルが変わるなど。