예제 #1
0
        public void Update(GameTime gameTime)
        {
            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            factions.Update(gameTime);
            resources.Update(gameTime);
            ships.Update(gameTime);
            cameras.Update(gameTime);

            sea.Update(elapsedTime);
            if (ambientOn)
            {
                waterWave.Update(elapsedTime);
            }

            cannonBalls.Update(gameTime);
            islands.Update(gameTime);
            if (ambientOn)
            {
                lowerParticles.Update(gameTime);
            }
            if (ambientOn)
            {
                upperParticles.Update(gameTime);
            }
            if (ambientOn)
            {
                lowerAmbients.Update(gameTime);
            }
            if (ambientOn)
            {
                upperAmbients.Update(gameTime);
            }
            map.Update(gameTime);

            //by 刘欣 李翔
            timeManager.Update(gameTime);
            collector.Update(gameTime);

            foreach (var item in ships)
            {
                item.PerformCommand();
                Vector2 p = item.AbsolutePosition;
                if (p.X > mapSize.X / 2 || p.X < -mapSize.X / 2 || p.Y > mapSize.Y / 2 || p.Y < -mapSize.Y / 2)
                {
                    item.Armor -= OutOfRangeDamage * elapsedTime;
                }
            }

            float t = -(InputState.previousMouseState.ScrollWheelValue - InputState.currentMouseState.ScrollWheelValue) / 1000f;

            if (t != 0)
            {
                commonCamera.Scale += t;
            }
            #region 判断胜负
            if (!isGameEnd)
            {
                if (f1 != null && f2 != null)
                {
                    //by 刘欣 李翔 剩余时间为零
                    if (timeManager.FullMilisecond <= 0)
                    {
                        isGameEnd = true;
                        //首先判断船的数量
                        int s1 = ships.Count(p => p.Faction == f1);
                        int s2 = ships.Count(p => p.Faction == f2);
                        if (s1 > s2)
                        {
                            winner = 1;
                        }
                        else if (s1 < s2)
                        {
                            winner = 2;
                        }
                        else
                        {
                            //然后判断岛的数量
                            if (f1.ResourceNum > f2.ResourceNum)
                            {
                                winner = 1;
                            }
                            else if (f1.ResourceNum < f2.ResourceNum)
                            {
                                winner = 2;
                            }
                            else
                            {
                                //然后判断总血量
                                float a1 = 0;
                                float a2 = 0;
                                foreach (var s in ships)
                                {
                                    if (s.Faction == f1)
                                    {
                                        a1 += s.Armor;
                                    }
                                    else if (s.Faction == f2)
                                    {
                                        a2 += s.Armor;
                                    }
                                }
                                if (a1 > a2)
                                {
                                    winner = 1;
                                }
                                else if (a1 < a2)
                                {
                                    winner = 2;
                                }
                                else
                                {
                                    //最后判断造成的总伤害
                                    if (f1.TotalDamage > f2.TotalDamage)
                                    {
                                        winner = 1;
                                    }
                                    else if (f1.TotalDamage < f2.TotalDamage)
                                    {
                                        winner = 2;
                                    }
                                    else
                                    {
                                        winner = 0;
                                    }
                                }
                            }
                        }
                    }

                    if (ships.Count(p => p.Faction == f1) == 0)//如果f1全挂了
                    {
                        //其它操作有待添加
                        isGameEnd = true;

                        winner = 2;
                    }

                    if (ships.Count(p => p.Faction == f2) == 0)//如果f2全挂了
                    {
                        //其它操作有待添加
                        isGameEnd = true;

                        winner = 1;
                    }
                }
            }
            #endregion

            if (game.IsActive)
            {
                #region 调整视野
                float screenWInGame = game.GraphicsDevice.Viewport.Width / currentCamera.Scale;
                float screenHInGame = game.GraphicsDevice.Viewport.Height / currentCamera.Scale;

                if (InputState.CurrentMousePosition.X <= 2 || InputState.currentMouseState.Y <= 2 ||
                    InputState.CurrentMousePosition.X >= GameOperators.GraphicsDevice.Viewport.Width - 2 ||
                    InputState.CurrentMousePosition.Y >= GameOperators.GraphicsDevice.Viewport.Height - 2)
                {
                    Vector2 screenCenter = new Vector2(GameOperators.GraphicsDevice.Viewport.Width / 2, GameOperators.GraphicsDevice.Viewport.Height / 2);
                    if (screenCenter != InputState.CurrentMousePosition)
                    {
                        Vector2 translator = Vector2.Normalize(InputState.CurrentMousePosition - screenCenter) * 1000f * (float)gameTime.ElapsedGameTime.TotalSeconds / currentCamera.Scale;
                        float   borderX    = Math.Abs(MapSize.X / 2 - screenWInGame / 2);
                        float   borderY    = Math.Abs(MapSize.Y / 2 - screenHInGame / 2);
                        //currentCamera.Center += translator;
                        if (translator.X > 0 && currentCamera.Center.X <= borderX)
                        {
                            currentCamera.Center.X += translator.X;
                        }
                        else if (translator.X < 0 && currentCamera.Center.X >= -borderX)
                        {
                            currentCamera.Center.X += translator.X;
                        }
                        if (translator.Y > 0 && currentCamera.Center.Y <= borderY)
                        {
                            currentCamera.Center.Y += translator.Y;
                        }
                        else if (translator.Y < 0 && currentCamera.Center.Y >= -borderY)
                        {
                            currentCamera.Center.Y += translator.Y;
                        }
                    }
                }

                #endregion
                if (InputState.IsKeyPressed(Keys.Space))
                {
                    currentCamera.Center = Vector2.Zero;
                }
                if (InputState.IsKeyPressed(Keys.Escape))
                {
                    isGameEnd = true;
                }
                #region  标选择
                mouseOnShip = null;
                if (InputState.currentMouseState.LeftButton == ButtonState.Pressed)
                {
                    if (!mouseDowned)
                    {
                        rec0        = InputState.CurrentMousePosition;
                        rec1        = rec0;
                        mouseDowned = true;
                    }
                    else
                    {
                        rec1 = InputState.CurrentMousePosition;
                    }
                }
                else if (InputState.currentMouseState.LeftButton == ButtonState.Released)
                {
                    if (mouseDowned)
                    {
                        bool playerControled = ships.Count(p => p.Faction.ControllerType == FactionControllerType.Player) > 0;
                        humanSelectedShips.Clear();

                        float ax, bx, ay, by;
                        ax = currentCamera.ReversePoint(rec0).X;
                        ay = currentCamera.ReversePoint(rec0).Y;
                        bx = currentCamera.ReversePoint(rec1).X;
                        by = currentCamera.ReversePoint(rec1).Y;
                        BoundingBox bb = new BoundingBox(new Vector3(MathHelper.Min(ax, bx), MathHelper.Min(ay, by), 0), new Vector3(MathHelper.Max(ax, bx), MathHelper.Max(ay, by), 0));

                        foreach (var item in ships)
                        {
                            BoundingSphere bs = new BoundingSphere(new Vector3(item.AbsolutePosition, 0), item.SelectionRadius);
                            if (bb.Intersects(bs))
                            {
                                if (!playerControled || (playerControled && item.Faction.ControllerType == FactionControllerType.Player))
                                {
                                    humanSelectedShips.Add(item);
                                }
                            }
                        }
                        mouseDowned = false;
                    }
                }

                foreach (var item in ships)
                {
                    if (item.IsMouseOn())
                    {
                        mouseOnShip = item;

                        if (InputState.CursorState == Base.CursorState.Normal)
                        {
                            InputState.CursorState = Base.CursorState.Select;
                        }

                        if (InputState.IsMouseButtonPressed(MouseButton.LeftButton) && (rec0 == rec1))
                        {
                            humanSelectedShips.Clear();
                            humanSelectedShips.Add(item);//选中船只
                        }
                        break;
                    }
                }
                #endregion
            }
            texts.Update(gameTime);

            foreach (var item in ships)
            {
                foreach (var item2 in ships)
                {
                    if (item != item2)
                    {
                        float d = 0;
                        if (IsShipCollided(item, item2, ref d))
                        {
                            if (Vector2.DistanceSquared(item.Positionl, item2.AbsolutePosition) > d)//如果碰撞且距离拉近(卡住)
                            {
                                float dmg = item.GetKnockDamage(item2);
                                if (dmg > 0)
                                {
                                    item2.ReceiveDamage(dmg, 0.1f, true);
                                    item.ReceiveDamage(dmg / 2, 0.1f, true);
                                }
                                item.Block(Vector2.Normalize(item.AbsolutePosition - item2.AbsolutePosition) * (item.BoundingRadius + item2.BoundingRadius + 0.2f - Vector2.Distance(item.AbsolutePosition, item2.AbsolutePosition)));
                                //item.Block();//那么就触发碰撞事件

                                /*if (!BlockedShips.Contains(item) && !BlockedShips.Contains(item2))
                                 * {
                                 *  BlockedShips.Add(item);
                                 *  BlockedShips.Add(item2);
                                 * }*/
                            }
                        }

                        //d < (float)Math.Pow(object1.BoundingRadius + object2.BoundingRadius,2)
                        if (item.IsMoving && Vector2.DistanceSquared(item.AbsolutePosition, item2.AbsolutePosition) <= (float)Math.Pow(item.BoundingRadius + item2.BoundingRadius + 0.5, 2))
                        {
                            item.IsBlocked = true;
                        }
                    }
                }
                foreach (var island in islands)
                {
                    float d = 0;
                    if (IsShipCollided(item, island, ref d))
                    {
                        if (Vector2.DistanceSquared(item.Positionl, island.AbsolutePosition) > d)    //如果碰撞且距离拉近(卡住)
                        {
                            item.Block(Vector2.Normalize(item.AbsolutePosition - island.AbsolutePosition) * (item.BoundingRadius + island.BoundingRadius + 0.2f - Vector2.Distance(item.AbsolutePosition, island.AbsolutePosition)));

                            /*if (!BlockedShips.Contains(item))
                             * {
                             *  BlockedShips.Add(item);
                             * }*/
                        }
                    }
                }
            }

            /*if (BlockedShips.Count != 0)
             * {
             *  MoreBlocked();
             *  foreach (Ship s in BlockedShips)
             *  {
             *      s.Block();
             *  }
             *  BlockedShips.Clear();
             * }*/
        }