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