打飞碟运动学与物理学结合版

架构:

在上一篇博客我介绍了运动学的打飞碟设计方法,但是在一些情况下我们希望飞碟能够在飞行过程中受到外力,比如风力或其他力的影响发生轨道的偏离。这就需要使用适配器的设计模式:

适配器模式:

适配器模式将某个类的接口转换成客户端期望的另一个接口表示,主的目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。其别名为包装器(Wrapper)。
UML图如下:
这里写图片描述
通过添加IActionManager的接口实现我们需要扩展的函数

具体实现:

承继上一篇博客,我将RoundActionManager中的addRandomAction取出作为接口函数,因为这个函数的作用在于设定飞碟的发射轨迹,因此,我只要在这个函数中获取飞碟的刚体并为之添加一个随机方向的力就可以实现飞碟的物理学飞行:

public class PhysisManager : SSActionManager, ISSActionCallback, IActionManager
{
    public RoundController scene;
    public MoveToAction action1, action2;
    public SequenceAction saction;
    float speed;
    public int If_Active;


    public void addRandomAction(GameObject gameObj)
    {
        Vector3 force = new Vector3 (
            UnityEngine.Random.Range(-10, 10),
            UnityEngine.Random.Range(-10, 10),
            UnityEngine.Random.Range(-10, 10)
        );
        int[] X = { -20, 20 };
        int[] Y = { -5, 5 };
        int[] Z = { -20, -20 };

        // 随机生成起始点和终点
        Vector3 starttPos = new Vector3(
            UnityEngine.Random.Range(-70, 70),
            UnityEngine.Random.Range(-10, 10),
            UnityEngine.Random.Range(100, 150)
        );

        gameObj.transform.position = starttPos;

        Vector3 randomTarget = new Vector3(
            X[UnityEngine.Random.Range(0, 2)],
            Y[UnityEngine.Random.Range(0, 2)],
            Z[UnityEngine.Random.Range(0, 2)]
        );
        gameObj.GetComponent<Rigidbody> ().velocity = Vector3.zero;
        gameObj.GetComponent<Rigidbody> ().AddForce (force, ForceMode.Impulse);
        MoveToAction action = MoveToAction.getAction(randomTarget, gameObj.GetComponent<DiskData>().speed);

        RunAction(gameObj, action, this);
    }

    protected  void Start()
    {
        scene = (RoundController)SSDirector.getInstance().currentScenceController;
        scene.physisManager = this;
    }

    protected new void Update()
    {
        base.Update();
    }

    public void actionDone(SSAction source)
    {
        Debug.Log("Done");
    }
}

在这之后,我们还需要在场景控制器中为这个类添加一个实例:

diskFactory = Singleton<DiskFactory>.Instance;
scoreRecorder = Singleton<ScoreRecorder>.Instance;
actionManager = Singleton<RoundActionManager>.Instance;
physisManager = Singleton<PhysisManager>.Instance;

之后,在调用这个函数的场景控制类中加上:

if(actionManager.If_Active == 1)
    actionManager.addRandomAction (disk);
else
    physisManager.addRandomAction (disk);

为了便于模式的切换,我在RoundActionManager中添加了切换的变量If_Active,当这个变量的值为1时,切换为运动学模式,否则为物理学模式。然后为飞碟的预设添加刚体,并把PhysisManager这个脚本挂载到main对象中,运行即可。
这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/hellowangld/article/details/80071445