WeaponCellScript.ts 4.8 KB

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