BaseEvent.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. */
  92. public closeOpt() {
  93. this.ff.control.closeEventBt()
  94. }
  95. /**
  96. * 全屏按钮(用于处理用户点击任意位置)
  97. * @param callback
  98. */
  99. public showBlockInput(callback: () => void) {
  100. this.ff.mBlockInputEvents.active = true;
  101. this.ff.setBlockInputCallback(callback);
  102. }
  103. public closeBlockInput() {
  104. this.ff.mBlockInputEvents.active = false;
  105. this.ff.setBlockInputCallback(null)
  106. }
  107. /**
  108. * 暂停当前游戏
  109. */
  110. public pause() {
  111. this.ff.pauseSprite(true)
  112. }
  113. /**
  114. * 恢复游戏
  115. */
  116. public resume() {
  117. this.ff.pauseSprite(false)
  118. }
  119. /**
  120. * 根据名字查找精灵节点
  121. * @param name
  122. */
  123. public findByName(name: string) {
  124. return cc.find(name, this.ff.mMap.mSprites)
  125. }
  126. /**
  127. * 对象头顶显示对话
  128. */
  129. public showDialog(node: cc.Node, dialogs: Array<string>, callback: () => void) {
  130. let mapDialog = new FMapDialog(this.ff, this.ff.mMapDialog);
  131. let pos = cc.v2();
  132. pos.x = node.x
  133. pos.y = node.y + node.height;
  134. mapDialog.showDialog(dialogs,
  135. pos,
  136. null,
  137. () => {
  138. callback()
  139. }
  140. );
  141. }
  142. /**
  143. * 对象头顶显示对话
  144. */
  145. public showDialogPos(pos: cc.Vec2, dialogs: Array<string>, callback: () => void) {
  146. let mapDialog = new FMapDialog(this.ff, this.ff.mMapDialog);
  147. mapDialog.showDialog(dialogs,
  148. pos,
  149. null,
  150. () => {
  151. callback()
  152. }
  153. );
  154. }
  155. /**
  156. * 播放spine动画
  157. * @param spine
  158. * @param callback
  159. */
  160. public spineAction(spine: sp.Skeleton, actionName: string, callback: () => void) {
  161. spine.setCompleteListener(() => {
  162. spine.setCompleteListener(null)
  163. callback()
  164. })
  165. spine.setAnimation(0, actionName, false)
  166. }
  167. /**
  168. * 捡起地图上的物品
  169. * @param objectId
  170. */
  171. public getMapObject(objectId: string, callback: Function) {
  172. let msg = {
  173. objectId: objectId
  174. }
  175. let main = this.ff.main
  176. main.gameHttp.sendJson('stage/v1/stageObject', msg, (state, reve: ReveData) => {
  177. main.stopLoad();
  178. if (state == HttpStateType.SUCCESS) {
  179. if (reve.retCode == 0) {
  180. //"_stage":{"good":{"1001":1}}
  181. let player = main.player;
  182. let stage = player.stage;
  183. stage.element.push(objectId);
  184. if (reve.data._stage) {
  185. stage.data[this.ff.mFFheader.stageData.id] = reve.data._stage;
  186. this.ff.mFFheader.flush();
  187. }
  188. main.showReward(reve, () => {
  189. callback && callback()
  190. })
  191. } else {
  192. main.showTips(reve.message);
  193. }
  194. } else {
  195. main.showTips('网络异常');
  196. }
  197. });
  198. }
  199. }