Example #1
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            var state  = Keyboard.GetState();
            var cstate = Mouse.GetState();

            int
                xOffset = player.PlayerLocation.X,
                yOffset = player.PlayerLocation.Y;

            Mod.Events.FireOnTick(this);

            player.Tick();

            #region Enable/Disable System Privledged Mods

            if (Mod.ModLoader.HasSystemRequests)
            {
                IMod i = Mod.ModLoader.SystemRequests()[0];

                if (LastSysReq != i)
                {
                    LastSysReq = i;
                    ServiceHandler.Chat("CAUTION: The mod " + i.ModName() + " version " + i.Version() + ", by " + i.Author() + @" requests system privledges. System Privledges allows mods to: Record Key strokes ( if you're in focus of the application ) Edit raw world data Draw any picture at any location of the game screen And not only that, but malicous mods can spread their system rights across to any other mods, so if you enable system rights for just that mod, thing again. Press 'Y' to enable, 'N' to disable.", Mod.ModLoader.GetSystemService(this, Mod.ModLoader.GetSystem(this)));
                }

                if ((state.IsKeyDown(Keys.Y) ||
                     state.IsKeyDown(Keys.N)) &&
                    CanRun)
                {
                    if (state.IsKeyDown(Keys.Y))
                    {
                        i.SystemPrivledgesGiven(Mod.ModLoader.GetSystemService(this, i));

                        ServiceHandler.Chat("System Privledges Granted.", Mod.ModLoader.GetSystemService(this, Mod.ModLoader.GetSystem(this)));
                    }
                    else if (state.IsKeyDown(Keys.N))
                    {
                        i.SystemPrivledgesDenied();

                        ServiceHandler.Chat("System Privledges Denied.", Mod.ModLoader.GetSystemService(this, Mod.ModLoader.GetSystem(this)));
                    }

                    i.Init();

                    CanRun = false;

                    System.Threading.Tasks.Task.Factory.StartNew((() =>
                    {
                        while (state.IsKeyDown(Keys.Y) || state.IsKeyDown(Keys.N))
                        {
                        }                                                                                      //Wait for the user to quit pressing y/n
                        CanRun = true;
                    }));

                    Mod.ModLoader.SystemRequests().RemoveAt(0);
                }
            }

            #endregion

            foreach (var i in state.GetPressedKeys())
            {
                var k = i.ToString().ToLower();

                Mod.Events.FireKeyStroke(this, new KeyStrokeArgs()
                {
                    Key = Convert.ToChar((i.ToString().Length > 1 ? (i.ToString().ToLower() == "space" ? " " : i.ToString().ToCharArray()[0].ToString()) : i.ToString()))
                });
            }

            if (state.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            #region Hotbar Selector

            int HotbarSet = -1;

            if (state.IsKeyDown(Keys.LeftShift) || state.IsKeyDown(Keys.RightShift))
            {
                HotbarSet = 0;
            }
            if (state.IsKeyDown(Keys.D0))
            {
                HotbarSet = 10;
            }
            if (state.IsKeyDown(Keys.D1))
            {
                HotbarSet = 1;
            }
            if (state.IsKeyDown(Keys.D2))
            {
                HotbarSet = 2;
            }
            if (state.IsKeyDown(Keys.D3))
            {
                HotbarSet = 3;
            }
            if (state.IsKeyDown(Keys.D4))
            {
                HotbarSet = 4;
            }
            if (state.IsKeyDown(Keys.D5))
            {
                HotbarSet = 5;
            }
            if (state.IsKeyDown(Keys.D6))
            {
                HotbarSet = 6;
            }
            if (state.IsKeyDown(Keys.D7))
            {
                HotbarSet = 7;
            }
            if (state.IsKeyDown(Keys.D8))
            {
                HotbarSet = 8;
            }
            if (state.IsKeyDown(Keys.D9))
            {
                HotbarSet = 9;
            }

            if (HotbarSet != -1)
            {
                Hotbar.SelectSlot(HotbarSet);
            }

            #endregion

            int x = cstate.Position.X;
            int y = cstate.Position.Y;

            //Mouse inside of building frame
            if (x < 0)
            {
                x = 0;
            }

            if (x >= 850)
            {
                x = 850;
            }

            if (y < 0)
            {
                y = 0;
            }

            if (y >= 500)
            {
                y = 500;
            }

            OutlinePosition.X = (x - xOffset) / 16;
            OutlinePosition.Y = (y - yOffset) / 16;

            //Mouse inside of world

            if (OutlinePosition.X < 0)
            {
                OutlinePosition.X = 0;
            }

            if (OutlinePosition.Y < 0)
            {
                OutlinePosition.Y = 0;
            }

            if (OutlinePosition.X > WorldWidth - 1)
            {
                OutlinePosition.X = WorldWidth - 1;
            }

            if (OutlinePosition.Y > WorldHeight - 1)
            {
                OutlinePosition.Y = WorldHeight - 1;
            }

            int
                x2 = OutlinePosition.X,
                y2 = OutlinePosition.Y;

            xPlace2 = x2;
            yPlace2 = y2;

            if (x2 > World.Width - 1)
            {
                x2 = World.Width - 1;
            }

            if (y2 > World.Height - 1)
            {
                y2 = World.Height - 1;
            }

            if (cstate.RightButton != ButtonState.Pressed && rightJustPressed)
            {
                rightJustPressed = false;
            }

            if (cstate.LeftButton == ButtonState.Pressed || cstate.RightButton == ButtonState.Pressed)
            {
                if (x > 178 && x < 180 + (Hotbar.Length * 16) &&
                    y > 480 && y < 498)
                {
                    x = x - 179;
                    x = x - (x % 16);
                    x = x / 16;

                    if (x == 11)
                    {
                        x--;
                    }

                    if (cstate.RightButton == ButtonState.Pressed && !rightJustPressed)
                    {
                        Hotbar.SetSlot(x, Hotbar.GetSlot(x) + 1);
                        rightJustPressed = true;
                        Hotbar.SelectSlot(x);
                    }
                    else if (cstate.RightButton != ButtonState.Pressed && rightJustPressed)
                    {
                        rightJustPressed = false;
                        Hotbar.SelectSlot(x);
                    }
                    else if (cstate.RightButton != ButtonState.Pressed && cstate.LeftButton == ButtonState.Pressed)
                    {
                        rightJustPressed = false;
                        Hotbar.SelectSlot(x);
                    }

                    BlockUsing = Hotbar.GetSlot(x);
                }
                else if (cstate.RightButton != ButtonState.Pressed)
                {
                    World.PlaceBlock(0, x2, y2, BlockUsing, "User");
                }
            }

            base.Update(gameTime);
        }