/// <summary> /// Creates a new Living Entity /// </summary> /// <param name="rect">The rectangle that represents the location and width and height of the entity</param> /// <param name="fileName">the location of the sprite for this entity</param> public LivingEntity(FloatRectangle rect, Sprite sprite, int health, AI.AI ai = null, int cash = 0) : base(rect, sprite) { inventory = new Inventory(); time = new GameTime(); lastShot = 60000D; interactRange = 32; // the below depend on texture, this should not be needed ever but because of the player texture it is... //this.interactBoundsOffsetY = interactBoundsOffsetY; //this.interactBoundsOffsetX = interactBoundsOffsetX; this.cash = cash; this.health = health; this.ai = ai; maxHealth = health; color = Color.Red; healthBar = new ProgressBar(new Vector2(60, 20)); healthBar.MaxValue = maxHealth; healthBar.CurrentValue = health; healthBar.IncludeText = Name; healthBar.LoadVisuals(Game1.Instance.Content, Game1.Instance.GraphicsDevice); controls.Add(healthBar); }
public void Close() { /* // set active weapon if changed... Item.Item item = matrix.GetItemFromLocation(new Vector2(-80, 0)); if (item != null && item != currentInventory.EntityInventory[currentInventory.ActiveWeapon]) currentInventory.ActiveWeapon = currentInventory.EntityInventory.IndexOf(item); currentInventory = null; matrix.Clear(); isInventoryLoaded = false; * */ currentInventory = null; weaponButtonsContainer.Clear(); isInventoryLoaded = false; }
/* void WeaponWheelUIV2_KeyClicked(object sender, KeysEventArgs e) { if(e.Keys.Contains(Keys.Q) || e.Keys.Contains(Keys.Escape)) { currentInventory = null; } } */ // <summary> /// Load the inventory you want to view. /// </summary> /// <param name="inventory"></param> public void Load(Inventory inventory) { // Load inventory currentInventory = inventory; // Get just weps. (need where?) List<Weapon> weapons = currentInventory.EntityInventory.Where(item => item is Weapon).Cast<Weapon>().ToList(); // Find angle interval angleInterval = (float)(2 * Math.PI) / currentInventory.Holster.Length; // weapon button side length // TO-DO automatic!! weaponButtonSideLength = 75; // or weaponStatsContainer.Size.X / 2? radius = ((sideLength / 2) - (weaponButtonSideLength / 2)); // Create button with weapon image and name. // if current weapon, highlight button and place stats in center. float currentAngle = 0; foreach(Weapon weapon in weapons) { ButtonContainer<Weapon> weaponButton = new ButtonContainer<Weapon>(); weaponButton.Size = new Vector2(weaponButtonSideLength, weaponButtonSideLength); weaponButton.Location = new Vector2(radius * (float)Math.Cos(currentAngle), radius * (float)Math.Sin(currentAngle)); weaponButton.Data = weapon; if(weapon == currentInventory.EntityInventory[currentInventory.ActiveWeapon]) { weaponButton.DefaultBorder = new BorderInfo(5, Color.Purple); weaponStatsContainer.Load(weapon); currentWeaponButton = weaponButton; } else { weaponButton.DefaultBorder = new BorderInfo(1, Color.Transparent); } weaponButton.Fill = new FillInfo(weapon.previewSprite.Texture, Color.White); weaponButton.Text = weapon.name; weaponButton.Click += weaponButton_Click; weaponButton.Alignment = ControlAlignment.Center; Add(weaponButton); currentAngle += angleInterval; } }
/// <summary> /// Load the inventory you want to view. /// </summary> /// <param name="inventory"></param> public void Load(Inventory inventory) { currentInventory = inventory; foreach (Item.Item i in inventory.EntityInventory.Where(i => !(i.Equals(inventory.EntityInventory[inventory.ActiveWeapon])))) inventoryMatrix.AddToMatrix(i); if (inventory.ActiveWeapon != -1) currentWeaponMatrix.AddToMatrix(inventory.EntityInventory[inventory.ActiveWeapon]); isInventoryLoaded = true; }
public void Close() { // set active weapon if changed... Item.Item item = currentWeaponMatrix[0]; if (item != null && item != currentInventory.EntityInventory[currentInventory.ActiveWeapon]) currentInventory.ActiveWeapon = currentInventory.EntityInventory.IndexOf(item); currentWeaponMatrix.Clear(); inventoryMatrix.Clear(); currentInventory = null; isInventoryLoaded = false; }
/// <summary> /// Load the inventory you want to view. /// </summary> /// <param name="inventory"></param> public void Load(Inventory inventory) { /* currentInventory = inventory; foreach (Item.Item i in inventory.EntityInventory.Where(i => !(i.Equals(inventory.EntityInventory[inventory.ActiveWeapon])))) matrix.AddToMatrix(new DragableControl(i)); if (inventory.ActiveWeapon != -1) matrix.AddOutsideMatrix(new DragableControl(inventory.EntityInventory[inventory.ActiveWeapon]), new Vector2(-90, -10)); isInventoryLoaded = true; * */ currentInventory = inventory; int count = 0; foreach(int i in inventory.Holster) { Weapon weapon = inventory.EntityInventory[i] as Weapon; Button weaponInfoRoot = new Button(); weaponInfoRoot.Location = new Vector2((weaponButtonsContainer.Size.X / inventory.Holster.Length) * count, 0); weaponInfoRoot.Size = new Vector2(weaponButtonsContainer.Size.X / inventory.Holster.Length, weaponButtonsContainer.Size.Y); weaponInfoRoot.Text = weapon.name; weaponInfoRoot.Click += weaponInfoRoot_Click; if(i == inventory.ActiveWeapon) { weaponInfoRoot.DefaultBorder = new BorderInfo(weaponInfoRoot.DefaultBorder.width, Color.Purple); weaponInfoRoot.DefaultFill = new FillInfo(Color.LightGray); weaponInfoRoot.HoverFill = new FillInfo(Color.LightGray); currentActive = weaponInfoRoot; } //weaponInfoRoot.parent = weaponButtonsContainer; weaponButtonsContainer.Add(weaponInfoRoot); // image // name // stats count++; } isInventoryLoaded = true; }
/// <summary> /// loads a save file /// </summary> private void LoadSave() { Console.WriteLine(saveLoc); Stream inStream = null; BinaryReader input = null; try { //open the file for reading inStream = File.OpenRead(saveLoc); input = new BinaryReader(inStream); //red the file string world = input.ReadString(); Console.WriteLine("World: " + world); int pX = input.ReadInt32(); int pY = input.ReadInt32(); Console.WriteLine("X, Y: " + pX + ", " + pY); int pHealth = input.ReadInt32(); Console.WriteLine("Health: " + pHealth); int pCash = input.ReadInt32(); Console.WriteLine("Cash: " + pCash); int pQuestPoints = input.ReadInt32(); Console.WriteLine("QuestPoints: " + pQuestPoints); //read the quests int numQuests = input.ReadInt32(); Console.WriteLine("Num quests: " + numQuests); object[,] quests = new object[numQuests, 2]; for (int i = 0; i < numQuests; i++) { quests[i, 0] = input.ReadString(); Console.WriteLine("\tName: " + quests[i, 0]); quests[i, 1] = input.ReadInt32(); Console.WriteLine("\tStatus: " + quests[i, 1]); } //read the inventory int numItems = input.ReadInt32(); Console.WriteLine("Num Items: " + numItems); string[] items = new string[numItems]; for (int i = 0; i < numItems; i++) { items[i] = input.ReadString(); Console.WriteLine("\t" + items[i]); } int activeItem = input.ReadInt32(); Console.WriteLine("Active Weapon: " + activeItem); //make the world if (MainGame.worldManager.worlds.ContainsKey(world)) { MainGame.worldManager.worlds[world] = LoadWorld(world); } else { MainGame.worldManager.worlds.Add(world, LoadWorld(world)); } //set the player in the world Player player = new Player( new FloatRectangle(pX, pY, 32, 32), Sprites.spritesDictionary["player"] ); player.Cash = pCash; player.health = pHealth; player.QuestPoints = pQuestPoints; MainGame.worldManager.CurrentWorld.manager.AddEntity(player); Game1.Instance.collisionManager.SwitchWorld(); //load all of the quests in the quest file LoadQuests(MainGame.worldManager.worldQuests); //load the quest status string quest; QuestLog log = MainGame.worldManager.worldQuests; QuestLog pLog = player.log; for (int i = 0; i < numQuests; i++) { quest = (string)quests[i, 0]; if (log.ContainsQuest(quest)) { pLog[quest] = log[quest]; pLog[quest].Status = (int)quests[i, 1]; } } //add new quests to quest log foreach (Quest newQuest in log) { if (!pLog.ContainsQuest(newQuest)) { pLog.Add(newQuest); } } //load the items Item.Inventory inventory = player.inventory; Item.Item newItem; for (int i = 0; i < numItems; i++) { string name = items[i]; //newItem.image = Sprites.spritesDictionary[newItem.name].Texture; //figure out the save path int startname = saveLoc.LastIndexOf('/') + 1; int endname = saveLoc.LastIndexOf('.'); string filename = saveLoc.Substring(startname, endname - startname); string directory = saveLoc.Substring(0, startname) + "/" + filename; newItem = LoadItem(directory + "/" + name + ".item"); inventory.Add(newItem); } inventory.ActiveWeapon = activeItem; //add the player to the world MainGame.worldManager.CurrentWorld.manager.AddEntity(player); //add the player to the quests foreach (Quest loopQuest in MainGame.worldManager.worldQuests) { loopQuest.player = player; } } catch (Exception e) { Console.WriteLine("Error reading file: " + e.Message); Console.WriteLine("Stack: \n\t" + e.StackTrace); } finally { if (input != null) { input.Close(); } } }
/// <summary> /// saves the game /// </summary> public void Save() { // Create a stream, then a writer Stream outStream = null; BinaryWriter output = null; try { if (!Directory.Exists("./Content/SaveFiles")) { Directory.CreateDirectory("./Content/SaveFiles"); } //initialize them outStream = File.OpenWrite(saveLoc); output = new BinaryWriter(outStream); //Get the player Entity.Player player = MainGame.worldManager.CurrentWorld.manager.GetPlayer(); //get the current world string world = MainGame.worldManager.CurrentWorldString; //get the player's stats int playerX = player.location.IntX; int playerY = player.location.IntY; int playerHealth = player.health; int pCash = player.Cash; int pQuestPoints = player.QuestPoints; //get the quest statuses QuestLog log = player.log; object[,] questStatuses = new object[log.Count, 2]; for (int i = 0; i < log.Count; i++) { questStatuses[i, 0] = log[i].Name; questStatuses[i, 1] = log[i].Status; } //get the items in the inventory List <string> items = new List <string>(); Item.Inventory inventory = player.inventory; foreach (Item.Item item in inventory) { items.Add(item.name); } //store all of the data in the file output.Write(world); output.Write(playerX); output.Write(playerY); output.Write(playerHealth); output.Write(pCash); output.Write(pQuestPoints); output.Write((Int32)log.Count); Console.WriteLine(log.Count); for (int i = 0; i < log.Count; i++) { output.Write((string)questStatuses[i, 0]); output.Write((int)questStatuses[i, 1]); } output.Write(items.Count); foreach (Item.Item item in inventory) { if (SaveItem(item)) { output.Write(item.name); } } output.Write(inventory.ActiveWeapon); } catch (Exception e) { Console.WriteLine("Error while saving the game: " + e.Message); Console.WriteLine("Stack: \n\t" + e.StackTrace); } finally { if (output != null) { // Done writing output.Close(); } } }