Unity-3d Day06 單例模式打地鼠


直接上代碼啦:

先搞一個老鼠類  模型一定要先構思好

using UnityEngine;
using System.Collections;

public class Mouse 
{

    //老鼠所在的位置
    public int x;
    public int y;
    //當前地鼠是否是隨機到的位置
    public bool isCurrentRandom = false;

    //記錄地鼠對象的生成時間
    public float createTime;
    //記錄地鼠對象的消失速度
    public int  level = 30;

    //求出當前的地鼠應該哪第幾張圖片
    public int Index()
    {
        if (!isCurrentRandom)
            return 0;

        int result = (int)((Time.time - createTime) * level) % 27;

        //當result = 0,1,2,3,4,5,6,7,8,9,10,11,12,13時,result不變

        //當result = 14,15,16,17,18,19,20,21,22,23,24,25,26時,
        //result   = 13,12,11,10,9 ,8 ,7 ,6 ,5 ,4 ,3 ,2 ,1

        int count = MouseManager.GetInstance ().textureList.Length;

        if (result  < count) 
        {
            isAscending = true;
        } else 
        {
            isAscending = false;
            result = (count * 2 - 1) - result;
        }


        return result;
    }
    public bool isAscending = true;


    public Mouse()
    {
        x = 0;
        y = 0;
        isCurrentRandom = false;
    }

    public Mouse(int px,int py)
    {
        x = px;
        y = py;
    }
    //重置地鼠的狀態...
    public void Reset(bool random)
    {
        createTime = Time.time;

        isCurrentRandom = random;

    }

    
}


老鼠模型的單例模式:

using UnityEngine;
using System.Collections;

public class MouseManager {
    //單例
    private MouseManager()
    {
    }

    static MouseManager s_MouseManager = null;

    public static MouseManager GetInstance()
    {
        if (null == s_MouseManager) 
        {
            s_MouseManager = new MouseManager ();
        }
        return s_MouseManager;
    }
    //老鼠運動的所有圖片
    public Texture2D[] textureList;
    //老鼠圖片的寬度
    private float texWidth = 75f;
    //老鼠圖片的高度
    private float texHeight = 75f;
    //是否有老鼠正在運動
    public bool hasMouse = false;

    //獲取一個老鼠對象應該放的位置...
    public Rect GetMouseRect(Mouse sender)
    {
        Rect rect = new Rect();

        rect.width = texWidth;
        rect.height = texHeight;

        rect.x = texWidth * sender.y;
        rect.y = texHeight * sender.x;

        return rect;
    }
    //獲取一個老鼠對象應該現實的圖片...
    public Texture2D GetMouseTexture(Mouse sender)
    {
        int index = sender.Index ();

        if (index == 1 && !sender.isAscending) 
        {
            hasMouse = false;

            sender.Reset (false);
        }

        return textureList [index];
    }

    //獲取所有老鼠對象應該擺放的區域
    public Rect GetMouseArea(int row,int column)
    {
        Rect rect = new Rect ();

        rect.x = 20f;
        rect.y = 100f;

        rect.width = column * texWidth;
        rect.height = row * texHeight;

        //Debug.Log (rect);
        return rect;
    }
}

