打飞碟物理运动兼容版

上次我们做了一个打飞碟的游戏,当时的打飞碟是运动学的模式,即并没有像现实生活中的一样受到重力,摩擦力等因素的影响。这次我们将为我们的打飞碟游戏添加物理运动,即让我们的飞碟受到物理因素的影响,

为了能重用我们上次的打飞碟的代码,我们这次使用Adpater的模式适配器模式
这里写图片描述

代码

这周的代码和上周的代码,大致一样,只不过是为了使用适配器模式,加了一个IActionMananager的结构

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface IActionManager {

    int getDiskNum();
    void StartThrow(Queue<GameObject> disks);
    void setDiskNum(int num);
}

还有CCPhysicsManager.cs, 重写了SSActionManger一些方法

using Com.Action;
using Com.MyGame;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CCPhysicsManager : SSActionManager, ISSActionCallback, IActionManager
{


    public int diskNum;
    public FirstController scene;
    float speed = 20f;
    CCFlyActionFactory flyActionFactory;
    void ISSActionCallback.SSActionEvent(SSAction source, SSActionEventType events, int intParam, string strParam, Object objectParam)
    {
        if (source is CCFlyAction)
        {
            diskNum--;
            DiskFactory df = Singleton<DiskFactory>.Instance;
            df.FreeDisk(source.gameObject);
            flyActionFactory.FreeAction((CCFlyAction)source);
        }
    }

    void Start()
    {
        scene = Director.getInstance().currentSceneController as FirstController;
        scene.actionManger = this;
        flyActionFactory = Singleton<CCFlyActionFactory>.Instance;
    }
    //重写一下SSActionManager的方法, 让其执行Action的FixedUpdate方法
    protected void Update()
    {
        foreach (SSAction ac in waitingAdd)
        {
            actions[ac.GetInstanceID()] = ac;
        }
        waitingAdd.Clear();

        foreach (KeyValuePair<int, SSAction> kv in actions)
        {
            SSAction ac = kv.Value;
            if (ac.distroy)
            {

                waitingDelete.Add(ac.GetInstanceID());
            }
            else if (ac.enable)
            {
                ac.FixedUpdate();
            }
        }
        foreach (int key in waitingDelete)
        {
            SSAction ac = actions[key];
            actions.Remove(key);
            DestroyObject(ac);
        }
        waitingDelete.Clear();
    }

    public void RunAction(GameObject gameobject, SSAction action, ISSActionCallback manager)
    {
        action.gameObject = gameobject;
        if(!action.gameObject.GetComponent<Rigidbody>())
            action.gameObject.AddComponent<Rigidbody>();
        action.transform = gameobject.transform;
        action.callback = manager;
        waitingAdd.Add(action);
        action.Start();
    }
    public void StartThrow(Queue<GameObject> disks)
    {
        foreach (GameObject disk in disks)
        {
            RunAction(disk, flyActionFactory.GetAction(), (ISSActionCallback)this);
        }
    }

    public int getDiskNum()
    {
        return diskNum;
    }

    public void setDiskNum(int num)
    {
        diskNum = num;
    }
}

对UserGUI进行一些修改

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Com.MyGame;

public class UserGUI : MonoBehaviour
{
    public FirstController scene;
    public int status = 0;
    public bool isFirst = true;
    GUIStyle style;
    GUIStyle buttonStyle;
    float time = 60;
    float second = 0;
    bool isSelect = false;
    void Start()
    {
        //scene = Director.getInstance().currentSceneController as FirstController;
        scene = Director.getInstance().currentSceneController as FirstController;
        style = new GUIStyle();
        style.fontSize = 40;
        style.normal.textColor = Color.red;
        style.alignment = TextAnchor.MiddleCenter;

        buttonStyle = new GUIStyle("button");
        buttonStyle.fontSize = 25;

    }
    private void Update()
    {
        //scene.click();

    }
    void OnGUI() {

        int buttonX = Screen.width / 2 - 45;
        int buttonY = Screen.height / 2 - 45;
        int labelX = Screen.width / 2 - 45;
        int labelY = Screen.height;
        if (scene.GetRound() == 2 && scene.actionManger.getDiskNum() == 0)
        {
            //Debug.Log(2);
            GUI.Button(new Rect(buttonX, buttonY, 90, 90), "GameOver");
            return;
        }
        if (!isSelect)
        {


            if (GUI.Button(new Rect(buttonX- 45, buttonY, 90, 90), "PHYSICS"))
            {
                scene.setMode(1);
                isSelect = true;
            }
            if (GUI.Button(new Rect(buttonX + 90, buttonY, 90, 90), "CCFLY")) {
                scene.setMode(0);
                isSelect = true;
            }    
            return;
        }
        scene.hit();
        //GUI.Label(new Rect(buttonX, buttonY, 90, 90), "GameOver");
        GUI.Label(new Rect(1000, 0, 400, 400), scene.getScore().ToString(),style);

        if (scene.GetGameState() == GameState.RUNNING && GUI.Button(new Rect(10, 10,90,90 ), "Pause",buttonStyle) )
        {
            scene.SetGameState(GameState.PAUSE);
            Time.timeScale = 0;
        }
        if (scene.GetGameState() == GameState.PAUSE && GUI.Button(new Rect(10, 10, 90,90), "Run", buttonStyle))
        {
            scene.SetGameState(GameState.RUNNING);
            Time.timeScale = 1;
        }

        if (scene.GetRound() == -1 && GUI.Button(new Rect(buttonX, buttonY, 90, 90), "Start")) {
            scene.SetGameState(GameState.ROUND_START);
        }

        if (scene.GetGameState() == GameState.ROUND_FINISH  && scene.GetRound() < 2 && GUI.Button(new Rect(buttonX, buttonY, 90, 90), "Next Round"))
        {
        scene.SetGameState(GameState.ROUND_START);
        }
    }
}

完整项目源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值