BaseEvent.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { HttpStateType, ReveData } from "../../../../util/CHttp";
  2. import FF from "../../FF";
  3. import FMap from "../../map/FMap";
  4. import FSprite from "../../object/FSprite";
  5. import FMapDialog from "../dialog/FMapDialog";
  6. /**
  7. * 场景内碰撞事件基类
  8. */
  9. const { ccclass, property } = cc._decorator;
  10. @ccclass
  11. export default class BaseEvent extends cc.Component {
  12. public ff: FF
  13. //当前处于碰撞区域的对象
  14. // public spriteList: Array<FSprite> = [];
  15. onLoad() {
  16. this.ff = this.node.parent.parent.getComponent(FMap).ff;
  17. }
  18. onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
  19. // if (other.node.group == 'A' && self.tag == 1) {
  20. // let obj = other.node.getComponent(FSprite);
  21. // if (this.spriteList.indexOf(obj) == -1) {
  22. // this.spriteList.push(obj);
  23. // }
  24. // }
  25. if (other.node.group == 'A' && other.tag == 1) {
  26. let obj = other.node.getComponent(FSprite);
  27. if (obj == this.ff.mainSprite) {
  28. this.onBegin(self.tag)
  29. }
  30. }
  31. }
  32. onEndContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider) {
  33. // if (other.node.group == 'A' && self.tag == 1) {
  34. // let obj = other.node.getComponent(FSprite);
  35. // this.removeSprite(obj);
  36. // }
  37. if (other.node.group == 'A' && other.tag == 1) {
  38. let obj = other.node.getComponent(FSprite);
  39. if (obj == this.ff.mainSprite) {
  40. this.onEnd(self.tag)
  41. }
  42. }
  43. }
  44. // private removeSprite(sprite: FSprite) {
  45. // for (let i = 0; i < this.spriteList.length; i++) {
  46. // const element = this.spriteList[i];
  47. // if (element == sprite) {
  48. // this.spriteList.splice(i, 1);
  49. // break
  50. // }
  51. // }
  52. // }
  53. /**
  54. * 主角进入碰撞区域
  55. * @param tag 碰撞组件编号
  56. */
  57. public onBegin(tag: number) {
  58. }
  59. /**
  60. * 主角离开碰撞区域
  61. * @param tag 碰撞组件编号
  62. */
  63. public onEnd(tag: number) {
  64. }
  65. /**
  66. * 移动摄像机
  67. * @param pos
  68. * @param callback
  69. */
  70. public moveCamera(pos: cc.Vec2, time: number, callback: () => void) {
  71. let map = this.ff.mMap;
  72. let camera = map.mCamera;
  73. let winsize = cc.winSize;
  74. pos.x -= winsize.width / 2;
  75. pos.y -= winsize.height / 2;
  76. cc.tween(camera.node).sequence(
  77. cc.moveTo(time, pos).easing(cc.easeOut(time)),
  78. cc.callFunc(() => {
  79. callback();
  80. })
  81. ).start()
  82. }
  83. /**
  84. * 显示界面上可操作按钮
  85. */
  86. public showOpt(spriteFrame: cc.SpriteFrame, callback: () => void) {
  87. this.ff.control.showEventBt(spriteFrame, callback)
  88. }
  89. /**
  90. *
  91. * @param spriteFrame
  92. * @param callback
  93. */
  94. public showOptTouch(spriteFrame: cc.SpriteFrame, startCallback: () => void,endCallback: () => void){
  95. this.ff.control.showEventBtTouch(spriteFrame,startCallback,endCallback)
  96. }
  97. /**
  98. * 关闭界面上可操作按钮
  99. */
  100. public closeOpt() {
  101. this.ff.control.closeEventBt()
  102. }
  103. public closeOptTouch(){
  104. this.ff.control.closeEventBtTouch()
  105. }
  106. /**
  107. * 全屏按钮(用于处理用户点击任意位置)
  108. * @param callback
  109. */
  110. public showBlockInput(callback: () => void) {
  111. this.ff.mBlockInputEvents.active = true;
  112. this.ff.setBlockInputCallback(callback);
  113. }
  114. public closeBlockInput() {
  115. this.ff.mBlockInputEvents.active = false;
  116. this.ff.setBlockInputCallback(null)
  117. }
  118. /**
  119. * 暂停当前游戏
  120. */
  121. public pause() {
  122. this.ff.pauseSprite(true)
  123. }
  124. /**
  125. * 恢复游戏
  126. */
  127. public resume() {
  128. this.ff.pauseSprite(false)
  129. }
  130. /**
  131. * 根据名字查找精灵节点
  132. * @param name
  133. */
  134. public findByName(name: string) {
  135. return cc.find(name, this.ff.mMap.mSprites)
  136. }
  137. /**
  138. * 对象头顶显示对话
  139. */
  140. public showDialog(node: cc.Node, dialogs: Array<string>, callback: () => void) {
  141. let mapDialog = new FMapDialog(this.ff, this.ff.mMapDialog);
  142. let pos = cc.v2();
  143. pos.x = node.x
  144. pos.y = node.y + node.height;
  145. mapDialog.showDialog(dialogs,
  146. pos,
  147. null,
  148. () => {
  149. callback()
  150. }
  151. );
  152. }
  153. /**
  154. * 对象头顶显示对话
  155. */
  156. public showDialogPos(pos: cc.Vec2, dialogs: Array<string>, callback: () => void) {
  157. let mapDialog = new FMapDialog(this.ff, this.ff.mMapDialog);
  158. mapDialog.showDialog(dialogs,
  159. pos,
  160. null,
  161. () => {
  162. callback()
  163. }
  164. );
  165. }
  166. /**
  167. * 播放spine动画
  168. * @param spine
  169. * @param callback
  170. */
  171. public spineAction(spine: sp.Skeleton, actionName: string, callback: () => void) {
  172. spine.setCompleteListener(() => {
  173. spine.setCompleteListener(null)
  174. callback()
  175. })
  176. spine.setAnimation(0, actionName, false)
  177. }
  178. /**
  179. * 捡起地图上的物品
  180. * @param objectId
  181. */
  182. public getMapObject(objectId: string, callback: Function) {
  183. let msg = {
  184. objectId: objectId
  185. }
  186. let main = this.ff.main
  187. main.gameHttp.sendJson('stage/v1/stageObject', msg, (state, reve: ReveData) => {
  188. main.stopLoad();
  189. if (state == HttpStateType.SUCCESS) {
  190. if (reve.retCode == 0) {
  191. //"_stage":{"good":{"1001":1}}
  192. let player = main.player;
  193. let stage = player.stage;
  194. stage.element.push(objectId);
  195. if (reve.data._stage) {
  196. stage.data[this.ff.mFFheader.stageData.id] = reve.data._stage;
  197. this.ff.mFFheader.flush();
  198. }
  199. main.showReward(reve, () => {
  200. callback && callback()
  201. })
  202. } else {
  203. main.showTips(reve.message);
  204. }
  205. } else {
  206. main.showTips('网络异常');
  207. }
  208. });
  209. }
  210. }