Example #1
0
        private void ConsumeItem()
        {
            var item = Util.GetCurrentItem();

            if (item == 0)
            {
                return;
            }

            var consumable = EntityManager.GetComponent <ConsumableComponent>(item);

            if (consumable == null)
            {
                UISystem.Message("I can't consume that!");
                return;
            }

            ItemConsumedEvent?.Invoke(ControlledEntity, item);
        }
Example #2
0
        private void Interact()
        {
            var tile = Util.CurrentFloor.GetTile(Util.GetPlayerPos());

            if (tile.Items != null && tile.Items.Count > 0)
            {
                PickupItemEvent?.Invoke(ControlledEntity);
                return;
            }

            if (tile.Structure != 0)
            {
                var interactableStructure = EntityManager.GetComponent <InteractableComponent>(tile.Structure);

                if (interactableStructure != null)
                {
                    InteractionEvent?.Invoke(Util.PlayerID, tile.Structure);
                    return;
                }
            }
        }
Example #3
0
        /// <summary>
        /// checks if item is usable
        /// prints problems to player if any
        /// </summary>
        /// <returns> wether item is usable </returns>
        private bool ItemUsable(int item, ItemUsage usage)
        {
            if (item == 0)
            {
                UISystem.Message("Your inventory is empty!");
                return(false);
            }

            var usableItem = EntityManager.GetComponent <UsableItemComponent>(item);

            if (usableItem == null)
            {
                UISystem.Message("You can't use that!");
                return(false);
            }

            if (!usableItem.Usages.Contains(usage))
            {
                UISystem.Message("Item usage:");
                foreach (var action in usableItem.Usages)
                {
                    Keys key = Keys.None;
                    switch (action)
                    {
                    case ItemUsage.Consume:
                        key = GetKeybinding(Command.ConsumeItem, CommandDomain.Inventory);
                        break;

                    case ItemUsage.Throw:
                        key = GetKeybinding(Command.ThrowItem, CommandDomain.Inventory);
                        break;
                    }
                    UISystem.Message(key + " -> " + action);
                }
                return(false);
            }
            return(true);
        }
Example #4
0
        /// <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()
        {
            graphics.PreferredBackBufferWidth  = Util.ScreenWidth;
            graphics.PreferredBackBufferHeight = Util.ScreenHeight;
            graphics.ApplyChanges();

            Util.ContentPath = Content.RootDirectory;

            Log.Init(AppDomain.CurrentDomain.BaseDirectory);
            Log.Message("Seed: " + Seed);

            var gameData = GameData.Instance = new GameData();

            var dataPath = Util.ContentPath + "/GameData/";

            gameData.LoadEntities(EntityType.Terrain, dataPath + "Entities/Terrains");
            gameData.LoadEntities(EntityType.Structure, dataPath + "Entities/Structures");
            gameData.LoadEntities(EntityType.Character, dataPath + "Entities/Characters");
            gameData.LoadEntities(EntityType.Item, dataPath + "Entities/Items");

            var herb          = gameData.CreateItem("randomHerb");
            var herbSubstance = EntityManager.GetComponent <SubstanceComponent>(herb);

            gameData.LoadTilesets(dataPath + "tilesets.json");
            gameData.LoadRoomTemplates(dataPath);

            //Floor test = new Floor(Content.RootDirectory + "/map.txt");
            Floor testFloor = new Floor(500, 500);

            //testFloor.GenerateSimple();
            testFloor.GenerateGraphBased();

            //EntityManager.Dump();

            Util.CurrentFloor = testFloor;
            Util.CurrentFloor.CalculateTileVisibility();

            // Delete entities that were removed during generation
            EntityManager.CleanUpEntities();

            //Log.Data(DescriptionSystem.GetDebugInfoEntity(Util.PlayerID));

            InputManager input = InputManager.Instance;

            // instantiate all the systems
            Log.Message("Loading Systems...");
            inputSystem        = new InputSystem();
            movementSystem     = new MovementSystem();
            renderSystem       = new RenderSystem(GraphicsDevice);
            collisionSystem    = new CollisionSystem();
            healthSystem       = new HealthSystem();
            combatSystem       = new CombatSystem();
            npcBehaviourSystem = new NPCBehaviourSystem();
            interactionSystem  = new InteractionSystem();
            itemSystem         = new ItemSystem();
            uiSystem           = new UISystem();
            statSystem         = new StatSystem();
            craftingSystem     = new CraftingSystem();

            // toggle FOW
            renderSystem.FogOfWarEnabled = true;

            // hook up all events with their handlers
            Log.Message("Registering Event Handlers...");

            input.MovementEvent             += movementSystem.HandleMovementEvent;
            input.InventoryToggledEvent     += uiSystem.HandleInventoryToggled;
            input.InventoryCursorMovedEvent += uiSystem.HandleInventoryCursorMoved;
            input.PickupItemEvent           += itemSystem.PickUpItem;
            input.InteractionEvent          += interactionSystem.HandleInteraction;
            input.ItemUsedEvent             += itemSystem.UseItem;
            input.ItemConsumedEvent         += itemSystem.ConsumeItem;

            // crafting
            input.AddItemAsIngredientEvent += craftingSystem.AddIngredient;
            input.CraftItemEvent           += craftingSystem.CraftItem;
            input.ResetCraftingEvent       += craftingSystem.ResetCrafting;

            interactionSystem.ItemAddedEvent += itemSystem.AddItem;

            itemSystem.HealthGainedEvent += healthSystem.HandleGainedHealth;
            itemSystem.HealthLostEvent   += healthSystem.HandleLostHealth;
            itemSystem.StatChangedEvent  += statSystem.ChangeStat;

            npcBehaviourSystem.EnemyMovedEvent += movementSystem.HandleMovementEvent;

            movementSystem.CollisionEvent   += collisionSystem.HandleCollision;
            movementSystem.BasicAttackEvent += combatSystem.HandleBasicAttack;
            movementSystem.InteractionEvent += interactionSystem.HandleInteraction;

            craftingSystem.ItemAddedEvent += itemSystem.AddItem;

            Util.TurnOverEvent += healthSystem.RegenerateEntity;
            Util.TurnOverEvent += statSystem.TurnOver;

            combatSystem.HealthLostEvent += healthSystem.HandleLostHealth;

            Log.Message("Loading Keybindings...");
            string keybindings = File.ReadAllText(Util.ContentPath + "/GameData/keybindings.json");

            input.LoadKeyBindings(keybindings);
            input.EnterDomain(InputManager.CommandDomain.Exploring); // start out with exploring as bottom level command domain
            input.ControlledEntity = Util.PlayerID;

            base.Initialize();
            Log.Message("Initialization completed!");

            //CraftableComponent.foo();

            int testRock = EntityManager.CreateEntity(new List <IComponent>()
            {
                new DescriptionComponent()
                {
                    Name = "Rock", Description = "You can't stop the rock!"
                },
                new ItemComponent()
                {
                    MaxCount = 5, Count = 3, Value = 1, Weight = 10
                }
            }, EntityType.Item);

            // fill inventory with test items
            itemSystem.AddItems(Util.PlayerID, new int[]
            {
                //testRock,
                GameData.Instance.CreateItem("healthPotion"),
                GameData.Instance.CreateItem("healthPotion"),
                GameData.Instance.CreateItem("healthPotion"),
                GameData.Instance.CreateItem("poisonPotion"),
                GameData.Instance.CreateItem("poisonPotion"),
                GameData.Instance.CreateItem("elementalPotion")
            });


            //EntityManager.Dump();
            //EntityManager.RemoveEntity(testBush);
            //EntityManager.CleanUpEntities();
            //EntityManager.Dump();

            //Log.Data(DescriptionSystem.GetDebugInfoEntity(Util.GetPlayerInventory().Items[0]));

            LocationSystem.UpdateDistanceMap(Util.PlayerID);
        }
