BaseEvent.ts 6.1 KB

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