Пример #1
0
        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();
            }
        }
Пример #2
0
        //移动完成返回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);
            }
        }
Пример #3
0
        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();
            }
        }