widget_base.ts 11 KB

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