SettlePanel.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { CMS, Tool } from "./JCLibrary";
  2. import WebView from "./WebView";
  3. const {ccclass, property} = cc._decorator;
  4. @ccclass
  5. export default class SettlePanel extends cc.Component {
  6. @property({type:cc.AudioClip})
  7. winAudio:cc.AudioClip = null;
  8. @property({type:cc.SpriteFrame})
  9. failFrame:cc.SpriteFrame = null;
  10. @property({type:cc.SpriteFrame})
  11. winFrame:cc.SpriteFrame = null;
  12. @property({type:cc.Node})
  13. blockGroups:cc.Node[] = [];
  14. @property({type:cc.SpriteFrame})
  15. foodFrames:cc.SpriteFrame[] = [];
  16. foodCalories:number[] = [10,50,100,150,200,250,300,350,400,450,500,550];
  17. titleSprite:cc.Sprite;
  18. calorieLabel:cc.Label;
  19. timeLabel:cc.Label;
  20. onLoad(){
  21. window.settlePanel = this;
  22. this.titleSprite = CMS.findChild(this.node,'PanelBG/Title',cc.Sprite);
  23. this.timeLabel = CMS.findChild(this.node,'PanelBG/Time',cc.Label);
  24. this.calorieLabel = CMS.findChild(this.node,'PanelBG/Tag/Calorie',cc.Label);
  25. CMS.findChild(this.node,'PanelBG/ButtonHome').on(cc.Node.EventType.TOUCH_END,()=>{
  26. window.modeId = undefined;
  27. cc.director.loadScene('Game');
  28. });
  29. CMS.findChild(this.node,'PanelBG/ButtonRestart').on(cc.Node.EventType.TOUCH_END,()=>{
  30. cc.director.loadScene('Game');
  31. });
  32. this.hide();
  33. }
  34. hide(){
  35. this.node.children.forEach((child)=>{
  36. child.active = false;
  37. });
  38. }
  39. show(winnerIndex:number){
  40. this.node.children.forEach((child)=>{
  41. child.active = true;
  42. });
  43. this.node.zIndex = 10;
  44. this.node.getChildByName('BlackBG').setContentSize(cc.winSize);
  45. this.node.setPosition(window.cameraNode.position);
  46. if (cc.winSize.width < cc.view.getDesignResolutionSize().width) {
  47. this.node.getChildByName('PanelBG').setScale(cc.winSize.width / cc.view.getDesignResolutionSize().width);
  48. }
  49. if (window.total_score === undefined) {
  50. window.total_score = 0;
  51. }
  52. if(winnerIndex==window.myPlayerInfo.index){
  53. window.total_score += 10;
  54. this.titleSprite.spriteFrame = this.winFrame;
  55. }else{
  56. window.total_score -= 10;
  57. this.titleSprite.spriteFrame = this.failFrame;
  58. }
  59. cc.audioEngine.playEffect(this.winAudio,false);
  60. let calorie = Math.abs(window.appGame.caloriUnit * window.appGame.jumpCount);
  61. let timeMills = (window.topBar.time_init - parseInt(window.topBar.timeLabel.string)) * 1000;
  62. if (window.total_calorie === undefined) {
  63. window.total_calorie = calorie;
  64. } else {
  65. window.total_calorie += calorie;
  66. }
  67. if (window.total_time === undefined) {
  68. window.total_time = timeMills;
  69. } else {
  70. window.total_time += timeMills;
  71. }
  72. this.calorieLabel.string = Tool.decimal(window.total_calorie, 2).toString();
  73. this.timeLabel.string = Tool.timeFormatHMS(window.total_time);
  74. // if (window.gm.appGame) {
  75. // WebView.getFruitInfo(window.totalCalorie);
  76. // } else {
  77. // this.renderFoods(this.getFoodIndexesByCalorie(window.totalCalorie));
  78. // }
  79. this.renderFoods(this.getFoodIndexesByCalorie(window.total_calorie));
  80. }
  81. getFoodIndexesByCalorie(calorie:number):number[]{
  82. let indexes = [];
  83. while(true){
  84. for(let i=this.foodCalories.length-1;i>=0;i--){
  85. if(calorie>=this.foodCalories[i]){
  86. indexes.push(i);
  87. calorie -= this.foodCalories[i];
  88. break;
  89. }else{
  90. if(i==0){
  91. return indexes;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. renderFoods(foodIndexes:number[]){
  98. for(let i=0;i<foodIndexes.length&&i<this.blockGroups.length;i++){
  99. let blockGroup = this.blockGroups[i];
  100. let firstBlock = blockGroup.children[0];
  101. let foodIndex = foodIndexes[i];
  102. for (let j = 0; j <= foodIndex; j++) {
  103. let duplicatedBlock:cc.Node = cc.instantiate(firstBlock);
  104. duplicatedBlock.getChildByName('Question').active = false;
  105. duplicatedBlock.getChildByName('Value').getComponent(cc.Label).string = this.foodCalories[j].toString();
  106. duplicatedBlock.y = (j + 1) * 200;
  107. let spriteNode = new cc.Node();
  108. spriteNode.addComponent(cc.Sprite).spriteFrame = this.foodFrames[j];
  109. duplicatedBlock.addChild(spriteNode);
  110. blockGroup.addChild(duplicatedBlock);
  111. if(spriteNode.height > 70 || spriteNode.width > 63){
  112. if(spriteNode.height - 70 > spriteNode.width - 63){
  113. spriteNode.setScale(70 / spriteNode.height);
  114. }else{
  115. spriteNode.setScale(63 / spriteNode.width);
  116. }
  117. }
  118. spriteNode.setPosition(0, 15);
  119. }
  120. blockGroup.runAction(cc.moveBy(
  121. 1,cc.v2(0,-(foodIndex+1)*200)
  122. ));
  123. }
  124. }
  125. }