//移动方向枚举类
var MoveDirection = cc.Enum({
NONE: 0,
UP: 1,
DOWN: 2,
LEFT: 3,
RIGHT: 4
}); var minTilesCount = 2;
var mapMoveStep = 1;
var minMoveValue = 50; cc.Class({
extends: cc.Component,
editor: {
requireComponent: cc.TiledMap
}, properties: {
_touchStartPos: {
default: null,
serializable: false,
},
_touching: {
default: false,
serializable: false,
}, _isMapLoaded : {
default: false,
serializable: false,
}, floorLayerName: {
default: 'floor'
}, barrierLayerName: {
default: 'barrier'
}, objectGroupName: {
default: 'players'
}, startObjectName: {
default:'SpawnPoint'
}, successObjectName: {
default:'SuccessPoint'
}
}, onLoad: function () {
console.log(" init") //获取英雄
this._player = this.node.getChildByName('player');
if (! this._isMapLoaded) {
this._player.active = false;
} //注册按键点击
var self = this;
cc.eventManager.addListener({
event: cc.EventListener.KEYBOARD,
onKeyPressed: function(keyCode, event) {
self._onKeyPressed(keyCode, event);
}
}, self); //注册触摸事件
this.node.on(cc.Node.EventType.TOUCH_START, function (event) {
self._touching = true;
self._touchStartPos = event.touch.getLocation(); //this._onTouchStart()
}, self); this.node.on(cc.Node.EventType.TOUCH_END, function (event) {
if (!self._touching) return; self._touching = false;
var touchPos = event.touch.getLocation();
var movedX = touchPos.x - self._touchStartPos.x;
var movedY = touchPos.y - self._touchStartPos.y;
var movedXValue = Math.abs(movedX);
var movedYValue = Math.abs(movedY); //过小
if (movedXValue < minMoveValue && movedYValue < minMoveValue) {
// touch moved not enough
return;
} //新建一个点
var newTile = cc.p(this._curTile.x, this._curTile.y);
var mapMoveDir = MoveDirection.NONE;
//x方向移动
if (movedXValue >= movedYValue)
{
// move to right or left
if (movedX > 0) {
newTile.x += 1;
mapMoveDir = MoveDirection.LEFT;
} else {
newTile.x -= 1;
mapMoveDir = MoveDirection.RIGHT;
}
}
//y方向移动
else
{
// move to up or down
if (movedY > 0) {
newTile.y -= 1;
mapMoveDir = MoveDirection.UP;
}
else
{
newTile.y += 1;
mapMoveDir = MoveDirection.DOWN;
}
}
this._tryMoveToNewTile(newTile, mapMoveDir);
}, self);
}, //我推测这个是回调函数
theMapLoaded: function(err) {
//有错误退出
if (err) return; console.log("map init") //初始化地图位置
this._initMapPos(); // 获取成功层
this._succeedLayer = this.node.getParent().getChildByName('succeedLayer');
this._succeedLayer.active = false; //初始化英雄 位置
this._tiledMap = this.node.getComponent('cc.TiledMap');
var objectGroup = this._tiledMap.getObjectGroup(this.objectGroupName);
if (!objectGroup) return; var startObj = objectGroup.getObject(this.startObjectName);
var endObj = objectGroup.getObject(this.successObjectName);
if (!startObj || !endObj) return; var startPos = cc.p(startObj.x, startObj.y);
var endPos = cc.p(endObj.x, endObj.y); this._layerFloor = this._tiledMap.getLayer(this.floorLayerName);
this._layerBarrier = this._tiledMap.getLayer(this.barrierLayerName);
if (!this._layerFloor || !this._layerBarrier) return; this._curTile = this._startTile = this._getTilePos(startPos);
this._endTile = this._getTilePos(endPos); if (this._player) {
this._updatePlayerPos();
this._player.active = true;
} this._isMapLoaded = true;
}, //讲地图设置为地步坐下端
_initMapPos: function() {
this.node.setPosition(cc.visibleRect.bottomLeft);
}, _getTilePos: function(posInPixel) {
var mapSize = this.node.getContentSize();
var tileSize = this._tiledMap.getTileSize();
var x = Math.floor(posInPixel.x / tileSize.width);
var y = Math.floor((mapSize.height - posInPixel.y) / tileSize.height); return cc.p(x, y);
}, _onKeyPressed: function(keyCode, event) {
if (!this._isMapLoaded || this._succeedLayer.active) return; var newTile = cc.p(this._curTile.x, this._curTile.y);
var mapMoveDir = MoveDirection.NONE;
switch(keyCode) {
case cc.KEY.up:
newTile.y -= 1;
mapMoveDir = MoveDirection.DOWN;
break;
case cc.KEY.down:
newTile.y += 1;
mapMoveDir = MoveDirection.UP;
break;
case cc.KEY.left:
newTile.x -= 1;
mapMoveDir = MoveDirection.RIGHT;
break;
case cc.KEY.right:
newTile.x += 1;
mapMoveDir = MoveDirection.LEFT;
break;
default:
return;
} this._tryMoveToNewTile(newTile, mapMoveDir);
}, _tryMoveToNewTile: function(newTile, mapMoveDir) {
var mapSize = this._tiledMap.getMapSize(); //超出边界
if (newTile.x < 0 || newTile.x >= mapSize.width) return;
if (newTile.y < 0 || newTile.y >= mapSize.height) return; //障碍
if (this._layerBarrier.getTileGIDAt(newTile)) {
cc.log('This way is blocked!');
return false;
} // update the player position
this._curTile = newTile;
this._updatePlayerPos(); // 必要的时候移动地图
this._tryMoveMap(mapMoveDir); // 检测是否成功
if (cc.pointEqualToPoint(this._curTile, this._endTile)) {
cc.log('succeed');
this._succeedLayer.active = true;
}
}, _updatePlayerPos: function() {
var pos = this._layerFloor.getPositionAt(this._curTile);
this._player.setPosition(pos);
}, _tryMoveMap: function(moveDir) {
// get necessary data
var mapContentSize = this.node.getContentSize();
var mapPos = this.node.getPosition();
var playerPos = this._player.getPosition();
var viewSize = cc.size(cc.visibleRect.width, cc.visibleRect.height);
var tileSize = this._tiledMap.getTileSize();
var minDisX = minTilesCount * tileSize.width;
var minDisY = minTilesCount * tileSize.height; var disX = playerPos.x + mapPos.x;
var disY = playerPos.y + mapPos.y;
var newPos;
switch (moveDir) {
case MoveDirection.UP:
if (disY < minDisY) {
newPos = cc.p(mapPos.x, mapPos.y + tileSize.height * mapMoveStep);
}
break;
case MoveDirection.DOWN:
if (viewSize.height - disY - tileSize.height < minDisY) {
newPos = cc.p(mapPos.x, mapPos.y - tileSize.height * mapMoveStep);
}
break;
case MoveDirection.LEFT:
if (viewSize.width - disX - tileSize.width < minDisX) {
newPos = cc.p(mapPos.x - tileSize.width * mapMoveStep, mapPos.y);
}
break;
case MoveDirection.RIGHT:
if (disX < minDisX) {
newPos = cc.p(mapPos.x + tileSize.width * mapMoveStep, mapPos.y);
}
break;
default:
return;
} if (newPos) {
// calculate the position range of map
var minX = viewSize.width - mapContentSize.width - cc.visibleRect.left;
var maxX = cc.visibleRect.left.x;
var minY = viewSize.height - mapContentSize.height - cc.visibleRect.bottom;
var maxY = cc.visibleRect.bottom.y; if (newPos.x < minX) newPos.x = minX;
if (newPos.x > maxX) newPos.x = maxX;
if (newPos.y < minY) newPos.y = minY;
if (newPos.y > maxY) newPos.y = maxY; if (!cc.pointEqualToPoint(newPos, mapPos)) {
cc.log('Move the map to new position: ', newPos);
this.node.setPosition(newPos);
}
}
}, restartGame: function() {
this._succeedLayer.active = false;
this._initMapPos();
this._curTile = this._startTile;
this._updatePlayerPos();
},
});

