/// <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 (Util.ErrorOccured) { if (!Util.BrutalModeOn) { UISystem.Message("A CRITICAL ERROR HAS OCCURED!"); Exit(); } } if (gameOver) { if (InputManager.Instance.CheckKey(Keys.Escape)) { Exit(); } // ignore all other input base.Update(gameTime); return; } // check for input if (InputManager.Instance.CheckInput(gameTime)) { InputManager.Instance.ExecuteCurrentCommand(); if (Util.PlayerTurnOver) { npcBehaviourSystem.EnemyTurn(); Util.PlayerTurnOver = false; } float playerHealth = EntityManager.GetComponent <HealthComponent>(Util.PlayerID).Amount; if (playerHealth <= 0) { UISystem.Message("---"); UISystem.Message("You died."); UISystem.Message(""); UISystem.Message("Press ESC to exit..."); gameOver = true; } // remove all entities that died this turn EntityManager.CleanUpEntities(); } base.Update(gameTime); }
/// <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); }