1.改进飞碟(Hit UFO)游戏:
- 游戏内容要求:
1.按adapter模式 设计图修改飞碟游戏
2.使它同时支持物理运动与运动学(变换)运动
本次作业只需要在第五次实验的基础上加上adapter模式,并且增加物理运动与运动学运动即可。
故本文只讲述变更的地方
一、物理学运动类和运动学运动类
两者的主要区别在于判定出界条件不同
public class CCAction : SSAction
{
float speed;
Vector3 direction;
public static CCAction GetSSAction(Vector3 dir, float speed)
{
CCAction ret = ScriptableObject.CreateInstance<CCAction>();
ret.speed = speed;
ret.direction = dir;
return ret;
}
public override void Start()
{
gameobject.GetComponent<Rigidbody>().isKinematic = true;
}
// Update is called once per frame
public override void Update()
{
//动作运行
transform.Translate(direction * speed * Time.deltaTime);
//判断动作是否结束(飞出屏幕外结束)
if (this.transform.position.y < -10 || this.transform.position.y > 20 || this.transform.position.x < -35 || this.transform.position.x > 35)
{
this.destroy = true;
this.enable = false;
this.callback.SSActionEvent(this);
}
}
}
public class PhysicalAction : SSAction
{
float speed;
Vector3 direction;
public static PhysicalAction GetSSAction(Vector3 dir, float speed)
{
PhysicalAction tmp = ScriptableObject.CreateInstance<PhysicalAction>();
tmp.speed = speed;
tmp.direction = dir;
return tmp;
}
public override void Start()
{
gameobject.GetComponent<Rigidbody>().isKinematic = false;
//为物体增加水平初速度,否则会由于受到重力严重影响运动状态。
gameobject.GetComponent<Rigidbody>().velocity = speed * direction;
}
// Update is called once per frame
public override void Update()
{
//动作运行
transform.Translate(direction * speed * Time.deltaTime);
//判断动作是否结束(落地结束)
if (this.transform.position.y < -10)
{
this.destroy = true;
this.enable = false;
this.callback.SSActionEvent(this);
}
}
}
二、物理学运动管理类和运动学运动管理类
两者相差不大
public class CCActionManager : SSActionManager, ISSActionCallback, IActionManager
{
public CCAction Action;
public FirstController controller;
public void Start()
{
controller = (FirstController)SSDirector.GetInstance().CurrentScenceController;
}
public void Fly(GameObject disk, float speed, Vector3 direction)
{
Action = CCAction.GetSSAction(direction, speed * 1.5f);
RunAction(disk, Action, this);
}
//回调函数
public void SSActionEvent(SSAction source,
SSActionEventType events = SSActionEventType.Competed,
int intParam = 0,
string strParam = null,
Object objectParam = null)
{
controller.diskFactory.FreeDisk(source.gameobject);//释放资源
}
}
public class PhysicalActionManager : SSActionManager, ISSActionCallback, IActionManager
{
public PhysicalAction Action;
public FirstController controller;
public void Start()
{
controller = (FirstController)SSDirector.GetInstance().CurrentScenceController;
}
public void Fly(GameObject disk, float speed, Vector3 direction)
{
Action = PhysicalAction.GetSSAction(direction, speed);
RunAction(disk, Action, this);
}
//回调函数
public void SSActionEvent(SSAction source,
SSActionEventType events = SSActionEventType.Competed,
int intParam = 0,
string strParam = null,
Object objectParam = null)
{
controller.diskFactory.FreeDisk(source.gameobject);//释放资源
}
}
三、切换模式
FirstController类中增加接口ChangeMode
public void ChangeMode(int mode)
{
if (mode == 0)
{
actionManager = Singleton<CCActionManager>.Instance;
}
else if (mode == 1)
{
actionManager = Singleton<PhysicalActionManager>.Instance;
}
}
UserGUI中添加组件给用户切换模式,每次点击调用ChangeMode接口
void OnGUI()
{
if (status == 0)
{
.......
mode = GUI.Toolbar(new Rect(240, 200, 200, 50), mode, mode_name);
userAction.ChangeMode(mode);
}
.......
}
效果如图:
四、adapter模式
IActionManager接口专用于飞碟的运动,无论是物理运动还是运动学运动都使用该接口。
public interface IActionManager
{
void Fly(GameObject disk, float speed, Vector3 direction);
}
除此以上其余代码同第五次作业相同。
运行结果图