WeaponCellScript.ts 5.3 KB

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