public CarMotion.Move CarMotionGetMoveDirection(CarMotion motion) { CarMotion.Move ret = motion.direction; // bool isNeedUpdate = UpdateRoadTilePostion(); // if (!isNeedUpdate) // { // return ret; // } int total = GetAllMoveDirection(listMoveNow); if (carMotion.runMode == CarMotion.RunMode.AUTO) { if (total > 1) { Debug.Log("CarMotionGetMoveDirection:total=" + total + " carMotion.direction=" + carMotion.direction + " now=(" + tileX + " ," + tileY + ") pre=(" + tileXPre + "," + tileYPre + ")"); } if (total > 0) { ret = GetOneMoveDirection(listMoveNow, total); } } else if (carMotion.runMode == CarMotion.RunMode.CMD) { ret = GetCmdDirection(indexCmd); Debug.Log("GetCmdDirection::ret=" + ret + " indexCmd=" + indexCmd); if (IsCmdError(motion, ret, total)) { //命令错误 停止运动 Debug.Log("cmd is wrong,stop..."); ret = CarMotion.Move.NONE; carMotion.runStatus = CarMotion.RunStatus.ERROR; if (iCarDelegate != null) { iCarDelegate.CarUpdateStatus(this, carMotion.runStatus); } } } //运动停止 if (total == 0) { //无路可走 Debug.Log("no road to go..."); ret = CarMotion.Move.NONE; carMotion.runStatus = CarMotion.RunStatus.STOP; if (iCarDelegate != null) { iCarDelegate.CarUpdateStatus(this, carMotion.runStatus); } } return(ret); }
/// <summary> /// Awake is called when the script instance is being loaded. /// </summary> void Awake() { base.Awake(); enableMove = false; carMotion = this.gameObject.AddComponent <CarMotion>(); carMotion.objCar = this.gameObject; carMotion.iDelegate = this; }
//判断命令是否错误 public bool IsCmdError(CarMotion motion, CarMotion.Move cmdDirection, int total) { bool ret = true; for (int i = 0; i < total; i++) { CarMotion.Move direction = listMoveNow[i]; if (cmdDirection == direction) { ret = false; } } return(ret); }
public bool CarMotionIsNearCenter(CarMotion motion) { bool ret = false; Vector3 posCenter = MapUtils.GetPositionCenter(tileX, tileY, mapSizeX, mapSizeY, sizeRect); motion.UpdateRoadTileCenter(posCenter); bool isNeedUpdate = UpdateRoadTilePostion(); float x, y, w, h; int vaule = 4; w = motion.moveStep * vaule; h = motion.moveStep * vaule; x = posCenter.x - w / 2; y = posCenter.y - h / 2; Rect rc = new Rect(x, y, w, h); posCenter.z = this.transform.localPosition.z; Vector2 pos = this.transform.localPosition; // Debug.Log("CarMotionIsNearCenter:tileX="+tileX+" rc="+rc+" posworld="+posworld+" pos="+pos+" sizeRect="+sizeRect); if (rc.Contains(pos)) { // Debug.Log("CarMotionIsNearCenter:ret = true"); //强制设置成中心位置 //this.transform.localPosition = posworld; ret = true; if (motion.runMode == CarMotion.RunMode.CMD) { bool isCorner = IsCornerItem(motion); if (isCorner) { //经过转弯角 if (carMotion.direction != CarMotion.Move.NONE) { //切换到下个命令 indexCmd++; } } } } return(ret); }
//判断是否是拐弯点 public bool IsCornerItem(CarMotion motion) { bool ret = false; CarMotion.Move[] listMove = new CarMotion.Move[4]; int total = GetAllMoveDirection(listMove); if (total > 1) { ret = true; } else if (total == 1) { if (listMove[0] != motion.direction) { ret = true; } } return(ret); }