牧师与魔鬼——动作分离版

本文介绍了将牧师与魔鬼游戏中的动作从Director类中分离出来,采用面向对象设计的方法,包括UML图、动作序列、事件实现、资源加载和用户交互。通过CCActionManager控制游戏事件,使用具体的事件类如CCBoatMoving、CCGetOffBoat和CCGetOnTheBoat,以及回调接口和Action队列进行事件处理。学习过程中,作者意识到面向对象编程的精髓和代码复用的重要性。
摘要由CSDN通过智能技术生成

牧师与魔鬼——动作分离版


在上周的作业中,牧师与魔鬼游戏中的各个事件,都是写在Director中,并且都是继承Monobehavior的。在这周动作分离的设计中,我将上船、下船以及船的移动都分离出来。参照老师给的设计模式,画出简略的UML图如下:

面向对象设计UML图

这里写图片描述

动作序列

在这里的设计中,参照之前师兄的设计模式,可以将动作序列分成如下几个:

文件名 功能
CCActionManager 具体动作作为类中的一个对象从而管理动作,同时继承SSActionManager
CCBoatMoving 船移动的具体方法,继承SSAction,将GenGameObject作为其中一个对象
CCGetOffBoat 牧师或魔鬼对象下船的具体方法,继承SSAction,其中的对象有int型判断河岸,与GameObject型对象接收具体作用的游戏对象
CCGetOnTheBoat 牧师或魔鬼对象上船的具体方法,继承SSAction,其中的对象有GameObject型接收具体作用的游戏对象
CCActionManager

CCActionManager是对具体游戏事件的控制,其中包含三个方法:Start(),Update(),和SSActionEvent()。Start()方法中用于将SSDirector实例化,Update()方法中用于实现具体的游戏事件,SSActionEvent()方法则是接口ISSActionCallback中的。
具体代码如下:

public class CCActionManager : SSActionManager, ISSActionCallback
{
    public GenGameObject sceneController;
    public CCGetOnTheBoat getonA;
    public CCGetOffBoat getoffB;
    public CCBoatMoving boatmovingC;

    // Use this for initialization
    protected void Start()
    {
        sceneController = (GenGameObject)SSDirector.getInstance().currentScenceController;
        sceneController.actionManager = this;
    }

    // Update is called once per frame
    protected new void Update()
    {
        if (Input.GetMouseButtonDown(0) && sceneController.game == 0)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.tag == "Devil" || hit.transform.tag == "Priest")
                {
                    if (hit.collider.gameObject == sceneController.boat[0] || hit.collider.gameObject == sceneController.boat[1])
                    {
                        if (hit.collider.gameObject == sceneController.boat[0])
                        {
                            getoffB = CCGetOffBoat.GetSSAction(0);
                            this.RunAction(hit.collider.gameObject, getoffB, this);
                        }
                        else
                        {
                            getoffB = CCGetOffBoat.GetSSAction(1);
                            this.RunAction(hit.collider.gameObject, getoffB, this);
                        }
                    }
                    else
                    {
                        getonA = CCGetOnTheBoat.GetSSAction();
                        this.RunAction(hit.collider.gameObject, getonA, this);
                    }
                }
                else if (hit.transform.tag == "Boat" && sceneController.boatCapacity != 2)
                {
                    print(hit.transform.tag);
                    boatmovingC = CCBoatMoving.GetSSAction();
                    this.RunAction(hit.collider.gameObject, boatmovingC, this);
                }
            }
        }
        base.Update();
    }

    public void S
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值