Example #1
0
        internal void MirrorPath(CollisionResult result)
        {
            Vector2 curVel       = ((NonInertiasPhiUpdater)this.PhisicalUpdater).Vel;
            float   mirVecLength = Vector2.Dot(curVel, result.NormalVector);
            Vector2 horizVel     = curVel - mirVecLength * result.NormalVector;
            Vector2 newVel       = horizVel + Math.Abs(mirVecLength) * result.NormalVector;

            ((NonInertiasPhiUpdater)this.PhisicalUpdater).Vel = newVel;
            ((NonInertiasPhiUpdater)this.PhisicalUpdater).Azi = MathTools.AziFromRefPos(newVel);
        }
Example #2
0
        public void Update(float seconds)
        {
            if (enemyInfo == null)
            {
                return;
            }

            Vector2 selfToEnemyVec = orderServer.Pos - enemyInfo.Pos;
            float   selfToEnemyAzi = MathTools.AziFromRefPos(selfToEnemyVec);
            float   deltaAzi       = selfToEnemyAzi - enemyInfo.TurretAimAzi;
            bool    deltaAngWise   = deltaAzi > 0;

            if (deltaAngWise != lastDeltaAngWise)
            {
                lastDeltaAngWise = deltaAngWise;
                Authorize();
            }
        }
Example #3
0
        public void Update(float seconds)
        {
            Vector2 curPos = orderServer.Pos;

            if (InputHandler.MouseJustPressRight)
            {
                aimPos = InputHandler.GetCurMousePosInLogic(BaseGame.RenderEngine);
                float aimAzi = MathTools.AziFromRefPos(aimPos - curPos);
                action.StopMove();
                action.StopRota();

                rotaing = true;
                action.AddOrder(new OrderRotaToAzi(aimAzi, 0,
                                                   delegate(IActionOrder order)
                {
                    rotaing = false;
                    SearchPath();
                }, false));

                waitOrder = false;
            }

            if (waitOrder)
            {
                return;
            }

            if (!rotaing)
            {
                SearchPath();
            }

            if (Vector2.Distance(curPos, aimPos) < 1)
            {
                action.StopMove();
                action.StopRota();
                waitOrder = true;
            }


            action.Update(seconds);
        }
Example #4
0
        public void Authorize()
        {
            if (enemyInfo == null)
            {
                return;
            }

            Vector2 selfToEnemyVec = orderServer.Pos - enemyInfo.Pos;
            float   selfToEnemyAzi = MathTools.AziFromRefPos(selfToEnemyVec);
            float   deltaAzi       = MathTools.AngTransInPI(selfToEnemyAzi - enemyInfo.TurretAimAzi);

            lastDeltaAngWise = deltaAzi > 0;

            Vector2 aimDir = Vector2.Transform(selfToEnemyVec,
                                               Matrix.CreateRotationZ((deltaAzi > 0 ? MathHelper.PiOver2 : -MathHelper.PiOver2) + deltaAzi));

            Area curArea = FindArea();

            if (curArea != Area.Middle)
            {
                Vector2 borderDir = FindBorderDir(curArea);
                if (Vector2.Dot(aimDir, borderDir) < 0)
                {
                    aimDir = -aimDir;
                }
            }

            float aimAzi = MathTools.AziFromRefPos(aimDir);

            Vector2 selfDir = orderServer.Direction;

            if (Vector2.Dot(selfDir, aimDir) > 0)
            {
                action.AddOrder(new OrderRotaToAzi(aimAzi));
                action.AddOrder(new OrderMove(100));
            }
            else
            {
                action.AddOrder(new OrderRotaToAzi(aimAzi + MathHelper.Pi));
                action.AddOrder(new OrderMove(-100));
            }
        }
