widget_base.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import { _decorator, Color, Component, Node, Size, Sprite, tween, Tween, UIOpacity, UITransform, Vec3 } from 'cc';
  2. import { ani_frame, att_ani_data, event_item, widget_item_data } from '../../../data/data';
  3. import { gameManager } from '../gameManager';
  4. import { ClientEvent } from '../../clientEvent';
  5. import { config } from '../../config';
  6. import { tools } from '../../tools';
  7. const { ccclass, property } = _decorator;
  8. class BindTarget{
  9. color : Color;
  10. opacity:number;
  11. size:Size;
  12. pos:Vec3;
  13. rotation:number;
  14. anchorPointX:number;
  15. anchorPointY:number;
  16. }
  17. @ccclass('widget_base')
  18. export class widget_base extends Component {
  19. @property(Node) icon:Node = null;
  20. protected mData:widget_item_data = null;
  21. protected mAnimationList:att_ani_data[] = []; //动画组
  22. protected mCurRunAnimation:ani_frame[] = [];
  23. protected mCurAnimation:att_ani_data = null;
  24. protected mPlayStatus:boolean = false;
  25. protected mIsFinish:boolean = false;
  26. protected mIsActive:boolean = false;
  27. protected mIsShow:boolean = false;
  28. private bindTarget:BindTarget = null;
  29. protected initWidget(data:widget_item_data){
  30. this.mData = data;
  31. this.icon.getComponent(UITransform).contentSize = new Size(this.mData.att.width,this.mData.att.height)
  32. this.icon.position = new Vec3(this.mData.att.x,this.mData.att.y)
  33. if(this.icon.getComponent(UIOpacity)===null){
  34. this.icon.addComponent(UIOpacity)
  35. }
  36. if(this.mData.att.src.length>0){
  37. this.icon.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(this.icon,this.mData.att.src)
  38. }
  39. this.updateDir()
  40. this.mIsShow = this.mData.att.is_show;
  41. this.mIsActive = this.mData.att.is_interaction;
  42. this.mAnimationList = this.mData.att.animation_list;
  43. if(this.mIsShow){
  44. this.node.active = true;
  45. this.onWidgetShow()
  46. }else{
  47. this.node.active = false;
  48. }
  49. this.init()
  50. // ClientEvent.on(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
  51. // ClientEvent.on(config.EventRun.WIDGET_HIDE,this.hide.bind(this),this)
  52. };
  53. updateDir(){
  54. switch (this.mData.att.img_dir) {
  55. case config.widget_scale_dir.left:
  56. this.node.scale = new Vec3(-1,1,1)
  57. break;
  58. case config.widget_scale_dir.up:
  59. this.node.scale = new Vec3(1,-1,1)
  60. break;
  61. case config.widget_scale_dir.normal:
  62. this.node.scale = new Vec3(1,1,1)
  63. break;
  64. }
  65. }
  66. protected onDestroy(): void {
  67. // ClientEvent.off(config.EventRun.NOTICE_EVENT,this.beActive.bind(this),this)
  68. }
  69. private getAniById(id:number){
  70. if(this.mAnimationList.length>0){
  71. for (let index = 0; index < this.mAnimationList.length; index++) {
  72. const element = this.mAnimationList[index];
  73. if(id==element.ani_id){
  74. return element;
  75. }
  76. }
  77. }
  78. return null;
  79. }
  80. onWidgetShow(){
  81. this.node.active = true;
  82. this.mIsShow = true;
  83. if(this.mData.att.animation_list.length>0){
  84. if(this.mData.att.animation_list[0].ani_frame_list.length<=0){
  85. }else{
  86. this.mCurAnimation = this.getAniById(this.mData.att.animation_list[0].ani_id)
  87. if(this.mCurAnimation){
  88. this.mCurRunAnimation = this.mCurAnimation.ani_frame_list;
  89. if(this.mCurRunAnimation.length<2){
  90. }else{
  91. this.runAnimation()
  92. }
  93. }else{
  94. return tools.showToast(`错误的动画配置!id:${this.mData.att.id}-请检查`)
  95. }
  96. }
  97. }
  98. }
  99. public hide(widgetId:number,event:event_item){
  100. if(widgetId===this.mData.att.id){
  101. this.mIsShow = false;
  102. this.node.active = false;
  103. }
  104. }
  105. public beActive(widgetId:number,event:event_item){
  106. if(widgetId===this.mData.att.id){
  107. if(event.type===config.event_type.hide){
  108. this.hide(widgetId,event)
  109. return
  110. }
  111. if(!this.mIsShow){
  112. this.onWidgetShow()
  113. }
  114. if(!this.mIsActive){
  115. this.registeredEvent()
  116. }
  117. this.mIsActive = true;
  118. switch (event.type) {
  119. case config.event_type.play_ani: //播放一个动画
  120. let event_data_nai = event.event_item_play_ani_data
  121. if(event_data_nai!=null){
  122. this.mCurAnimation = this.getAniById(event_data_nai.ani_id)
  123. if(this.mCurAnimation){
  124. this.mCurRunAnimation = this.mCurAnimation.ani_frame_list;
  125. this.runAnimation()
  126. }else{
  127. return tools.showToast("错误的动画配置!请检查")
  128. }
  129. }
  130. break;
  131. case config.event_type.change_one_item_status: //改变自己的突变资源属性
  132. let event_data_change_one_item_status = event.event_item_change_one_item_status_data
  133. if(event_data_change_one_item_status!=null){
  134. gameManager.initUiBaseAtt(this.icon,event_data_change_one_item_status.att)
  135. }
  136. break;
  137. case config.event_type.show_new_item: //显示自己同时被激活
  138. break;
  139. // case config.event_type.collect_event: //自己被触发后,被收集类给收集
  140. // break;
  141. // case config.event_type.active_event: //立刻被激活
  142. // break;
  143. case config.event_type.be_event: //被动激活,等待被玩家交互
  144. console.log("this.be_event",this.mIsShow)
  145. break;
  146. }
  147. }
  148. }
  149. runAnimation(){
  150. if(this.bindTarget!=null){
  151. Tween.stopAllByTarget(this.bindTarget)
  152. this.bindTarget = null;
  153. }
  154. if(this.mCurAnimation.isMove==undefined||this.mCurAnimation.isMove==null){
  155. this.mCurAnimation.isMove = true;
  156. }
  157. // Tween.stopAll()
  158. let getFrameData = (index)=>{
  159. let cur_frame:ani_frame =this.mCurRunAnimation[index];
  160. let up_frame:ani_frame = index>0?this.mCurRunAnimation[index-1]:this.mCurRunAnimation[0];
  161. return {"cur_frame":cur_frame,"up_frame":up_frame}
  162. }
  163. let cur_index = 0;
  164. let data = getFrameData(cur_index);
  165. let call_back = ()=>{
  166. cur_index++;
  167. if(cur_index>=this.mCurRunAnimation.length){
  168. if(this.mCurAnimation.isLoop){
  169. cur_index = 0;
  170. }else{
  171. this.mPlayStatus = false;
  172. this.onFinishAnimation()
  173. return;
  174. }
  175. }
  176. let data = getFrameData(cur_index);
  177. this.runFrame(data.cur_frame,data.up_frame,call_back)
  178. }
  179. this.runFrame(data.cur_frame,data.up_frame,call_back)
  180. }
  181. runFrame(frame:ani_frame,up_frame:ani_frame,call){
  182. let self = this;
  183. let tweenDuration: number = frame.next_time;
  184. // let origin_x = self.mCurRunAnimation[0].pos_x;
  185. // let origin_y = self.mCurRunAnimation[0].pos_y
  186. this.icon.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName( this.icon,frame.url)
  187. let n_pos:Vec3 = new Vec3(frame.pos_x,frame.pos_y)
  188. let _color = new Color()
  189. _color = _color.fromHEX(frame.color)
  190. this.bindTarget = new BindTarget();
  191. let rotation = up_frame.rotation == undefined?0:up_frame.rotation
  192. this.bindTarget.rotation = rotation
  193. this.bindTarget.color = new Color().fromHEX(up_frame.color)
  194. this.bindTarget.opacity = up_frame.transparent;
  195. this.bindTarget.size = new Size(up_frame.size_width,up_frame.size_height)
  196. this.bindTarget.pos = new Vec3(up_frame.pos_x,up_frame.pos_y)
  197. this.bindTarget.anchorPointX = up_frame.anchor_point_x==undefined?0.5:up_frame.anchor_point_x
  198. this.bindTarget.anchorPointY = up_frame.anchor_point_y==undefined?0.5:up_frame.anchor_point_y
  199. let color_opactiy_tw_size = tween(this.bindTarget)
  200. .to( tweenDuration, { pos:n_pos,color: _color,opacity:frame.transparent,size:new Size(frame.size_width,frame.size_height),rotation:frame.rotation, anchorPointX:frame.anchor_point_x, anchorPointY:frame.anchor_point_y }, {
  201. onUpdate(tar:BindTarget){
  202. self.icon.getComponent(Sprite).color = tar.color;
  203. self.icon.getComponent(UIOpacity).opacity = tar.opacity;
  204. self.icon.getComponent(UITransform).setContentSize(tar.size)
  205. self.icon.getComponent(UITransform).setAnchorPoint(tar.anchorPointX, tar.anchorPointY)
  206. if(self.mCurAnimation.isMove){
  207. self.node.position = tar.pos
  208. }
  209. self.node.angle = tar.rotation;
  210. }
  211. }).call(()=>{
  212. call()
  213. });
  214. color_opactiy_tw_size.start();
  215. }
  216. public getIsFinish():boolean{
  217. return this.mIsFinish
  218. }
  219. public getIsShow():boolean{
  220. if(this.icon.getComponent(UIOpacity).opacity==0||this.mIsFinish){
  221. return false;
  222. }
  223. return this.mIsShow;
  224. }
  225. protected registeredEvent(){
  226. }
  227. public offEvent(){
  228. this.node.off(Node.EventType.TOUCH_START)
  229. this.node.off(Node.EventType.TOUCH_END)
  230. this.node.off(Node.EventType.TOUCH_CANCEL)
  231. this.node.off(Node.EventType.TOUCH_MOVE)
  232. this.mIsFinish = true;
  233. }
  234. protected onFinishAnimation(){
  235. }; //动画播放完成
  236. protected onFinishEvent(){
  237. this.offEvent()
  238. ClientEvent.dispatchEvent(config.EventRun.WIDGET_FINISH,this.mData.att.id)
  239. }; //完成自身
  240. protected onFialEvent(){}; // 失败完成
  241. protected init(){};
  242. public initView(data:widget_item_data){
  243. this.initWidget(data)
  244. };
  245. public showZhaoButongFinishStatus(){
  246. }
  247. }