项目:

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {

     public class Constant : MonoBehaviour {

         public static string m_plateTag = "Plate";  //碟标签
         public static string m_ingredientTag = "Ingredient";    //原材料标签
         public static string m_trashBinTag = "TrashBin";    //垃圾桶标签
         public static string m_customerTag = "Customer";    //客户标签

         public static string m_dragTag = "Drag";    //拖动的标签
         public static string m_dragSortingLayer = "Drag";   //拖动的排序层

         public static string m_uiSortingLayer = "UI";   //UI的排序层

         ;   //在盘子中的原材料上限
     }
 }

Constant

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {

     public class Customer : MonoBehaviour {

         public Product m_product;  //客户需要的产品

         public AudioClip m_deliverySuccessAudioClip; //交付成功音乐
         public AudioClip m_deliveryFailedAudioClip;  //交付失败音乐

         private GameManager m_gameManager;
         private SpriteRenderer m_customerSpriteRenderer;    //客户精灵渲染器
         private SpriteRenderer m_productSpriteRenderer; //产品精灵渲染器
         private SpriteRenderer[] m_ingredientSpriteRenderers;  //原料精灵渲染器

         private AudioSource m_audioSource;  //音乐

         void Start() {
             m_gameManager = GameManager.m_instance;
             m_audioSource = GetComponent<AudioSource>();
             InitProduct();
             LoadProductAndIngredientsImage();
         }

         //初始化产品
         void InitProduct() {
             m_product = new Product();
             m_product.m_productID = Random.Range(,);
             m_product.m_productPrice = Random.Range(,);

             ,);
             m_product.m_totalIngredients = new int[ingredientCount];
             ;i < ingredientCount;i++) {
                 m_product.m_totalIngredients[i] = Random.Range(,);
             }
         }

         //加载产品和原料图片
         void LoadProductAndIngredientsImage() {
             m_ingredientSpriteRenderers = new SpriteRenderer[m_product.m_totalIngredients.Length];

             SpriteRenderer[] spriteRenderers = transform.GetComponentsInChildren<SpriteRenderer>();

             m_customerSpriteRenderer = spriteRenderers[];

             m_productSpriteRenderer = spriteRenderers[];
             m_productSpriteRenderer.sprite = m_gameManager.m_productSprites[m_product.m_productID - ];

             ;i < m_product.m_totalIngredients.Length;i++) {
                 m_ingredientSpriteRenderers[i] = spriteRenderers[i + ];
             }

             ;i < spriteRenderers.Length;i++) {
                 spriteRenderers[i].sprite = null;
             }

             ;i < m_ingredientSpriteRenderers.Length;i++) {
                 m_ingredientSpriteRenderers[i].sprite = m_gameManager.m_ingredientSprites[m_product.m_totalIngredients[i]];
                 m_ingredientSpriteRenderers[i].transform.localScale = );
             }
         }

         void OnTriggerEnter2D(Collider2D other) {
             //盘子移动到客户上
             if(other.CompareTag(Constant.m_plateTag)) {
                 m_gameManager.m_dragPlateArrivedCustomer = true;

                 Color newColor = m_customerSpriteRenderer.material.color;
                 newColor.a = 0.5f;
                 m_customerSpriteRenderer.material.color = newColor;
             }
         }

         void OnTriggerExit2D(Collider2D other) {
             //盘子离开客户
             if(other.CompareTag(Constant.m_plateTag) && m_gameManager.m_dragPlateArrivedCustomer) {
                 m_gameManager.m_dragPlateArrivedCustomer = false;

                 Color newColor = m_customerSpriteRenderer.material.color;
                 newColor.a = 1f;
                 m_customerSpriteRenderer.material.color = newColor;
             }
         }

         //客户检查原料是否正确
         public void CheckPlate() {
             //获取盘子里的原料列表
             Ingredient[] ingredients = FindObjectOfType<Plate>().GetComponentsInChildren<Ingredient>();
             bool isMatch = true;

             if(ingredients.Length == m_product.m_totalIngredients.Length) {
                 ;i < ingredients.Length;i++) {
                     ;j < m_product.m_totalIngredients.Length;j++) {
                         if(ingredients[i].m_ingredientID == m_product.m_totalIngredients[j]) {
                             Debug.Log();
                             break;
                         } ) {
                             isMatch = false;
                         }
                     }
                     if(!isMatch) {
                         break;
                     }
                 }
             } else {
                 isMatch = false;
             }

             if(isMatch) {
                 if(!m_audioSource.isPlaying) {
                     m_audioSource.clip = m_deliverySuccessAudioClip;
                     m_audioSource.Play();
                 }
             } else {
                 if(!m_audioSource.isPlaying) {
                     m_audioSource.clip = m_deliveryFailedAudioClip;
                     m_audioSource.Play();
                 }
             }
             InitProduct();
             LoadProductAndIngredientsImage();
             ;i >= ;i--) {
                 Destroy(m_gameManager.m_ingredientsInPlate[i]);
                 m_gameManager.m_ingredientsInPlate.RemoveAt(i);
             }
         }
     }
 }

