WeaponCellScript.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { Component, director, find, LabelComponent, loader, Node, SpriteComponent, SpriteFrame, systemEvent, SystemEventType, Touch, Vec2, Vec3, _decorator } from 'cc';
  2. import { NoticeManager } from '../../../engines/notices/NoticeManager';
  3. import NodeUtils from '../../../engines/utils/NodeUtils';
  4. import StringUtils from '../../../engines/utils/StringUtils';
  5. import GameConfigManager from '../../models/GameConfigManager';
  6. import { GameModel } from '../../models/GameModel';
  7. import { WeaponCell } from '../../models/weapons/WeaponCell';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('WeaponCellScript')
  10. export class WeaponCellScript extends Component {
  11. @property({
  12. type:LabelComponent
  13. })
  14. levelLabel:LabelComponent=null;
  15. @property({
  16. type:SpriteComponent
  17. })
  18. iconLoader:SpriteComponent=null;
  19. @property({
  20. type:SpriteComponent
  21. })
  22. goldIconLoader:SpriteComponent=null;
  23. @property({
  24. type:Node
  25. })
  26. imgState1:Node=null;
  27. @property({
  28. type:Node
  29. })
  30. imgState2:Node=null;
  31. @property({
  32. type:Node
  33. })
  34. imgState3:Node=null;
  35. @property({
  36. type:Node
  37. })
  38. imgState4:Node=null;
  39. @property({
  40. type:Node
  41. })
  42. labelGroup:Node=null;
  43. @property({
  44. type:LabelComponent
  45. })
  46. earningsLabel:LabelComponent=null;
  47. /**
  48. * 武器槽数据
  49. */
  50. public weaponCell:WeaponCell;
  51. /**
  52. * 当前武器的配置
  53. */
  54. private weaponConfig:any;
  55. /**
  56. * 状态 0 空槽状态 1有枪常态 2 可合成
  57. */
  58. private state:number=0;
  59. start () {
  60. this.AddEvent();
  61. }
  62. private AddEvent():void{
  63. }
  64. private RemoveEvent():void{
  65. }
  66. public UpdateWeaponCell(value:WeaponCell):void{
  67. this.weaponCell=value;
  68. if(this.weaponCell.weaponId<0){
  69. this.state=0;
  70. }else{
  71. this.state=1;
  72. }
  73. this.RefreshState();
  74. //取消选中
  75. this.ChangeSelected(null);
  76. if(this.weaponCell.weaponId<0){
  77. this.earningsLabel.node.active=false;
  78. this.labelGroup.active=false;
  79. this.levelLabel.string="";
  80. this.iconLoader.spriteFrame=null;
  81. this.goldIconLoader.node.active=false;
  82. }else{
  83. this.goldIconLoader.node.active=true;
  84. this.weaponConfig=GameConfigManager.GetWeaponConfig(this.weaponCell.weaponId);
  85. //收益
  86. this.earningsLabel.node.active=true;
  87. this.earningsLabel.string=StringUtils.numberUtilsEn(this.weaponConfig.earnings*GameModel.single.earningTime/1000);
  88. //等级
  89. this.labelGroup.active=true;
  90. if(GameConfigManager.WeaponIsMaxLevel(this.weaponCell.weaponId)){
  91. this.levelLabel.string="Max"
  92. }else{
  93. this.levelLabel.string=this.weaponConfig.level.toString();
  94. }
  95. loader.loadRes(this.weaponConfig.icon+"/spriteFrame",SpriteFrame,this.loadCompleteHandler.bind(this));
  96. }
  97. }
  98. private loadCompleteHandler(err:Error,asset:SpriteFrame):void{
  99. if(err!=null){
  100. return;
  101. }
  102. this.iconLoader.spriteFrame=asset;
  103. }
  104. /**
  105. * 选中
  106. */
  107. public ChangeSelected(value:WeaponCell):void{
  108. if(this.weaponCell==value){
  109. this.imgState4.active=true;
  110. }else{
  111. this.imgState4.active=false;
  112. }
  113. }
  114. /**
  115. * 标记为可以
  116. */
  117. public Mark(value:boolean):void{
  118. if(value){
  119. this.state=2;
  120. this.RefreshState();
  121. }else{
  122. this.UpdateWeaponCell(this.weaponCell);
  123. }
  124. }
  125. private RefreshState():void{
  126. if(this.state==0){//没有武器
  127. this.imgState1.active=true;
  128. this.imgState2.active=this.imgState3.active=this.imgState4.active=this.iconLoader.node.active=false;
  129. }else if(this.state==1){//有武器
  130. this.imgState1.active=false;
  131. this.imgState3.active=false;
  132. this.imgState2.active=true;
  133. this.iconLoader.node.active=true;
  134. }else if(this.state==2)//可合成状态
  135. {
  136. this.imgState1.active=false;
  137. this.imgState3.active=true;
  138. this.imgState2.active=true;
  139. }
  140. }
  141. update (deltaTime: number) {
  142. if(this.weaponCell.weaponId<0){
  143. return;
  144. }
  145. let currentTime:number=director.getCurrentTime();
  146. let IntervalTime:number=GameModel.single.earningTime;
  147. let IntervalTimeS:number=IntervalTime/1000;
  148. if(currentTime-this.weaponCell.lastOutputTime>IntervalTime){
  149. let startPos:Vec3=new Vec3();
  150. NodeUtils.GetNodePos(this.node,startPos);
  151. startPos.x+=this.node.width/2;
  152. NoticeManager.ShowPromptByPos("金币:+"+StringUtils.numberUtilsEn(this.weaponCell.weaponConfig.earnings* IntervalTimeS),startPos);
  153. this.weaponCell.lastOutputTime=currentTime;
  154. }
  155. }
  156. destroy():void{
  157. super.destroy();
  158. this.RemoveEvent();
  159. }
  160. }