WeaponCellScript.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. let value:number=GameModel.single.earningsInterval/GameModel.single.earningTime;
  92. this.earningsLabel.string=StringUtils.numberUtilsEn(this.weaponConfig.earnings*GameModel.single.earningsInterval/1000*value);
  93. //等级
  94. this.labelGroup.active=true;
  95. if(GameConfigManager.WeaponIsMaxLevel(this.weaponCell.weaponId)){
  96. this.levelLabel.string="Max"
  97. }else{
  98. this.levelLabel.string=this.weaponConfig.level.toString();
  99. }
  100. loader.loadRes(this.weaponConfig.icon+"/spriteFrame",SpriteFrame,this.loadCompleteHandler.bind(this));
  101. }
  102. }
  103. private loadCompleteHandler(err:Error,asset:SpriteFrame):void{
  104. if(err!=null){
  105. return;
  106. }
  107. this.iconLoader.spriteFrame=asset;
  108. }
  109. /**
  110. * 选中
  111. */
  112. public ChangeSelected(value:WeaponCell):void{
  113. if(this.weaponCell==value){
  114. this.imgState4.active=true;
  115. }else{
  116. this.imgState4.active=false;
  117. }
  118. }
  119. /**
  120. * 标记为可以
  121. */
  122. public Mark(value:boolean):void{
  123. if(value){
  124. this.state=2;
  125. this.RefreshState();
  126. }else{
  127. this.UpdateWeaponCell(this.weaponCell);
  128. }
  129. }
  130. private RefreshState():void{
  131. if(this.state==0){//没有武器
  132. this.imgState1.active=true;
  133. this.imgState2.active=this.imgState3.active=this.imgState4.active=this.iconLoader.node.active=false;
  134. }else if(this.state==1){//有武器
  135. this.imgState1.active=false;
  136. this.imgState3.active=false;
  137. this.imgState2.active=true;
  138. this.iconLoader.node.active=true;
  139. }else if(this.state==2)//可合成状态
  140. {
  141. this.imgState1.active=false;
  142. this.imgState3.active=true;
  143. this.imgState2.active=true;
  144. }
  145. if(GameModel.single.currentWeaponCell==this.weaponCell){
  146. this.useing.active=true;
  147. }else{
  148. this.useing.active=false;
  149. }
  150. }
  151. update (deltaTime: number) {
  152. if(this.weaponCell.weaponId<0){
  153. return;
  154. }
  155. let currentTime:number=director.getCurrentTime();
  156. let IntervalTime:number=GameModel.single.earningsInterval;
  157. let IntervalTimeS:number=GameModel.single.earningsInterval/1000;
  158. let value:number=GameModel.single.earningsInterval/GameModel.single.earningTime;
  159. if(currentTime-this.weaponCell.lastOutputTime>IntervalTime){
  160. let startPos:Vec3=new Vec3();
  161. NodeUtils.GetNodePos(this.node,startPos);
  162. startPos.x+=this.node.width/2;
  163. NoticeManager.ShowPromptByPos("金币:+"+StringUtils.numberUtilsEn(this.weaponCell.weaponConfig.earnings* IntervalTimeS*value),startPos);
  164. this.weaponCell.lastOutputTime=currentTime;
  165. }
  166. }
  167. destroy():void{
  168. super.destroy();
  169. this.RemoveEvent();
  170. }
  171. }