Customer

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

 namespace VoidGame {

     public class GameManager : MonoBehaviour {

         public static GameManager m_instance;   //单例

         public GameObject m_ingredientBackgroundPrefab; //原料背景预设
         public List<GameObject> m_ingredients;  //原料预设列表

         public List<Sprite> m_productSprites;   //产品精灵列表
         public List<Sprite> m_ingredientSprites;    //原料精灵列表

         [HideInInspector]
         public GameObject m_dragIngredient; //拖动中的原材料
         [HideInInspector]
         public bool m_dragIngredientArrived = false;    //原材料是否到达碟内

         [HideInInspector]
         public bool m_dragPlateArrivedTrashBin = false;  //盘子是否到达垃圾桶
         [HideInInspector]
         public bool m_dragPlateArrivedCustomer = false; //盘子是否到达客户

         [HideInInspector]
         public List<GameObject> m_ingredientsInPlate = new List<GameObject>();  //在盘子中的原材料

         private void Awake() {
             m_instance = this;
         }

         private void Start() {
             GenerateIngredientBackground();
         }

         //生成原料背景和原料
         private void GenerateIngredientBackground() {
             Transform ingredientParentTransform = transform.Find("/Ingredients");
             Transform ingredientBackgroundParentTransform = transform.Find("/Background/IngredientsBackground");
             Vector3 newPosition = ingredientBackgroundParentTransform.position;
             newPosition.y = ingredientBackgroundParentTransform.position.y + ;

             ;i < ;i++) {
                 newPosition.x = ingredientBackgroundParentTransform.position.x - ;
                 newPosition.y--;
                 ;j < ;j++) {
                     newPosition.x++;
                     GameObject ingredientBackground = GameObject.Instantiate(m_ingredientBackgroundPrefab,newPosition,Quaternion.identity,ingredientBackgroundParentTransform) as GameObject;
                     GameObject.Instantiate(m_ingredients[i *  + j],newPosition,Quaternion.identity,ingredientParentTransform);
                 }
             }
         }
     }
 }