最新文章

  1. 自己开发一个 vsts agent 的 task
  2. vim基本命令之剪切复制粘贴替换
  3. HDU 5936 Difference
  4. 第三百五十六天 how can I 坚持
  5. WIN10主动推升级,有点意思
  6. C# CacheHepler Class
  7. Linux 字符集
  8. selenium 远程调用浏览器
  9. markdown 字体颜色
  10. 温故而知新----stack
  11. 哪些异常是RuntimeException?Sql异常属于RuntimeException吗?Spring下SQL异常事务回滚
  12. 015_ICMP专项研究监控
  13. android layout文件优化
  14. kafka之consumer参数auto.offset.reset 0.10+
  15. java及spark2.X连接mongodb3.X单机或集群的方法(带认证及不带认证)
  16. 用matlab绘制中国地图
  17. mvc core2.1 Identity.EntityFramework Core 注册 (二)
  18. 初始 DQN 程序 所遇到的问题
  19. 构建RequestDelegate管道
  20. opencv3.2.0实现读取多张图片的方法(利用sprintf()函数)

热门文章

  1. C#学习笔记----C#中的闭包机制
  2. JavaWeb学习之转发和重定向、会话技术:cookie、session、验证码实例、URLConnection使用(下载网页)(4)
  3. .NET Framework 4 与 .NET Framework 4 Client Profile
  4. .NET Nancy 详解(四) Self Host
  5. android 入门-动画与容器
  6. WPF中加载高分辨率图片性能优化
  7. linux 操作mysql
  8. Android 大牛的 blog 值得推荐 (转 整理)
  9. C# 获取wave文件信息【转】
  10. objective-c 遍历文件夹查看文件