コード例 #1
0
ファイル: Game1.cs プロジェクト: hudson-pace/Platformer
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            ItemManager.RegisterItems();

            player.AddToInventory(new Items.HealthPotion(3));
            player.AddToInventory(new Items.ManaPotion(3));
            player.AddToInventory(new Items.SwordItem(1));
            player.AddToInventory(new Items.ScytheItem(1));

            previousKeyboardState = Keyboard.GetState();

            base.Initialize();
        }
コード例 #2
0
 public void UnEquip()
 {
     if (equippedItem != null)
     {
         player.AddToInventory(equippedItem.GetItem());
         equippedItem = null;
     }
 }
コード例 #3
0
        public void Update(KeyboardState state, MouseState mouseState, OrthographicCamera camera)
        {
            player.Update(state, tiles, mouseState);
            for (int i = enemies.Count - 1; i >= 0; i--) // updating may cause enemy to be removed, so iterate backwards.
            {
                enemies[i].Update(player, tiles);
            }
            spawners.ForEach(spawner => spawner.Update(enemies));
            items.ForEach(item => item.Update(state, tiles));
            NPCList.ForEach(npc => npc.Update(state, tiles, mouseState));

            foreach (Tile[] row in tiles)
            {
                foreach (Tile tile in row)
                {
                    if (tile is Tiles.UpdatableTile)
                    {
                        ((Tiles.UpdatableTile)tile).Update(player);
                    }
                }
            }


            if (state.IsKeyDown(Keys.Q) && !previousQPressed)
            {
                foreach (NPC character in NPCList)
                {
                    if ((character.GetDialogBox() != null && character.GetDialogBox().GetIsActive()) || Collisions.EntityCollisions(player.hitBox, character.hitBox))
                    {
                        character.ToggleDialog(screenWidth, screenHeight, graphicsDevice);
                        break;
                    }
                }
            }
            previousQPressed = state.IsKeyDown(Keys.Q);

            Item[] itemTempList = new Item[items.Count];
            items.CopyTo(itemTempList);
            items.Clear();
            foreach (Item item in itemTempList)
            {
                if (Collisions.EntityCollisions(item.hitBox, player.hitBox) && item.CanBePickedUp)
                {
                    player.AddToInventory(item);
                }
                else
                {
                    items.Add(item);
                }
            }

            Vector2 playerScreenLocation = camera.WorldToScreen(player.location.X, player.location.Y);

            if (playerScreenLocation.X > screenWidth - 500)
            {
                camera.Move(new Vector2(playerScreenLocation.X - (screenWidth - 500), 0));
            }
            else if (playerScreenLocation.X < 500)
            {
                camera.Move(new Vector2(-1 * (500 - playerScreenLocation.X), 0));
            }

            if (playerScreenLocation.Y > screenHeight - 300)
            {
                camera.Move(new Vector2(0, playerScreenLocation.Y - (screenHeight - 300)));
            }
            else if (playerScreenLocation.Y < 300)
            {
                camera.Move(new Vector2(0, (-1 * (300 - playerScreenLocation.Y))));
            }

            if (camera.Position.X < 50)
            {
                camera.Move(new Vector2((-1 * camera.Position.X) + 50, 0));
            }
            else if (camera.Position.X > ((width - 1) * 50) - camera.BoundingRectangle.Width)
            {
                camera.Move(new Vector2((((width - 1) * 50) - camera.BoundingRectangle.Width) - camera.Position.X, 0));
            }

            if (camera.Position.Y < 50)
            {
                camera.Move(new Vector2(0, (-1 * camera.Position.Y) + 50));
            }
            else if (camera.Position.Y > ((height - 1) * 50) - camera.BoundingRectangle.Height)
            {
                camera.Move(new Vector2(0, (((height - 1) * 50) - camera.BoundingRectangle.Height) - camera.Position.Y));
            }
        }