GameManager

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {

     public class Ingredient : MonoBehaviour {

         public int m_ingredientID;  //原料ID

         private AudioSource m_audioSource;
         private GameManager m_gameManager;

         private void Start() {
             m_gameManager = GameManager.m_instance;
             m_audioSource = GetComponent<AudioSource>();
         }

         protected void Update() {
             if(m_gameManager.m_dragIngredient != null) {
                 FollowCursor();
             }
         }

         //点击原材料,创建一个相同的可拖动的原材料
         private void OnMouseDown() {
             if(m_gameManager.m_ingredientsInPlate.Count < Constant.m_maxIngredientsInPlate) {
                 CreateDragIngredient();
             }
         }

         //抬起鼠标时原料在盘子里,将原料设置为盘子的子对象.
         //抬起鼠标时原料不在盘子里,删除这个原料
         private void OnMouseUp() {
             if(m_gameManager.m_dragIngredient != null && m_gameManager.m_dragIngredientArrived) {
                 GameObject plate = GameObject.FindGameObjectWithTag(Constant.m_plateTag);
                 m_gameManager.m_dragIngredient.transform.SetParent(plate.transform);
                 m_gameManager.m_dragIngredient.transform.position = plate.transform.position;
                 m_gameManager.m_dragIngredient.GetComponent<BoxCollider2D>().enabled = false;

                 m_gameManager.m_dragIngredient.transform.localScale = );
                 m_gameManager.m_ingredientsInPlate.Add(m_gameManager.m_dragIngredient);
                 m_gameManager.m_dragIngredient = null;
                 m_gameManager.m_dragIngredientArrived = false;

             } else {
                 Destroy(m_gameManager.m_dragIngredient);
                 m_gameManager.m_dragIngredient = null;
             }

         }

         //创建可拖动的原材料
         private void CreateDragIngredient() {
             Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             newPosition.z = ;
             m_gameManager.m_dragIngredient = Instantiate(gameObject,newPosition,Quaternion.identity) as GameObject;
             m_gameManager.m_dragIngredient.GetComponent<SpriteRenderer>().sortingLayerName = Constant.m_dragSortingLayer;
             //m_gameManager.m_dragIngredient.GetComponent<SpriteRenderer>().tag = Constant.m_dragTag;
             if(!m_audioSource.isPlaying) {
                 m_audioSource.Play();
             }
         }

         //原材料跟随鼠标
         private void FollowCursor() {
             Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             newPosition.z = ;
             m_gameManager.m_dragIngredient.transform.position = newPosition;
         }
     }
 }

Ingredient

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {

     public class Plate : MonoBehaviour {

         private Vector3 m_originPosition;   //原始位置

         private GameManager m_gameManager;
         private SpriteRenderer m_spriteRenderer;    

         private void Start() {
             m_gameManager = GameManager.m_instance;
             m_spriteRenderer = GetComponent<SpriteRenderer>();
             m_originPosition = transform.position;
         }

         private void OnTriggerEnter2D(Collider2D other) {
             //原料移动到盘子上
             if(other.CompareTag(Constant.m_ingredientTag)) {
                 m_gameManager.m_dragIngredientArrived = true;
             //盘子移动到垃圾桶上
             } else if(other.CompareTag(Constant.m_trashBinTag)) {
                 Color newColor = m_spriteRenderer.material.color;
                 newColor.a = 0.5f;
                 m_spriteRenderer.material.color = newColor;
             //盘子移动到客户身上
             } else if(other.CompareTag(Constant.m_customerTag)) {
                 Color newColor = m_spriteRenderer.material.color;
                 newColor.a = 0.5f;
                 m_spriteRenderer.material.color = newColor;
             }
         }

         private void OnTriggerExit2D(Collider2D other) {
             //原料离开盘子
             if(other.CompareTag(Constant.m_ingredientTag) && m_gameManager.m_dragIngredientArrived) {
                 m_gameManager.m_dragIngredientArrived = false;
             //盘子离开垃圾桶
             } else if(other.CompareTag(Constant.m_trashBinTag)) {
                 Color newColor = m_spriteRenderer.material.color;
                 newColor.a = 1f;
                 m_spriteRenderer.material.color = newColor;
             //盘子离开客户
             } else if(other.CompareTag(Constant.m_customerTag)) {
                 Color newColor = m_spriteRenderer.material.color;
                 newColor.a = 1f;
                 m_spriteRenderer.material.color = newColor;
             }
         }

         //如果盘子里有原料,修改排序层.
         private void OnMouseDown() {
             ) {
                 m_spriteRenderer.sortingLayerName = Constant.m_dragSortingLayer;
             }
         }

         //如果盘子里有原料,可以拖动.如果盘子里没材料,不可以拖动
         private void OnMouseDrag() {
             ) {
                 Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                 newPosition.z = ;
                 transform.position = newPosition;
             }
         }

         //如果盘子在垃圾桶里,删除盘子里的原料
         private void OnMouseUp() {
             if(m_gameManager.m_dragPlateArrivedTrashBin) {
                 TrashBin trashBin = FindObjectOfType<TrashBin>();
                 trashBin.ClearIngredientInPlate();
             } else if(m_gameManager.m_dragPlateArrivedCustomer) {
                 Customer customer = FindObjectOfType<Customer>();
                 customer.CheckPlate();
             }
             transform.position = m_originPosition;
             m_spriteRenderer.sortingLayerName = Constant.m_uiSortingLayer;
         }
     }
 }

