private TouchDir GetTouchDir() { TouchDir touchDir = TouchDir.None; if (Input.GetMouseButtonDown(0)) { PrePos_ = Input.mousePosition; } if (Input.GetMouseButton(0)) { OffsetPos_ = Input.mousePosition - PrePos_; if (Mathf.Abs(OffsetPos_.x) > TouchSensitive_) { touchDir = OffsetPos_.x > 0 ? TouchDir.Right : TouchDir.Left; PrePos_ = Input.mousePosition; SlideAngleCounter_ += OffsetPos_.x * SlideRatio_; if (!CanSlide(touchDir)) { SlideAngleCounter_ += -OffsetPos_.x * SlideRatio_; touchDir = TouchDir.None; } } } return(touchDir); }
private IEnumerator SlideTime() { isSlide = TouchDir.Bottom; yield return(new WaitForSeconds(1.4f)); isSlide = TouchDir.None; }
protected override void OnUpdate() { AdjustIcons(); PlayTween(); if (false == IsKnapsackOpen) { return; } TouchDir touchDir = GetTouchDir(); if (touchDir != TouchDir.None) { float angle = -OffsetPos_.x * SlideRatio_; //AdjustIcons(); if (touchDir == TouchDir.Left && KnapsackTran_.localEulerAngles.z + angle > (397 + 32 * (TestPropNum_ - 6)) % 360) { KnapsackTran_.localEulerAngles = Vector3.forward * (397 + 32 * (TestPropNum_ - 6)); SlideAngleCounter_ = -MaxSlideToLeft_; return; } else if (touchDir == TouchDir.Right && KnapsackTran_.localEulerAngles.z + angle < 5.75f) { SlideAngleCounter_ = 0; KnapsackTran_.localEulerAngles = Vector3.forward * 366; return; } KnapsackTran_.localEulerAngles += Vector3.forward * angle; } }
private bool CanSlide(TouchDir touchDir) { if (TestPropNum_ <= 5) { return(false); } if (touchDir == TouchDir.Left) { float r = RotationAngle_ + Mathf.Abs(SlideAngleCounter_); if (r > MaxSlideToLeft_) { KnapsackTran_.localEulerAngles = Vector3.forward * (397 + 32 * (TestPropNum_ - 6)); return(false); } } else { if (SlideAngleCounter_ >= 0) { KnapsackTran_.localEulerAngles = Vector3.forward * 366; return(false); } } return(true); }
private void MoveControl() { TouchDir dir = GetTouchDir(); if (nowLineIndex != targetLineIndex) { float moveLengh = Mathf.Lerp(0, moveHorizontal, moveHorizontalSpeed * Time.deltaTime); transform.position = new Vector3(transform.position.x + moveLengh, transform.position.y, transform.position.z); moveHorizontal -= moveLengh; if (Mathf.Abs(moveHorizontal) < 0.5f) { transform.position = new Vector3(transform.position.x + moveHorizontal, transform.position.y, transform.position.z); moveHorizontal = 0; nowLineIndex = targetLineIndex; } } if (isSliding) { slideTimer += Time.deltaTime; if (slideTimer >= slideTime) { slideTimer = 0; isSliding = false; } } if (isJumping) { float yMove = jumpSpeed * Time.deltaTime; if (isUp) { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y + yMove, prisoner.position.z); haveJumpHeight += yMove; if (Mathf.Abs(jumpHeight - haveJumpHeight) < 0.5f) { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y + jumpHeight - haveJumpHeight, prisoner.position.z); isUp = false; haveJumpHeight = jumpHeight; } } else if (isUp == false) { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y - yMove, prisoner.position.z); haveJumpHeight -= yMove; if (Mathf.Abs(haveJumpHeight - 0) < 0.5f) { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y - haveJumpHeight - 0, prisoner.position.z); isJumping = false; haveJumpHeight = 0; } } } }
void MoveControl() { TouchDir dir = GetTouchDir(); if (targetLaneIndex != nowLaneIndex) { float moveLength = Mathf.Lerp(0, moveHorizontal, Time.deltaTime * horizontalSpeed); transform.position = new Vector3(transform.position.x + moveLength, transform.position.y, transform.position.z); moveHorizontal -= moveLength; if (Mathf.Abs(moveHorizontal) < 0.5f) { transform.position = new Vector3(transform.position.x + moveHorizontal, transform.position.y, transform.position.z); moveHorizontal = 0; nowLaneIndex = targetLaneIndex; } } if (isSliding) { slideTimer += Time.deltaTime; if (slideTimer > slideTime) { isSliding = false; slideTimer = 0; } } if (isJumping) { float y = Time.deltaTime * jumpSpeed; if (isUp) //正在向上跳跃 { player.position = new Vector3(player.position.x, player.position.y + y, player.position.z); nowHeight += y; if (targetHeight - nowHeight < 0.5f) { isUp = false; player.position = new Vector3(player.position.x, targetHeight, player.position.z); nowHeight = targetHeight; } } else //正在向下降落 { player.position = new Vector3(player.position.x, player.position.y - y, player.position.z); nowHeight -= y; if (nowHeight - 0 < 0.5f) { isJumping = false; player.position = new Vector3(player.position.x, 0, player.position.z); nowHeight = 0; } } } }
private void MoveContorl() //位置处理 { TouchDir dir = GetTouchDir(); //得到移动方向 if (targetTrack != currentTrack) //左右位置处理 { float leftDistance = Mathf.Lerp(0, moveDistance, moveDirectionSpeed * Time.deltaTime); //player位置为0,目标距离为14或者-14 transform.position = new Vector3(transform.position.x + leftDistance, transform.position.y, transform.position.z); moveDistance -= leftDistance; if (Mathf.Abs(moveDistance) < 0.5f) { transform.position = new Vector3(transform.position.x + moveDistance, transform.position.y, transform.position.z); moveDistance = 0; //上面两句直接写成leftDistance=0.5;可以不,等会试试 currentTrack = targetTrack; //移动结束,现在的跑道就是目标跑道; } } if (isSliding) //滑行处理 { slideTimer += Time.deltaTime; if (slideTimer > slideTime) //如果时间到了,停止播放 这里别写等于,因为是float所以很难刚好精确的等于,大于就好了 { slideTimer = 0; isSliding = false; } } if (isJumping) { float yMove = jumpSpeed * Time.deltaTime; if (isUp) //跳起 { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y + yMove, prisoner.position.z); jumppedHeight += yMove; if (jumpHeight - jumppedHeight < 0f) //把绝对值去了,因为小于0之后变负的,但是绝度值一下又正了,于是越跳越高 { isUp = false; //说明最高点了,可以下落了。 } } if (!isUp) //下落 这里不应该用else,用else 就在上升最高的地方还要等下一次update更新再下落。我们要马上就下落。 { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y - yMove, prisoner.position.z); jumppedHeight -= yMove; if (jumppedHeight < 0.1f) //离地高度 { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y - jumppedHeight, prisoner.position.z); isJumping = false; jumppedHeight = 0; } } } }
void MoveControll() { if (GvrViewer.Instance.VRModeEnabled = true) { } else { touchDir = GetTouchDir(); } switch (touchDir) { case TouchDir.None: break; case TouchDir.Right: if (targetLaneIndex < 2) { targetLaneIndex++; moveHorizontal = 3; } break; case TouchDir.Left: if (targetLaneIndex > 0) { targetLaneIndex--; moveHorizontal = -3; } break; case TouchDir.Top: if (isJumping == false) { isJumping = true; isUp = true; // targetJumpHeight = jumpHeight; } break; case TouchDir.Bottom: if (!isJumping) { isSliding = true; } break; } }
private void CheckAndMoveNum() { if (Input.GetMouseButtonDown(0)) { mouseDownPosition = Input.mousePosition; } if (Input.GetMouseButtonUp(0) == false) { return; } //如果鼠标抬起则 TouchDir touchDir = GetTouchDir(); ToMove(touchDir); }
private void MoveControl() { GetTouchDir(); if (nowLandIndex != targetLand) { float movelength = Mathf.Lerp(0, moveHorizontal, moveSpeed * Time.deltaTime); transform.position = new Vector3(transform.position.x + movelength, transform.position.y, transform.position.z); moveHorizontal -= movelength; if (Mathf.Abs(moveHorizontal) < 0.5f) { transform.position = new Vector3(transform.position.x + moveHorizontal, transform.position.y, transform.position.z); moveHorizontal = 0; nowLandIndex = targetLand; } } if (isJump == TouchDir.Top) { float yMove = jumpSpeed * Time.deltaTime; if (isUp) { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y + yMove, prisoner.position.z); jumpHeight -= yMove; if (Mathf.Abs(jumpHeight) < 0.5f) { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y + jumpHeight, prisoner.position.z); isUp = false; jumpHeight = 0; } } else { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y - yMove, prisoner.position.z); jumpHeight += yMove; if (Mathf.Abs(jumpHeight) > 19.5f) { prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y - 20f + jumpHeight, prisoner.position.z); isUp = true; isJump = TouchDir.None; jumpHeight = 20f; } } } }
private void MoveControl() { TouchDir dir = GetTouchDir(); Debug.Log(laneIndex); if (dir == TouchDir.Right || dir == TouchDir.Left) { Vector3 v = transform.position; v.x += laneOffset; } if (mIsSliding) { mSlidingTime += Time.deltaTime; if (mSlidingTime > 1.40f) { mSlidingTime = 0; mIsSliding = false; } } if (mIsJump) { if (mIsJumpUp) { mHasJumpHeight += mJumpSpeed * Time.deltaTime; if (mJumpHeight - mHasJumpHeight < 0.5) { mHasJumpHeight = mJumpHeight; mIsJumpUp = false; } prisoner.position = new Vector3(prisoner.position.x, mHasJumpHeight, prisoner.position.z); } else { mHasJumpHeight -= mJumpSpeed * Time.deltaTime; if (mHasJumpHeight < 8.9) { mHasJumpHeight = 8.5f; mIsJump = false; } prisoner.position = new Vector3(prisoner.position.x, mHasJumpHeight, prisoner.position.z); } } }
void MoveControll() { TouchDir touchDir = GetTouchDir(); switch (touchDir) { case TouchDir.None: break; case TouchDir.Right: if (targetLaneIndex < 2) { targetLaneIndex++; moveHorizontal = 14; } break; case TouchDir.Left: if (targetLaneIndex > 0) { targetLaneIndex--; moveHorizontal = -14; } break; case TouchDir.Top: if (isJumping == false) { isJumping = true; targetJumpHeight = jumpHeight; } break; case TouchDir.Bottom: if (!isJumping) { isSliding = true; } break; } }
private void GetTouchDir() { if (Input.GetMouseButtonDown(0)) { lastMouseDown = Input.mousePosition; } if (Input.GetMouseButtonUp(0)) { Vector3 mouseUp = Input.mousePosition; Vector3 touchOffset = mouseUp - lastMouseDown; if (Mathf.Abs(touchOffset.x) > 50 || Mathf.Abs(touchOffset.y) > 50) { if (Mathf.Abs(touchOffset.x) > Mathf.Abs(touchOffset.y) && touchOffset.x > 0) { if (targetLand != 2) { ++targetLand; moveHorizontal = 14f; } } else if (Mathf.Abs(touchOffset.x) > Mathf.Abs(touchOffset.y) && touchOffset.x < 0) { if (targetLand != 0) { --targetLand; moveHorizontal = -14f; } } else if (Mathf.Abs(touchOffset.x) < Mathf.Abs(touchOffset.y) && touchOffset.y > 0) { isJump = TouchDir.Top; } else if (Mathf.Abs(touchOffset.x) < Mathf.Abs(touchOffset.y) && touchOffset.y < 0) { StartCoroutine(SlideTime()); } } } }
// Update is called once per frame void Update() { if (GameController.gameState == GameState.Playing) { Vector3 pos = transform.position; Vector3 nextWayPoint = fc.GetNextWayPoint(pointIndex); if (fc.curForest.tag == "Forest4" && this.transform.localPosition.z < -389.4) { //Debug.Log(this.transform.localPosition.z); SceneManager.LoadScene("Test"); } if (Vector3.Distance(this.transform.position, nextWayPoint) < 1.0f) { //Debug.Log("pointIndex:" + pointIndex); if (pointIndex != 0) { pointIndex--; nextWayPoint = fc.GetNextWayPoint(pointIndex); } else { //change forest fc.ChangeForest(); pointIndex = fc.GetCurrentForestWPNum() - 1; } } nextWayPoint = new Vector3(nextWayPoint.x + GameController.xOffsets[targetLaneIndex], nextWayPoint.y, nextWayPoint.z); Vector3 dir = nextWayPoint - transform.position; Vector3 moveDir = dir.normalized * speed * Time.deltaTime; //Debug.Log ("moveDir z:" + moveDir.z); this.transform.position += moveDir; transform.rotation = Quaternion.LookRotation(new Vector3(nextWayPoint.x, transform.position.y, nextWayPoint.z) - transform.position, Vector3.up); //transform.LookAt(nextWayPoint); if (targetLaneIndex != nowLaneIndex) { float move = moveHorizontal * horizontalMoveSpeed; moveHorizontal -= moveHorizontal * horizontalMoveSpeed; this.transform.position = new Vector3(transform.position.x + move, transform.position.y, transform.position.z); if (Mathf.Abs(moveHorizontal) < 0.5f) { this.transform.position = new Vector3(transform.position.x + moveHorizontal, transform.position.y, transform.position.z); nowLaneIndex = targetLaneIndex; } } if (isJumping) { anim.SetBool("RunToJump", true); } else { anim.SetBool("RunToJump", false); } if (isJumping) { Debug.Log("jump move"); float yMove = jumpSpeed * Time.deltaTime; if (isUp) { Debug.Log("isUp true"); this.prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y + yMove, prisoner.position.z); haveJumped += yMove; if (Mathf.Abs(jumpHeight - haveJumped) < 0.5) { this.prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y + jumpHeight - haveJumped, prisoner.position.z); isUp = false; haveJumped = jumpHeight; } } else { //isUp = false Debug.Log("isUp false"); if (prisoner.position.y - yMove > 0) { this.prisoner.position = new Vector3(prisoner.position.x, prisoner.position.y - yMove, prisoner.position.z); } yMove = 0.7f * jumpSpeed * Time.deltaTime; haveJumped -= yMove; if (haveJumped < 0.3 && haveJumped > 0) { this.prisoner.position = new Vector3(prisoner.position.x, 0, prisoner.position.z); isJumping = false; haveJumped = 0; if (!footLand.isPlaying) { footLand.Play(); } } //isUp = true; } touchDir = TouchDir.None; } if (isSliding) { slideTimer += Time.deltaTime; if (slideTimer > slideTime) { isSliding = false; slideTimer = 0; } } MoveControll(); } }
private void MoveControl() { TouchDir dir = GetTouchDir(); if (targetLaneIndex != nowLaneIndex) { float move = moveHorizontal * horizontalMoveSpeed; // 移动的距离 moveHorizontal -= moveHorizontal * horizontalMoveSpeed; this.transform.position = new Vector3(transform.position.x + move, transform.position.y, transform.position.z); if (Mathf.Abs(moveHorizontal) < 0.5f) { this.transform.position = new Vector3(transform.position.x + moveHorizontal, transform.position.y, transform.position.z); nowLaneIndex = targetLaneIndex; } } if (isSliding) { slideTimer += Time.deltaTime; if (slideTimer > slideTime) { isSliding = false; slideTimer = 0; } /*float MoveY = slideSpeed * Time.deltaTime; * if (isDown) * { * player.position = new Vector3(player.position.x, player.position.y - MoveY, player.position.z); * haveSlideHeight -= MoveY; * if (Mathf.Abs(haveSlideHeight - 0) < 0.01f) * { * player.position = new Vector3(player.position.x, player.position.y - (haveSlideHeight - 0), player.position.z); * isDown = false; * haveSlideHeight = 0; * } * } * else * { * player.position = new Vector3(player.position.x, player.position.y + MoveY, player.position.z); * haveSlideHeight += MoveY; * if (Mathf.Abs(slideHeight - haveSlideHeight) < 0.01f) * { * player.position = new Vector3(player.position.x, player.position.y + (slideHeight - haveSlideHeight), player.position.z); * isSliding = false; * haveSlideHeight = slideHeight; * } * }*/ } if (isJumping) { float MoveY = jumpSpeed * Time.deltaTime; if (isUp) { player.position = new Vector3(player.position.x, player.position.y + MoveY, player.position.z); haveHeight += MoveY; if (Mathf.Abs(jumpHeight - haveHeight) < 0.1f) { player.position = new Vector3(player.position.x, player.position.y + jumpHeight - haveHeight, player.position.z); isUp = false; haveHeight = jumpHeight; } } else { player.position = new Vector3(player.position.x, player.position.y - MoveY, player.position.z); haveHeight -= MoveY; if (Mathf.Abs(haveHeight - 0) < 0.1f) { player.position = new Vector3(player.position.x, player.position.y - (haveHeight - 0), player.position.z); isJumping = false; haveHeight = 0; } } } }
//private Vector2 SelectEmptyPos() { // int emptyNumCount = 0; // int newX=9, newY=9;//若输出9,9;则已经没有空位,游戏结束 // for (int x = 0; x < 4; x++) // { // for (int y = 0; y < 4; y++) // { // if (NumIndexArray[x][y] == 0) { // emptyNumCount++; // } // } // } // if (emptyNumCount != 0) // { // int randomIndex = Random.Range(1, emptyNumCount + 1); // int Count = 0; // for (int x = 0; x < 4; x++) // { // for (int y = 0; y < 4; y++) // { // if (NumIndexArray[x][y] == 0) // { // Count++; // if (Count == randomIndex) // { // newX = x; // newY = y; // } // } // } // } // } // return new Vector2(newX, newY); //} //返回值还未设置 private Vector2 SelectEmptyPos(TouchDir touchDir) { int newX = 9, newY = 9; if (touchDir == TouchDir.Top) { #region moveToTop int emptyNumCount = 0; int y = 3; for (int x = 0; x < 4; x++) { if (NumIndexArray[x][y] == 0) { emptyNumCount++; } } if (emptyNumCount != 0) { int randomIndex = Random.Range(1, emptyNumCount + 1); int count = 0; for (int x = 0; x < 4; x++) { if (NumIndexArray[x][y] == 0) { count++; if (count == randomIndex) { newX = x; newY = y; } } } } #endregion } else if (touchDir == TouchDir.Bottom) { #region moveToBottom int emptyNumCount = 0; int y = 0; for (int x = 0; x < 4; x++) { if (NumIndexArray[x][y] == 0) { emptyNumCount++; } } if (emptyNumCount != 0) { int randomIndex = Random.Range(1, emptyNumCount + 1); int count = 0; for (int x = 0; x < 4; x++) { if (NumIndexArray[x][y] == 0) { count++; if (count == randomIndex) { newX = x; newY = y; } } } } #endregion } else if (touchDir == TouchDir.Right) { #region moveToRight int emptyNumCount = 0; int x = 0; for (int y = 0; y < 4; y++) { if (NumIndexArray[x][y] == 0) { emptyNumCount++; } } if (emptyNumCount != 0) { int randomIndex = Random.Range(1, emptyNumCount + 1); int count = 0; for (int y = 0; y < 4; y++) { if (NumIndexArray[x][y] == 0) { count++; if (count == randomIndex) { newX = x; newY = y; } } } } #endregion } else if (touchDir == TouchDir.Left) { #region moveToLeft int emptyNumCount = 0; int x = 3; for (int y = 0; y < 4; y++) { if (NumIndexArray[x][y] == 0) { emptyNumCount++; } } if (emptyNumCount != 0) { int randomIndex = Random.Range(1, emptyNumCount + 1); int count = 0; for (int y = 0; y < 4; y++) { if (NumIndexArray[x][y] == 0) { count++; if (count == randomIndex) { newX = x; newY = y; } } } } #endregion } else if (touchDir == TouchDir.None) { #region AddOne int emptyNumCount = 0; for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { if (NumIndexArray[x][y] == 0) { emptyNumCount++; } } } if (emptyNumCount != 0) { int randomIndex = Random.Range(1, emptyNumCount + 1); int Count = 0; for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { if (NumIndexArray[x][y] == 0) { Count++; if (Count == randomIndex) { newX = x; newY = y; } } } } } #endregion } return(new Vector2(newX, newY)); }
private void ToMove(TouchDir touchDir) { switch (touchDir) { case TouchDir.Top: #region ToTop for (int x = 0; x < 4; x++) { int emptyCount = 0; Num preNum = null; for (int y = 0; y < 4; y++) { if (NumIndexArray[x][y] == 0) { emptyCount++; } else { //初始Num if (preNum == null) { preNum = NumAssemblyArray[x][y]; } //合并 else if (NumAssemblyArray[x][y].NumIndex == preNum.NumIndex) { emptyCount++; //启动删除标记 NumAssemblyArray[x][y].isDestory = true; preNum.isDestory = true; //将响应表格中的引用清除 preNum = null; //延时并创建一个num //CreateNum(x + emptyCount, y, NumAssemblyArray[x][y].NumIndex * 2); StartCoroutine(WaitAndCreateNum(x, y - emptyCount, NumAssemblyArray[x][y].NumIndex * 2, NumAssemblyArray[x][y].moveTime)); } else { preNum = NumAssemblyArray[x][y]; } //移动 int newX = NumAssemblyArray[x][y].InitX; int newY = NumAssemblyArray[x][y].InitY - emptyCount; NumAssemblyArray[x][y].UpdatePos(newX, newY); if (newX != x || newY != y) { NumAssemblyArray[newX][newY] = NumAssemblyArray[x][y]; NumIndexArray[newX][newY] = NumIndexArray[x][y]; NumAssemblyArray[x][y] = null; NumIndexArray[x][y] = 0; } } } } CreateNum(TouchDir.Top); #endregion break; case TouchDir.Bottom: #region ToButtom for (int x = 0; x < 4; x++) { int emptyCount = 0; Num preNum = null; for (int y = 3; y >= 0; y--) { if (NumIndexArray[x][y] == 0) { emptyCount++; } else { //初始Num if (preNum == null) { preNum = NumAssemblyArray[x][y]; } //合并 else if (NumAssemblyArray[x][y].NumIndex == preNum.NumIndex) { emptyCount++; //启动删除标记 NumAssemblyArray[x][y].isDestory = true; preNum.isDestory = true; //将响应表格中的引用清除 preNum = null; //延时并创建一个num //CreateNum(x + emptyCount, y, NumAssemblyArray[x][y].NumIndex * 2); StartCoroutine(WaitAndCreateNum(x, y + emptyCount, NumAssemblyArray[x][y].NumIndex * 2, NumAssemblyArray[x][y].moveTime)); } else { preNum = NumAssemblyArray[x][y]; } //移动 int newX = NumAssemblyArray[x][y].InitX; int newY = NumAssemblyArray[x][y].InitY + emptyCount; NumAssemblyArray[x][y].UpdatePos(newX, newY); if (newX != x || newY != y) { NumAssemblyArray[newX][newY] = NumAssemblyArray[x][y]; NumIndexArray[newX][newY] = NumIndexArray[x][y]; NumAssemblyArray[x][y] = null; NumIndexArray[x][y] = 0; } } } } CreateNum(TouchDir.Bottom); #endregion break; case TouchDir.Left: #region ToLeft for (int y = 0; y < 4; y++) { int emptyCount = 0; Num preNum = null; for (int x = 0; x < 4; x++) { if (NumIndexArray[x][y] == 0) { emptyCount++; } else { //初始Num if (preNum == null) { preNum = NumAssemblyArray[x][y]; } //合并 else if (NumAssemblyArray[x][y].NumIndex == preNum.NumIndex) { emptyCount++; //启动删除标记 NumAssemblyArray[x][y].isDestory = true; preNum.isDestory = true; //将响应表格中的引用清除 preNum = null; //延时并创建一个num //CreateNum(x + emptyCount, y, NumAssemblyArray[x][y].NumIndex * 2); StartCoroutine(WaitAndCreateNum(x - emptyCount, y, NumAssemblyArray[x][y].NumIndex * 2, NumAssemblyArray[x][y].moveTime)); } else { preNum = NumAssemblyArray[x][y]; } //移动 int newX = NumAssemblyArray[x][y].InitX - emptyCount; int newY = NumAssemblyArray[x][y].InitY; NumAssemblyArray[x][y].UpdatePos(newX, newY); if (newX != x || newY != y) { NumAssemblyArray[newX][newY] = NumAssemblyArray[x][y]; NumIndexArray[newX][newY] = NumIndexArray[x][y]; NumAssemblyArray[x][y] = null; NumIndexArray[x][y] = 0; } } } } CreateNum(TouchDir.Left); #endregion break; case TouchDir.Right: #region ToRight for (int y = 0; y < 4; y++) { int emptyCount = 0; Num preNum = null; for (int x = 3; x >= 0; x--) { if (NumIndexArray[x][y] == 0) { emptyCount++; } else { //初始Num if (preNum == null) { preNum = NumAssemblyArray[x][y]; } //合并 else if (NumAssemblyArray[x][y].NumIndex == preNum.NumIndex) { emptyCount++; //启动删除标记 NumAssemblyArray[x][y].isDestory = true; preNum.isDestory = true; //将响应表格中的引用清除 preNum = null; //延时并创建一个num //CreateNum(x + emptyCount, y, NumAssemblyArray[x][y].NumIndex * 2); StartCoroutine(WaitAndCreateNum(x + emptyCount, y, NumAssemblyArray[x][y].NumIndex * 2, NumAssemblyArray[x][y].moveTime)); } else { preNum = NumAssemblyArray[x][y]; } //移动 int newX = NumAssemblyArray[x][y].InitX + emptyCount; int newY = NumAssemblyArray[x][y].InitY; NumAssemblyArray[x][y].UpdatePos(newX, newY); if (newX != x || newY != y) { NumAssemblyArray[newX][newY] = NumAssemblyArray[x][y]; NumIndexArray[newX][newY] = NumIndexArray[x][y]; NumAssemblyArray[x][y] = null; NumIndexArray[x][y] = 0; } } } } CreateNum(TouchDir.Right); #endregion break; } }
private void CreateNum(TouchDir touchDir) { Vector2 newPos = SelectEmptyPos(touchDir); if (newPos.x != 9 || newPos.y != 9) { GameObject item = Instantiate(NumPrefab) as GameObject; Num numItem = item.GetComponent <Num>(); numItem.NumIndex = 2; numItem.InitX = (int)newPos.x; numItem.InitY = (int)newPos.y; item.transform.parent = this.transform; NumIndexArray[(int)newPos.x][(int)newPos.y] = 2; NumAssemblyArray[(int)newPos.x][(int)newPos.y] = numItem; StatisticsScore(); } else { //游戏失败//newX、newY均为9并且TouchDir不为none gameState = GameState.End; bool isEnd1 = true; bool isEnd2 = true; int index = 0; #region 循环判断是否有相邻的相同数字 for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { if (index == 0) { index = NumIndexArray[x][y]; } else { if (index != NumIndexArray[x][y]) { index = NumIndexArray[x][y]; } else { isEnd1 = false; break; } } } } index = 0; for (int y = 0; y < 4; y++) { for (int x = 0; x < 4; x++) { if (index == 0) { index = NumIndexArray[x][y]; } else { if (index != NumIndexArray[x][y]) { index = NumIndexArray[x][y]; } else { isEnd2 = false; break; } } } } #endregion if (!isEnd1 || !isEnd2) { gameState = GameState.Playing; } StatisticsScore(); } }
void MoveCtrl() { TouchDir dir = GetTouchDir(); Debug.Log(dir); // if player is grounded, he can turn left right or slide if ((cc.isGrounded)) { isJumping = false; if ((dir == TouchDir.Left) && (laneIndex > -1) && (!controlLocked)) { PlayAudio(1, 2f); // leftAnim = true; moveHorizontal = -4; laneIndex--; controlLocked = true; } // else // { // leftAnim = false; // } if ((dir == TouchDir.Right) && (laneIndex < 1) && (!controlLocked)) { PlayAudio(1, 2f); // rightAnim = true; moveHorizontal = 4; laneIndex++; controlLocked = true; } // else // { // rightAnim = false; // } if ((dir == TouchDir.Down) && (!isShrinking)) { PlayAudio(9, 1f); //Play animation to let player shrink shrinkAnim = true; Debug.Log("SLIDE"); //Change the height of charactercontroller cc.height = 1f; //Change the center of charactercontroller Vector3 tempCenter = cc.center; tempCenter.y -= 0.5f; cc.center = tempCenter; isShrinking = true; // Use Coroutine to make the charactercontroller resume StartCoroutine("ShrinkTimer"); } } else { //Let player have gravity moveDir.y -= gravity * Time.deltaTime; } //Jump control, double jump condition if ((dir == TouchDir.Up) && (!isJumping)) { PlayAudio(0, 1f); moveDir.y = jumpSpeed; jumpTimes++; anim.SetTrigger("JumpSet"); if (canDoubleJump && (jumpTimes < 2)) { isJumping = false; } else { isJumping = true; jumpTimes = 0; } } // Attack control, Attack condition if ((dir == TouchDir.Attack) && (canShoot)) { Debug.Log("Shoot it"); attackAnim = true; PlayAudio(8, 1f); // Instantiate a prefab to attack GameObject shootPrefab = Instantiate(swordLight, firePos.position, Quaternion.identity) as GameObject; shootPrefab.GetComponent <Rigidbody>().velocity = shootPrefab.transform.forward * shootSpeed; // Destroy this prefab after 1 second Destroy(shootPrefab, 1.0f); } else { attackAnim = false; } }
private void MoveNum() { bool isAnyNumMove = false; int countCombine = 0; //得到触摸或键盘输入的方向 if (Input.GetMouseButtonDown(0)) { mouseDownPosition = Input.mousePosition; } if (Input.GetMouseButtonUp(0) == false) { return; } TouchDir dir = GetTouchDir(); switch (dir) { case TouchDir.None: return; case TouchDir.Right: for (int y = 0; y < 4; y++) { Num preNum = null; int index = 4; for (int x = 3; x >= 0; x--) { bool isNeedUpdateComponentArray = true; if (numArray[x][y] == 0) { continue; } if (preNum == null) { preNum = numComponentArray[x][y]; index--; } else { if (preNum.nums == numComponentArray[x][y].nums) { //合并两个数字到目标位置 然后消失 然后创建出来合并后的数字 //this.GetComponent<dfControl>().AddPrefab(numPrefab).get countCombine++; GenerateNumber(index, y, preNum.nums + 1, 0.5f); //计算得分 ScoreManager._instance.AddScore((int)Mathf.Pow(2, preNum.nums + 1)); preNum.Disappear(); numComponentArray[x][y].Disappear(); preNum = null; isNeedUpdateComponentArray = false; } else { preNum = numComponentArray[x][y]; index--; } } //move to (index,y) if (numComponentArray[x][y].MoveToPosition(index, y, isNeedUpdateComponentArray)) { isAnyNumMove = true; } } } break; case TouchDir.Left: for (int y = 0; y < 4; y++) { Num preNum = null; int index = -1; for (int x = 0; x < 4; x++) { bool isNeedUpdateComponentArray = true; if (numArray[x][y] == 0) { continue; } if (preNum == null) { preNum = numComponentArray[x][y]; index++; } else { if (preNum.nums == numComponentArray[x][y].nums) { //合并两个数字到目标位置 然后消失 然后创建出来合并后的数字 //this.GetComponent<dfControl>().AddPrefab(numPrefab).get countCombine++; GenerateNumber(index, y, preNum.nums + 1, 0.5f); //计算得分 ScoreManager._instance.AddScore((int)Mathf.Pow(2, preNum.nums + 1)); preNum.Disappear(); numComponentArray[x][y].Disappear(); preNum = null; isNeedUpdateComponentArray = false; } else { preNum = numComponentArray[x][y]; index++; } } //move to (index,y) if (numComponentArray[x][y].MoveToPosition(index, y, isNeedUpdateComponentArray)) { isAnyNumMove = true; } } } break; case TouchDir.Top: for (int x = 0; x < 4; x++) { Num preNum = null; int index = -1; for (int y = 0; y < 4; y++) { bool isNeedUpdateComponentArray = true; if (numArray[x][y] == 0) { continue; } if (preNum == null) { preNum = numComponentArray[x][y]; index++; } else { if (preNum.nums == numComponentArray[x][y].nums) { //合并两个数字到目标位置 然后消失 然后创建出来合并后的数字 //this.GetComponent<dfControl>().AddPrefab(numPrefab).get countCombine++; GenerateNumber(x, index, preNum.nums + 1, 0.5f); //计算得分 ScoreManager._instance.AddScore((int)Mathf.Pow(2, preNum.nums + 1)); preNum.Disappear(); numComponentArray[x][y].Disappear(); preNum = null; isNeedUpdateComponentArray = false; } else { preNum = numComponentArray[x][y]; index++; } } //move to (index,y) if (numComponentArray[x][y].MoveToPosition(x, index, isNeedUpdateComponentArray)) { isAnyNumMove = true; } } } break; case TouchDir.Bottom: for (int x = 0; x < 4; x++) { Num preNum = null; int index = 4; for (int y = 3; y >= 0; y--) { bool isNeedUpdateComponentArray = true; if (numArray[x][y] == 0) { continue; } if (preNum == null) { preNum = numComponentArray[x][y]; index--; } else { if (preNum.nums == numComponentArray[x][y].nums) { //合并两个数字到目标位置 然后消失 然后创建出来合并后的数字 //this.GetComponent<dfControl>().AddPrefab(numPrefab).get countCombine++; GenerateNumber(x, index, preNum.nums + 1, 0.5f); //计算得分 ScoreManager._instance.AddScore((int)Mathf.Pow(2, preNum.nums + 1)); preNum.Disappear(); numComponentArray[x][y].Disappear(); preNum = null; isNeedUpdateComponentArray = false; } else { preNum = numComponentArray[x][y]; index--; } } //move to (index,y) if (numComponentArray[x][y].MoveToPosition(x, index, isNeedUpdateComponentArray)) { isAnyNumMove = true; } } } break; } if (countCombine > 0) { } if (isAnyNumMove) { GenerateNumber(); } if (CheckWin() || CheckGameOver()) { //显示出游戏结束的界面 } }
//行动状态 private void MoveNum() { bool isAnyNumMove = false; int countCombine = 0; if (Input.GetMouseButtonDown(0)) { mouseDownPosition = Input.mousePosition; } else if (!Input.GetMouseButtonUp(0)) { return; } TouchDir dir = GetTouchDir(); switch (dir) { //向左移动 case TouchDir.Left: for (int y = 0; y < 4; y++) { Num preNum = null; int index = -1; for (int x = 0; x < 4; x++) { bool isNeedUpdateComponentArray = true; if (m_NumArray[x, y] == 0) { continue; } if (preNum == null) { preNum = m_NumComponentArray[x, y]; index++; } else { if (preNum.num == m_NumComponentArray[x, y].num) { //如果当前 2048 预置体的值等于该 Num 表格中的 num 的值,则可以进行合并 countCombine++; GenerateNumber(index, y, preNum.num + 1); //表示 Y 轴不变,X 轴移动到 index 记数后的位置 if (m_CurrentNum < preNum.num + 1) { //如果当前 2048 预置体小过计数后合并的值 m_CurrentNum = preNum.num + 1; } LevelDirection.Instance.CurrentScore += (int)Mathf.Pow(2, preNum.num + 1); isNeedUpdateComponentArray = false; preNum.IsDisappear = true; m_NumComponentArray[x, y].IsDisappear = true; preNum = null; } else { preNum = m_NumComponentArray[x, y]; index++; } } // move to (index,y) if (m_NumComponentArray[x, y].MoveToPosition(index, y, isNeedUpdateComponentArray)) { isAnyNumMove = true; } } } break; case TouchDir.Right: for (int y = 0; y < 4; y++) { Num preNum = null; int index = 4; for (int x = 3; x >= 0; x--) { bool isNeedUpdateComponentArray = true; if (m_NumArray[x, y] == 0) { continue; } if (preNum == null) { preNum = m_NumComponentArray[x, y]; index--; } else { if (preNum.num == m_NumComponentArray[x, y].num) { countCombine++; GenerateNumber(index, y, preNum.num + 1); if (m_CurrentNum < preNum.num + 1) { m_CurrentNum = preNum.num + 1; } LevelDirection.Instance.CurrentScore += (int)Mathf.Pow(2, preNum.num + 1); isNeedUpdateComponentArray = false; preNum.IsDisappear = true; m_NumComponentArray[x, y].IsDisappear = true; preNum = null; } else { preNum = m_NumComponentArray[x, y]; index--; } } // move to (index,y) if (m_NumComponentArray[x, y].MoveToPosition(index, y, isNeedUpdateComponentArray)) { isAnyNumMove = true; } } } break; case TouchDir.Top: for (int x = 0; x < 4; x++) { Num preNum = null; int index = -1; for (int y = 0; y < 4; y++) { bool isNeedUpdateComponentArray = true; if (m_NumArray[x, y] == 0) { continue; } if (preNum == null) { preNum = m_NumComponentArray[x, y]; index++; } else { if (preNum.num == m_NumComponentArray[x, y].num) { countCombine++; GenerateNumber(x, index, preNum.num + 1); if (m_CurrentNum < preNum.num + 1) { m_CurrentNum = preNum.num + 1; } LevelDirection.Instance.CurrentScore += (int)Mathf.Pow(2, preNum.num + 1); isNeedUpdateComponentArray = false; preNum.IsDisappear = true; m_NumComponentArray[x, y].IsDisappear = true; preNum = null; } else { preNum = m_NumComponentArray[x, y]; index++; } } if (m_NumComponentArray[x, y].MoveToPosition(x, index, isNeedUpdateComponentArray)) { isAnyNumMove = true; } } } break; case TouchDir.Bottom: for (int x = 0; x < 4; x++) { Num preNum = null; int index = 4; for (int y = 3; y >= 0; y--) { bool isNeedUpdateComponentArray = true; if (m_NumArray[x, y] == 0) { continue; } if (preNum == null) { preNum = m_NumComponentArray[x, y]; index--; } else { if (preNum.num == m_NumComponentArray[x, y].num) { countCombine++; GenerateNumber(x, index, preNum.num + 1); if (m_CurrentNum < preNum.num + 1) { m_CurrentNum = preNum.num + 1; } LevelDirection.Instance.CurrentScore += (int)Mathf.Pow(2, preNum.num + 1); isNeedUpdateComponentArray = false; preNum.IsDisappear = true; m_NumComponentArray[x, y].IsDisappear = true; preNum = null; } else { preNum = m_NumComponentArray[x, y]; index--; } } if (m_NumComponentArray[x, y].MoveToPosition(x, index, isNeedUpdateComponentArray)) { isAnyNumMove = true; } } } break; case TouchDir.None: return; } if (countCombine > 0) { m_Audio.Play(); } if (isAnyNumMove) { GenerateNumber(); } if (CheckGameWin() || CheckGameOver()) { ShowGameOverPanel(); } }