count_down.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { _decorator, Component, Label, Node, ProgressBar } 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. gameManager.initUiBaseAtt(this.ProgressBar_Bar,this.m_data.ProgressBar_Bar)
  22. gameManager.initUiBaseAtt(this.ProgressBar_Bg,this.m_data.ProgressBar_Bg)
  23. gameManager.initUiBaseAtt(this.img_text,this.m_data.img_text)
  24. this.ProgressBar_Bg.getComponent(ProgressBar).totalLength = this.m_data.ProgressBar_Bg.width;
  25. if(this.m_data.time_count>0){
  26. if(this.m_data.is_show_time){
  27. this.lab_time.getComponent(Label).string = `${this.m_data.time_count}秒`
  28. this.lab_time.active = true;
  29. }else{
  30. this.lab_time.active = false;
  31. }
  32. }else{
  33. this.lab_time.active = false;
  34. }
  35. }
  36. public getData(){
  37. return this.m_data
  38. }
  39. public addTime(widget_id:number,time:number){
  40. if(this.m_widget_data.att.id==widget_id){
  41. this.add_time+=time;
  42. }
  43. }
  44. public startCountDown(){
  45. this.schedule(()=>{
  46. this.time_count+=1;
  47. let all_time = this.m_data.time_count+this.add_time
  48. if(this.time_count>all_time){
  49. this.unscheduleAllCallbacks()
  50. if(this.m_over_call!=null){
  51. this.m_over_call(true)
  52. }
  53. return
  54. }
  55. this.ProgressBar_Bg.getComponent(ProgressBar).progress = 1-this.time_count/all_time
  56. if(this.m_data.is_show_time){
  57. this.lab_time.getComponent(Label).string = `${all_time-this.time_count}秒`
  58. }
  59. },1)
  60. }
  61. }