Unity 2D游戏开发: 添加敌人

Nan (/)
2024-05-20T21:58:17+08:00

一修改 Character 類

using System.Collections;
using UnityEngine;

public abstract class Character : MonoBehaviour
{
    public float maxHitPoints; // 最大血量
    public float startHitPoints; // 初始血量

    /// 死亡
    public virtual void Kill()
    {
        Destroy(gameObject);
    }

    /// 重置
    public abstract void Reset();
    public abstract IEnumerator Damage(int value, float interval);
}

一新建 Enemy 類

using System.Collections;
using UnityEngine;

public class Enemy : Character
{
    public int strength; // 攻击力
    Coroutine damageCoroutine; // 协程
    float hitPoints; // 血量

    /// 脚本启用后
    private void OnEnable()
    {
        Reset();
    }

    /// 重写重置
    public override void Reset()
    {
        hitPoints = startHitPoints;
    }

    /// 重写伤害
    public override IEnumerator Damage(int value, float interval)
    {
        while (true)
        {
            hitPoints = hitPoints - value;

            if (hitPoints <= float.Epsilon)
            {
                Kill();
                break;
            }

            if (interval > float.Epsilon)
            {
                yield return new WaitForSeconds(interval);
            }
            else
            {
                break;
            }
        }
    }

    /// 碰撞检测
    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            Player player = collision.gameObject.GetComponent<Player>();
            if (damageCoroutine == null)
            {
                damageCoroutine = StartCoroutine(player.Damage(strength, 1.0f));
            }
        }
    }

    /// 碰撞检测
    void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            if (damageCoroutine != null)
            {
                StopCoroutine(damageCoroutine);
                damageCoroutine = null;
            }
        }
    }
}

一修改 Player 類

using System.Collections;
using UnityEngine;

public class Player : Character
{
    public PlayerHitPointsScriptable hitPointsScriptable; // 当前血量
    public PlayerHealthBar healthBarPrefab; // 血条預制件
    public Bag bagPrefab; // 背包預制件

    private PlayerHealthBar healthBar; // 血条实例
    private Bag bag; // 背包实例
    private AudioSource audioSource;

    void OnEnable()
    {
        Reset();
    }

    ....

    /// 重写伤害
    public override IEnumerator Damage(int value, float interval)
    {
        while (true)
        {
            hitPointsScriptable.value -= value;

            if (hitPointsScriptable.value <= float.Epsilon)
            {
                Kill();
                break;
            }

            if (interval > float.Epsilon)
            {
                yield return new WaitForSeconds(interval);
            }
            else
            {
                break;
            }
        }
    }

    /// 重写死亡
    public override void Kill()
    {
        base.Kill();
        Destroy(healthBar.gameObject);
        Destroy(bag.gameObject);
    }

    /// 重写重置
    public override void Reset()
    {
        // 血条
        hitPointsScriptable.value = startHitPoints;
        healthBar = Instantiate(healthBarPrefab);
        healthBar.character = this;
        // 背包
        bag = Instantiate(bagPrefab);
        // 声音
        audioSource = GetComponent<AudioSource>();
    }
}