本次实现效果图如下
Unity中物理引擎
物理引擎是一种软件组件,用于仿真物理世界运动。这种仿真包括刚体力学、流体力学以及碰撞检测。
物理引擎通过为刚性物体赋予真实的物理属性,在外部力的作用下,计算运动、旋转和碰撞
Unity中的常见物理组件:
- Rigidbody 刚体组件
- 物体运动控制属性
- Collider 碰撞器
- 物体碰撞与碰撞检测属性
- Mesh 网格
- 物体形状属性
- Joint 连接器
Unity中物体发生碰撞的条件
Static Colider 静态碰撞器 | Rigibody Colider 刚体碰撞器 | Kinematic Rigibody Colider 运动学碰撞器 | |
---|---|---|---|
Static Colider 静态碰撞器 | Y | ||
Rigibody Colider 刚体碰撞器 | Y | Y | Y |
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();
}
}
详细代码请戳传送门