Example #5
0
        public void Authorize()
        {
            Vector2 curPos   = orderServer.Pos;
            Vector2 enemyVec = Vector2.Normalize(curPos - enemyPos);
            Vector2 curDir   = orderServer.Direction;

            Vector2 lineVec = new Vector2(enemyVec.Y, -enemyVec.X);

            if (Vector2.Dot(lineVec, curDir) < 0)
            {
                lineVec = -lineVec;
            }

            Vector2 departVec = Vector2.Zero;

            if (lastState == State.Far)
            {
                departVec = -enemyVec;
            }
            else
            {
                departVec = enemyVec;
            }

            Vector2 aimVec = Vector2.Normalize(lineVec * departFactor + departVec);

            action.AddOrder(new OrderRotaToAzi(MathTools.AziFromRefPos(aimVec)));
            if (Vector2.Dot(departVec, curDir) < 0 && lastState == State.Near)
            {
                action.AddOrder(new OrderMove(-maxDistance));
            }
            else if (RandomHelper.GetRandomFloat(-1, 1) > -0.3f)
            {
                action.AddOrder(new OrderMove(maxDistance));
            }
            else
            {
                action.AddOrder(new OrderMove(-maxDistance));
            }
        }
Example #6
0
        public void Observe()
        {
            List <IEyeableInfo> eyeableInfo = orderServer.GetEyeableInfo();

            if (eyeableInfo.Count != 0)
            {
                IEyeableInfo first = eyeableInfo[0];
                if (first is TankSinTur.TankCommonEyeableInfo)
                {
                    enemyInfo = (TankSinTur.TankCommonEyeableInfo)first;
                    float selfToEnemyAzi = MathTools.AziFromRefPos(orderServer.Pos - enemyInfo.Pos);
                    float enemyTurretAzi = enemyInfo.TurretAimAzi;
                    if (Math.Abs(MathTools.AngTransInPI(enemyTurretAzi - selfToEnemyAzi)) < guardAng)
                    {
                        curPriority = ConsiderPriority.High;
                        return;
                    }
                }
            }

            curPriority = ConsiderPriority.Vacancy;
        }
Example #7
0
        public void Authorize()
        {
            float curTurretAzi = orderServer.TurretAimAzi;

            float t = 0;

            float   maxt     = 30;
            Vector2 aimPos   = Vector2.Zero;
            bool    canShoot = false;

            while (t < maxt)
            {
                aimPos = enemyPos + enemyVel * t;
                float timeRota  = Math.Abs(MathTools.AngTransInPI(MathTools.AziFromRefPos(aimPos - orderServer.TurretAxePos) - curTurretAzi) / orderServer.MaxRotaTurretSpeed);
                float timeShell = (Vector2.Distance(aimPos, orderServer.TurretAxePos) - orderServer.TurretLength) / orderServer.ShellSpeed;
                if (MathTools.FloatEqual(timeRota + timeShell, t, 0.05f))
                {
                    canShoot = true;
                    break;
                }
                t += 0.05f;
            }

            if (canShoot)
            {
                aimmingTime = t;
                aimmingPos  = aimPos;
                aimming     = true;
                action.AddOrder(new OrderRotaTurretToPos(aimPos, 0,
                                                         delegate(IActionOrder order)
                {
                    orderServer.Fire();
                    aimming = false;
                }, true));
            }
        }
Example #8
0
 public void Draw()
 {
     if (!isEnd)
     {
         BaseGame.SpriteMgr.alphaSprite.Draw(tex, BaseGame.CoordinMgr.ScreenPos(basePos + curPos), sourceRect, curColor, MathTools.AziFromRefPos(curDir), texOrign,
                                             curRadius / (float)tex.Width, SpriteEffects.None, layerDepth);
     }
 }
