widget_base.ts 11 KB

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