weixudong
2020-11-19 69e0fea6c92fd4e153d45e5f26ef89baecf0405a
Assets/Scripts/TowerDefense/UI/BulletUICtl.cs
@@ -1,40 +1,92 @@
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine;
/// <summary>
/// 子弹充能对应的数据结构.
/// </summary>
public class BulletUICtl : MonoBehaviour
{
    public Image bulletScaleMask;
    public float fAdjValue;
    /// <summary>
    /// 最大子弹数目.
    /// </summary>
    public int maxBulletNum;
    /// <summary>
    /// 当前的子弹数目.
    /// 当前的子弹数目
    /// </summary>
    protected int curBulletNum;
    private int critNum = 1;
    /// <summary>
    /// 默认的暴击子弹数量
    /// </summary>
    public static int defaultCritNum { get; private set; }
    /// <summary>
    /// 暴击子弹数量
    /// </summary>
    /// <value></value>
    public int CritBulletNum
    {
        get { return critNum; }
        set
        {
            if (value < 1) value = 1;
            else if (value > maxBulletNum) value = maxBulletNum;
            critNum = value;
            UpdateBulletType();
        }
    }
    /// <summary>
    /// 子弹底图
    /// </summary>
    public SpriteRenderer[] BottomArr;
    public SpriteRenderer[] BulletArr;
    public Sprite NormalSprite;
    public Sprite CritSprite;
    // Start is called before the first frame update
    void Start()
    {
        resetToMaxBullet();
        defaultCritNum = critNum;
        ResetToMaxBullet();
        UpdateBulletType();
    }
    /// <summary>
    /// 获取进度条进度.
    /// </summary>
    /// <returns></returns>
    public int GetCtlProgress()
    {
        return curBulletNum;
    }
    /// <summary>
    /// 设置进度.
    /// </summary>
    /// <param name="pro"></param>
    public void SetCtlProcess(int pro)
    {
        curBulletNum = pro;
        updateBulletUI(pro, maxBulletNum);
    }
    /// <summary>
    /// 重设子弹数目到最大.
    /// </summary>
    public void resetToMaxBullet()
    public void ResetToMaxBullet()
    {
        this.curBulletNum = maxBulletNum;
        this.updateBulletUI(curBulletNum, maxBulletNum);
    }
        if (curBulletNum == maxBulletNum) return;
        curBulletNum = maxBulletNum;
        updateBulletUI(curBulletNum, maxBulletNum);
    }
    /// <summary>
    /// 减少子弹,返回减少后的子弹数目。
@@ -42,27 +94,52 @@
    /// <returns></returns>
    public int decBullet()
    {
        if (this.curBulletNum <= 0) return 0;
        this.curBulletNum--;
        this.updateBulletUI(curBulletNum, maxBulletNum);
        if (curBulletNum <= 0) return 0;
        curBulletNum--;
        updateBulletUI(curBulletNum, maxBulletNum);
        return curBulletNum;
    }
    /// <summary>
    /// 更新子弹界面对应的缩放信息
    /// 更新子弹显示
    /// </summary>
    /// <param name="curBNum"></param>
    /// <param name="totalBNum"></param>
    public void updateBulletUI( int curBNum,int totalBNum)
    public void updateBulletUI(int curBNum, int totalBNum)
    {
        if (bulletScaleMask == null) return;
        float scaleY = 1.0f - curBNum / (float)totalBNum - fAdjValue;
        bulletScaleMask.rectTransform.DOScaleY( scaleY, 0.1f);
        // 隐藏的子弹数量
        int hideCount = maxBulletNum - curBNum;
        for (int i = 0; i < BulletArr.Length; ++i)
        {
            if (hideCount > 0)
            {
                --hideCount;
                BulletArr[i].enabled = false;
            }
            else
                BulletArr[i].enabled = true;
        }
    }
    // Update is called once per frame
    void Update()
    /// <summary>
    /// 更新子弹类型
    /// </summary>
    private void UpdateBulletType()
    {
        int count = critNum;
        for (int i = BulletArr.Length - 1; i >= 0; --i)
        {
            if (count > 0)
            {
                --count;
                BulletArr[i].sprite = CritSprite;
            }
            else
                BulletArr[i].sprite = NormalSprite;
        }
    }
}