Example #9
0
        private void SearchPath()
        {
            Vector2 curPos = orderServer.Pos;
            float   curAzi = MathTools.AngTransInPI(orderServer.Azi);
            float   aimAzi = MathTools.AziFromRefPos(aimPos - curPos);

            bool  aimObstruct = false;
            bool  curObstruct = false;
            bool  crossPi     = false;
            bool  crossZero   = false;
            float minAziMinus = 0;
            float maxAziMinus = -MathHelper.Pi;
            float minAziPlus  = MathHelper.Pi;
            float maxAziPlus  = 0;

            foreach (EyeableBorderObjInfo borderObjInfo in orderServer.EyeableBorderObjInfos)
            {
                //if (!((SceneCommonObjInfo)borderObjInfo.EyeableInfo.ObjInfo.SceneInfo).isTankObstacle)
                //    continue;

                if (InputHandler.JustPressKey(Microsoft.Xna.Framework.Input.Keys.B))
                {
                    //borderObjInfo.UpdateConvexHall();

                    foreach (BordPoint bordP in borderObjInfo.Border.VisiBorder)
                    {
                        Vector2 logicP = Vector2.Transform(ConvertHelper.PointToVector2(bordP.p), borderObjInfo.EyeableInfo.CurTransMatrix);
                        float   azi    = MathTools.AziFromRefPos(logicP - curPos);

                        if (azi < 0)
                        {
                            minAziMinus = Math.Min(minAziMinus, azi);
                            maxAziMinus = Math.Max(maxAziMinus, azi);
                        }
                        else
                        {
                            minAziPlus = Math.Min(minAziPlus, azi);
                            maxAziPlus = Math.Max(maxAziPlus, azi);
                        }

                        if (MathTools.FloatEqualZero(MathTools.AngTransInPI(azi - MathHelper.Pi), 0.1f))
                        {
                            crossPi = true;
                        }

                        if (MathTools.FloatEqualZero(azi, 0.1f))
                        {
                            crossZero = true;
                        }

                        if (MathTools.FloatEqual(azi, aimAzi, 0.1f) && Vector2.Distance(logicP, curPos) < Vector2.Distance(aimPos, curPos))
                        {
                            aimObstruct = true;
                        }

                        if (MathTools.FloatEqual(curAzi, azi, 0.1f) && Vector2.Distance(logicP, curPos) < Vector2.Distance(aimPos, curPos))
                        {
                            curObstruct = true;
                        }
                    }
                }
            }

            if (!aimObstruct)
            {
                if (!MathTools.FloatEqual(curAzi, aimAzi, 0.1f))
                {
                    action.AddOrder(new OrderMoveToPosDirect(aimPos));
                }
                else
                {
                    orderServer.ForwardSpeed = orderServer.MaxForwardSpeed;
                }
            }
            else if (!curObstruct)
            {
                orderServer.ForwardSpeed = orderServer.MaxForwardSpeed;
            }
            else
            {
                orderServer.ForwardSpeed = 0;

                float aziEage1 = 0;
                float aziEage2 = 0;

                if (!crossZero && !crossPi)
                {
                    if (minAziMinus == 0)
                    {
                        aziEage1 = minAziPlus;
                        aziEage2 = maxAziPlus;
                    }
                    else
                    {
                        aziEage1 = minAziMinus;
                        aziEage2 = maxAziMinus;
                    }
                }
                else if (crossZero)
                {
                    aziEage1 = minAziMinus;
                    aziEage2 = maxAziPlus;
                }
                else if (crossPi)
                {
                    aziEage1 = minAziPlus;
                    aziEage2 = maxAziMinus;
                }
                else
                {
                }

                float curAimAzi = 0;

                if (Math.Abs(MathTools.AngTransInPI(curAzi - aziEage1)) < Math.Abs(MathTools.AngTransInPI(curAzi - aziEage2)))
                {
                    curAimAzi = aziEage1 - 0.1f;
                }
                else
                {
                    curAimAzi = aziEage2 + 0.1f;
                }

                rotaing = true;
                action.AddOrder(new OrderRotaToAzi(curAimAzi, 0,
                                                   delegate(IActionOrder order)
                {
                    rotaing = false;
                    orderServer.ForwardSpeed = orderServer.MaxForwardSpeed;
                }, false));
            }
        }
