WeaponCellScript.ts 4.9 KB

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