/// <summary> /// loads an item based on the item file /// </summary> /// <param name="path">the filepath of the item</param> /// <returns>The item</returns> public Item.Item LoadItem(string path) { Item.Item newItem = null; using (Stream inStream = File.OpenRead(path)) { using (BinaryReader input = new BinaryReader(inStream)) { string type = input.ReadString(); if (type.ToUpper() == "WEAPON") { Item.Weapon newWeapon; int rof = input.ReadInt32(); int damage = input.ReadInt32(); double reload = input.ReadDouble(); int clip = input.ReadInt32(); double spread = input.ReadDouble(); int loadedAmmo = input.ReadInt32(); int ammo = input.ReadInt32(); string name = input.ReadString(); string sprite = input.ReadString(); newWeapon = new Item.Weapon(rof, damage, reload, name, clip, spread); Console.WriteLine("ROF:\t" + rof); Console.WriteLine("Damage:\t" + damage); Console.WriteLine("Reload:\t" + reload); Console.WriteLine("Clip:\t" + clip); Console.WriteLine("Spread:\t" + spread); Console.WriteLine("Loaded Ammo:\t" + loadedAmmo); Console.WriteLine("Ammo:\t" + ammo); Console.WriteLine("Name:\t" + name); Console.WriteLine("Sprite:\t" + sprite); newWeapon.Ammo = ammo; newWeapon.LoadedAmmo = loadedAmmo; if (Sprites.spritesDictionary.ContainsKey(sprite)) { newWeapon.previewSprite = Sprites.spritesDictionary[sprite]; } else { newWeapon.previewSprite = Sprites.spritesDictionary["NULL"]; } return(newWeapon); } else { newItem = new Item.Item(); string name = input.ReadString(); string sprite = input.ReadString(); } } } return(newItem); }
//constructor public Mugging(string name, string objective, string description, Vector2 start, Player player) : base(name, objective, description, start, player, WinCondition.EnemyDies, 0, 10) { mugger = new LivingEntity(new FloatRectangle(start.X, start.Y, MUGGER_WIDTH, MUGGER_WIDTH), Sprites.spritesDictionary["mugger"], 10); knife = new Weapon(20, 1, 3, "Knife", 1, 99); muggerAI = new LowAI(mugger); mugger.ai = muggerAI; entitites = new List<Entity.Entity>(); entitites.Add(mugger); EnemyToKill = mugger; //manager.CurrentWorld.manager.AddEntity(mugger); }
/// <summary> /// What happens with Update? /// </summary> public void HandleInput(KeyboardState keyState, MouseState mouseState, GameTime time) { player = Game1.Instance.worldManager.CurrentWorld.manager.GetPlayer(); if (Game1.state == GameState.Game) { //Detects if the player wants to move #region Movement Input int speed = 4; //int deltaX = 0; //int deltaY = 0; //The following statements check the movement keys if (keyState.IsKeyDown(Keys.W)) //Move up { //deltaY -= speed; player.movement.Y = speed * -1; } if (keyState.IsKeyDown(Keys.A)) //move down { //deltaX -= speed; player.movement.X = speed * -1; } if (keyState.IsKeyDown(Keys.S)) //move left { //deltaY += speed; player.movement.Y = speed; } if (keyState.IsKeyDown(Keys.D)) //move right { //deltaX += speed; player.movement.X = speed; } if (player.movement.X == 0) { if (player.movement.Y < 0) { player.direction = Entity.Direction.Up; } else if (player.movement.Y > 0) { player.direction = Entity.Direction.Down; } } else if (player.movement.X < 0) { if (player.movement.Y < 0) { player.direction = Entity.Direction.UpLeft; } else if (player.movement.Y == 0) { player.direction = Entity.Direction.Left; } else if (player.movement.Y > 0) { player.direction = Entity.Direction.DownLeft; } } else { if (player.movement.Y < 0) { player.direction = Entity.Direction.UpRight; } else if (player.movement.Y == 0) { player.direction = Entity.Direction.Right; } else if (player.movement.Y > 0) { player.direction = Entity.Direction.DownRight; } } /* * if (Game1.Instance.worldManager.CurrentWorld.tiles[(int)(player.location.X / GUI.Tile.SIDE_LENGTH)][(int)(player.location.Y / GUI.Tile.SIDE_LENGTH)] is GUI.Door) * { * GUI.Door door = Game1.Instance.worldManager.CurrentWorld.tiles[(int)(player.location.X / GUI.Tile.SIDE_LENGTH)][(int)(player.location.Y / GUI.Tile.SIDE_LENGTH)] as GUI.Door; * Game1.Instance.worldManager.current = door.World; * player.location.X = door.Destination.X * GUI.Tile.SIDE_LENGTH; * player.location.Y = door.Destination.Y * GUI.Tile.SIDE_LENGTH; * }*/ #endregion Item.Weapon weapon = Game1.Instance.worldManager.CurrentWorld.manager.GetPlayer().inventory.GetEquippedPrimary(); //Selects the weapon the player wants #region Quick Weapon Select if (keyState.IsKeyDown(Keys.D1)) //Quick Weapon Select { weapon.Reloading = false; player.lastShot = 0D; player.inventory.ActiveWeapon = 1; } if (keyState.IsKeyDown(Keys.D2)) //Quick Weapon Select { weapon.Reloading = false; player.lastShot = 0D; player.inventory.ActiveWeapon = 2; } if (keyState.IsKeyDown(Keys.D3)) //Quick Weapon Select { weapon.Reloading = false; player.lastShot = 0D; player.inventory.ActiveWeapon = 3; } if (keyState.IsKeyDown(Keys.D4)) //Quick Weapon Select { weapon.Reloading = false; player.lastShot = 0D; player.inventory.ActiveWeapon = 4; } if (keyState.IsKeyDown(Keys.D5)) //Quick Weapon Select { weapon.Reloading = false; player.lastShot = 0D; player.inventory.ActiveWeapon = 5; } if (keyState.IsKeyDown(Keys.D6)) //Quick Weapon Select { weapon.Reloading = false; player.lastShot = 0D; player.inventory.ActiveWeapon = 6; } if (keyState.IsKeyDown(Keys.D7)) //Quick Weapon Select { weapon.Reloading = false; player.lastShot = 0D; player.inventory.ActiveWeapon = 7; } if (keyState.IsKeyDown(Keys.D8)) //Quick Weapon Select { weapon.Reloading = false; player.lastShot = 0D; player.inventory.ActiveWeapon = 8; } if (keyState.IsKeyDown(Keys.D9)) //Quick Weapon Select { weapon.Reloading = false; player.lastShot = 0D; player.inventory.ActiveWeapon = 9; } if (keyState.IsKeyDown(Keys.D0)) //Quick Weapon Select { weapon.Reloading = false; player.lastShot = 0D; player.inventory.ActiveWeapon = 0; } #endregion player.cursor = mouseState.Position; // Douglas Gliner #region mouse aim (move to player?) // mouse location relative to CENTER OF SCREEN (not player since those coords are in a different system) //float newAngle = 0f; /* * if (mouseState.Position.X != RenderManager.ViewportWidth / 2 || mouseState.Position.Y != RenderManager.ViewportHeight / 2) * { * newAngle = (float)Math.Acos(Vector2.Dot(new Vector2(0, -1), mousePositionVector) / mousePositionVector.Length()); // original angle vector length should always be 1 * } * * // i dont remember how to math......... * // range of invCos is 0-pi, can i extend it to 2pi using multiplication or whatever? i dont remember ;-;.... * // PERHAPS SLOW DOWN PLAYER IF NOT MOVING IN SAME DIRECTION AS FACING (as an interval of course, otherwise always slow unless at the exact angle.) * if (mousePositionVector.X < 0) * WorldManager.player.faceDirection = newAngle * -1; * else * WorldManager.player.faceDirection = newAngle; */ // Its this simple!! Vector2 mousePositionVector = new Vector2(mouseState.Position.X - (RenderManager.ViewportWidth / 2), mouseState.Position.Y - (RenderManager.ViewportHeight / 2)); player.faceDirection = (float)(Math.Atan2(mousePositionVector.Y, mousePositionVector.X) + (Math.PI / 2)); #endregion //handles mouse input if (mouseState.LeftButton == ButtonState.Pressed) //Primary fire { player.Attack(0, player.inventory.GetEquippedPrimary()); } } if ((keyState.IsKeyDown(Keys.Q) && !previousState.IsKeyDown(Keys.Q)) || mouseState.MiddleButton == ButtonState.Pressed) //weapon wheel { if (Game1.state == GameState.Game) { Game1.state = GameState.WeaponWheel; } } if (keyState.IsKeyDown(Keys.E)) //Interact { player.Interact(); //Game1.Instance.worldManager.CurrentWorld.playerMap.printMap(); } if (keyState.IsKeyDown(Keys.R)) //Reload { if (player.inventory.GetEquippedPrimary().LoadedAmmo != player.inventory.GetEquippedPrimary().maxClip&& player.inventory.GetEquippedPrimary().Ammo > 0 && !player.inventory.GetEquippedPrimary().Reloading) { player.Reload(); } } if (keyState.IsKeyDown(Keys.I)) //Inventory { if (Game1.state == GameState.Game || Game1.state == GameState.Pause || Game1.state == GameState.QuestLog) { Game1.state = GameState.Inventory; } } if (keyState.IsKeyDown(Keys.G)) //Quest log { if (Game1.state == GameState.Game || Game1.state == GameState.Pause || Game1.state == GameState.Inventory) { // DEBUG DIALOG TEST //DialogBox test = new DialogBox(new Vector2(200, 150), DialogBoxType.OkCancel, "Test dialog!!", "You've opened the quest log!", new Vector2(300, 300)); //test.yesEvent += test_yesEvent; //test.noEvent += test_noEvent; //NPCTalkUI test = new NPCTalkUI(new Vector2(200, 150), "TESTER", "lololo wow ok hi XD!!"); //RenderManager.AddDialog(test); Game1.state = GameState.QuestLog; } } if (keyState.IsKeyDown(Keys.Escape) && !previousState.IsKeyDown(Keys.Escape)) //Escape { if (Game1.state == GameState.Game || Game1.state == GameState.Inventory || Game1.state == GameState.QuestLog) { Game1.state = GameState.Pause; } else if (Game1.state == GameState.Pause) { Game1.state = GameState.Game; } } previousState = keyState; }
/// <summary> /// loads an item based on the item file /// </summary> /// <param name="path">the filepath of the item</param> /// <returns>The item</returns> public Item.Item LoadItem(string path) { Item.Item newItem = null; using(Stream inStream = File.OpenRead(path)) { using(BinaryReader input = new BinaryReader(inStream)) { string type = input.ReadString(); if(type.ToUpper() == "WEAPON") { Item.Weapon newWeapon; int rof = input.ReadInt32(); int damage = input.ReadInt32(); double reload = input.ReadDouble(); int clip = input.ReadInt32(); double spread = input.ReadDouble(); int loadedAmmo = input.ReadInt32(); int ammo = input.ReadInt32(); string name = input.ReadString(); string sprite = input.ReadString(); newWeapon = new Item.Weapon(rof, damage, reload, name, clip, spread); Console.WriteLine("ROF:\t" + rof); Console.WriteLine("Damage:\t" + damage); Console.WriteLine("Reload:\t" + reload); Console.WriteLine("Clip:\t" + clip); Console.WriteLine("Spread:\t" + spread); Console.WriteLine("Loaded Ammo:\t" + loadedAmmo); Console.WriteLine("Ammo:\t" + ammo); Console.WriteLine("Name:\t" + name); Console.WriteLine("Sprite:\t" + sprite); newWeapon.Ammo = ammo; newWeapon.LoadedAmmo = loadedAmmo; if (Sprites.spritesDictionary.ContainsKey(sprite)) newWeapon.previewSprite = Sprites.spritesDictionary[sprite]; else newWeapon.previewSprite = Sprites.spritesDictionary["NULL"]; return newWeapon; } else { newItem = new Item.Item(); string name = input.ReadString(); string sprite = input.ReadString(); } } } return newItem; }
/* public override void DrawUI(SpriteBatch spriteBatch, GameTime gameTime) { healthBar.Draw(spriteBatch, gameTime); }*/ /// <summary> /// The Living Entity attacks /// </summary> /// <param name="type">0 is primary fire, 1 is secondary</param> /// <param name="weapon">The weapon with which they are attacking</param> public virtual void Attack(int type, Weapon weapon = null) { if (type == 0) { if (weapon.LoadedAmmo > 0 && lastShot > (60000D / (weapon.rateOfFire)) && !weapon.Reloading) { lastShot = 0D; double trajectory = faceDirection + ((rand.NextDouble() * Math.PI / 2) - (Math.PI / 4)) * (weapon.spread / 100); // or we could make the cone the weapon spread amount (-weapon.spread to weapon.spread) // this is just using the weapon.spread as a multiplier for a max range being -pi/2 to pi/2 relative to face direction // need to get FRONT face, location will not work since that is not rotated, only the sprite is. // we must utilize facedirection to calculate where on the outside of the sprite the shooting location should be set then apply an offset for weaponry... //Game1.Instance.worldManager.CurrentWorld.manager.FireBullet(location.X, location.Y, (float)System.Math.Cos(trajectory), (float)System.Math.Sin(trajectory), inventory.GetEquippedPrimary().Damage, this); // Douglas Gliner Game1.Instance.worldManager.CurrentWorld.manager.FireBullet(((int) (sprite.Texture.Width / 2) * (float) Math.Cos(faceDirection - Math.PI / 4)) + location.Center.X, ((int) (sprite.Texture.Height / 2) * (float) Math.Sin(faceDirection - Math.PI / 4)) + location.Center.Y, (float) Math.Cos(trajectory - Math.PI / 2), (float) Math.Sin(trajectory - Math.PI / 2), inventory.GetEquippedPrimary().Damage, this); weapon.LoadedAmmo--; } } }
/// <summary> /// writes to a file the details behind an item /// </summary> /// <param name="item">the item to save</param> /// <returns>if the save was successful</returns> public bool SaveItem(Item.Item item) { // Create a stream, then a writer Stream outStream = null; BinaryWriter output = null; bool successful; try { //figure out the save path int startname = saveLoc.LastIndexOf('/') + 1; int endname = saveLoc.LastIndexOf('.'); string name = saveLoc.Substring(startname, endname - startname); string directory = saveLoc.Substring(0, startname) + "/" + name; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } //initialize the binary writer outStream = File.OpenWrite(directory + item.name + ".item"); output = new BinaryWriter(outStream); //write data if (item is Item.Weapon) { output.Write("Weapon"); Item.Weapon weapon = (Item.Weapon)item; output.Write(weapon.rateOfFire); output.Write(weapon.Damage); output.Write(weapon.ReloadTime); output.Write(weapon.maxClip); output.Write(weapon.spread); output.Write(weapon.LoadedAmmo); output.Write(weapon.Ammo); } else { output.Write("Item"); } output.Write(item.name); //get the key of the texture Dictionary <String, Sprite> sDict = Sprites.spritesDictionary; string key = "NULL"; foreach (string spriteKey in sDict.Keys.ToList()) { if (sDict[spriteKey] == item.previewSprite) { key = spriteKey; } } output.Write(key); successful = true; } catch (Exception e) { Console.WriteLine("Error saving item: " + e.Message); successful = false; } finally { if (output != null) { output.Close(); } } return(successful); }