Example #10
0
        private void HandlerControl(float seconds)
        {
            if (!isDead && openControl)
            {
                float maxSpeed = SpaceWarConfig.SpeedMax;
                float accel    = SpaceWarConfig.SpeedAccel;
                if (stillTimer < 0)
                {
                    // 硬直状态
                    // maxSpeed *= cStillSpeedScale;
                    accel *= SpaceWarConfig.StillSpeedScale;
                }

                #region  获取键盘输入

                bool Up    = InputHandler.IsKeyDown(Keys.W);
                bool Left  = InputHandler.IsKeyDown(Keys.A);
                bool Down  = InputHandler.IsKeyDown(Keys.S);
                bool Right = InputHandler.IsKeyDown(Keys.D);

                Vector2 deltaSpeed = new Vector2();
                bool    noInput    = false;

                // 左上
                if (Up && Left && !Right && !Down)
                {
                    deltaSpeed.X = -cRootTwoDivideTwo;
                    deltaSpeed.Y = -cRootTwoDivideTwo;
                }
                // 右上
                else if (Up && Right && !Left && !Down)
                {
                    deltaSpeed.X = cRootTwoDivideTwo;
                    deltaSpeed.Y = -cRootTwoDivideTwo;
                }
                // 右下
                else if (Down && Right && !Up && !Left)
                {
                    deltaSpeed.X = cRootTwoDivideTwo;
                    deltaSpeed.Y = cRootTwoDivideTwo;
                }
                // 左下
                else if (Down && Left && !Up && !Right)
                {
                    deltaSpeed.X = -cRootTwoDivideTwo;
                    deltaSpeed.Y = cRootTwoDivideTwo;
                }
                // 上
                else if (Up && !Down)
                {
                    deltaSpeed.X = 0;
                    deltaSpeed.Y = -1.0f;
                }
                // 下
                else if (Down && !Up)
                {
                    deltaSpeed.X = 0;
                    deltaSpeed.Y = 1.0f;
                }
                // 左
                else if (Left && !Right)
                {
                    deltaSpeed.X = -1.0f;
                    deltaSpeed.Y = 0;
                }
                // 右
                else if (Right && !Left)
                {
                    deltaSpeed.X = 1.0f;
                    deltaSpeed.Y = 0;
                }
                else
                {
                    noInput = true;
                }

                #endregion

                #region 获得鼠标状态

                if (InputHandler.LastMouseLeftDown)
                {
                    Vector2 mousePos = InputHandler.GetCurMousePosInLogic(BaseGame.RenderEngine);
                    float   shootAzi = MathTools.AziFromRefPos(mousePos - this.Pos);

                    this.Shoot(shootAzi);
                }

                #endregion


                if (!noInput)
                {
                    phisicalUpdater.Azi = MathTools.AziFromRefPos(Vel);
                    deltaSpeed         *= seconds * accel;
                    Vel += deltaSpeed;
                    float SpeedAbs = Vel.Length();
                    if (SpeedAbs > maxSpeed)
                    {
                        Vel *= maxSpeed / SpeedAbs;
                    }
                }
                else // 衰减
                {
                    // 假设时间间隔是均匀的,并且间隔很小。
                    if (SpaceWarConfig.SpeedDecay * seconds < 1)
                    {
                        Vel *= (1 - SpaceWarConfig.SpeedDecay * seconds);
                    }
                }
                phisicalUpdater.Vel = Vel;
            }
        }
