|
修改飞机基类Aircraft,加入Health属性和IsAlive属性,然后再加一个ReceiveDamage方法用来计算伤害,代码如下:
namespace Frederick.ProjectAircraft
{
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 飞机。
/// </summary>
[RequireComponent(typeof (AircraftGraphic))]
public class Aircraft : MonoBehaviour
{
/// <summary>
/// 获取一个值,表示飞机是否存活。
/// </summary>
public bool IsAlive
{
get { return Health > 0; }
}
/// <summary>
/// 获取飞机所有的部分。
/// </summary>
public IEnumerable<AircraftPart> Parts
{
get { return mParts.AsReadOnly(); }
}
public int Health;
/// <summary>
/// 令飞机直接爆炸。
/// </summary>
public void Explode()
{
Health = 0;
Debug.Log("exploded");
Destroy(gameObject);
}
/// <summary>
/// 飞机受到指定伤害。
/// </summary>
/// <param name="damage伤害值</param>
public void ReceiveDamage(int damage)
{
Health -= damage;
if (Health <= 0)
Explode();
}
protected virtual void Awake()
{
mGraphic = GetComponent<AircraftGraphic>();
var self = GetComponent<AircraftPart>();
var children = GetComponentsInChildren<AircraftPart>();
if (self != null)
mParts.Add(self);
mParts.AddRange(children);
foreach (var part in mParts)
{
part.Aircraft = this;
var dispatcher = part.GetComponent<CollisionEventDispatcher>();
dispatcher.TriggerEntered += OnPartTriggerEnter;
}
}
protected virtual void OnDestroy()
{
foreach (var part in mParts)
{
part.Aircraft = null;
var dispatcher = part.GetComponent<CollisionEventDispatcher>();
dispatcher.TriggerEntered -= OnPartTriggerEnter;
}
}
/// <summary>
/// 当飞机的一部分触发碰撞时调用此方法。
/// </summary>
/// <param name="part碰撞的部分</param>
/// <param name="other碰撞的碰撞器</param>
protected virtual void OnPartTriggerEnter(GameObject part, Collider other) {}
private readonly List<AircraftPart> mParts = new List<AircraftPart>();
}
}
修改完机体,还要继续调整Missile,加入Power属性和对机体ReceiveDamage方法的调用。
namespace Frederick.ProjectAircraft
{
using UnityEngine;
/// <summary>
/// 导弹。
/// </summary>
[RequireComponent(typeof (BoxCollider))]
public class Missile : MonoBehaviour
{
public int Power = 1;
public float StantardSpeed = 800;
protected void Awake()
{
mTransform = transform;
}
protected void OnTriggerEnter(Collider other)
{
if (other.tag != "Enemy")
return;
Destroy(gameObject);
var part = other.GetComponent<AircraftPart>();
part.Aircraft.ReceiveDamage(Power);
}
protected void Update()
{
var position = mTransform.localPosition + mDirection * StantardSpeed * Time.deltaTime;
if (position.y > 960)
{
Destroy(gameObject);
return;
}
mTransform.localPosition = position;
}
private readonly Vector3 mDirection = Vector3.up;
private Transform mTransform;
}
}
调整完脚本后,在检视面板里将敌方机体的Health属性修改为2,也就是被命中两次后摧毁。测试运行游戏,我方飞机已经可以通过发射导弹将敌方击毁了:)好了,现在只剩下当我方机体和敌方机体相撞时的伤害计算了。创建一个新的EnemyAircraft,继承自Aircraft,并重写OnPartTriggerEnter方法。注意这里需要将玩家机体的Body部分的Tag设置为Player。
namespace Frederick.ProjectAircraft
{
using UnityEngine;
/// <summary>
/// 敌方的飞机。
/// </summary>
public class EnemyAircraft : Aircraft
{
/// <summary>
/// 当飞机的一部分触发碰撞时调用此方法。
/// </summary>
/// <param name="part碰撞的部分</param>
/// <param name="other碰撞的碰撞器</param>
protected override void OnPartTriggerEnter(GameObject part, Collider other)
{
if (other.tag != "Player")
return;
var aircraftPart = other.GetComponent<AircraftPart>();
aircraftPart.Aircraft.Explode();
}
}
}
由于之前已经为敌方机体附加了Aircraft,所以先要移除这个组件,并添加上新的EnemyAircraft。最后点击运行游戏,嗯……击毁飞机的效果的确是有点简陋,完全没有传说中的“打击感”。没事,千里之行始于足下,哈哈。下一篇把爆炸动画和声音效果加上。
|
|