FMap.ts 12 KB

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