unity简单的商店模式开发(点餐功能和服务员的交互)(二)

上一篇开发了简单的服务员叫号 和顾客简单的移动的demo

这篇博客接上一篇 实现了点餐的功能
再次给出demo
在这里插入图片描述
就是这样的
然后其中我用写了两个类 还用了队列
如果大家对队列的使用不是很熟悉
我之前写过的栈和队列的地址给大家参考

之后就是场景的搭建了UI很简单 但是这次我为了降低耦合度
大部分赋值是通过代码赋值的
之后给出代码 大家可以自行研究

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

public class food
{
    public string name;
    public int price;
    public int count;
    public int time;
    public food(string name,int price,int count,int time)//构造方法
    {
        this.name = name;
        this. price = price;
        this.count = count;
        this.time = time;
    }
}
public class Indent
{
    //订单类
    public int time;//这个订单所用的时间

    public List<food> foodQueue;//订单中所有食物 
}
public class menu : MonoBehaviour
{
    #region 声明变量
    private List<food> foods;//存放食物的队列
    private Queue<Indent> indents; //存放订单的队列
    //食物订单按钮集合
    private Button hamburgerButton;
    private Button colaButton;
    private Button chickenButton;
    private  Button coffeeButton;

    //食物集合
    public food Hamburger;
    public food cola;
    public food chicken;
    public food coffee;

    //食物UI数量显示
    public Text hamburgerText;
    public Text colaText;
    public Text chickenText;
    public Text coffeeText;
    public Text TotalPriceText;

    private  float timer = 0;
    private float TotalTime = 0;//一次订单所需要的的时间
    private string Talk;//服务员说的话
    private  bool isstart = false;//是否开始做

    private int TotalPrive;//所有支付的金额

    protected int BuyIndex = 0;//下单次数
    #endregion
    /// <summary>
    /// 增加订单
    /// </summary>
    private void Start()
    {
        
        indents = new Queue<Indent>();
        //new 出来对象
        Hamburger = new food("汉堡",50,0,5);
        cola = new food("可乐", 20, 0,3);
        chicken = new food("炸鸡", 80, 0,10);
        coffee = new food("咖啡", 30, 0,3);

        //赋值和注册事件
        hamburgerButton = GameObject.Find("hamburger").GetComponent<Button>();
        colaButton = GameObject.Find("cola").GetComponent<Button>();
        chickenButton = GameObject.Find("chicken").GetComponent<Button>();
        coffeeButton = GameObject.Find("coffee").GetComponent<Button>();


        hamburgerButton.onClick.AddListener(delegate
        {
            AddFood(Hamburger);
        });
        colaButton.onClick.AddListener(delegate
        {
            AddFood(cola);
        });
        chickenButton.onClick.AddListener(delegate
        {
            AddFood(chicken);
        });
        coffeeButton.onClick.AddListener(delegate
        {
            AddFood(coffee);
        });
    }

    public void AddFood(food foods)//按钮事件
    {
        foods.count++;
    }
    /// <summary>
    /// 给食物数量赋值
    /// </summary>
    private void FoodCount()
    {
        hamburgerText.text = "数量:"+Hamburger.count;
        colaText.text = "数量:" + cola.count;
        chickenText.text = "数量:" + chicken.count;
        coffeeText.text = "数量:" + coffee.count;

        TotalPrive = Hamburger.count * Hamburger.price + cola.count * cola.price + coffee.count * coffee.price + chicken.count * chicken.price;
        TotalPriceText.text = "共消费:" + TotalPrive;
    }

    /// <summary>
    /// 购买自己所选的食物 下单(确定键)
    /// </summary>
    public void BuyIsToggleFood()
    {
        foods = new List<food>();
        for (int i=0;i< Hamburger.count; i++)
        {
            foods.Add (Hamburger);
        }
        for (int i = 0; i < cola.count; i++)
        {
            foods.Add(cola);
        }
        for (int i = 0; i < chicken.count; i++)
        {
            foods.Add(chicken);
        }
        for (int i = 0; i < coffee.count; i++)
        {
            foods.Add(coffee);
        }
        ChinarDelegate.Instance.CloseBuyFood();//关闭购买界面

       

        Indent indent = new Indent();//实例化出来一个订单
        indent.foodQueue = foods;

        List<food> repetition = new List<food>();//查重数组
        foreach (var a in indent.foodQueue)
        {
            if (!repetition.Contains(a))//查重
            {
                indent.time += a.count * a.time;
                Talk += a.count + "个" + a.name;
                repetition.Add(a);
            }
        }
        TotalTime = indent.time;
        
        indents.Enqueue(indent);
        
        isstart = true;

       
        Hamburger.count = 0;
        cola.count = 0;
        coffee.count = 0;
        chicken.count = 0;

       
    }

    /// <summary>
    /// 重置所有食物
    /// </summary>
    public void ReSelect()
    {
        Hamburger.count = 0;
        cola.count = 0;
        coffee.count = 0;
        chicken.count = 0;
    }
    /// <summary>
    /// 计算做饭时间的方法
    /// </summary>
    public void CookTime()
    {
        if (indents.Count > 0)
        {
            isstart = true;
        }
        else
        {
            isstart = false ;
        }
        if (isstart)
        {
            timer += Time.deltaTime;
            if (timer >TotalTime)
            {
                indents.Dequeue();
                ChinarDelegate.Instance.Content("谁的"+Talk+"?" , "上一个顾客取餐");
                Talk = "";
                timer = 0;
            }
        }
       

    }
    private void Update()
    {
        FoodCount();

        CookTime();
        
       
    }


}

代码看上去比较长 但是仔细看 不是很复杂
希望对大家有用 如果有问题随时可以联系我
主页有联系方式

猜你喜欢

转载自blog.csdn.net/weixin_44302602/article/details/108185595