Esempio n. 1
0
    //先更新移动再更新状态
    public void UpdateState(AnimalEntity diffSideFirstAnim, float roadLength, float otherPower)
    {
        for (int i = 0; i < SideAnimals.Count; i++)
        {
            //如果第一个动物未连接,则判断第一个动物和反向动物第一个碰头的时候设置为链接状态
            if (i == 0 && SideAnimals[0].CurState != AnimalState.Connect)
            {
                bool canConnect = false;
                if (diffSideFirstAnim != null)
                {
                    canConnect = CanDiffSideConnect(diffSideFirstAnim, SideAnimals[0], roadLength);
                }
                //如果链接起来则设置此方向的力度
                if (canConnect)
                {
                    SidePower += SideAnimals[0].Power;
                    SideAnimals[0].SetState(AnimalState.Connect);
                }
            }
            else
            {
                //如果对面没动物了 要把所有设置为非链接状态并且方向力清零
                if (diffSideFirstAnim == null)
                {
                    if (SideAnimals[i].CurState == AnimalState.Connect)
                    {
                        SideAnimals[i].SetState(AnimalState.Run);
                    }
                    SidePower = 0;
                }
                //如果为链接状态则按照链接状态移动
                if (SideAnimals[i].CurState == AnimalState.Run)
                {//非链接状态判断是否需要链接 和前面一个动物的距离差值是否为前一动物的身长 是则链接 否则则按照非链接状态移动
                    bool canConnect = false;
                    if (i > 0 && SideAnimals[0].CurState == AnimalState.Connect)
                    {
                        canConnect = CanSameSideConnect(SideAnimals[i - 1], SideAnimals[i]);
                    }
                    //如果链接起来方向力度增加
                    if (canConnect)
                    {
                        SidePower += SideAnimals[i].Power;
                        SideAnimals[i].SetState(AnimalState.Connect);
                    }
                }
            }
        }

        if (SideAnimals.Count > 0)
        {
            //判断地0个动物是否跑完 跑完则把后面链接的动物的状态设为run,并且把方向力设置为0
            if (SideAnimals[0].MoveDistance >= roadLength + 0.01f)
            {
                var entity = SideAnimals[0];
                if (entity.CurState == AnimalState.Connect)
                {
                    SidePower -= entity.Power;
                }
                entity.SetState(AnimalState.Finish);
                Points += entity.FinshPoint;
                SideAnimals.RemoveAt(0);
                AnimalFactory.AddToCollectList(entity);
                if (SideAnimals.Count > 0)
                {
                    SideAnimals[0].SetState(AnimalState.Run);
                }
                SidePower = 0;
                for (int i = 0; i < SideAnimals.Count; i++)
                {
                    //如果第0个动物是Run则后面每个动物都是run
                    if (SideAnimals[0].CurState == AnimalState.Run && SideAnimals[i].CurState == AnimalState.Connect)
                    {
                        SideAnimals[i].SetState(AnimalState.Run);
                    }
                }
            }
            //如果被对面推回去则方向力减小
            int lastIndex = SideAnimals.Count - 1;
            if (SideAnimals.Count > 0 && SideAnimals[lastIndex].MoveDistance <= 0)
            {
                var entity = SideAnimals[lastIndex];
                if (entity.CurState == AnimalState.Connect)
                {
                    SidePower -= entity.Power;
                }
                entity.SetState(AnimalState.Dead);
                SideAnimals.RemoveAt(lastIndex);
                AnimalFactory.AddToCollectList(entity);
            }
        }
    }