FMap.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. import GuideBase from "../evnet/guide/GuideBase";
  2. import FF from "../FF";
  3. import FSprite from "../object/FSprite";
  4. const { ccclass, property } = cc._decorator;
  5. @ccclass
  6. export default class FMap extends cc.Component {
  7. @property(cc.Node)
  8. mSprites: cc.Node = null;//精灵层
  9. @property(cc.Node)
  10. mInit: cc.Node = null;//初始位置
  11. @property(cc.Node)
  12. mRooms: cc.Node = null;//地图
  13. @property(cc.AudioClip)
  14. mBgm: cc.AudioClip = null;//背景音乐
  15. @property(GuideBase)
  16. mGuide: GuideBase = null;//地图进入场景时候的引导
  17. @property(cc.TiledLayer)
  18. mColliderTiled: cc.TiledLayer = null;//地图碰撞层
  19. public ff:FF;
  20. shoot: cc.Vec2 = cc.v2();//摄像机位置
  21. public mCamera: cc.Camera = null//地图上的摄像机
  22. /**
  23. * 所有房间
  24. */
  25. public rooms:Array<cc.Node> = null
  26. private midWidth = 360;
  27. private midHeidht = 640;
  28. //当前所处房间
  29. private curRoom:cc.Node = null;
  30. /**
  31. * 单元格尺寸
  32. */
  33. private cellSize = 64;
  34. onLoad() {
  35. this.midWidth = cc.winSize.width/2;
  36. this.midHeidht = cc.winSize.height/2;
  37. this.rooms = this.mRooms.children
  38. this.addCollider()
  39. }
  40. /**
  41. * 添加碰撞
  42. */
  43. private addCollider(){
  44. //地图边界碰撞墙
  45. let width = this.node.width;
  46. let height = this.node.height;
  47. let node = new cc.Node();
  48. node.group = 'map'
  49. let body = node.addComponent(cc.RigidBody);
  50. body.type = cc.RigidBodyType.Static;
  51. this._addBound(node, width/2, height, width, 20);
  52. this._addBound(node, width/2, 0, width, 20);
  53. this._addBound(node, 0, height/2, 20, height);
  54. this._addBound(node, width, height/2, 20, height);
  55. node.parent = this.node;
  56. //地形碰撞处理
  57. if(this.mColliderTiled){
  58. let size = this.mColliderTiled.getLayerSize();
  59. for (let i = size.height - 1; i >= 0; i--) {
  60. let count = 0;
  61. let isEnd = true;
  62. let startX = 0;
  63. let startY = size.height - i - 1;
  64. for (let j = 0; j < size.width; j++) {
  65. let t = this.mColliderTiled.getTileGIDAt(j,i);
  66. if(t > 0){
  67. if(count <= 0){
  68. startX = j;
  69. }
  70. count ++
  71. isEnd = false;
  72. }else{
  73. isEnd = true
  74. }
  75. if(count > 0 && isEnd){
  76. isEnd = true;
  77. let node = new cc.Node();
  78. node.x = startX*this.cellSize
  79. node.y = startY*this.cellSize
  80. node.group = 'map'
  81. let body = node.addComponent(cc.RigidBody);
  82. body.type = cc.RigidBodyType.Static;
  83. let cwidth = this.cellSize*count
  84. this._addBound(node,cwidth/2,32,cwidth,this.cellSize);
  85. node.parent = this.node;
  86. count = 0;
  87. }
  88. }
  89. if(count > 0){
  90. let node = new cc.Node();
  91. node.x = startX*this.cellSize
  92. node.y = startY*this.cellSize
  93. node.group = 'map'
  94. let body = node.addComponent(cc.RigidBody);
  95. body.type = cc.RigidBodyType.Static;
  96. let cwidth = this.cellSize*count
  97. this._addBound(node,cwidth/2,32,cwidth,this.cellSize);
  98. node.parent = this.node;
  99. }
  100. }
  101. }
  102. }
  103. private _addBound (node, x, y, width, height) {
  104. // cc.log('nodeXY:',node.x,node.y)
  105. // cc.log(x,y,width,height)
  106. let collider:cc.PhysicsBoxCollider = node.addComponent(cc.PhysicsBoxCollider);
  107. collider.offset.x = x;
  108. collider.offset.y = y;
  109. collider.size.width = width
  110. collider.size.height = height;
  111. collider.tag = 100;
  112. }
  113. /**
  114. * 获取当前所在房间
  115. */
  116. private getRoom(){
  117. for (let i = 0; i < this.rooms.length; i++) {
  118. const element = this.rooms[i];
  119. if(this.inRoom(element)){
  120. return element;
  121. }
  122. }
  123. return null;
  124. }
  125. private inRoom(node:cc.Node):boolean{
  126. if (this.shoot.x <= node.x
  127. || this.shoot.y <= node.y
  128. || this.shoot.x >= node.x + node.width
  129. || this.shoot.y >= node.y + node.height
  130. ) {
  131. return false;
  132. }
  133. return true;
  134. }
  135. public setFF(ff:FF){
  136. this.ff = ff;
  137. let nodes = this.mSprites.children;
  138. nodes.forEach(node => {
  139. if(this.checkIn(node.name)){
  140. node.destroy();
  141. }
  142. });
  143. if(this.mBgm){
  144. ff.main.playMusicByClip(this.mBgm);
  145. }
  146. }
  147. public checkIn(name:string):boolean{
  148. let stage = this.ff.main.player.stage;
  149. return stage.element.indexOf(name) >= 0;
  150. }
  151. /**
  152. * 初始化摄像机位置
  153. */
  154. public initCamera(){
  155. let winSize = cc.winSize;
  156. let targetPos = this.shoot;//摄像机的目标位置
  157. let targetX = targetPos.x - this.midWidth;
  158. if (targetX != this.mCamera.node.x) {
  159. this.mCamera.node.x = targetX;
  160. if (this.mCamera.node.x < 0) {
  161. this.mCamera.node.x = 0;
  162. }
  163. if (this.mCamera.node.x > this.node.width - winSize.width) {
  164. this.mCamera.node.x = this.node.width - winSize.width;
  165. }
  166. }
  167. let targetY = targetPos.y - this.midHeidht;
  168. if (targetY != this.mCamera.node.y) {
  169. this.mCamera.node.y = targetY;
  170. if (this.mCamera.node.y < 0) {
  171. this.mCamera.node.y = 0;
  172. }
  173. if (this.mCamera.node.y > this.node.height - winSize.height) {
  174. this.mCamera.node.y = this.node.height - winSize.height;
  175. }
  176. }
  177. if(this.mGuide){
  178. this.mGuide.ff = this.ff;
  179. this.mGuide.run();
  180. }
  181. }
  182. private pauseFF(){
  183. this.ff.mainSprite.setPause(true);
  184. cc.tween(this).sequence(
  185. cc.delayTime(0.6),
  186. cc.callFunc(()=>{
  187. this.ff.mainSprite.setPause(false);
  188. })
  189. ).start()
  190. }
  191. lateUpdate(dt) {
  192. this.spriteOrder();
  193. this.ff.mMu.x = this.mCamera.node.x + this.midWidth
  194. this.ff.mMu.y = this.mCamera.node.y + this.midHeidht
  195. if(!this.ff.lockCamera){
  196. return;
  197. }
  198. let room = this.getRoom();
  199. if(!room){
  200. return;
  201. }
  202. if(this.curRoom != room){
  203. if(this.curRoom != null){
  204. this.pauseFF();
  205. }
  206. this.curRoom = room;
  207. }
  208. let winSize = cc.winSize;
  209. let targetPos = this.shoot;//摄像机的目标位置
  210. if(room.width > winSize.width){
  211. let targetX = targetPos.x - this.midWidth;
  212. let sideWidth = room.x + room.width - winSize.width;
  213. if(targetX < room.x){
  214. targetX = room.x
  215. }else if(targetX > sideWidth){
  216. targetX = sideWidth
  217. }
  218. if (targetX != this.mCamera.node.x) {
  219. let offsetx = targetX - this.mCamera.node.x;
  220. if (offsetx >= -5 && offsetx <= 5) {
  221. this.mCamera.node.x = targetX;
  222. } else {
  223. let dx = offsetx / 10;
  224. if(Math.abs(dx) < 5){
  225. if(dx > 0){
  226. dx = 5;
  227. }else{
  228. dx = -5;
  229. }
  230. }
  231. dx = dx*(1+dt)
  232. this.mCamera.node.x += dx;
  233. }
  234. }
  235. }else{
  236. let targetX = targetPos.x - this.midWidth;
  237. if(targetX < room.x){
  238. targetX = room.x
  239. }
  240. if (targetX != this.mCamera.node.x) {
  241. let offsetx = targetX - this.mCamera.node.x;
  242. if (offsetx >= -5 && offsetx <= 5) {
  243. this.mCamera.node.x = targetX;
  244. } else {
  245. let dx = offsetx / 10;
  246. if(Math.abs(dx) < 5){
  247. if(dx > 0){
  248. dx = 5;
  249. }else{
  250. dx = -5;
  251. }
  252. }
  253. dx = dx*(1+dt)
  254. this.mCamera.node.x += dx;
  255. }
  256. }
  257. }
  258. if(room.height > winSize.height){
  259. let targetY = targetPos.y - this.midHeidht;
  260. let sideHeight = room.y + room.height - winSize.height;
  261. if(targetY < room.y){
  262. targetY = room.y
  263. }else if(targetY > sideHeight){
  264. targetY = sideHeight
  265. }
  266. if (targetY != this.mCamera.node.y) {
  267. let offsety = targetY - this.mCamera.node.y;
  268. if (offsety >= -5 && offsety <= 5) {
  269. this.mCamera.node.y = targetY;
  270. } else {
  271. let dy = offsety / 10;
  272. if(Math.abs(dy) < 5){
  273. if(dy > 0){
  274. dy = 5;
  275. }else{
  276. dy = -5;
  277. }
  278. }
  279. dy = dy*(1+dt)
  280. this.mCamera.node.y += dy;
  281. }
  282. }
  283. }else{
  284. let targetY = targetPos.y - this.midHeidht;
  285. if(targetY < room.y){
  286. targetY = room.y
  287. }
  288. if (targetY != this.mCamera.node.y) {
  289. let offsety = targetY - this.mCamera.node.y;
  290. if (offsety >= -5 && offsety <= 5) {
  291. this.mCamera.node.y = targetY;
  292. } else {
  293. let dy = offsety / 10;
  294. if(Math.abs(dy) < 5){
  295. if(dy > 0){
  296. dy = 5;
  297. }else{
  298. dy = -5;
  299. }
  300. }
  301. dy = dy*(1+dt)
  302. this.mCamera.node.y += dy;
  303. }
  304. }
  305. }
  306. }
  307. /**
  308. * 地图上所有精灵显示排序
  309. */
  310. private spriteOrder() {
  311. let children = this.mSprites.children;
  312. for (let i = 0; i < children.length; i++) {
  313. const e = children[i];
  314. if(e.zIndex == 9999 || e.zIndex == -9999){
  315. }else{
  316. let zIndex = (cc.macro.MAX_ZINDEX - e.y) / 10;
  317. e.zIndex = zIndex;
  318. }
  319. }
  320. }
  321. public addSprite(sprite:FSprite) {
  322. sprite.ff = this.ff;
  323. sprite.map = this;
  324. this.mSprites.addChild(sprite.node);
  325. }
  326. public setCamera(mCamera: cc.Camera) {
  327. this.mCamera = mCamera;
  328. }
  329. public updateShoot(x, y):boolean {
  330. if(this.shoot.x == x && this.shoot.y == y){
  331. return false;
  332. }
  333. this.shoot.x = x;
  334. this.shoot.y = y;
  335. return true;
  336. }
  337. public getSprites(): Array<cc.Node> {
  338. return this.mSprites.children;
  339. }
  340. /**
  341. * 当前摄像机是否停止
  342. */
  343. public cameraStop():boolean{
  344. return false;
  345. }
  346. }