private void EquipItemFromAnywhere(Item item, UI targetEquipment, UI SourceEquipment = null)
 {
     if (SourceEquipment == null)
     {
         map.EquipItem(item, targetEquipment, null, false);
     }
     else
     {
         map.EquipItem(item, targetEquipment, SourceEquipment, false);
     }
 }
        private Item CreateWorldItemFromListItem(int ID, Coordinates pos = null)
        {
            Item newItem;
            if(pos == null) { pos = new Coordinates(0, 0); }
            for (int i = 0; i < map.ItemList.Count; i++)
            {
                if (map.ItemList[i].ID == ID)
                {
                    newItem = new Item(map.ItemList[i].Name, map.ItemList[i].WearSlot, pos, map.ItemList[i].SpriteID, map.WorldItems.Count + 1, map.ItemList[i].Strength, map.ItemList[i].Defense, map.ItemList[i].Visible);
                    map.WorldItems.Add(newItem);
                    return map.WorldItems[map.WorldItems.Count - 1];
                }
            }

            return new Item();
        }
        /// <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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                SaveWorld();
                Exit();
            }

            // For debug purposes: Window.Title = gameTime.TotalGameTime.Seconds.ToString();

            if (gameTime.TotalGameTime.TotalMilliseconds - TimeOfLastMovement > 250)
            {
                if (this.IsActive)
                {
                    if (Keyboard.GetState().IsKeyDown(Keys.Right))
                    {
                        mostRecentKey = Keys.Right;
                    }
                    else if (Keyboard.GetState().IsKeyDown(Keys.Left))
                    {
                        mostRecentKey = Keys.Left;
                    }
                    else if (Keyboard.GetState().IsKeyDown(Keys.Up))
                    {
                        mostRecentKey = Keys.Up;
                    }
                    else if (Keyboard.GetState().IsKeyDown(Keys.Down))
                    {
                        mostRecentKey = Keys.Down;
                    }
                }

                if (!Walking && mostRecentKey != lastPressedKey)
                {
                    if (mostRecentKey == Keys.Right)
                    {
                        Walking_Direction = Direction_East;
                        Walking = true;
                    }
                    else if (mostRecentKey == Keys.Left)
                    {
                        Walking_Direction = Direction_West;
                        Walking = true;
                    }
                    else if (mostRecentKey == Keys.Up)
                    {
                        Walking_Direction = Direction_North;
                        Walking = true;
                    }
                    else if (mostRecentKey == Keys.Down)
                    {
                        Walking_Direction = Direction_South;
                        Walking = true;
                    }
                    lastPressedKey = mostRecentKey;
                    mostRecentKey = Keys.None;
                }

                if (Walking && map.Players[0].Health > 0)
                {
                    if (Walking_Direction == Direction_East)
                    {
                        map.MoveCreature(map.Players[0], new Coordinates(map.Players[0].Position.X + Coordinates.Step, map.Players[0].Position.Y));
                    }
                    else if (Walking_Direction == Direction_West)
                    {
                        map.MoveCreature(map.Players[0], new Coordinates(map.Players[0].Position.X - Coordinates.Step, map.Players[0].Position.Y));
                    }
                    else if (Walking_Direction == Direction_South)
                    {
                        map.MoveCreature(map.Players[0], new Coordinates(map.Players[0].Position.X, map.Players[0].Position.Y + Coordinates.Step));
                    }
                    else if (Walking_Direction == Direction_North)
                    {
                        map.MoveCreature(map.Players[0], new Coordinates(map.Players[0].Position.X, map.Players[0].Position.Y - Coordinates.Step));
                    }
                    TimeOfLastMovement = (int) gameTime.TotalGameTime.TotalMilliseconds;
                    Walking = false;
                    mostRecentKey = Keys.None;
                    lastPressedKey = Keys.None;

                    if(map.Players[0].hasPath())
                    {
                        map.Players[0].ResetPath();
                    }
                }
            }

            if (gameTime.TotalGameTime.TotalMilliseconds - TimeOfLastMovement > 250)
            {
                if (map.Players[0].hasPath())
                {
                    map.MovePlayer();
                    TimeOfLastMovement = (int)gameTime.TotalGameTime.TotalMilliseconds;
                }
            }

            if (Mouse.GetState().RightButton == ButtonState.Released && RightClicked)
            {
                RightClicked = false;
            }

            if (this.IsActive)
            {
                if (Mouse.GetState().RightButton == ButtonState.Pressed && !RightClicked)
                {
                    int x = Mouse.GetState().X, y = Mouse.GetState().Y;
                    int creatureID = GetCreatureByMousePosition(x, y).ID;
                    if (creatureID != -1)
                    {
                        if (map.Players[0].TargetID == creatureID)
                        {
                            map.Players[0].TargetID = -1;
                        }
                        else
                        {
                            map.Players[0].TargetID = creatureID;
                        }
                    }
                    else
                    {
                        Spell CastSpell = GetSpellByMousePosition(x, y);
                        if (CastSpell.ID != -1)
                        {
                            if (map.Players[0].CastSpell(CastSpell, (int)gameTime.TotalGameTime.TotalMilliseconds))
                            {
                                SpellDamage.Add(new DamageObject(map.Players[0], CastSpell.Damage, CastSpell.HealSpell, (int)gameTime.TotalGameTime.TotalMilliseconds, (int)gameTime.TotalGameTime.TotalMilliseconds + 1500, CastSpell.ID, map.Players[0].Position));
                                dmgDone.AddRange(map.PlayerCastSpell(map.Players[0], CastSpell, map.GetCreatureByID(map.Players[0].TargetID), gameTime));
                            }
                        }
                        else
                        {
                            UI container = GetListUIByMousePosition(x, y);
                            if (container.Name == ItemSlot.Bag)
                            {
                                Item bag = GetItemByMousePosition(x, y);
                                if (bag.ID != -1)
                                {
                                    if (!bag.Container.Open)
                                    {
                                        OpenBag(bag);
                                    }
                                    else
                                    {
                                        CloseBag(bag);
                                    }
                                }
                            }
                            else
                            {
                                // TODO: Add potion usage here, use the following code as a base:
                                Item item = GetItemFromBagCoordinates(x, y);
                                if(item.ID != -1)
                                {
                                    if(item.Container != null)
                                    {
                                        if (!item.Container.Open)
                                        {
                                            OpenBag(item);
                                        }
                                        else
                                        {
                                            CloseBag(item);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    // For debug purposes: Window.Title = map.Players[0].TargetID.ToString();
                    RightClicked = true;
                }
            }

            if (Mouse.GetState().LeftButton == ButtonState.Released && LeftClicked)
            {
                int mx = Mouse.GetState().X, my = Mouse.GetState().Y;
                int x = (mx / 32) * 32;
                int y = (my / 32) * 32;
                Coordinates target = new Coordinates(x, y);
                if (gameTime.TotalGameTime.TotalMilliseconds - leftClickTime < 200)
                {
                    if (!map.OutOfBoundaries(target))
                    {
                        if (map.IsTileWalkable(target))
                        {
                            map.GeneratePathFromCreature(map.Players[0], target);
                        }
                    }
                }
                else
                {
                    if (dragItem.ID != -1)
                    {
                        UI targetEquipment = GetListUIByMousePosition(x, y);
                        UI sourceEquipment = GetListUIByMousePosition(dragItem.Position.X, dragItem.Position.Y);
                        Item FromBag = GetItemFromBagCoordinates(dragItem.Position.X, dragItem.Position.Y);
                        Backpack ToBag = GetBagFromCoordinates(x, y);
                        if (FromBag.ID == -1)
                        {
                            if (targetEquipment.ID == -1)
                            {
                                if (ToBag.Open)
                                {
                                    if (sourceEquipment.ID == -1)
                                    {
                                        ThrowItemToBag(ToBag, target);
                                    }
                                    else
                                    {
                                        ThrowItemToBag(ToBag, target, sourceEquipment);
                                    }
                                }
                                else
                                {
                                    if (sourceEquipment.ID == -1)
                                    {
                                        if (!map.OutOfBoundaries(target))
                                        {
                                            map.DragItem(dragItem, target);
                                        }
                                    }
                                    else
                                    {
                                        if (!map.OutOfBoundaries(target))
                                        {
                                            UnequipItem(sourceEquipment, target);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (sourceEquipment.ID == -1)
                                {
                                    EquipItem(targetEquipment);
                                }
                                else
                                {
                                    EquipItem(targetEquipment, sourceEquipment);
                                }
                            }
                        }
                        else
                        {
                            if(targetEquipment.ID == -1)
                            {
                                if (ToBag.Open)
                                {
                                    ThrowItemToBag(ToBag, target);
                                }
                                else
                                {
                                    if (!map.OutOfBoundaries(target))
                                    {
                                        ThrowItemFromBag(target);
                                    }
                                }
                            }
                            else
                            {
                                EquipItemFromBag(targetEquipment);
                            }
                        }
                    }
                }
                // For debug purposes: Window.Title = map.Players[0].TargetID.ToString();*/
                // For debug purposes: Window.Title = x.ToString() + " " + y.ToString();
                LeftClicked = false;
                currentMouse = 0;
            }

            if (this.IsActive)
            {
                if (Mouse.GetState().LeftButton == ButtonState.Pressed && (!Keyboard.GetState().IsKeyDown(Keys.LeftControl) && !Keyboard.GetState().IsKeyDown(Keys.RightControl)))
                {
                    if (!LeftClicked)
                    {
                        int mx = Mouse.GetState().X, my = Mouse.GetState().Y;
                        int x = (mx / 32) * 32;
                        int y = (my / 32) * 32;
                        leftClickTime = (int) gameTime.TotalGameTime.TotalMilliseconds;
                        dragItem = GetItemByMousePosition(mx, my);
                        LeftClicked = true;
                    }
                    else
                    {
                        if(gameTime.TotalGameTime.TotalMilliseconds - leftClickTime > 200)
                        {
                            currentMouse = 1;
                        }
                    }
                }
                else if (Mouse.GetState().LeftButton == ButtonState.Pressed && (Keyboard.GetState().IsKeyDown(Keys.LeftControl) || Keyboard.GetState().IsKeyDown(Keys.RightControl)) && !LeftClicked && !Looked)
                {
                    int mx = Mouse.GetState().X, my = Mouse.GetState().Y;
                    int x = (mx / 32) * 32;
                    int y = (my / 32) * 32;
                    Item lookItem = GetItemByMousePosition(mx, my);
                    if (lookItem.ID != -1)
                    {
                        TextPopUps.Add(new Animation(1000, 0, 1, (int)gameTime.TotalGameTime.TotalMilliseconds, "You see " + lookItem.Name + " (ID: " + lookItem.ID + ")."));
                    }
                    Looked = true;
                }
            }

            if(Mouse.GetState().LeftButton == ButtonState.Released && Looked)
            {
                Looked = false;
            }

            if (gameTime.TotalGameTime.Milliseconds % 250 == 0)
            {
                MoveCreatures(gameTime);
            }

            if (gameTime.TotalGameTime.Milliseconds % 1000 == 0)
            {
                //map.GeneratePaths();// <- Uncomment this to make monsters move
            }

            if (gameTime.TotalGameTime.Seconds % 2 == 0 && !regenerated)
            {
                regenerated = true;
                BaseRegeneration();
            }
            else if(gameTime.TotalGameTime.Seconds % 2 == 1 && regenerated)
            {
                regenerated = false;
            }

            if(gameTime.TotalGameTime.Seconds % 10 == 0 && !respawned)
            {
                respawned = true;
                Respawn();
            }
            else if(gameTime.TotalGameTime.Seconds % 10 == 1 && respawned)
            {
                respawned = false;
            }

            if (gameTime.TotalGameTime.TotalMilliseconds - map.Players[0].TimeOfLastAttack > 1000)
            {
                if (map.CanAttack(map.Players[0], map.GetCreatureByID(map.Players[0].TargetID)) && map.Players[0].hasTarget())
                {
                    int targetID = map.Players[0].TargetID;
                    int dmgDealt = map.PlayerAttack(map.Players[0]);
                    if (dmgDealt != -1)
                    {
                        int currentTime = (int)gameTime.TotalGameTime.TotalMilliseconds;
                        dmgDone.Add(new DamageObject(map.GetCreatureByID(targetID), dmgDealt, false, currentTime, currentTime + DamageObject.DamageDuration));
                        map.Players[0].TimeOfLastAttack = (int)gameTime.TotalGameTime.TotalMilliseconds;
                    }
                }
            }

            base.Update(gameTime);
        }
 private void CloseBag(Item bag)
 {
     bag.Container.Open = false;
     OpenBackpacks.Remove(bag.Container);
 }
 private void OpenBag(Item bag)
 {
     bag.Container.Open = true;
     OpenBackpacks.Add(bag.Container);
 }