예제 #1
0
 public Menu(Menu parent, string name, MenuAction action, object context = null)
 {
     MenuOptions = new List<Menu>();
     Name = name;
     Action = action;
     Context = context;
     SelectedIndex = 0;
     Parent = parent;
 }
예제 #2
0
        public static void DrawMenu(Menu menu, Vector2 startLocation, SpriteBatch spriteBatch, GraphicsDevice device)
        {
            if (menu != null)
            {
                foreach (Menu m in menu.MenuOptions)
                {
                    int max = 0;
                    foreach (var option in menu.MenuOptions)
                    {
                        if (option.Name.Length > max)
                        {
                            max = option.Name.Length;
                        }
                    }

                    Texture2D texture = new Texture2D(device, (max * 14) + 25, 40 * menu.MenuOptions.Count);

                    Color color = Color.White;

                    Color[] texData = new Color[texture.Width * texture.Height];

                    for (int i = 0; i < texData.Length; i++)
                    {
                        texData[i] = color;
                    }

                    texture.SetData(texData);

                    spriteBatch.Draw(texture, startLocation, Color.White);
                    for (int i = 0; i < menu.MenuOptions.Count; i++)
                    {
                        if (i == menu.SelectedIndex)
                        {
                            spriteBatch.Draw(GetTexture(15, 15, Color.Black, "Pointer", device), startLocation + new Vector2(5, 10 + i * 40), Color.White);
                            DrawMenu(menu.MenuOptions[i], startLocation + new Vector2((max * 14) + 25, 10 + i * 40), spriteBatch, device);
                        }
                        FontRenderer.DrawText(spriteBatch, (int)startLocation.X + 25, (int)startLocation.Y + i * 40 + 10, menu.MenuOptions[i].Name);
                    }
                }
            }
        }
예제 #3
0
        private static Menu GetAllActions(WorldModel worldModel)
        {
            Tile current = worldModel.Player.CurrentTile;

            Menu menu = new Menu();

            Menu atackOption = new Menu(menu, "Attack", null);
            Menu eatOption = new Menu(menu, "Eat", null);
            Menu pickUpOption = new Menu(menu, "Pick Up", null);
            Menu buildOption = new Menu(menu, "Build", null);
            Menu useOption = new Menu(menu, "Use", null);

            List<IEntity> entities = current.TileContents.Where(f => !(f is Player)).ToList();
            entities.AddRange(worldModel.Player.Inventory);
            foreach (IEntity entity in entities)
            {
                if (entity is IEdible)
                {
                    IAnimate i = entity as IAnimate;
                    if (i != null)
                    {
                        // if the entity is animate it can fight back, so kill it before eating it
                        if (i.Health > 0)
                        {
                            atackOption.AddOption(entity.Name, worldModel.Player.Attack, entity);
                        }
                        else
                        {
                            // entity is dead, can be eaten
                            CheckIfCanBeEaten(worldModel, entity, eatOption);
                        }
                    }
                    else
                    {
                        // entity is not animate so it cannot attack, eat it without struggle
                        CheckIfCanBeEaten(worldModel, entity, eatOption);
                    }
                }

                IItem item = entity as IItem;
                if (item != null)
                {
                    if (!worldModel.Player.Inventory.Contains(entity) && item.CanLoot())
                    {
                        pickUpOption.AddOption(entity.Name, worldModel.Player.PickUp, entity);
                    }
                }
            }

            if (worldModel.Player.Inventory.Any(i => i is Rock))
            {
                buildOption.AddOption("Rock wall", worldModel.Player.Build, new Wall());
            }

            foreach (var item in worldModel.Player.Inventory.Where(i => i is IUseable))
            {
                useOption.AddOption(item.Name, worldModel.Player.Use, worldModel.Player.Inventory.First(f => f is IUseable));
            }

            if (atackOption.MenuOptions.Count > 0)
                menu.MenuOptions.Add(atackOption);

            if (eatOption.MenuOptions.Count > 0)
                menu.MenuOptions.Add(eatOption);

            if (pickUpOption.MenuOptions.Count > 0)
                menu.MenuOptions.Add(pickUpOption);

            if (buildOption.MenuOptions.Count > 0)
                menu.MenuOptions.Add(buildOption);

            if (useOption.MenuOptions.Count > 0)
                menu.MenuOptions.Add(useOption);

            menu.MenuOptions.Add(new Menu(menu, "Stay", worldModel.Player.Stay));

            worldModel.ActiveMenu = menu;

            return menu;
        }
예제 #4
0
 private static void CheckIfCanBeEaten(WorldModel worldModel, IEntity entity, Menu eatOption)
 {
     IEdible e = entity as IEdible;
     if (e != null && worldModel.Player.DesiredFood.HasFlag(e.ProvidesFoodType) && e.CanBeEaten())
     {
         eatOption.AddOption(e.FoodName, worldModel.Player.Eat, entity);
     }
 }