5Unity-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();
    }
}

感觉上用了单例模式反倒麻烦了呢 可能是还没有写比较麻烦的游戏吧

时间: 2024-08-11 03:28:57

5Unity-3d Day06 单例模式打地鼠的相关文章

屏幕坐标和世界坐标的转换+对象池技术(3D打地鼠小游戏)

游戏中可能经常会遇到需要某个物体跟着鼠标移动,然后又需要把物体放在某个鼠标指定的位置 实现方式 Camera.main.WorldToScreenPoint Camera.main.ScreenToWorldPoint 3D打地鼠实例 我这里用到的素材都比较简陋,几乎全是用Unity做的 首先是锤子 就是两个Cylinder,在把手的位置放一个空物体用于模拟锤子的动作,命名为Hammer,把锤子作为Hammer的子物体,给Hammer添加Animation动画: 在三个关键帧位置设置Hammer

轻装上阵,安卓工程师之路---day06(HTTP &amp; Servlet)

01 HTTP协议入门 HTTP协议是Web客户端和Web服务端通信的规则,用于定义客户端与web服务器通迅的格式,它是一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的过程. HTTP协议分类二个版本: 1_HTTP1.0版本 客户端请求服务器后,服务器响应信息后,立即断开,且只能请求和响应一个资源 缺点:客户端创建连接不容易,需要消耗大量的时间和资源,这时服务器就只响应一个资源就断开,下次客户端再请求,又要创建创建新的连接 2_HTTP1.1版本 客户端请求服务器后,服务器响应

网页3D引擎“Babylon.JS”入门教程翻译总结

使用三个月的业余时间把官方教程的入门部分译为中文并上传到github,在下一步编程前做一个总结. 历程: 最早接触游戏编程是在大三下学期,用汇编语言和实验室里的单片机.触摸屏.电机(提供声效)编的打地鼠程序.后来因为大四的毕业设计与三维空间模拟有关,又自学了MFC编程和NEHE的OpenGL教程(国人翻译版)(C++3D编程的复杂会给任何参与者留下深刻的印象,向先驱者们致敬).工作后自学了JavaScript语言和前端知识,感觉js与C++相比极其简单易用,如果能以js代替C++进行3D开发可以

使用Cocos开发一款简单的3D VR抓钱游戏

使用Cocos开发一款简单的3D VR抓钱游戏 最近VR成为了一个新兴的热点,很多以前从事游戏开发的团队都在关注这个方向.如何在VR时代来临之际快速的掌握开发VR游戏的方法,这对于很多中小团队来说,是一个要考虑的问题. 目前市面上有很多3D引擎已经开始支持VR功能.特别是虚幻,Unity等引擎对于VR这个领域都非常重视,但是国内这几年有大量的手游团队在使用Cocos2d-x来开发游戏项目,现有人员的的经验对于游戏开发非常宝贵,更何况目前VR领域并未出现很好的CP盈利案例.短期内贸然转型,放弃现有

基础学习day06---面向对象二---static,类的初始化和调用顺序、单例模式

一.static关键字 1.1.static关键字 静态:static用法:是一个修饰符,用于修饰成员(成员变量,成员函数)static 修饰的内容,所有对象共享当成员被静态修饰后,就多了一个调用方式,除了可以被对象调用外,还可以被类名.静态成员 package com.day06; /** * 人类 * @author Administrator *静态:static *用法:是一个修饰符,用于修饰成员(成员变量,成员函数) *static 修饰的内容,所有对象共享 *当成员被静态修饰后,就多

Java设计模式学习笔记,一:单例模式

开始学习Java的设计模式,因为做了很多年C语言,所以语言基础的学习很快,但是面向过程向面向对象的编程思想的转变还是需要耗费很多的代码量的.所有希望通过设计模式的学习,能更深入的学习. 把学习过程中的笔记,记录下来,只记干货. 第一部分:单例模式的内容 单例模式:类只能有一个实例. 类的特点:1.私有构造器:2.内部构造实例对象:3.对外提供获取唯一实例的public方法. 常见的单例模式实现有五种形式: 1.饿汉式. 2.懒汉式. 3.双重检查锁式. 4.静态内部类式. 5.枚举式. 以下分别

HTML5 CSS3 诱人的实例: 3D立方体旋转动画

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/34120047 创意来自:http://www.html5tricks.com/demo/html5-3d-cube/index.html , 同学给我发的样例,感觉非常不错,只是实在想不出来实际的用处.可是效果非常炫~ 效果图: 知识点: 1.perspective ,transform 的复习 2.css3 backgroud实现格格背景.即面上的小格格 3. @-webki

【python之路33】开发模式单例模式

1.单例模式指的是创建单个实例,例如:数据库连接池中包含10个数据库连接,用户访问数据时随机从连接池中拿出一个进行连接,其他用户再次访问时不再创建对象进行连接 #!usr/bin/env python # -*- coding:utf-8 -*- class ConnecttionPool: __instance = None def __init__(self): self.ip = '222.133.177.67' self.port = '3306' self.username = 'od

.Net 单例模式(Singleton)

每台计算机可以有若干个打印机,但只能有一个Printer Spooler, 以避免两个打印作业同时输出到打印机中.每台计算机可以有若干传真卡,但是只应该有一个软件负责管理传真卡,以避免出现两份传真作业同时传到传真卡中的情况.每台计算机可以有若干通信端口,系统应当集中管理这些通信端口,以避免一个通信端口同时被两个请求同时调用. 问题描述: 单例模式 Singleton Pattern 问题解决: (1)单例模式简介: Singleton模式要求一个类有且仅有一个实例,并且提供了一个全局的访问点.这