gamemanager:

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

    //背景紋理圖片
    public Texture2D backgroundTex;

    //地鼠對象隊列
    private Mouse[] mouseList;

    //地鼠對象用的紋理隊列
    public Texture2D[] mouseTexList;
    //打中圖片
    public Texture2D[] hitTexList;

    public int level;

    //分數計算結果
    public float score;
    //計時
    private float time = 60f;


    private bool isHit;
    private int temp_index;
    private Texture2D temp_tex;
    private float temp_time = 0f;
    private int temp_count = 0;
    void OnGUI()
    {
        //添加紋理背景...
        DrawBackground ();

        //修正游戲級別
        level = level >= 30 && level <= 110 ? level : 20;
        //獲取單例對象
        MouseManager mger = MouseManager.GetInstance ();

        GUILayout.BeginArea (mger.GetMouseArea(row,column));
        //添加一個地鼠
        for (int index = 0; index < mouseList.Length; index++) {

            //修改每個地鼠對象的級別
            mouseList[index].level = level;

            Rect mouseRect = mger.GetMouseRect (mouseList [index]);
            Texture2D mouseTex = mger.GetMouseTexture (mouseList [index]);

            Mouse theMouse = mouseList[index];
            if (GUI.Button (mouseRect, mouseTex)) {
                if(theMouse.isCurrentRandom){
                    score += 10;
                    isHit = true;
                    temp_index = index;
                }
            }
        }
        GUILayout.EndArea ();
        //重新設置鼠標紋理
        ResetMouseTexture();
        if(isHit){
            //添加打中效果
            print("打中效果");
            DrawHitTexture();
        }

        //設置文字顏色
        GUI.color = Color.blue;
        //添加分數顯示label
        DrawScoreLable();
        //添加時間顯示label
        DrawTimeLable();
    }
    void DrawHitTexture(){
        MouseManager mger = MouseManager.GetInstance ();
        Rect mouseRect = mger.GetMouseRect (mouseList [temp_index]);
        Rect rect = new Rect(mouseRect.x + 5f, mouseRect.y + 85f, mouseRect.width + 30f, mouseRect.height+ 30f);
        GUI.DrawTexture(rect, temp_tex);
        HitTime();
    }
    //打擊動畫計時
    void HitTime(){
        if(Time.time - temp_time > 5*Time.deltaTime){
            temp_count++;
            temp_tex = hitTexList[temp_count];
            temp_time = Time.time;
        }
        if(temp_count == 2){
            temp_count = 0;
            isHit = false;
        }
    }
    void DrawScoreLable()
    {
        Rect rect = new Rect();
        rect.x = 0f;
        rect.y = 0f;
        rect.width = 60f;
        rect.height = 30;
        GUI.Label(rect, "分數:" + score.ToString());

    }
    void DrawTimeLable()
    {
        Rect rect = new Rect();
        rect.x = 0f;
        rect.y = 40f;
        rect.width = 60f;
        rect.height = 30;
        ShowTextTime();
        GUI.Label(rect, "時間:" + time.ToString());
    }
    //計時方法
    void ShowTextTime()
    {
        time = 60 - (int)Time.time;
    }
    void JudgeTime()
    {
        if(time == 0f){
            Time.timeScale = 0;
        }
        //每過5秒增加2的速度
        if(Time.time % 5  < Time.deltaTime){
            level += 2;
        }

    }
    private Texture2D mouseCurrentTexture;
    public Texture2D mouseRaiseTexture;
    public Texture2D mouseDownTexture; 
    void ResetMouseTexture()
    {
        Screen.showCursor = false;
        Rect cursorRect = new Rect();
        cursorRect.x = Input.mousePosition.x - 10f;
        cursorRect.y = Input.mousePosition.y + 20f;
        cursorRect.y = Screen.height - cursorRect.y;
//        cursorRect.width = mouseCurrentTexture.width;
//        cursorRect.height = mouseCurrentTexture.height;
        cursorRect.width = 75f;
        cursorRect.height = 75f;
        //繪制當前使用的鼠標紋理
        GUI.DrawTexture(cursorRect, mouseCurrentTexture);

    }
    //繪制背景用的函數方法...
    void DrawBackground()
    {
        Rect rect = new Rect (0, 0, backgroundTex.width, backgroundTex.height);

        //在rect變量的范圍內,繪制背景紋理...
        GUI.DrawTexture (rect, backgroundTex);
    }
        
    //行數
    public int row = 1;
    //列數
    public int column = 1;

    //游戲初始化方法...
    void GameInit()
    {
        row = row >= 4 && row <= 6 ? row : 4;
        column = column >= 4 && column <= 15 ? column : 4;

        //設置鼠標默認紋理  是抬起的圖片
        mouseCurrentTexture =  mouseRaiseTexture;
        //
        temp_tex = hitTexList[0];

        //將紋理圖片的地址賦值給單例對象
        MouseManager.GetInstance ().textureList = this.mouseTexList;
        MouseManager.GetInstance ().hasMouse = false;

        //通過(行數 * 列數)得到老鼠的個數
        mouseList = new Mouse[row * column];

        //通過2維數組來計算每個老鼠對象的位置...
        for (int x = 0; x < row; x++) {

            for (int y = 0; y < column; y++) {

                //創建一個老鼠對象
                Mouse m = new Mouse (x, y);

                mouseList[x * column + y] = m;
            }

        }
    }
    //隨機一個老鼠,特別厲害的老鼠
    void RandomMouse()
    {
        if (MouseManager.GetInstance ().hasMouse)
            return;

        int x = Random .Range(0, row);
        int y = Random .Range(0, column);

        int index = x * column + y;

        Mouse m = mouseList [index];

        //重置老鼠對象的值為真...
        m.Reset (true);

        //當有一個老鼠提起來之后,MouseManager的hasMouse設置為true
        MouseManager.GetInstance ().hasMouse = true;
    }

    // Use this for initialization
    void Start () 
    {
        //游戲初始化...
        GameInit ();
    }
    
    // Update is called once per frame
    void Update () 
    {
        RandomMouse ();

        if(Input.GetMouseButtonDown(0)){
            mouseCurrentTexture = mouseDownTexture;
        }
        if(Input.GetMouseButtonUp(0)){
            mouseCurrentTexture = mouseRaiseTexture;
        }
        JudgeTime();
    }
}


感覺上用了單例模式反倒麻煩了呢 可能是還沒有寫比較麻煩的游戲吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM