改进飞碟(Hit UFO)游戏
- 游戏要求:
类的适配器模式就是把适配的类的API转换成为目标类的API
从上图可以看出:
Target期待调用Request方法,而Adaptee并没有,为使Target能够使用Adaptee类里的SpecificRequest方法,提供一个Adapter类,继承Adaptee并实现Target接口。
- 创建Target接口
- 创建源类
- 创建适配器类Adapter
- 定义具体使用目标类,并通过Adapter类调用所需要的方法从而实现目标
实现思路
实现物理运动
定义一个物理运动的类PhysicsAction
实现飞碟的物理运动,添加Rigidbody组件,模拟物理运动1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class PhysicsAction : Action
{
private Vector3 startDirection; // 初速度方向
private float power; // 控制飞碟速度的变量
public static PhysicsAction GetAction(Vector3 direction, float angle, float power)
{
// 初始化飞碟的初速度方向
PhysicsAction action = CreateInstance<PhysicsAction>();
if (direction.x == -1)
{
action.startDirection = Quaternion.Euler(new Vector3(0, 0, -angle)) * Vector3.left * power;
}
else
{
action.startDirection = Quaternion.Euler(new Vector3(0, 0, angle)) * Vector3.right * power;
}
action.power = power;
return action;
}
定义物理运动的管理器
1 | public class PhysicsActionManager : ActionManager, ActionCallback |
定义通用接口
适配器继承这个接口来进行运动的适配
1 | public interface IActionManager |
实现适配器
适配器中包含两个运动管理器的实例,调用不同的运动管理器实现不同的运动模式
1 | public class ActionManagerAdapter : MonoBehaviour, IActionManager |
运动学运动实现方法
在Disk上添加Rigidbody组件,取消勾选重力