这次在场记中加了一个选项,场景中的空对象GameObject添加的场记脚本中可以选择是否使用物理引擎:
下面是Adapter模式的设计。
按照老师给的这张图:
我们将使用IActionManager接口,来进行调用发射飞碟的方法,实现SceneController与CCActionManager的分离,使它们不直接交互。
我们完成了一个Interface.cs文件,里面定义了我们需要的接口:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Interfaces
{
public interface ISceneController
{
void LoadResources();
}
public interface UserAction
{
void Hit(Vector3 pos);
void Restart();
int GetScore();
bool RoundStop();
int GetRound();
}
public enum SSActionEventType : int { Started, Completed }
public interface SSActionCallback
{
void SSActionCallback(SSAction source);
}
public interface IActionManager
{
void PlayDisk(Disk disk);
bool IsAllFinished();
}
}
然后是运动控制类CCActionManager,需要实现继承的接口的方法:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Interfaces;
public class CCActionManager : SSActionManager, SSActionCallback, IActionManager
{
int count = 0;
public SSActionEventType Complete = SSActionEventType.Completed;
public void PlayDisk(Disk disk)
{
count++;
Complete = SSActionEventType.Started;
CCMoveToAction action = CCMoveToAction.getAction(disk.speed);
addAction(disk.gameObject, action, this);
}
public void SSActionCallback(SSAction source)
{
count--;
Complete = SSActionEventType.Completed;
source.gameObject.SetActive(false);
}
public bool IsAllFinished()
{
if (count == 0) return true;
else return false;
}
}
同理,要实现基于Unity物理引擎的运动控制类,使用刚体属性以及施加力来实现:
CCPhysicsActionManager类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Interfaces;
public class CCPhysicsActionManager : SSActionManager, SSActionCallback, IActionManager
{
int count = 0;
public SSActionEventType Complete = SSActionEventType.Completed;
public void PlayDisk(Disk disk)
{
count++;
Complete = SSActionEventType.Started;
CCPhysicsAction action = CCPhysicsAction.getAction(disk.speed);
addAction(disk.gameObject, action, this);
}
public void SSActionCallback(SSAction source)
{
count--;
Complete = SSActionEventType.Completed;
source.gameObject.SetActive(false);
}
public bool IsAllFinished()
{
if (count == 0) return true;
else return false;
}
}
CCPhysicsAction类:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CCPhysicsAction : SSAction
{
public float speedx;
// Use this for initialization
public override void Start()
{
if (!this.gameObject.GetComponent<Rigidbody>())
{
this.gameObject.AddComponent<Rigidbody>();
}
this.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.up * 9.8f * 0.6f, ForceMode.Acceleration);
this.gameObject.GetComponent<Rigidbody>().AddForce(new Vector3(speedx, 0, 0), ForceMode.VelocityChange);
}
private CCPhysicsAction()
{
}
public static CCPhysicsAction getAction(float speedx)
{
CCPhysicsAction action = CreateInstance<CCPhysicsAction>();
action.speedx = speedx;
return action;
}
// Update is called once per frame
override public void Update()
{
if (transform.position.y <= 3)
{
Destroy(this.gameObject.GetComponent<Rigidbody>());
destroy = true;
CallBack.SSActionCallback(this);
}
}
}
运行结果: