示例#1
0
        public void Update()
        {
            if (!GameClass.Game.GameInputEnabled || DesyncPause)
            {
                return;
            }

            EditorUpdate();

            float FpsRateModifier = 1;

            //const float MaxZoomOut = 5.33333f, MaxZoomIn = 200f;
            //const float MaxZoomOut = 3.2f, MaxZoomIn = 200f;
            //const float MaxZoomOut = 1f, MaxZoomIn = 200f; // Full zoom-in/out

            float MaxZoomOut, MaxZoomIn;

            if (MapEditorActive)
            {
                // Full zoom-in/out
                MaxZoomOut = .733f;
                MaxZoomIn  = 200f;
            }
            else
            {
                MaxZoomOut = World.StaticMaxZoomOut;
                MaxZoomIn  = 200f; // Full zoom-in, Partial zoom-out
            }

            // Focus on player's dragon lord.
            if (!GameClass.Game.ShowChat && Keys.Space.Down())
            {
                var dl_pos = PrevDragonLordPos[MyPlayerNumber];

                if (dl_pos > vec(1, 1))
                {
                    CameraPos  = GridToWorldCood(dl_pos);
                    CameraZoom = 24;
                }
            }

            // Zoom all the way out
            //if (!GameClass.Game.ShowChat && Keys.Space.Down())
            //{
            //    CameraZoom = MaxZoomOut;
            //}

            // Zoom in/out, into the location of the cursor
            var world_mouse_pos = ScreenToWorldCoord(Input.CurMousePos);
            var hold_camvec     = camvec;

            if (GameClass.MouseEnabled)
            {
                float MouseWheelZoomRate = 1.3333f * FpsRateModifier;
                if (Input.DeltaMouseScroll < 0)
                {
                    CameraZoom /= MouseWheelZoomRate;
                }
                else if (Input.DeltaMouseScroll > 0)
                {
                    CameraZoom *= MouseWheelZoomRate;
                }
            }

            float KeyZoomRate = 1.125f * FpsRateModifier;

            if (!GameClass.Game.ShowChat && (Buttons.X.Down() || Keys.X.Down() || Keys.E.Down()))
            {
                CameraZoom /= KeyZoomRate;
            }
            else if (!GameClass.Game.ShowChat && (Buttons.A.Down() || Keys.Z.Down() || Keys.Q.Down()))
            {
                CameraZoom *= KeyZoomRate;
            }

            if (CameraZoom < MaxZoomOut)
            {
                CameraZoom = MaxZoomOut;
            }
            if (CameraZoom > MaxZoomIn)
            {
                CameraZoom = MaxZoomIn;
            }

            if (GameClass.MouseEnabled && !(Buttons.A.Pressed() || Buttons.X.Pressed()))
            {
                if (MouseOverMinimap)
                {
                    //var zoom_center = (UiMousePos - MinimapQuad.pos) / MinimapQuad.size;

                    //var shifted = GetShiftedCameraMinimap(Input.CurMousePos, camvec, zoom_center);
                    //CameraPos = shifted;
                }
                else
                {
                    vec2 zoom_center = world_mouse_pos;

                    var shifted = GetShiftedCamera(Input.CurMousePos, camvec, zoom_center);
                    CameraPos = shifted;
                }
            }

            // Move the camera via: Click And Drag
            //float MoveRate_ClickAndDrag = .00165f * FpsRateModifier;
            //if (Input.LeftMouseDown)
            //    CameraPos += Input.DeltaMousPos / CameraZoom * MoveRate_ClickAndDrag * new vec2(-1, 1);

            // Move the camera via: Push Edge
            if (GameClass.MouseEnabled &&
                (GameClass.Game.CurrentConfig.Fullscreen || BoxSelecting) &&
                (!Program.DisableScreenEdge || BoxSelecting))
            {
                float MoveRate_PushEdge = .075f * FpsRateModifier;
                var   push_dir          = vec2.Zero;
                float EdgeRatio         = .005f;
                push_dir.x += -CoreMath.Restrict(0, 1, (EdgeRatio * GameClass.Screen.x - Input.CurMousePos.x) / (EdgeRatio * GameClass.Screen.x));
                push_dir.x += CoreMath.Restrict(0, 1, (Input.CurMousePos.x - (1 - EdgeRatio) * GameClass.Screen.x) / (EdgeRatio * GameClass.Screen.x));
                push_dir.y -= -CoreMath.Restrict(0, 1, (EdgeRatio * GameClass.Screen.y - Input.CurMousePos.y) / (EdgeRatio * GameClass.Screen.y));
                push_dir.y -= CoreMath.Restrict(0, 1, (Input.CurMousePos.y - (1 - EdgeRatio) * GameClass.Screen.y) / (EdgeRatio * GameClass.Screen.y));

                CameraPos += push_dir / CameraZoom * MoveRate_PushEdge;
            }

            // Move the camera via: Keyboard or Gamepad
            if (!GameClass.Game.ShowChat && !(MapEditorActive && InputHelper.CtrlDown()))
            {
                var dir = Input.Direction();

                float MoveRate_Keyboard = .07f * FpsRateModifier;
                CameraPos += dir / CameraZoom * MoveRate_Keyboard;
            }

            // Move the camera via: Minimap
            if ((LeftMouseDown || Input.DeltaMouseScroll != 0) && !BoxSelecting && MouseOverMinimap)
            {
                CameraPos = MinimapWorldPos();
            }


            // Make sure the camera doesn't go too far offscreen
            y_edge = 1 + 0.433f / CameraZoom;
            x_edge = Math.Max(.5f * (CameraAspect / CameraZoom) + .5f * (CameraAspect / MaxZoomOut), 1); // Old style zoom out bounds.
            x_edge = Math.Min(x_edge, y_edge * CameraAspect);
            x_edge = CoreMath.LogLerpRestrict(MaxZoomIn, 1 + .35f / CameraZoom, MaxZoomOut, x_edge, CameraZoom);

            if (CameraZoom == MaxZoomOut)
            {
                CameraPos = vec(0, -0.07f);
            }

            var TR = ScreenToWorldCoord(new vec2(GameClass.Screen.x, 0));

            if (TR.x > x_edge)
            {
                CameraPos = new vec2(CameraPos.x - (TR.x - x_edge), CameraPos.y);
            }
            if (TR.y > y_edge)
            {
                CameraPos = new vec2(CameraPos.x, CameraPos.y - (TR.y - y_edge));
            }
            var BL = ScreenToWorldCoord(new vec2(0, GameClass.Screen.y));

            if (BL.x < -x_edge)
            {
                CameraPos = new vec2(CameraPos.x - (BL.x + x_edge), CameraPos.y);
            }
            if (BL.y < -y_edge)
            {
                CameraPos = new vec2(CameraPos.x, CameraPos.y - (BL.y + y_edge));
            }


            // Switch to chat
            if (!GameClass.Game.ShowChat && Keys.Enter.Pressed() && ChatInhibitor <= 0)
            {
                GameClass.Game.ChatGlobal = !(Keys.LeftShift.Down() || Keys.RightShift.Down());
                GameClass.Game.ToggleChatViaFlag();
            }
            if (GameClass.Game.ShowChat)
            {
                ChatInhibitor = 5; return;
            }
            if (ChatInhibitor > 0 && !Keys.Enter.Down())
            {
                ChatInhibitor--;
            }

            // Switch input modes

            // Switch to spells (must be playing, not in editor)
            if (!SimulationPaused)
            {
                if (Keys.D1.Pressed())
                {
                    StartSpell(Spells.Fireball);
                }
                if (Keys.D2.Pressed())
                {
                    StartSpell(Spells.SkeletonArmy);
                }
                if (Keys.D3.Pressed())
                {
                    StartSpell(Spells.Necromancer);
                }
                if (Keys.D4.Pressed())
                {
                    StartSpell(Spells.TerracottaArmy);
                }
            }

            // Switch to building placement
            if (Keys.B.Down())
            {
                StartPlacingBuilding(UnitType.Barracks);
            }
            if (Keys.G.Down())
            {
                StartPlacingBuilding(UnitType.GoldMine);
            }
            if (Keys.J.Down())
            {
                StartPlacingBuilding(UnitType.JadeMine);
            }

            // Switch to standard select
            if (Keys.Escape.Down() || Keys.Back.Down() || Input.RightMousePressed)
            {
                SetModeToSelect();
            }

            // Switch to unit placement (editor only)
            if (MapEditorActive)
            {
                if (Keys.R.Down())
                {
                    StartUnitPaint(UnitType.Footman);
                }
                if (Keys.T.Down())
                {
                    StartUnitPaint(UnitType.DragonLord);
                }
                if (Keys.Y.Down())
                {
                    StartUnitPaint(UnitType.Necromancer);
                }
                if (Keys.U.Down())
                {
                    StartUnitPaint(UnitType.Skeleton);
                }
                if (Keys.I.Down())
                {
                    StartUnitPaint(UnitType.ClaySoldier);
                }

                if (Keys.C.Down())
                {
                    StartTilePaint(TileType.Dirt);
                }
                if (Keys.V.Down())
                {
                    StartTilePaint(TileType.Grass);
                }
                if (Keys.N.Down())
                {
                    StartTilePaint(TileType.Trees);
                }

                if (Keys.Tab.Pressed())
                {
                    SetUnitPlaceStyle((int)Math.Round(UnitPlaceStyle) + 1);
                }
            }
        }
示例#2
0
        void GameLogic(GameTime gameTime)
        {
            //Send("setMode", "in-game");
            //Send("setScreen", "in-game-ui");

            switch (State)
            {
            case GameState.ToEditor:
                NewWorldEditor();
                State = GameState.Game;

                Send("setMode", "in-game");
                Send("setScreen", "editor-ui");

                break;

            case GameState.ToMap:
                Render.StandardRenderSetup();
                DrawFullScreen(Assets.ScreenLoading);

                if (Program.StartupMap == null)
                {
                    Program.StartupMap = "Beset.m3n";
                }
                SetScenarioToLoad(Program.StartupMap);

                break;

            case GameState.TitleScreen:
                StartMenuMusicIfNeeded();
                AmbientSounds.EndAll();

                // No mouse input to web browser.
                SteamWrapper.SteamHtml.AllowMouseEvents = false;

                Render.StandardRenderSetup();

                DrawFullScreen(Assets.ScreenTitle);

                if (gameTime.TotalGameTime.Seconds < .005f)
                {
                    break;
                }

                if (MouseMovedSome)
                {
                    World.DrawArrowCursor();
                }
                else
                {
                    if (Input.DeltaMousPos.Length() > 40)
                    {
                        MouseMovedSome = true;
                    }
                }

                if (InputHelper.SomethingPressed())
                {
                    State = GameState.MainMenu;
                    Send("setMode", "main-menu");
                    Send("setScreen", "game-menu");
                }

                break;

            case GameState.MainMenu:
                StartMenuMusicIfNeeded();
                AmbientSounds.EndAll();

                if (!InputHelper.SomethingDown())
                {
                    SteamWrapper.SteamHtml.AllowMouseEvents = true;
                }

                if (_MapLoading != MapLoading)
                {
                    _MapLoading = MapLoading;
                    SetMapLoading();
                }

                if (MapLoading && NewMap != null)
                {
                    World      = NewMap;
                    MapLoading = false;

                    SetMapLoading();
                }

                Render.StandardRenderSetup();
                if (DrawMapPreview && World != null && World.DataGroup != null)
                {
                    Render.UnsetDevice();
                    try
                    {
                        World.DataGroup.UpdateGradient_ToPlayers();
                        World.DataGroup.UpdateGradient_ToBuildings();
                        World.DataGroup.UpdateGradient_ToPlayers();
                        World.DataGroup.UpdateGradient_ToBuildings();
                    }
                    catch
                    {
                    }

                    World.UpdateMinimap();

                    GridHelper.GraphicsDevice.SetRenderTarget(null);
                }

                Render.StandardRenderSetup();
                DrawFullScreen(Assets.ScreenDark);
                DrawWebView();

                if (DrawMapPreview && World != null && World.DataGroup != null)
                {
                    MapPreviewPos  = new vec2(0.76f, 0.32f);
                    MapPreviewSize = new vec2(.4f, .4f);

                    bool UseSolidColor = MapLoading || World == BlankWorld;
                    World.DrawMinimap(MapPreviewPos, MapPreviewSize, ShowCameraBox: false, SolidColor: UseSolidColor);
                }

                World.DrawArrowCursor();

                break;

            case GameState.Loading:
                FadeOutMenuMusicIfNeeded();

                PreGame();

                Render.StandardRenderSetup();
                DrawFullScreen(Assets.ScreenLoading);

                if (ScenarioToLoad != null)
                {
                    Send("setMode", "in-game");
                    Send("setScreen", "in-game-ui");

                    World = new World(
                        GameParams: Program.StartupGameParams,
                        RemoveComputerDragonLords: Program.RemoveComputerDragonLords);

                    TimeLoading          = 0;
                    World.LoadPlayerInfo = false;
                    World.Load(Path.Combine(MapDirectory, ScenarioToLoad));

                    Program.WorldLoaded = true;
                    Networking.ToServer(new Message(MessageType.DoneLoading));

                    ScenarioToLoad = null;
                    TimeSinceLoad  = 0;
                    DrawFullScreen(Assets.ScreenLoading);

                    GetNames();


                    //World.SaveCurrentStateInBuffer();
                    ////var m = new MessageGameState(World.SimStep, World.WorldBytes);
                    //var m = new Message(MessageType.DoneLoading);
                    //var s = m.Encode();

                    //Networking.SendString(new SteamPlayer(SteamCore.PlayerId()), s);
                    //var t = Networking.ReceiveString();
                    //var _s = t.Item2;

                    //Console.WriteLine("!");
                }

                if (Program.GameStarted)
                {
                    if (Program.Spectate)
                    {
                        State = GameState.Game;
                    }
                }
                else
                {
                    TimeLoading += DeltaT;

                    if (TimeLoading > 25)
                    {
                        OnFailedToJoinGame();
                    }

                    TimeSinceLoad = 0;
                    break;
                }

                FadeOutLoading();

                break;

            case GameState.Game:
                StartGameMusicIfNeeded();

                CalculateMouseDownOverUi();

                DrawGame(gameTime);

                if (Program.Spectate && ShouldDrawFading())
                {
                    Render.StandardRenderSetup();
                    DrawFullScreen(Assets.ScreenLoading);

                    FadeOutLoading();
                }
                else
                {
                    DrawWebView();

                    World.DrawUi();

                    if (TimeSinceLoad < 1.5f)
                    {
                        BlackOverlay(1f - (float)(TimeSinceLoad - 1.3f) / .2f);
                    }
                }

                break;
            }
        }