核心类图:


策略模式在游戏开发中的应用-LMLPHP
Demo截图:
策略模式在游戏开发中的应用-LMLPHP

弹药类的实现

  • 定义弹药的接口,IWeapon.cs如下:
public interface IWeapon {
	void Shoot (Vector3 pos);
}
  • 定义一个枚举来表示当前选择的弹药的类型
public enum WeaponType {
	Missile,
	Bullet
}
  • 定义具体的Bullet(子弹类)并实现Iweapon接口
public class Bullet : IWeapon {
	public  void  Shoot (Vector3 pos) {
		Vector3 initialPosition = new Vector3 (pos.x, pos.y + 1f, 0);
		GameObject bullet = MonoBehaviour.Instantiate(
        Resources.Load ("BulletPrefab", typeof (GameObject))) as GameObject;
		bullet.transform.position = initialPosition;
		bullet.GetComponent<Rigidbody2D> ().velocity = new Vector2 (0f, 3f);
	}
}
  • 定义具体的Missile(导弹类)并实现Iweapon接口
public class Missile : IWeapon {
	public  void Shoot (Vector3 pos) {
		Vector3 initialPosition = new Vector3 (pos.x, pos.y + 1f, 0);
		GameObject missile = MonoBehaviour.Instantiate (
        Resources.Load ("MissilePrefab", typeof (GameObject))) as GameObject;
		missile.transform.position = initialPosition;
		missile.GetComponent<Rigidbody2D> ().velocity = new Vector2 (0f, 3f);
	}
}

火焰类的实现

  • 定义火焰的接口IFlame(火焰),IFlame.cs如下:
public interface IFlame{
	void ShowFlame();//显示当前颜色的火焰
	void DestroyFlame();//销毁当前火焰
}
  • 定义一个枚举来表示当前选中火焰的类型
public enum FlameType {
	Blue,
	Red
}
  • 定义具体的BlueFlame(蓝色火焰)并实现IFlame接口
public class BlueFlame:MonoBehaviour,IFlame{
	private GameObject flame;
	public void ShowFlame(){
		if (flame!=null)return;
		GameObject blueFlame = Instantiate(Resources.Load("Flame-blue",
        typeof(GameObject))) as GameObject;
		flame = blueFlame;
		blueFlame.transform.parent=transform;
	}
	public void DestroyFlame(){
		Destroy(flame);
	}
}
  • 定义具体的RedFlame(红色火焰)并实现IFlame接口
public class RedFlame:MonoBehaviour,IFlame{
	private GameObject flame;
	public void ShowFlame(){
		if (flame!=null)return;
		GameObject redFlame = Instantiate(Resources.Load("Flame-red",
        typeof(GameObject))) as GameObject;
		flame = redFlame;
		redFlame.transform.parent=transform;
	}
	public void DestroyFlame(){
		Destroy(flame);
	}
}

Context类(ShipController.cs)的实现

  • 根据用户当前选择的弹药类型,实例化相对应的弹药
private void HandleWeaponType () {
		var weponTypeEnumLength=System.Enum.GetNames(weaponType.GetType()).Lengt;
		var currentWeaponType=(WeaponType)((seed++)%weponTypeEnumLength);
        #region 策略
		switch (currentWeaponType) {
			case WeaponType.Bullet:
				iWeapon =new Bullet ()  ;
				break;
			case WeaponType.Missile:
				iWeapon = new Missile ();
				break;
			default:
				iWeapon = new Bullet () ;
				break;
		}
        #endregion
	}
  • 根据用户当前选择的火焰类型,实例化相对应的火焰
public void HandleFlameColor () {
		var flameColorEnumLength=System.Enum.GetNames(flameColorType.GetType()).Length;
		var currentFlameColor=(FlameType)((seed++)%flameColorEnumLength);
		Component c=this.GetComponent<IFlame>() as Component;
		if (c!=null){
			Destroy(c);
			iFlame.DestroyFlame();
		}
		#region 策略
		switch (currentFlameColor) {
			case FlameType.Blue:
				iFlame = gameObject.AddComponent<BlueFlame> ();
				break;
			case FlameType.Red:
				iFlame = gameObject.AddComponent<RedFlame> ();
				break;
			default:
				iFlame = gameObject.AddComponent<BlueFlame> ();
				break;
		}
		#endregion
	}
END
原文参考及Code来源:

http://www.theappguruz.com/blog/learn-strategy-pattern-in-unity-in-less-than-15-minutes

完整Demo

扫码->关注->历史消息->当前文章末尾(获取Demo下载地址)


策略模式在游戏开发中的应用-LMLPHP
03-22 10:53