Puzzle.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. var MoveDirection = cc.Enum({
  2. NONE: 0,
  3. UP: 1,
  4. DOWN: 2,
  5. LEFT: 3,
  6. RIGHT: 4
  7. });
  8. var minTilesCount = 2;
  9. var mapMoveStep = 1;
  10. var minMoveValue = 50;
  11. cc.Class({
  12. extends: cc.Component,
  13. editor: {
  14. requireComponent: cc.TiledMap
  15. },
  16. properties: {
  17. _touchStartPos: {
  18. default: null,
  19. serializable: false,
  20. },
  21. _touching: {
  22. default: false,
  23. serializable: false,
  24. },
  25. _isMapLoaded : {
  26. default: false,
  27. serializable: false,
  28. },
  29. floorLayerName: {
  30. default: 'floor'
  31. },
  32. barrierLayerName: {
  33. default: 'barrier'
  34. },
  35. objectGroupName: {
  36. default: 'players'
  37. },
  38. startObjectName: {
  39. default:'SpawnPoint'
  40. },
  41. successObjectName: {
  42. default:'SuccessPoint'
  43. }
  44. },
  45. // use this for initialization
  46. onLoad: function () {
  47. this._player = this.node.getChildByName('player');
  48. if (! this._isMapLoaded) {
  49. this._player.active = false;
  50. }
  51. cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this._onKeyPressed, this);
  52. this.node.on(cc.Node.EventType.TOUCH_START, (event) => {
  53. this._touching = true;
  54. this._touchStartPos = event.touch.getLocation();
  55. });
  56. this.node.on(cc.Node.EventType.TOUCH_END, (event) => {
  57. if (!this._touching || !this._isMapLoaded || this._succeedLayer.active) return;
  58. this._touching = false;
  59. var touchPos = event.touch.getLocation();
  60. var movedX = touchPos.x - this._touchStartPos.x;
  61. var movedY = touchPos.y - this._touchStartPos.y;
  62. var movedXValue = Math.abs(movedX);
  63. var movedYValue = Math.abs(movedY);
  64. if (movedXValue < minMoveValue && movedYValue < minMoveValue) {
  65. // touch moved not enough
  66. return;
  67. }
  68. var newTile = cc.v2(this._curTile.x, this._curTile.y);
  69. var mapMoveDir = MoveDirection.NONE;
  70. if (movedXValue >= movedYValue) {
  71. // move to right or left
  72. if (movedX > 0) {
  73. newTile.x += 1;
  74. mapMoveDir = MoveDirection.LEFT;
  75. } else {
  76. newTile.x -= 1;
  77. mapMoveDir = MoveDirection.RIGHT;
  78. }
  79. } else {
  80. // move to up or down
  81. if (movedY > 0) {
  82. newTile.y -= 1;
  83. mapMoveDir = MoveDirection.DOWN;
  84. } else {
  85. newTile.y += 1;
  86. mapMoveDir = MoveDirection.UP;
  87. }
  88. }
  89. this._tryMoveToNewTile(newTile, mapMoveDir);
  90. });
  91. },
  92. onDestroy () {
  93. cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this._onKeyPressed, this);
  94. },
  95. restartGame: function() {
  96. this._succeedLayer.active = false;
  97. this._initMapPos();
  98. this._curTile = this._startTile;
  99. this._updatePlayerPos();
  100. },
  101. start: function(err) {
  102. if (err) return;
  103. // init the map position
  104. this._initMapPos();
  105. // init the succeed layer
  106. this._succeedLayer = this.node.getParent().getChildByName('succeedLayer');
  107. this._succeedLayer.active = false;
  108. // init the player position
  109. this._tiledMap = this.node.getComponent('cc.TiledMap');
  110. var objectGroup = this._tiledMap.getObjectGroup(this.objectGroupName);
  111. if (!objectGroup) return;
  112. var startObj = objectGroup.getObject(this.startObjectName);
  113. var endObj = objectGroup.getObject(this.successObjectName);
  114. if (!startObj || !endObj) return;
  115. var startPos = cc.v2(startObj.x, startObj.y);
  116. var endPos = cc.v2(endObj.x, endObj.y);
  117. this._layerFloor = this._tiledMap.getLayer(this.floorLayerName);
  118. this._layerBarrier = this._tiledMap.getLayer(this.barrierLayerName);
  119. if (!this._layerFloor || !this._layerBarrier) return;
  120. this._curTile = this._startTile = this._getTilePos(startPos);
  121. this._endTile = this._getTilePos(endPos);
  122. if (this._player) {
  123. this._updatePlayerPos();
  124. this._player.active = true;
  125. }
  126. this._isMapLoaded = true;
  127. },
  128. _initMapPos: function() {
  129. this.node.setPosition(cc.visibleRect.bottomLeft);
  130. },
  131. _updatePlayerPos: function() {
  132. var pos = this._layerFloor.getPositionAt(this._curTile);
  133. this._player.setPosition(pos);
  134. },
  135. _getTilePos: function(posInPixel) {
  136. var mapSize = this.node.getContentSize();
  137. var tileSize = this._tiledMap.getTileSize();
  138. var x = Math.floor(posInPixel.x / tileSize.width);
  139. var y = Math.floor((mapSize.height - posInPixel.y) / tileSize.height);
  140. return cc.v2(x, y);
  141. },
  142. _onKeyPressed: function(event) {
  143. if (!this._isMapLoaded || this._succeedLayer.active) return;
  144. var newTile = cc.v2(this._curTile.x, this._curTile.y);
  145. var mapMoveDir = MoveDirection.NONE;
  146. switch(event.keyCode) {
  147. case cc.macro.KEY.up:
  148. newTile.y -= 1;
  149. mapMoveDir = MoveDirection.DOWN;
  150. break;
  151. case cc.macro.KEY.down:
  152. newTile.y += 1;
  153. mapMoveDir = MoveDirection.UP;
  154. break;
  155. case cc.macro.KEY.left:
  156. newTile.x -= 1;
  157. mapMoveDir = MoveDirection.RIGHT;
  158. break;
  159. case cc.macro.KEY.right:
  160. newTile.x += 1;
  161. mapMoveDir = MoveDirection.LEFT;
  162. break;
  163. default:
  164. return;
  165. }
  166. this._tryMoveToNewTile(newTile, mapMoveDir);
  167. },
  168. _tryMoveToNewTile: function(newTile, mapMoveDir) {
  169. var mapSize = this._tiledMap.getMapSize();
  170. if (newTile.x < 0 || newTile.x >= mapSize.width) return;
  171. if (newTile.y < 0 || newTile.y >= mapSize.height) return;
  172. if (this._layerBarrier.getTileGIDAt(newTile)) {
  173. cc.log('This way is blocked!');
  174. return false;
  175. }
  176. // update the player position
  177. this._curTile = newTile;
  178. this._updatePlayerPos();
  179. // move the map if necessary
  180. this._tryMoveMap(mapMoveDir);
  181. // check the player is success or not
  182. if (this._curTile.equals(this._endTile)) {
  183. cc.log('succeed');
  184. this._succeedLayer.active = true;
  185. }
  186. },
  187. _tryMoveMap: function(moveDir) {
  188. // get necessary data
  189. var mapContentSize = this.node.getContentSize();
  190. var mapPos = this.node.getPosition();
  191. var playerPos = this._player.getPosition();
  192. var viewSize = cc.size(cc.visibleRect.width, cc.visibleRect.height);
  193. var tileSize = this._tiledMap.getTileSize();
  194. var minDisX = minTilesCount * tileSize.width;
  195. var minDisY = minTilesCount * tileSize.height;
  196. var disX = playerPos.x + mapPos.x;
  197. var disY = playerPos.y + mapPos.y;
  198. var newPos;
  199. switch (moveDir) {
  200. case MoveDirection.UP:
  201. if (disY < minDisY) {
  202. newPos = cc.v2(mapPos.x, mapPos.y + tileSize.height * mapMoveStep);
  203. }
  204. break;
  205. case MoveDirection.DOWN:
  206. if (viewSize.height - disY - tileSize.height < minDisY) {
  207. newPos = cc.v2(mapPos.x, mapPos.y - tileSize.height * mapMoveStep);
  208. }
  209. break;
  210. case MoveDirection.LEFT:
  211. if (viewSize.width - disX - tileSize.width < minDisX) {
  212. newPos = cc.v2(mapPos.x - tileSize.width * mapMoveStep, mapPos.y);
  213. }
  214. break;
  215. case MoveDirection.RIGHT:
  216. if (disX < minDisX) {
  217. newPos = cc.v2(mapPos.x + tileSize.width * mapMoveStep, mapPos.y);
  218. }
  219. break;
  220. default:
  221. return;
  222. }
  223. if (newPos) {
  224. // calculate the position range of map
  225. var minX = viewSize.width - mapContentSize.width - cc.visibleRect.left;
  226. var maxX = cc.visibleRect.left.x;
  227. var minY = viewSize.height - mapContentSize.height - cc.visibleRect.bottom;
  228. var maxY = cc.visibleRect.bottom.y;
  229. if (newPos.x < minX) newPos.x = minX;
  230. if (newPos.x > maxX) newPos.x = maxX;
  231. if (newPos.y < minY) newPos.y = minY;
  232. if (newPos.y > maxY) newPos.y = maxY;
  233. if (!newPos.equals(mapPos)) {
  234. cc.log('Move the map to new position: ', newPos);
  235. this.node.setPosition(newPos);
  236. }
  237. }
  238. }
  239. });