Plate

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {

     [System.Serializable]
     public class Product {

         public int m_productID; //产品ID
         public int m_productPrice;  //产品价格
         public int[] m_totalIngredients;  //产品需要的原料ID

     }
 }

Product

 using UnityEngine;
 using System.Collections;

 namespace VoidGame {

     public class TrashBin : MonoBehaviour {

         private GameManager m_gameManager;
         private GameObject m_plate; //盘子

         private AudioSource m_audioSource;
         private SpriteRenderer m_spriteRenderer;

         private void Start() {
             m_gameManager = GameManager.m_instance;
             m_audioSource = GetComponent<AudioSource>();
             m_spriteRenderer = GetComponent<SpriteRenderer>();
             m_plate = GameObject.FindGameObjectWithTag(Constant.m_plateTag);
         }

         //盘子移动到垃圾桶上
         private void OnTriggerEnter2D(Collider2D other) {
             if(other.CompareTag(Constant.m_plateTag)) {
                 m_gameManager.m_dragPlateArrivedTrashBin = true;

                 Color newColor = m_spriteRenderer.material.color;
                 newColor.a = 0.5f;
                 m_spriteRenderer.material.color = newColor;
             }
         }

         //盘子离开垃圾桶
         private void OnTriggerExit2D(Collider2D other) {
             if(other.CompareTag(Constant.m_plateTag) && m_gameManager.m_dragPlateArrivedTrashBin) {
                 m_gameManager.m_dragPlateArrivedTrashBin = false;

                 Color newColor = m_spriteRenderer.material.color;
                 newColor.a = 1f;
                 m_spriteRenderer.material.color = newColor;
             }
         }

         //清除盘子里的原材料
         public void ClearIngredientInPlate() {
             ;i >= ;i--) {
                 Destroy(m_gameManager.m_ingredientsInPlate[i]);
                 m_gameManager.m_ingredientsInPlate.RemoveAt(i);
             }
             if(!m_audioSource.isPlaying) {
                 m_audioSource.Play();
             }
         }
     }
 }

TrashBin