Example #11
0
        public void Update(float seconds)
        {
            #region OLDCODE
            //if (InputHandler.IsKeyDown( Keys.W ))
            //{
            //    orderServer.ForwardSpeed = 1000;
            //}
            //else if (InputHandler.IsKeyDown( Keys.S ))
            //{
            //    orderServer.ForwardSpeed = -1000;
            //}
            //else
            //{
            //    orderServer.ForwardSpeed = 0;
            //}

            //if (InputHandler.IsKeyDown( Keys.D ))
            //{
            //    orderServer.TurnRightSpeed = 20;
            //}
            //else if (InputHandler.IsKeyDown( Keys.A ))
            //{
            //    orderServer.TurnRightSpeed = -20;
            //}
            //else
            //{
            //    orderServer.TurnRightSpeed = 0;
            //}
            #endregion

            aimmingTime -= seconds;

            List <IEyeableInfo> eyeableInfos = orderServer.GetEyeableInfo();
            if (eyeableInfos.Count != 0)
            {
                orderServer.TurnRaderWiseSpeed = 0;

                if (eyeableInfos[0] is ItemEyeableInfo)
                {
                    lastRaderRotaWise = MathTools.AziFromRefPos(((ItemEyeableInfo)eyeableInfos[0]).Pos - orderServer.Pos) > 0;
                    action.AddOrder(new OrderRotaRaderToPos(((ItemEyeableInfo)eyeableInfos[0]).Pos));
                }
                else if (eyeableInfos[0] is TankSinTur.TankCommonEyeableInfo)
                {
                    lastRaderRotaWise = MathTools.AziFromRefPos(((TankSinTur.TankCommonEyeableInfo)eyeableInfos[0]).Pos - orderServer.Pos) > 0;
                    action.AddOrder(new OrderRotaRaderToPos(((TankSinTur.TankCommonEyeableInfo)eyeableInfos[0]).Pos));
                }

                if (eyeableInfos[0] is ItemEyeableInfo)
                {
                    lastItemPos = ((ItemEyeableInfo)eyeableInfos[0]).Pos;
                    vel         = ((ItemEyeableInfo)eyeableInfos[0]).Vel;
                }
                else if (eyeableInfos[0] is TankSinTur.TankCommonEyeableInfo)
                {
                    lastItemPos = ((TankSinTur.TankCommonEyeableInfo)eyeableInfos[0]).Pos;
                    vel         = ((TankSinTur.TankCommonEyeableInfo)eyeableInfos[0]).Vel;
                }


                if (!aimming)
                {
                    float curTurretAzi = orderServer.TurretAimAzi;

                    float t = 0;

                    float maxt     = 30;
                    bool  canShoot = false;
                    while (t < maxt)
                    {
                        aimPos = lastItemPos + vel * t;
                        float timeRota  = Math.Abs(MathTools.AngTransInPI(MathTools.AziFromRefPos(aimPos - orderServer.TurretAxePos) - curTurretAzi) / orderServer.MaxRotaTurretSpeed);
                        float timeShell = (Vector2.Distance(aimPos, orderServer.TurretAxePos) - orderServer.TurretLength) / orderServer.ShellSpeed;
                        if (MathTools.FloatEqual(timeRota + timeShell, t, 0.03f))
                        {
                            canShoot = true;
                            break;
                        }
                        t += 0.03f;
                    }

                    if (canShoot)
                    {
                        aimming     = true;
                        aimmingTime = t;
                        action.AddOrder(new OrderRotaTurretToPos(aimPos, 0,
                                                                 delegate(IActionOrder order)
                        {
                            orderServer.Fire();
                            aimming = false;
                        }, false));
                    }
                    else
                    {
                        action.AddOrder(new OrderRotaTurretToPos(lastItemPos));
                    }
                }
                else
                {
                    if ((lastItemPos + aimmingTime * vel - aimPos).Length() > 4)
                    {
                        aimming = false;
                    }
                }
            }
            else
            {
                orderServer.TurnRaderWiseSpeed = orderServer.MaxRotaRaderSpeed * (lastRaderRotaWise ? 1 : -1);
            }

            action.Update(seconds);
        }