3D游戏编程——物理系统与碰撞

这次在场记中加了一个选项,场景中的空对象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);
        }
    }
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值