widget_base.ts 9.9 KB

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