一修改 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>();
}
}
语言简洁明快,用词精准,毫无赘余。
?未来展望类?
对国际规则的解读具有前瞻性。
?叙事类评语?
这是一篇佳作,无论是从内容、语言还是结构上,都堪称完美。