首页 > 代码库 > Restaurant & Cooking Starter Kit v1.2.1

Restaurant & Cooking Starter Kit v1.2.1

技术分享

项目:

技术分享
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 namespace VoidGame {
 5 
 6     public class Constant : MonoBehaviour {
 7 
 8         public static string m_plateTag = "Plate";  //碟标签
 9         public static string m_ingredientTag = "Ingredient";    //原材料标签
10         public static string m_trashBinTag = "TrashBin";    //垃圾桶标签
11         public static string m_customerTag = "Customer";    //客户标签
12 
13         public static string m_dragTag = "Drag";    //拖动的标签
14         public static string m_dragSortingLayer = "Drag";   //拖动的排序层
15 
16         public static string m_uiSortingLayer = "UI";   //UI的排序层
17 
18         public static int m_maxIngredientsInPlate = 6;   //在盘子中的原材料上限
19     }
20 }
Constant
技术分享
  1 using UnityEngine;
  2 using System.Collections;
  3 
  4 namespace VoidGame {
  5 
  6     public class Customer : MonoBehaviour {
  7 
  8         public Product m_product;  //客户需要的产品
  9 
 10         public AudioClip m_deliverySuccessAudioClip; //交付成功音乐
 11         public AudioClip m_deliveryFailedAudioClip;  //交付失败音乐
 12 
 13         private GameManager m_gameManager;
 14         private SpriteRenderer m_customerSpriteRenderer;    //客户精灵渲染器
 15         private SpriteRenderer m_productSpriteRenderer; //产品精灵渲染器
 16         private SpriteRenderer[] m_ingredientSpriteRenderers;  //原料精灵渲染器
 17 
 18         private AudioSource m_audioSource;  //音乐
 19 
 20         void Start() {
 21             m_gameManager = GameManager.m_instance;
 22             m_audioSource = GetComponent<AudioSource>();
 23             InitProduct();
 24             LoadProductAndIngredientsImage();
 25         }
 26 
 27 
 28         //初始化产品
 29         void InitProduct() {
 30             m_product = new Product();
 31             m_product.m_productID = Random.Range(1,25);
 32             m_product.m_productPrice = Random.Range(0,201);
 33 
 34             int ingredientCount = Random.Range(1,7);
 35             m_product.m_totalIngredients = new int[ingredientCount];
 36             for(int i = 0;i < ingredientCount;i++) {
 37                 m_product.m_totalIngredients[i] = Random.Range(0,15);
 38             }
 39         }
 40 
 41         //加载产品和原料图片
 42         void LoadProductAndIngredientsImage() {
 43             m_ingredientSpriteRenderers = new SpriteRenderer[m_product.m_totalIngredients.Length];
 44 
 45             SpriteRenderer[] spriteRenderers = transform.GetComponentsInChildren<SpriteRenderer>();
 46 
 47             m_customerSpriteRenderer = spriteRenderers[0];
 48 
 49             m_productSpriteRenderer = spriteRenderers[2];
 50             m_productSpriteRenderer.sprite = m_gameManager.m_productSprites[m_product.m_productID - 1];
 51 
 52             for(int i = 0;i < m_product.m_totalIngredients.Length;i++) {
 53                 m_ingredientSpriteRenderers[i] = spriteRenderers[i + 3];
 54             }
 55 
 56             for(int i = 3;i < spriteRenderers.Length;i++) {
 57                 spriteRenderers[i].sprite = null;
 58             }
 59             
 60 
 61             for(int i = 0;i < m_ingredientSpriteRenderers.Length;i++) {
 62                 m_ingredientSpriteRenderers[i].sprite = m_gameManager.m_ingredientSprites[m_product.m_totalIngredients[i]];
 63                 m_ingredientSpriteRenderers[i].transform.localScale = new Vector3(0.15f,0.15f,0);
 64             }
 65         }
 66 
 67         void OnTriggerEnter2D(Collider2D other) {
 68             //盘子移动到客户上
 69             if(other.CompareTag(Constant.m_plateTag)) {
 70                 m_gameManager.m_dragPlateArrivedCustomer = true;
 71 
 72                 Color newColor = m_customerSpriteRenderer.material.color;
 73                 newColor.a = 0.5f;
 74                 m_customerSpriteRenderer.material.color = newColor;
 75             }
 76         }
 77 
 78         void OnTriggerExit2D(Collider2D other) {
 79             //盘子离开客户
 80             if(other.CompareTag(Constant.m_plateTag) && m_gameManager.m_dragPlateArrivedCustomer) {
 81                 m_gameManager.m_dragPlateArrivedCustomer = false;
 82 
 83                 Color newColor = m_customerSpriteRenderer.material.color;
 84                 newColor.a = 1f;
 85                 m_customerSpriteRenderer.material.color = newColor;
 86             }
 87         }
 88 
 89         //客户检查原料是否正确
 90         public void CheckPlate() {
 91             //获取盘子里的原料列表
 92             Ingredient[] ingredients = FindObjectOfType<Plate>().GetComponentsInChildren<Ingredient>();
 93             bool isMatch = true;
 94 
 95             if(ingredients.Length == m_product.m_totalIngredients.Length) {
 96                 for(int i = 0;i < ingredients.Length;i++) {
 97                     for(int j = 0;j < m_product.m_totalIngredients.Length;j++) {
 98                         if(ingredients[i].m_ingredientID == m_product.m_totalIngredients[j]) {
 99                             Debug.Log(1);
100                             break;
101                         } else if(j == m_product.m_totalIngredients.Length - 1) {
102                             isMatch = false;
103                         }
104                     }
105                     if(!isMatch) {
106                         break;
107                     }
108                 }
109             } else {
110                 isMatch = false;
111             }
112 
113             if(isMatch) {
114                 if(!m_audioSource.isPlaying) {
115                     m_audioSource.clip = m_deliverySuccessAudioClip;
116                     m_audioSource.Play();
117                 }
118             } else {
119                 if(!m_audioSource.isPlaying) {
120                     m_audioSource.clip = m_deliveryFailedAudioClip;
121                     m_audioSource.Play();
122                 }
123             }
124             InitProduct();
125             LoadProductAndIngredientsImage();
126             for(int i = m_gameManager.m_ingredientsInPlate.Count - 1;i >= 0;i--) {
127                 Destroy(m_gameManager.m_ingredientsInPlate[i]);
128                 m_gameManager.m_ingredientsInPlate.RemoveAt(i);
129             }
130         }
131     }
132 }
Customer
技术分享
 1 using UnityEngine;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 
 5 namespace VoidGame {
 6 
 7     public class GameManager : MonoBehaviour {
 8 
 9         public static GameManager m_instance;   //单例
10 
11         public GameObject m_ingredientBackgroundPrefab; //原料背景预设
12         public List<GameObject> m_ingredients;  //原料预设列表
13 
14         
15         public List<Sprite> m_productSprites;   //产品精灵列表
16         public List<Sprite> m_ingredientSprites;    //原料精灵列表
17         
18         
19         [HideInInspector]
20         public GameObject m_dragIngredient; //拖动中的原材料
21         [HideInInspector]
22         public bool m_dragIngredientArrived = false;    //原材料是否到达碟内
23 
24         [HideInInspector]
25         public bool m_dragPlateArrivedTrashBin = false;  //盘子是否到达垃圾桶
26         [HideInInspector]
27         public bool m_dragPlateArrivedCustomer = false; //盘子是否到达客户
28 
29         [HideInInspector]
30         public List<GameObject> m_ingredientsInPlate = new List<GameObject>();  //在盘子中的原材料
31 
32         
33 
34         private void Awake() {
35             m_instance = this;
36         }
37 
38         private void Start() {
39             GenerateIngredientBackground();
40         }
41 
42         //生成原料背景和原料
43         private void GenerateIngredientBackground() {
44             Transform ingredientParentTransform = transform.Find("/Ingredients");
45             Transform ingredientBackgroundParentTransform = transform.Find("/Background/IngredientsBackground");
46             Vector3 newPosition = ingredientBackgroundParentTransform.position;
47             newPosition.y = ingredientBackgroundParentTransform.position.y + 1;
48 
49             for(int i = 0;i < 3;i++) {
50                 newPosition.x = ingredientBackgroundParentTransform.position.x - 1;
51                 newPosition.y--;
52                 for(int j = 0;j < 5;j++) {
53                     newPosition.x++;
54                     GameObject ingredientBackground = GameObject.Instantiate(m_ingredientBackgroundPrefab,newPosition,Quaternion.identity,ingredientBackgroundParentTransform) as GameObject;
55                     GameObject.Instantiate(m_ingredients[i * 5 + j],newPosition,Quaternion.identity,ingredientParentTransform);
56                 }
57             }
58         }
59     }
60 }
GameManager
技术分享
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 namespace VoidGame {
 5 
 6     public class Ingredient : MonoBehaviour {
 7 
 8         public int m_ingredientID;  //原料ID
 9 
10         private AudioSource m_audioSource;
11         private GameManager m_gameManager;
12 
13 
14         private void Start() {
15             m_gameManager = GameManager.m_instance;
16             m_audioSource = GetComponent<AudioSource>();
17         }
18 
19         protected void Update() {
20             if(m_gameManager.m_dragIngredient != null) {
21                 FollowCursor();
22             }
23         }
24 
25         //点击原材料,创建一个相同的可拖动的原材料
26         private void onm ouseDown() {
27             if(m_gameManager.m_ingredientsInPlate.Count < Constant.m_maxIngredientsInPlate) {
28                 CreateDragIngredient();
29             }
30         }
31 
32         //抬起鼠标时原料在盘子里,将原料设置为盘子的子对象.
33         //抬起鼠标时原料不在盘子里,删除这个原料
34         private void onm ouseUp() {
35             if(m_gameManager.m_dragIngredient != null && m_gameManager.m_dragIngredientArrived) {
36                 GameObject plate = GameObject.FindGameObjectWithTag(Constant.m_plateTag);
37                 m_gameManager.m_dragIngredient.transform.SetParent(plate.transform);
38                 m_gameManager.m_dragIngredient.transform.position = plate.transform.position;
39                 m_gameManager.m_dragIngredient.GetComponent<BoxCollider2D>().enabled = false;
40 
41                 m_gameManager.m_dragIngredient.transform.localScale = new Vector3(2f,2f,0);
42                 m_gameManager.m_ingredientsInPlate.Add(m_gameManager.m_dragIngredient);
43                 m_gameManager.m_dragIngredient = null;
44                 m_gameManager.m_dragIngredientArrived = false;
45                 
46             } else {
47                 Destroy(m_gameManager.m_dragIngredient);
48                 m_gameManager.m_dragIngredient = null;
49             }
50 
51         }
52         
53         //创建可拖动的原材料
54         private void CreateDragIngredient() {
55             Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
56             newPosition.z = 0;
57             m_gameManager.m_dragIngredient = Instantiate(gameObject,newPosition,Quaternion.identity) as GameObject;
58             m_gameManager.m_dragIngredient.GetComponent<SpriteRenderer>().sortingLayerName = Constant.m_dragSortingLayer;
59             //m_gameManager.m_dragIngredient.GetComponent<SpriteRenderer>().tag = Constant.m_dragTag;
60             if(!m_audioSource.isPlaying) {
61                 m_audioSource.Play();
62             }
63         }
64 
65         //原材料跟随鼠标
66         private void FollowCursor() {
67             Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
68             newPosition.z = 0;
69             m_gameManager.m_dragIngredient.transform.position = newPosition;
70         }
71     }
72 }
Ingredient
技术分享
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 namespace VoidGame {
 5 
 6     public class Plate : MonoBehaviour {
 7 
 8         private Vector3 m_originPosition;   //原始位置
 9 
10         private GameManager m_gameManager;
11         private SpriteRenderer m_spriteRenderer;    
12 
13         private void Start() {
14             m_gameManager = GameManager.m_instance;
15             m_spriteRenderer = GetComponent<SpriteRenderer>();
16             m_originPosition = transform.position;
17         }
18 
19         
20         private void OnTriggerEnter2D(Collider2D other) {
21             //原料移动到盘子上
22             if(other.CompareTag(Constant.m_ingredientTag)) {
23                 m_gameManager.m_dragIngredientArrived = true;
24             //盘子移动到垃圾桶上
25             } else if(other.CompareTag(Constant.m_trashBinTag)) {
26                 Color newColor = m_spriteRenderer.material.color;
27                 newColor.a = 0.5f;
28                 m_spriteRenderer.material.color = newColor;
29             //盘子移动到客户身上
30             } else if(other.CompareTag(Constant.m_customerTag)) {
31                 Color newColor = m_spriteRenderer.material.color;
32                 newColor.a = 0.5f;
33                 m_spriteRenderer.material.color = newColor;
34             }
35         }
36 
37         private void OnTriggerExit2D(Collider2D other) {
38             //原料离开盘子
39             if(other.CompareTag(Constant.m_ingredientTag) && m_gameManager.m_dragIngredientArrived) {
40                 m_gameManager.m_dragIngredientArrived = false;
41             //盘子离开垃圾桶
42             } else if(other.CompareTag(Constant.m_trashBinTag)) {
43                 Color newColor = m_spriteRenderer.material.color;
44                 newColor.a = 1f;
45                 m_spriteRenderer.material.color = newColor;
46             //盘子离开客户
47             } else if(other.CompareTag(Constant.m_customerTag)) {
48                 Color newColor = m_spriteRenderer.material.color;
49                 newColor.a = 1f;
50                 m_spriteRenderer.material.color = newColor;
51             }
52         }
53 
54         //如果盘子里有原料,修改排序层.
55         private void onm ouseDown() {
56             if(m_gameManager.m_ingredientsInPlate.Count > 0) {
57                 m_spriteRenderer.sortingLayerName = Constant.m_dragSortingLayer;
58             }
59         }
60 
61         //如果盘子里有原料,可以拖动.如果盘子里没材料,不可以拖动
62         private void onm ouseDrag() {
63             if(m_gameManager.m_ingredientsInPlate.Count > 0) {
64                 Vector3 newPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
65                 newPosition.z = 0;
66                 transform.position = newPosition;
67             }
68         }
69 
70         //如果盘子在垃圾桶里,删除盘子里的原料
71         private void onm ouseUp() {
72             if(m_gameManager.m_dragPlateArrivedTrashBin) {
73                 TrashBin trashBin = FindObjectOfType<TrashBin>();
74                 trashBin.ClearIngredientInPlate();
75             } else if(m_gameManager.m_dragPlateArrivedCustomer) {
76                 Customer customer = FindObjectOfType<Customer>();
77                 customer.CheckPlate();
78             }
79             transform.position = m_originPosition;
80             m_spriteRenderer.sortingLayerName = Constant.m_uiSortingLayer;
81         }
82     }
83 }
Plate
技术分享
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 namespace VoidGame {
 5 
 6     [System.Serializable]
 7     public class Product {
 8 
 9         public int m_productID; //产品ID
10         public int m_productPrice;  //产品价格
11         public int[] m_totalIngredients;  //产品需要的原料ID
12 
13     }
14 }
Product
技术分享
 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 namespace VoidGame {
 5 
 6     public class TrashBin : MonoBehaviour {
 7 
 8 
 9         private GameManager m_gameManager;
10         private GameObject m_plate; //盘子
11 
12         private AudioSource m_audioSource;
13         private SpriteRenderer m_spriteRenderer;
14 
15         private void Start() {
16             m_gameManager = GameManager.m_instance;
17             m_audioSource = GetComponent<AudioSource>();
18             m_spriteRenderer = GetComponent<SpriteRenderer>();
19             m_plate = GameObject.FindGameObjectWithTag(Constant.m_plateTag);
20         }
21 
22         //盘子移动到垃圾桶上
23         private void OnTriggerEnter2D(Collider2D other) {
24             if(other.CompareTag(Constant.m_plateTag)) {
25                 m_gameManager.m_dragPlateArrivedTrashBin = true;
26 
27                 Color newColor = m_spriteRenderer.material.color;
28                 newColor.a = 0.5f;
29                 m_spriteRenderer.material.color = newColor;
30             }
31         }
32 
33         //盘子离开垃圾桶
34         private void OnTriggerExit2D(Collider2D other) {
35             if(other.CompareTag(Constant.m_plateTag) && m_gameManager.m_dragPlateArrivedTrashBin) {
36                 m_gameManager.m_dragPlateArrivedTrashBin = false;
37 
38                 Color newColor = m_spriteRenderer.material.color;
39                 newColor.a = 1f;
40                 m_spriteRenderer.material.color = newColor;
41             }
42         }
43 
44         //清除盘子里的原材料
45         public void ClearIngredientInPlate() {
46             for(int i = m_gameManager.m_ingredientsInPlate.Count - 1;i >= 0;i--) {
47                 Destroy(m_gameManager.m_ingredientsInPlate[i]);
48                 m_gameManager.m_ingredientsInPlate.RemoveAt(i);
49             }
50             if(!m_audioSource.isPlaying) {
51                 m_audioSource.Play();
52             }
53         }
54     }
55 }
TrashBin

 

Restaurant & Cooking Starter Kit v1.2.1