3D Computer Game Programming-Note 6

1、改进飞碟(Hit UFO)游戏:

  • 游戏内容要求:
    • 按 adapter 模式设计图修改飞碟游戏
    • 使它同时支持物理运动与运动学(变换)运动

【游戏设计】

  在打飞碟(v1.0)的基础上增加飞碟的物理力学运动,并提供运动模式的切换。

【结构设计】

  adapter 模式,即适配器模式,是一种通过将一个类的接口转换成客户期望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作的设计模式。简而言之,我们的任务就是运用一个适配器来实现飞碟动作管理器不同类的接口的衔接。
  根据课程主页的 adapter 模式设计图修改程序结构如下:

Unity-物理系统与碰撞 - 图1

【编程实现】

  • IUserAction.cs: 增加对模式转换和当前模式判断的支持的接口
  1. public interface IUserAction {
  2. /* ... */
  3. bool isPhysics();
  4. void SwitchMode();
  5. }
  • FirstController.cs: 补充实现上述接口,并新增一个人机交互事件——用户可以在游戏过程中随时按下鼠标右键实现模式切换;为了使游戏难度更加自然,需要对发射飞碟的参数进行调整
  1. public class FirstController : MonoBehaviour, ISceneController, IUserAction {
  2. public IActionManager actionManager;
  3. public DiskFactory diskFactory;
  4. public ScoreRecorder scoreRecorder;
  5. private int round = 1;
  6. private int trial = 0;
  7. private bool running = false;
  8. private int count = 0;
  9. private bool isPhysicsMode = false;
  10. void Start () {
  11. SSDirector.GetInstance().CurrentSceneController = this;
  12. diskFactory = Singleton<DiskFactory>.Instance;
  13. scoreRecorder = Singleton<ScoreRecorder>.Instance;
  14. gameObject.AddComponent<FlyActionManager>();
  15. gameObject.AddComponent<DiskFlyPhysicsActionManager>();
  16. actionManager = Singleton<FlyActionManager>.Instance;
  17. gameObject.AddComponent<UserGUI>();
  18. }
  19. void Update () {
  20. if(running) {
  21. count++;
  22. //用户按下鼠标左键时进行Hit事件处理
  23. if (Input.GetButtonDown("Fire1")) {
  24. Hit(Input.mousePosition);
  25. }
  26. //用户按下鼠标右键可切换飞碟运动模式
  27. if (Input.GetMouseButtonDown(1))
  28. {
  29. this.SwitchMode();
  30. }
  31. //根据游戏轮次设计飞碟的类型和发射速率
  32. /* ... */
  33. }
  34. }
  35. private void SendDisk(int type) {
  36. GameObject disk = diskFactory.GetDisk(type);
  37. float power = 0;
  38. float angle = 0;
  39. /* 物理运动模式下 power 不宜过大 */
  40. if (type == 1) {
  41. power = isPhysicsMode ? Random.Range(1f, 2f) : Random.Range(5f, 10f);
  42. angle = Random.Range(10f, 14f);
  43. }
  44. else if (type == 2) {
  45. power = isPhysicsMode ? Random.Range(1f, 2f) : Random.Range(8f, 13f);
  46. angle = Random.Range(12f, 16f);
  47. }
  48. else {
  49. power = isPhysicsMode ? Random.Range(1f, 2f) : Random.Range(13f, 18f);
  50. angle = Random.Range(16f, 20f);
  51. }
  52. actionManager.DiskFly(disk, angle, power);
  53. }
  54. public void FreeDisk(GameObject disk) {
  55. diskFactory.FreeDisk(disk);
  56. }
  57. public bool isPhysics() {
  58. return isPhysicsMode;
  59. }
  60. public void SwitchMode() {
  61. isPhysicsMode = !isPhysicsMode;
  62. actionManager = isPhysicsMode ? Singleton<DiskFlyPhysicsActionManager>.Instance : Singleton<FlyActionManager>.Instance as IActionManager;
  63. }
  64. /* ... */
  65. }
  • DiskFlyPhysicsAction.cs: 为飞碟增加刚体属性及恒定力,通过对其施加一个垂直向下的力使其从某一随机点开始做自由落体运动
  1. public class DiskFlyPhysicsAction : SSAction {
  2. private Vector3 speed = Vector3.zero;
  3. private DiskFlyPhysicsAction() { }
  4. public override void Start() { }
  5. public static DiskFlyPhysicsAction GetSSAction(GameObject disk, float angle, float power) {
  6. ConstantForce constantForce = disk.GetComponent<ConstantForce>();
  7. if (constantForce) {
  8. constantForce.enabled = true;
  9. constantForce.force = new Vector3(0, -power, 0);
  10. }
  11. else {
  12. disk.AddComponent<Rigidbody>().useGravity = false;
  13. disk.AddComponent<ConstantForce>().force = new Vector3(0, -power, 0);
  14. }
  15. float x = Random.Range(-15f, 15f), y = Random.Range(15f, 20f), z = Random.Range(5f, 10f);
  16. disk.transform.position = new Vector3(x, y, z);
  17. DiskFlyPhysicsAction action = CreateInstance<DiskFlyPhysicsAction>();
  18. action.speed = Quaternion.Euler(new Vector3(0, 0, angle)) * Vector3.right * power;
  19. return action;
  20. }
  21. public override void Update() { }
  22. }
  • IActionManager: 以 IActionManager 为适配器,实现不同动作管理器类的接口的衔接,使飞行的动作模式能够相互切换
  1. public interface IActionManager {
  2. void DiskFly(GameObject disk, float angle, float power);
  3. }
  • DiskFlyPhysicsActionManager: 参照 FlyActionManager.cs 编写,基本一致
  1. public class DiskFlyPhysicsActionManager : SSActionManager, IActionManager, ISSActionCallback {
  2. public DiskFlyPhysicsAction flyAction;
  3. public FirstController sceneController;
  4. protected void Start() {
  5. sceneController = (FirstController)SSDirector.GetInstance().CurrentSceneController;
  6. sceneController.actionManager = this;
  7. }
  8. public void DiskFly(GameObject disk, float angle, float power) {
  9. int direction = (disk.transform.position.x > 0) ? -1 : 1;
  10. flyAction = DiskFlyPhysicsAction.GetSSAction(disk, angle, power);
  11. this.RunAction(disk, flyAction, this);
  12. }
  13. public void SSActionEvent(SSAction source,
  14. SSActionEventType events = SSActionEventType.Competeted,
  15. int intParam = 0,
  16. string strParam = null,
  17. Object objectParam = null) {
  18. sceneController.FreeDisk(source.gameobject);
  19. }
  20. }
  • UserGUI.cs: 在游戏开始界面增加“Mode”按钮,用户可以使用鼠标左键点击该按钮进行模式切换,所选模式会显示在界面左上方;在游戏进行界面左上方同样会显示所选的模式
  1. public class UserGUI : MonoBehaviour {
  2. private IUserAction userAction;
  3. private bool index = true;
  4. public string result;
  5. void OnGUI() {
  6. /* ... */
  7. if (index) {
  8. GUI.Label(new Rect(250, 80, 60, 300), "Hit UFO", style1);
  9. if (GUI.Button(new Rect(320, 200, 60, 50), "Play", style2)) {
  10. index = false;
  11. userAction.ReStart();
  12. }
  13. if (GUI.Button(new Rect(310, 250, 60, 50), "Mode", style7)) {
  14. userAction.SwitchMode();
  15. }
  16. if (userAction.isPhysics()) {
  17. GUI.Label(new Rect(20, 20, 200, 50), "PHYSICS", style6);
  18. }
  19. else {
  20. GUI.Label(new Rect(20, 20, 200, 50), "ORIGIN", style6);
  21. }
  22. }
  23. else {
  24. GUI.Label(new Rect(12, 12, 200, 50), "Round:" + userAction.GetRound(), style3);
  25. GUI.Label(new Rect(Screen.width - 90, 12, 200, 50), "Score:" + userAction.GetScore(), style3);
  26. GUI.Label(new Rect(12, 42, 200, 50), "Mode", style3);
  27. if (userAction.isPhysics()) {
  28. GUI.Label(new Rect(72, 42, 200, 50), "PHYSICS", style5);
  29. }
  30. else {
  31. GUI.Label(new Rect(72, 42, 200, 50), "ORIGIN", style5);
  32. }
  33. /* ... */
  34. }
  35. }
  36. }

【游戏效果】

Unity-物理系统与碰撞 - 图2

Unity-物理系统与碰撞 - 图3

Unity-物理系统与碰撞 - 图4

Unity-物理系统与碰撞 - 图5

【动态展示】

  🔗视频链接