count_down.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { _decorator, Color, Component, Label, Node, ProgressBar, UITransform } from 'cc';
  2. import { att_count_down, widget_item_data } from '../../../data/data';
  3. import { gameManager } from '../gameManager';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('count_down')
  6. export class count_down extends Component {
  7. private m_data:att_count_down = null;
  8. @property(Node) ProgressBar_Bg:Node = null;
  9. @property(Node) ProgressBar_Bar:Node = null;
  10. @property(Node) img_text:Node = null;
  11. @property(Node) lab_time:Node = null;
  12. private time_count:number = 0;
  13. private add_time:number = 0;
  14. private m_over_call = null;
  15. private m_widget_data:widget_item_data = null;
  16. public init(data:att_count_down,on_over_call,widget_data:widget_item_data){
  17. this.m_data = data;
  18. this.m_widget_data = widget_data;
  19. this.m_over_call = on_over_call;
  20. this.add_time = 0;
  21. if(this.m_data.font_info!=undefined){
  22. this.lab_time.getComponent(Label).fontSize = this.m_data.font_info.font_size;
  23. this.lab_time.getComponent(Label).lineHeight = this.m_data.font_info.font_size;
  24. this.lab_time.getComponent(Label).color = new Color(this.m_data.font_info.font_color.r,this.m_data.font_info.font_color.g,this.m_data.font_info.font_color.b)
  25. }
  26. gameManager.initUiBaseAtt(this.ProgressBar_Bar,this.m_data.ProgressBar_Bar)
  27. gameManager.initUiBaseAtt(this.ProgressBar_Bg,this.m_data.ProgressBar_Bg)
  28. gameManager.initUiBaseAtt(this.img_text,this.m_data.img_text)
  29. this.ProgressBar_Bg.getComponent(ProgressBar).totalLength = this.m_data.ProgressBar_Bg.width;
  30. if(this.m_data.time_count>0){
  31. if(this.m_data.is_show_time){
  32. this.lab_time.getComponent(Label).string = `${this.m_data.time_count}秒`
  33. this.lab_time.active = true;
  34. }else{
  35. this.lab_time.active = false;
  36. }
  37. }else{
  38. this.lab_time.active = false;
  39. }
  40. }
  41. public getData(){
  42. return this.m_data
  43. }
  44. public addTime(widget_id:number,time:number){
  45. if(this.m_widget_data.att.id==widget_id){
  46. this.add_time+=time;
  47. }
  48. }
  49. public startCountDown(){
  50. this.schedule(()=>{
  51. this.time_count+=1;
  52. let all_time = this.m_data.time_count+this.add_time
  53. if(this.time_count>all_time){
  54. this.unscheduleAllCallbacks()
  55. if(this.m_over_call!=null){
  56. this.m_over_call(true)
  57. }
  58. return
  59. }
  60. this.ProgressBar_Bg.getComponent(ProgressBar).progress = 1-this.time_count/all_time
  61. if(this.m_data.is_show_time){
  62. this.lab_time.getComponent(Label).string = `${all_time-this.time_count}秒`
  63. }
  64. },1)
  65. }
  66. public stopCountDown() {
  67. this.unscheduleAllCallbacks()
  68. }
  69. }