Пример #1
0
        public void Update(GameTime gameTime)
        {
            if (KeyboardUtil.KeyPressed(Keys.E) || KeyboardUtil.KeyPressed(Keys.Escape))
            {
                Game1.CloseMenu();
                return;
            }

            Vector2 mouseRelative = MouseUtil.XandY - Position;

            if (MouseUtil.X >= Position.X && MouseUtil.X < Position.X + GetSize().X&&
                MouseUtil.Y >= Position.Y && MouseUtil.Y < Position.Y + GetSize().Y)
            {
                int slot = (int)((Math.Floor(mouseRelative.Y / 128) * 4) + Math.Floor(mouseRelative.X / 128));
                if (KeyboardUtil.IsKeyDown(Keys.LeftShift) && this.heldSlot == -1)
                {
                    if (originalSelected == -1)
                    {
                        originalSelected = slot;
                    }
                    currentSelected = slot;
                    Rectangle originRect  = new Rectangle((originalSelected % 4) * 128, (originalSelected / 4) * 128, 128, 128);
                    Rectangle currentRect = new Rectangle((currentSelected % 4) * 128, (currentSelected / 4) * 128, 128, 128);
                    slotSelection = Rectangle.Union(originRect, currentRect);
                    if (MouseUtil.ButtonPressed(MouseButton.LeftButton))
                    {
                        List <int> slots = new List <int>();
                        for (int i = 0; i < Game1.Player.Inventory.Length; i++)
                        {
                            int slotX = (i % 4) * 128;
                            int slotY = (i / 4) * 128;
                            if (slotSelection.Value.Contains(slotX, slotY))
                            {
                                if (Game1.Player.Inventory[i] != null)
                                {
                                    slots.Add(i);
                                }
                            }
                        }
                        List <Item> craftItems = new List <Item>();
                        slots.ForEach(_ => craftItems.Add(Game1.Player.Inventory[_]));
                        Item craft = Crafting.Craft(craftItems);
                        if (craft != null)
                        {
                            foreach (var i in slots)
                            {
                                Game1.Player.Inventory[i] = null;
                            }
                            Game1.Player.Inventory[slot] = craft;
                        }
                    }
                }
                else if (MouseUtil.ButtonPressed(MouseButton.LeftButton))
                {
                    if (Game1.Player.Inventory[slot] != null)
                    {
                        this.heldSlot = slot;
                    }
                }
                else if (MouseUtil.IsButtonUp(MouseButton.LeftButton) && this.heldSlot != -1)
                {
                    Item replacedWith = Game1.Player.Inventory[slot];
                    Game1.Player.Inventory[slot]          = Game1.Player.Inventory[this.heldSlot];
                    Game1.Player.Inventory[this.heldSlot] = replacedWith;
                    this.heldSlot = -1;
                }
            }
            else
            {
                if (MouseUtil.IsButtonUp(MouseButton.LeftButton) && this.heldSlot != -1)
                {
                    if (this.overTrash)
                    {
                        Game1.PlaySound("trash", 0.2f);
                        Game1.Player.Inventory[this.heldSlot] = null;
                        this.heldSlot = -1;
                    }
                    else
                    {
                        this.heldSlot = -1;
                    }
                }
            }
            if (this.heldSlot != -1 && Game1.Player.Inventory[this.heldSlot] == null)
            {
                this.heldSlot = -1;
            }

            if (this.heldSlot != -1)
            {
                Texture2D trash = ContentLibrary.Sprites["ui:trash1"];
                if (MouseUtil.X >= Position.X + GetSize().X&& MouseUtil.X < Position.X + GetSize().X + trash.Width &&
                    MouseUtil.Y >= Position.Y + GetSize().Y - trash.Height && MouseUtil.Y < Position.Y + GetSize().Y)
                {
                    overTrash = true;
                }
                else
                {
                    overTrash = false;
                }
            }
            else
            {
                overTrash = false;
            }

            if (KeyboardUtil.IsKeyUp(Keys.LeftShift))
            {
                slotSelection    = null;
                currentSelected  = -1;
                originalSelected = -1;
            }
        }
Пример #2
0
 public override void Update(GameTime gameTime)
 {
     if (MouseUtil.ButtonPressed(MouseButton.LeftButton) && !lockedShot && !chargingShot)
     {
         chargingShot = true;
     }
     if (MouseUtil.IsButtonUp(MouseButton.LeftButton) && !lockedShot && chargingShot)
     {
         joj          = 0;
         timeUntilJoj = 0;
         chargingShot = false;
     }
     if (chargingShot || lockedShot)
     {
         Game1.Player.speedMult = 0.5f;
         if (MouseUtil.X < Game1.Player.XandY.DrawPos().X)
         {
             Game1.Player.facing = 1;
         }
         else if (MouseUtil.X > Game1.Player.XandY.DrawPos().X)
         {
             Game1.Player.facing = 0;
         }
         if (joj < 3)
         {
             timeUntilJoj += gameTime.ElapsedGameTime.Milliseconds;
             if (timeUntilJoj >= 200)
             {
                 joj++;
                 timeUntilJoj = 0;
             }
         }
         if (joj == 3 && !lockedShot)
         {
             chargingShot = false;
             lockedShot   = true;
             Game1.PlaySound("lock");
         }
     }
     else
     {
         Game1.Player.speedMult = 1;
     }
     if (MouseUtil.ButtonPressed(MouseButton.LeftButton) && lockedShot)
     {
         double my    = MouseUtil.Y - Game1.Player.XandY.DrawPos().Y;
         double mx    = MouseUtil.X - Game1.Player.XandY.DrawPos().X;
         Arrow  arrow = new Arrow(Math.Atan2(my, mx), 16, 1000);
         arrow.X = Game1.Player.X;
         arrow.Y = Game1.Player.Y;
         Game1.Player.Owner.AddEntity(arrow);
         Game1.PlaySound("shoot");
         lockedShot   = false;
         joj          = 0;
         timeUntilJoj = 0;
     }
     if (MouseUtil.IsButtonUp(MouseButton.LeftButton) && !lockedShot)
     {
         joj          = 0;
         timeUntilJoj = 0;
     }
     base.Update(gameTime);
 }