Example #5
0
        public static void Render(SpriteBatch spriteBatch)
        {
            StringBuilder sb = new StringBuilder();
            // ------------------------------------------------------------------------------------------------------------------------------------------------------------
            //spriteBatch.Draw(TextureManager.GetTexture("messageLogBox"), new Vector2(0, Util.WorldViewPixelHeight), Color.White);
            //spriteBatch.DrawString(Util.BigFont, "Message Log", new Vector2(10, Util.WorldHeight + 10), Color.Black);

            string messageLogString = "";

            for (int i = 0; i < MessageLogLineCount; i++)
            {
                messageLogString += MessageLog[i] + '\n';
            }
            //spriteBatch.DrawString(Util.DefaultFont, messageLogString, new Vector2(10, Util.WorldViewPixelHeight + 10), Color.Black);

            MessageLogWindow.Content.Clear();
            MessageLogWindow.Content.Add(messageLogString);
            MessageLogWindow.Draw(spriteBatch);

            // ------------------------------------------------------------------------------------------------------------------------------------------------------------
            //spriteBatch.Draw(TextureManager.GetTexture("inventory"), new Vector2(Util.WorldViewPixelWidth, 220), InventoryOpen ? Color.Aquamarine : Color.White);
            //spriteBatch.DrawString(Util.BigFont, "Inventory", new Vector2(Util.WorldViewPixelWidth + 10, 220 + 10), Color.Black);
            InventoryWindow.Content.Clear();

            var weapon = CombatSystem.GetEquippedWeapon(Util.PlayerID);

            if (weapon != null)
            {
                InventoryWindow.Content.Add(weapon.ToString());
            }

            var armor = CombatSystem.GetEquippedArmor(Util.PlayerID);

            if (armor != null)
            {
                InventoryWindow.Content.Add(armor.ToString());
            }

            InventoryWindow.Content.Add("");

            var items = EntityManager.GetComponent <InventoryComponent>(Util.PlayerID).Items;

            SyncInventoryCursor();

            for (int i = 0; i < items.Count; i++)
            {
                int  item     = items[i];
                bool selected = i == InventoryCursorPosition - 1;
                var  itemC    = EntityManager.GetComponent <ItemComponent>(item);
                InventoryWindow.Content.Add(String.Format("{0} {1} x{2}", selected ? "#" : "  ", DescriptionSystem.GetName(item), itemC.Count));
            }

            InventoryWindow.Draw(spriteBatch, InventoryOpen ? Color.CornflowerBlue : Color.White);

            // ------------------------------------------------------------------------------------------------------------------------------------------------------------
            //spriteBatch.Draw(TextureManager.GetTexture("tooltip"), new Vector2(Util.WorldViewPixelWidth, 0), Color.White);


            InputManager.CommandDomain curDomain = InputManager.Instance.GetCurrentDomain();

            string name        = "";
            string description = "";

            if (curDomain == InputManager.CommandDomain.Exploring)
            {
                name        = "Player (Turns: " + Util.TurnCount + ")";
                description = DescriptionSystem.GetCharacterTooltip(Util.PlayerID);
                //description = "HP: " + EntityManager.GetComponent<HealthComponent>(Util.PlayerID).GetString();
            }
            else if (curDomain == InputManager.CommandDomain.Inventory || curDomain == InputManager.CommandDomain.Crafting)
            {
                if (items.Count == 0)
                {
                    name        = "Your inventory is empty!";
                    description = "";
                }
                else
                {
                    int item = items[InventoryCursorPosition - 1];
                    name        = DescriptionSystem.GetName(item);
                    description = DescriptionSystem.GetItemTooltip(item);
                }
            }
            else if (curDomain == InputManager.CommandDomain.Targeting)
            {
                var pos       = EntityManager.GetComponent <TransformComponent>(Util.TargetIndicatorID).Position;
                int character = Util.CurrentFloor.GetCharacter(pos);
                int item      = Util.CurrentFloor.GetFirstItem(pos);
                int structure = Util.CurrentFloor.GetStructure(pos);
                int terrain   = Util.CurrentFloor.GetTerrain(pos);

                int descrEntity = 0;

                if (character != 0)
                {
                    descrEntity = character;
                    Log.Data("Character", DescriptionSystem.GetDebugInfoEntity(character));
                }
                else if (item != 0)
                {
                    descrEntity = item;
                    Log.Data("Item", DescriptionSystem.GetDebugInfoEntity(item));
                }
                else if (structure != 0)
                {
                    descrEntity = structure;
                    Log.Data("Structure", DescriptionSystem.GetDebugInfoEntity(structure));
                }
                else if (terrain != 0)
                {
                    descrEntity = terrain;
                    Log.Data("Terrain", DescriptionSystem.GetDebugInfoEntity(terrain));
                }

                if (descrEntity == 0)
                {
                    name        = "Floor";
                    description = "There's nothing there!";
                }
                else
                {
                    var descriptionC = EntityManager.GetComponent <DescriptionComponent>(descrEntity);
                    if (descriptionC == null)
                    {
                        name        = "???";
                        description = "???";
                    }
                    else
                    {
                        name        = descriptionC.Name;
                        description = descriptionC.Description;
                    }
                }
            }

            // draw name
            //spriteBatch.DrawString(Util.BigFont, name, new Vector2(Util.WorldViewPixelWidth + 10, 10), Color.Black);

            // split description into multiple lines
            int rowLength = 40;
            int cur       = 0;
            int limit     = 55;

            while (cur + rowLength < description.Length)
            {
                if (limit-- == 0)
                {
                    return;
                }
                //Console.WriteLine(cur + "|" + description.Length);
                int newlPos = description.IndexOf('\n', cur, rowLength);
                if (newlPos >= 0)
                {
                    cur = newlPos + 1;
                    continue;
                }

                int insertHere = description.LastIndexOf(' ', cur + rowLength, rowLength);

                if (insertHere == -1)
                {
                    insertHere = cur + rowLength; // no space found? just cut off the word, lol
                }
                else
                {
                    insertHere++; // insert after space
                }

                description = description.Insert(insertHere, "\n");
                cur         = insertHere + 1;
            }

            // draw description text
            //spriteBatch.DrawString(Util.MonospaceFont, description, new Vector2(Util.WorldViewPixelWidth + 10, 40), Color.Black);
            // ------------------------------------------------------------------------------------------------------------------------------------------------------------

            TooltipWindow.Name = name;
            TooltipWindow.Content.Clear();

            TooltipWindow.Content.Add(description);

            TooltipWindow.Draw(spriteBatch);

            // draw crafting window
            if (CraftingMode)
            {
                PopUpWindow.Name        = "Crafting";
                PopUpWindow.DisplayName = true;

                PopUpWindow.Content.Clear();
                PopUpWindow.Content.Add("(press 'R' to reset and 'Enter' to craft)");
                PopUpWindow.Content.Add("");
                PopUpWindow.Content.Add("Current Recipe:");

                foreach (var ingredientName in CraftingSystem.Instance.GetIngredientNames())
                {
                    PopUpWindow.Content.Add("- " + ingredientName);
                }

                PopUpWindow.Draw(spriteBatch);
            }
            // ------------------------------------------------------------------------------------------------------------------------------------------------------------
        }