count_down.ts 3.0 KB

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