使用耗材时增加健康

时间:2019-02-23 13:12:40

标签: c# unity3d

我的游戏中有一个清单,玩家可以在其中装备或喝东西。在我的库存中,我有可以由玩家使用的药水。玩家使用药水会增加角色的生命值。所以,我有这样的错误:无法将类型void隐式转换为Player。为什么会有这样的错误。真的很感谢你们的回答。谢谢!

IConsumable.cs

public interface IConsumable {
    void Consume(Player player);
    void Consume(CharacterStats stats);
}

ConsumableController.cs

public class ConsumableController : MonoBehaviour {

    CharacterStats stats;

    Player player;

    int health = 100;

    void Start() {
        stats = GetComponent<Player> ().characterStats; 
        player = GetComponent<Player> ().IncreaseHealth(health);
    }

    public void ConsumeItem(Item item) {
        GameObject itemToSpawn = Instantiate (Resources.Load<GameObject>("Consumables/" + item.ObjectSlug));

        if (item.ItemModifier) {
            itemToSpawn.GetComponent<IConsumable> ().Consume (stats);
        } else {
            itemToSpawn.GetComponent<IConsumable> ().Consume (player);
        }
    }
}

PotionLog.cs

public class PotionLog : MonoBehaviour, IConsumable {

    public void Consume(Player player) {
        Debug.Log ("You drank a swig of the potion. Cool!");

        Destroy (gameObject);
    }

    public void Consume(CharacterStats stats) {
        Debug.Log ("You drank a swig of the potion. Rad!");

    }

}

Player.cs

public class Player : MonoBehaviour {

    public CharacterStats characterStats;

    public int currentHealth;

    public int maxHealth;

    public PlayerLevel PlayerLevel { get; set; }

    void Awake() {
        PlayerLevel = GetComponent<PlayerLevel> ();
        //this.currentHealth = this.maxHealth;
        characterStats = new CharacterStats (5, 10, 2);
    }

    public void TakeDamage(int amount) {
        Debug.Log ("Player takes: " + amount + " damage!");
        currentHealth -= amount;

        if (currentHealth <= 0)
            Die ();

        UIEventHandler.HealthChanged (this.currentHealth, this.maxHealth);
    }

    public void IncreaseHealth(int health) {
        currentHealth += health;

        if (currentHealth == maxHealth)
            return;

        UIEventHandler.HealthChanged (this.currentHealth, this.maxHealth);
    }

    private void Die() {
        Debug.Log ("Player dead! Reset health.");
        this.currentHealth = this.maxHealth;
        UIEventHandler.HealthChanged (this.currentHealth, this.maxHealth);
    }
}

0 个答案:

没有答案