//移动完成返回true,未完成返回false public bool MoveNumber(INumberObject[,] numbers, Vector2 origin, Vector2 dest, float speed) { INumberObject current_o = numbers[(int)origin.x, (int)origin.y]; INumberObject current_d = numbers[(int)dest.x, (int)dest.y]; Vector2 pos_current = current_o.GetCurrentPos(); Vector2 dir_origin = dest - pos_current; if (Mathf.Abs(dir_origin.magnitude) > 0.001) { pos_current += dir_origin * speed * Time.deltaTime; current_o.SetPosition(pos_current); return(false); } else { current_d.SetPosition(origin); numbers[(int)origin.x, (int)origin.y] = current_d; current_o.SetPosition(dest); numbers[(int)dest.x, (int)dest.y] = current_o; return(true); } }
void InitGame() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { Vector2 index = new Vector2(i, j); INumberObject numberObject = (INumberObject)gameRender.CreateObject(RenderProtocol.CreateNumberObject, 0); numberObject.SetNumber(2); numberObject.SetIndex(index); numberObject.SetPosition(index * IndexToPos); numberObjList.Add(numberObject); numberTable[i, j] = new NumberData { Index = new Vector2(i, j), TargetIndex = new Vector2(i, j), Number = 2 }; } } for (int i = 0; i < 4; i++) { SetNewNumber(2); } }
void MoveAllNum() { //通过targetIndexList来规划运动轨迹 CurrentTime += Time.deltaTime; if (CurrentTime < MoveTime) { for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { if (numberTable[x, y].TargetIndex != numberTable[x, y].Index) { Vector2 TargetPos = numberTable[x, y].TargetIndex * IndexToPos; Vector2 StartPos = numberTable[x, y].Index * IndexToPos; INumberObject numberObject = GetNumberObject(numberTable[x, y].Index); numberObject.SetPosition(Vector2.MoveTowards(numberObject.GetCurrentPos(), TargetPos, Vector2.Distance(TargetPos, StartPos) / (MoveTime / Time.deltaTime))); } } } } else { //foreach (INumberObject numberObject in numberObjList) //{ // Vector2 index = numberObject.GetIndex(); // Vector2 targetindex = numberTable[(int)index.x, (int)index.y].TargetIndex; // if (index != targetindex) // { // numberTable[(int)targetindex.x, (int)targetindex.y].Number += numberObject.GetNumber(); // numberTable[(int)index.x, (int)index.y].Number = 0; // } //} for (int x = 0; x < 4; x++) { for (int y = 0; y < 4; y++) { numberTable[x, y].Index = new Vector2(x, y); numberTable[x, y].TargetIndex = new Vector2(x, y); numberTable[x, y].bMerged = false; } } foreach (INumberObject numberObject in numberObjList) { Vector2 index = numberObject.GetIndex(); numberObject.SetPosition(index * IndexToPos); numberObject.SetNumber(numberTable[(int)index.x, (int)index.y].Number); } bGetInput = true; CurrentTime = 0; for (int i = 0; i < 2; i++) { SetNewNumber(2); } DebugLog(); } }
public void Moving() { speed += 10f * Time.deltaTime; Vector2 pos = moveNumberObj.GetCurrentPos(); pos += moveDir * speed; if (parent == null && Vector2.Dot(endPos - pos, moveDir) <= 0) { pos = endPos; moveFinished = true; } else if (parent != null && parent.moveFinished == true) { Vector2 tempEndPos = parent.moveNumberObj.GetCurrentPos() - distanceWithParent * moveDir; bool needMerge = parent.moveNumberObj.GetNumber() == moveNumberObj.GetNumber() && this.hasMerged == false && parent.hasMerged == false; if (needMerge) { tempEndPos = parent.moveNumberObj.GetCurrentPos(); } if (Vector2.Dot(tempEndPos - pos, moveDir) <= 0) { pos = tempEndPos; moveFinished = true; if (needMerge) { GameFramework.singleton.getGameRender().DestroyObject(this.moveNumberObj); parent.next = this.next; if (this.next != null) { this.next.parent = parent; } parent.hasMerged = true; parent.moveNumberObj.SetNumber(parent.moveNumberObj.GetNumber() * 2); } } } moveNumberObj.SetPosition(pos); if (next != null) { next.Moving(); } }
public void Moving(float speed, int i, int j) { Debug.Log("移动"); Vector2 last = numbers[i, j].GetLastIndex(); Vector2 current = numbers[i, j].GetIndex(); if (last != current) { Vector2 direct = (current - last).normalized; Vector2 currentPos = numbers[i, j].GetCurrentPos(); currentPos += direct * Time.deltaTime * speed * Mathf.Abs(current.x + current.y - last.x - last.y); numbers[i, j].SetPosition(currentPos); if (Mathf.Abs((currentPos - current).magnitude) < 0.1) { int num = numbers[i, j].GetDestNum(); GameFramework.singleton.getGameRender().DestroyObject(numbers[i, j]); INumberObject number1 = GameFramework.singleton.getGameRender().CreateObject(RenderProtocol.CreateNumberObject, size) as INumberObject; number1.SetNumber(num); number1.SetDestNum(num); number1.SetIndex(current); number1.SetLastIndex(current); number1.SetPosition(current); GameFramework.singleton.getGameRender().DestroyObject(numbers[(int)current.x, (int)current.y]); numbers[(int)current.x, (int)current.y] = number1; INumberObject number2 = GameFramework.singleton.getGameRender().CreateObject(RenderProtocol.CreateNumberObject, size) as INumberObject; number2.SetNumber(0); number2.SetNumber(0); number2.SetIndex(new Vector2(i, j)); number2.SetLastIndex(new Vector2(i, j)); number2.SetPosition(new Vector2(i, j)); numbers[i, j] = number2; } } }