widget_base.ts 11 KB

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