Restaurant & Cooking Starter Kit v1.2.1的更多相关文章

  1. Restaurant & Cooking Starter Kit v1.2.1 学习

    项目: using UnityEngine; using System.Collections; namespace VoidGame { public class Constant : MonoBe ...

  2. Microsoft IoT Starter Kit 开发初体验

    1. 引子 今年6月底,在上海举办的中国国际物联网大会上,微软中国面向中国物联网社区推出了Microsoft IoT Starter Kit ,并且免费开放1000套的申请.申请地址为:http:// ...

  3. asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料

    asp.net的3个经典范例(ASP.NET Starter Kit ,Duwamish,NET Pet Shop)学习资料 NET Pet Shop .NET Pet Shop是一个电子商务的实例, ...

  4. Microsoft IoT Starter Kit 开发初体验-反馈控制与数据存储

    在上一篇文章<Microsoft IoT Starter Kit 开发初体验>中,讲述了微软中国发布的Microsoft IoT Starter Kit所包含的硬件介绍.开发环境搭建.硬件 ...

  5. React Starter Kit 中文文档

    最近没事又翻译了个玩意. Github上的一个Star 非常高的 React 样板程序. 由Node.js,Express,GraphQL和React构建,可选加入Redux等,并可以包含Webpac ...

  6. Window 64bit环境搭建Web Starter Kit

    最近在学习https://developers.google.com/web/fundamentals/这里的内容,其中就有一部分是安装Web Starter Kit的教程,我总结一下自己的安装过程. ...

  7. Unity Game Starter Kit for Windows Store and Windows Phone Store games

    原地址:http://digitalerr0r.wordpress.com/2013/09/30/unity-game-starter-kit-for-windows-store-and-window ...

  8. Microsoft IoT Starter Kit

    Microsoft IoT Starter Kit 开发初体验 1. 引子 今年6月底,在上海举办的中国国际物联网大会上,微软中国面向中国物联网社区推出了Microsoft IoT Starter K ...

  9. 介绍使用Cordova和Web Starter Kit开发Android

    介绍 如今,每个人都想制作移动应用程序,为什么不呢?世界上有更多的移动设备比任何其他用户设备.Android尤其流行,但是为什么不从一个众所周知的跨平台应用的基础开始呢?Android的开发显然比其他 ...

随机推荐

  1. iOS 指南针的制作 附带源码

    iOS  指南针的制作  附带源码 代码下载地址: http://vdisk.weibo.com/s/HK4yE   http://pan.baidu.com/share/link?shareid=7 ...

  2. Android 权限管理

    从 Android 6.0(API 级别 23)开始,用户开始在应用运行时向其授予权限,而不是在应用安装时授予.此方法可以简化应用安装过程,因为用户在安装或更新应用时不需要授予权限.它还让用户可以对应 ...

  3. 【Zookeeper】源码分析之持久化--FileSnap

    一.前言 前篇博文已经分析了FileTxnLog的源码,现在接着分析持久化中的FileSnap,其主要提供了快照相应的接口. 二.SnapShot源码分析 SnapShot是FileTxnLog的父类 ...

  4. 读书笔记:《HTML5开发手册》Web表单

    这是补充HTML5基础知识的第五篇内容,其他为: 一.HTML5-- 新的结构元素 二.HTML5-- figure.time.details.mark 三.HTML5-- details活学活用 四 ...

  5. RocketMQ源码 — 二、 NameServer

    NameServer 作用:Producer和Consumer获取Broker的地址 目的:解耦Broker和Producer.Consumer 原理:使用netty作为通信工具,监听指定端口,如果是 ...

  6. Centos 7 意外断电如何处理

    拔U盘的时候,不小心碰到了主机上的开机键-- 还好默认的响应动作是睡眠-- 还不知道 CentOS 怎么样应对意外断电呢?!

  7. SSH 一些错误的解决办法

    1.主动访问的机器需要创建私钥和公钥 (client) #cd ~#mkdir .ssh#chmod 700 .ssh#cd .ssh#ssh-keygen -t rsa //一路回车,各种提示按默认 ...

  8. MySql开启远程访问(Linux)

    Linux服务器上安装了MySql数据库服务器之后,在远程访问出现了61错误.经检查后,发现需要在MySql配置文件中取消绑定IP.具体做法如下: 打开my.cnf配置文件.连接到服务器之后,在终端中 ...

  9. 文档在线预览开源实现方案二:OpenOffice + pdf.js

    文档在线预览的另一种实现方式采用的技术栈是OpenOffice + pdf.js, office文档转换为pdf的工作依然由OpenOffice的服务来完成,pdf生成后我们不再将其转换为其他文件而是 ...

  10. Android抽屉效果 DrawerLayout 入门经验总结

    今天试了试这个抽屉布局的效果,结果很崩溃无语 网上很多资料都千篇一律,感觉都有问题,下面总结下几点经验: 先上个效果图: 1.  layout 布局文件中怎么写: <android.suppor ...