游戏开发之Unity学习(六)——鼠标打飞碟,适配器(Adapter)模式,结合物理引擎

本次实现效果图如下
这里写图片描述

Unity中物理引擎

物理引擎是一种软件组件,用于仿真物理世界运动。这种仿真包括刚体力学、流体力学以及碰撞检测。
物理引擎通过为刚性物体赋予真实的物理属性,在外部力的作用下,计算运动、旋转和碰撞

Unity中的常见物理组件:

  • Rigidbody 刚体组件
  • 物体运动控制属性
  • Collider 碰撞器
  • 物体碰撞与碰撞检测属性
  • Mesh 网格
  • 物体形状属性
  • Joint 连接器

Unity中物体发生碰撞的条件

Static Colider 静态碰撞器Rigibody Colider 刚体碰撞器Kinematic Rigibody Colider 运动学碰撞器
Static Colider 静态碰撞器Y
Rigibody Colider 刚体碰撞器YYY
Kinematic Rigibody Colider 运动学碰撞器Y

适配器(Adapter)模式

适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。

更多开发模式和详细内容

本次实验的UML图:

这里写图片描述
相比较上一份代码,这一次的实现也是在原来的基础上修改了一部分内容,其中将组合在FirstSceneController里面的动作管理器改为了动作管理器接口,然后两个适配器(CCActionManager和PhysicsActionManager)继承这个接口。

具体实现

CCActionManager修改Update函数为PlayDisk函数。只需要修改名字即可,其余不变。
FirstSceneController函数修改三个地方

//原来的代码为public CCActionManager actionManager;
public IActionManager actionManager;
/*原来的代码为this.gameObject.AddComponent<CCActionManager>();
 *如果想使用动作管理器版,请勿修改此处
 */
this.gameObject.AddComponent<PhysicsActionManager>();
public void Update()
{
    if (times < 30 && flag == 0)
    {
        if (interval <= 0)
        {
            interval = Random.Range(3, 5);
            times++;
            df.GenDisk();
        }
        interval -= Time.deltaTime;
    }
    //这一句是添加的代码
    actionManager.PlayDisk();
}

修改完这三个函数,就可以在动作管理器版和物理引擎版之间切换了
然后实现PhysicsEmitAction

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PhysicsEmitAction : SSAction {
    public Vector3 speed;

    public static PhysicsEmitAction GetSSAction()
    {
        PhysicsEmitAction action = CreateInstance<PhysicsEmitAction>();
        return action;
    }
    public override void Start()
    {
    }
    public override void Update()
    {
        if (transform.position.y < -10 || transform.position.x <= -20 || transform.position.x >= 20)
        {
            gameObject.GetComponent<Rigidbody>().isKinematic = true;
            gameObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
            transform.position = Vector3.down;
            callback.SSActionEvent(this);
        }
    }
}

然后实现适配器PhysicsActionManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PhysicsActionManager : SSActionManager, ISSActionCallback, IActionManager {
    public FirstSceneController sceneController;
    public List<PhysicsEmitAction> seq = new List<PhysicsEmitAction>();
    public UserClickAction userClickAction;
    public DiskFactory disks;

    protected void Start()
    {
        sceneController = (FirstSceneController)SSDirector.getInstance().currentSceneController;
        sceneController.actionManager = this;
        disks = Singleton<DiskFactory>.Instance;
    }
    public void SSActionEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null)
    {
        disks.RecycleDisk(source.gameObject);
        seq.Remove(source as PhysicsEmitAction);
        source.destory = true;
        if (FirstSceneController.times >= 30)
            sceneController.flag = 1;
    }
    public void CheckEvent(SSAction source, SSActionEventType events = SSActionEventType.Completed, int intParam = 0, string strParam = null, Object objParam = null)
    {
    }
    public void Pause()
    {
        if (sceneController.flag == 0)
        {
            foreach (var k in seq)
            {
                k.speed = k.transform.GetComponent<Rigidbody>().velocity;
                k.transform.GetComponent<Rigidbody>().isKinematic = true;
            }
            sceneController.flag = 2;
        }
        else if (sceneController.flag == 2)
        {
            foreach (var k in seq)
            {
                k.transform.GetComponent<Rigidbody>().isKinematic = false;
                k.transform.GetComponent<Rigidbody>().velocity = k.speed;
            }
            sceneController.flag = 0;
        }
    }
    public void PlayDisk()
    {
        if (disks.used.Count > 0)
        {
            GameObject disk = disks.used[0];
            float x = Random.Range(-5, 5);
            disk.GetComponent<Rigidbody>().isKinematic = false;
            disk.GetComponent<Rigidbody>().velocity = new Vector3(x, 8 * (Mathf.CeilToInt(FirstSceneController.times / 10) + 1), 6);
            disk.GetComponent<Rigidbody>().AddForce(new Vector3(0,8.8f, 0),ForceMode.Force);
            PhysicsEmitAction physicsEmitAction = PhysicsEmitAction.GetSSAction();
            seq.Add(physicsEmitAction);
            this.RunAction(disk, physicsEmitAction, this);
            disks.used.RemoveAt(0);
        }
        if (Input.GetMouseButtonDown(0) && sceneController.flag == 0)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitGameObject;
            if (Physics.Raycast(ray, out hitGameObject))
            {
                GameObject gameObject = hitGameObject.collider.gameObject;
                Debug.Log(gameObject.tag);
                if (gameObject.tag == "disk")
                {
                    gameObject.transform.position=new Vector3(100,100,100);
                    userClickAction = UserClickAction.GetSSAction();
                    this.RunAction(gameObject, userClickAction, this);
                }
            }
        }
        base.Update();
    }
}

详细代码请戳传送门

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值