//Overrrides the shoot method to create a more accurate short range projectile public override Projectile Shoot(ContentManager content, Character p, Camera c, int tileSize, Character e) { return new Projectile(content, p.Loc.X, p.Loc.Y, p.Direction + GetSpread() * (Math.PI / 180.0), 5, "EmptyTile", true, new Rectangle((int)((c.camPos.X + p.Loc.X) * tileSize), (int)((c.camPos.Y + p.Loc.Y) * tileSize), tileSize, tileSize), 1, false, e.IsPlayer); }
/// <summary> /// Checks to see if there is collision /// </summary> /// <param name="e"></param> /// <param name="tileSize"></param> /// <returns></returns> public string CheckCollide(Entity e, Camera c, bool left, bool right, bool top, bool bot) { //First checks to see if the object has collision before testing for collision if (!collision) { //Returns none if there is no collision return "none"; } //Checks to see if the entity is a Projectile if (e is Projectile) { //Then checks for collision if (this.loc.X - 0.5 <= e.Loc.X && this.loc.X + 0.5 >= e.Loc.X && this.loc.Y - 0.5 <= e.Loc.Y && this.loc.Y + 0.5 >= e.Loc.Y) { //Returns hit if the object does collide return "hit"; } } //Checks to see if the entity is a character and if it is the player else if (e is Character) { //Gets the boolean values of the the corresponding four corners of the player bool topLeftCorner = (this.loc.X - 0.5 <= e.Loc.X - 0.5 && this.loc.X + 0.5 >= e.Loc.X - 0.5 && this.loc.Y - 0.5 <= e.Loc.Y - 0.5 && this.loc.Y + 0.5 >= e.Loc.Y - 0.5); bool topRightCorner = (this.loc.X - 0.5 <= e.Loc.X + 0.5 && this.loc.X + 0.5 >= e.Loc.X + 0.5 && this.loc.Y - 0.5 <= e.Loc.Y - 0.5 && this.loc.Y + 0.5 >= e.Loc.Y - 0.5); bool botLeftCorner = (this.loc.X - 0.5 <= e.Loc.X - 0.5 && this.loc.X + 0.5 >= e.Loc.X - 0.5 && this.loc.Y - 0.5 <= e.Loc.Y + 0.5 && this.loc.Y + 0.5 >= e.Loc.Y + 0.5); bool botRightCorner = (this.loc.X - 0.5 <= e.Loc.X + 0.5 && this.loc.X + 0.5 >= e.Loc.X + 0.5 && this.loc.Y - 0.5 <= e.Loc.Y + 0.5 && this.loc.Y + 0.5 >= e.Loc.Y + 0.5); //Checks if any corners collide if (topLeftCorner || topRightCorner || botLeftCorner || botRightCorner) { //Creates variables to store the corners being checked double corner1X, corner2X, corner1Y, corner2Y; //Checks the top left corner to determine the side of collision if (topLeftCorner) { //Gets the points for the two corners to check corner1X = Math.Abs((this.loc.X + 0.5) - (e.Loc.X - 0.5)); corner1Y = Math.Abs((this.loc.Y - 0.5) - (e.Loc.Y - 0.5)); corner2X = Math.Abs((this.loc.X - 0.5) - (e.Loc.X - 0.5)); corner2Y = Math.Abs((this.loc.Y + 0.5) - (e.Loc.Y - 0.5)); //Uses the distance formula to see which side it is closer to and returns the closer side if (Math.Sqrt(Math.Pow(corner1X, 2) + Math.Pow(corner1Y, 2)) <= Math.Sqrt(Math.Pow(corner2X, 2) + Math.Pow(corner2Y, 2)) && !right) { e.Loc.X += corner1X; if (e.IsPlayer) { c.camPos.X -= corner1X; } //Console.WriteLine("Right"); } else if (Math.Sqrt(Math.Pow(corner1X, 2) + Math.Pow(corner1Y, 2)) > Math.Sqrt(Math.Pow(corner2X, 2) + Math.Pow(corner2Y, 2)) && !bot) { e.Loc.Y += corner2Y; if (e.IsPlayer) { c.camPos.Y -= corner2Y; } //Console.WriteLine("Bottom"); } } if (topRightCorner) { //Gets the points for the two corners to check corner1X = Math.Abs((this.loc.X - 0.5) - (e.Loc.X + 0.5)); corner1Y = Math.Abs((this.loc.Y - 0.5) - (e.Loc.Y - 0.5)); corner2X = Math.Abs((this.loc.X + 0.5) - (e.Loc.X + 0.5)); corner2Y = Math.Abs((this.loc.Y + 0.5) - (e.Loc.Y - 0.5)); //Uses the distance formula to see which side it is closer to and returns the closer side if (Math.Sqrt(Math.Pow(corner1X, 2) + Math.Pow(corner1Y, 2)) <= Math.Sqrt(Math.Pow(corner2X, 2) + Math.Pow(corner2Y, 2)) && !left) { e.Loc.X -= corner1X; if (e.IsPlayer) { c.camPos.X += corner1X; } //Console.WriteLine("Left"); } else if (Math.Sqrt(Math.Pow(corner1X, 2) + Math.Pow(corner1Y, 2)) > Math.Sqrt(Math.Pow(corner2X, 2) + Math.Pow(corner2Y, 2)) && !bot) { e.Loc.Y += corner2Y; if (e.IsPlayer) { c.camPos.Y -= corner2Y; } //Console.WriteLine("Bottom"); } } if (botLeftCorner) { //Gets the points for the two corners to check corner1X = Math.Abs((this.loc.X - 0.5) - (e.Loc.X - 0.5)); corner1Y = Math.Abs((this.loc.Y - 0.5) - (e.Loc.Y + 0.5)); corner2X = Math.Abs((this.loc.X + 0.5) - (e.Loc.X - 0.5)); corner2Y = Math.Abs((this.loc.Y + 0.5) - (e.Loc.Y + 0.5)); //Uses the distance formula to see which side it is closer to and returns the closer side if (Math.Sqrt(Math.Pow(corner1X, 2) + Math.Pow(corner1Y, 2)) <= Math.Sqrt(Math.Pow(corner2X, 2) + Math.Pow(corner2Y, 2)) && !top) { e.Loc.Y -= corner1Y; if (e.IsPlayer) { c.camPos.Y += corner1Y; } //Console.WriteLine("Top"); } else if (Math.Sqrt(Math.Pow(corner1X, 2) + Math.Pow(corner1Y, 2)) > Math.Sqrt(Math.Pow(corner2X, 2) + Math.Pow(corner2Y, 2)) && !right) { e.Loc.X += corner2X; if (e.IsPlayer) { c.camPos.X -= corner2X; } //Console.WriteLine("Right"); } } if (botRightCorner) { //Gets the points for the two corners to check corner1X = Math.Abs((this.loc.X + 0.5) - (e.Loc.X + 0.5)); corner1Y = Math.Abs((this.loc.Y - 0.5) - (e.Loc.Y + 0.5)); corner2X = Math.Abs((this.loc.X - 0.5) - (e.Loc.X + 0.5)); corner2Y = Math.Abs((this.loc.Y + 0.5) - (e.Loc.Y + 0.5)); //Uses the distance formula to see which side it is closer to and returns the closer side if (Math.Sqrt(Math.Pow(corner1X, 2) + Math.Pow(corner1Y, 2)) <= Math.Sqrt(Math.Pow(corner2X, 2) + Math.Pow(corner2Y, 2)) && !top) { e.Loc.Y -= corner1Y; if (e.IsPlayer) { c.camPos.Y += corner1Y; } //Console.WriteLine("Top"); } else if (Math.Sqrt(Math.Pow(corner1X, 2) + Math.Pow(corner1Y, 2)) > Math.Sqrt(Math.Pow(corner2X, 2) + Math.Pow(corner2Y, 2)) && !left) { e.Loc.X -= corner2X; if (e.IsPlayer) { c.camPos.X += corner2X; } //Console.WriteLine("Left"); } } } } //Returns none if there is no collision return "none"; }
// constructor that reads fro a file map.cs public Map(ContentManager content, string filename, Camera c, Character player, List<Enemy> enemies, int screenWidth) { Texture2D empTexture = content.Load<Texture2D>("EmptyTile"); tileSize = screenWidth / 20; BinaryReader input = new BinaryReader(File.OpenRead("Content/" + filename)); int mapWidth = input.ReadInt32(); int mapHeight = input.ReadInt32(); tileMap = new Texture2D[mapWidth, mapHeight]; objectMap = new MapObject[mapWidth, mapHeight]; tileRot = new int[tileMap.GetLength(0), tileMap.GetLength(1)]; objRot = new int[tileMap.GetLength(0), tileMap.GetLength(1)]; for (int i = 0; i < tileMap.GetLength(0); i++) { for (int j = 0; j < tileMap.GetLength(1); j++) { string txtrString = input.ReadString(); if (!txtrString.Equals("null")) { Texture2D texture = content.Load<Texture2D>(txtrString); tileMap[i, j] = texture; } else { tileMap[i, j] = empTexture; } } } for (int i = 0; i < tileMap.GetLength(0); i++) { for (int j = 0; j < tileMap.GetLength(1); j++) { tileRot[i, j] = input.ReadInt32(); } } for (int i = 0; i < objectMap.GetLength(0); i++) { for (int j = 0; j < objectMap.GetLength(1); j++) { string txtrString = input.ReadString(); if (!txtrString.Equals("null")) { objectMap[i, j] = new MapObject(content, true, txtrString, i, j); } else { objectMap[i, j] = null; } } } for (int i = 0; i < objectMap.GetLength(0); i++) { for (int j = 0; j < objectMap.GetLength(1); j++) { objRot[i, j] = input.ReadInt32(); } } int entWidth = input.ReadInt32(); int entHieght = input.ReadInt32(); int[,] entRot = new int[entWidth, entHieght]; for (int i = 0; i < entRot.GetLength(0); i++) { for (int j = 0; j < entRot.GetLength(1); j++) { entRot[i, j] = input.ReadInt32(); } } for (int x = 0; x < entWidth; x++) { for (int y = 0; y < entHieght; y++) { string txtrString = input.ReadString(); if (txtrString.Equals("null")) { continue; } else if (txtrString.Equals("Enemy")) { CreateEnemy.CreateNormalEnemy(ref enemies, content, c, this, x, y, entRot[x,y] * -1.5708f); } else if (txtrString.Equals("RiotEnemy")) { CreateEnemy.CreateRiotEnemy(ref enemies, content, c, this, x, y, entRot[x, y] * -1.5708f); } } } string playerPos = input.ReadString(); string[] playerParts = playerPos.Split(','); double distX = player.Loc.X - double.Parse(playerParts[1]); double distY = player.Loc.Y - double.Parse(playerParts[2]); player.Loc.X -= distX; player.Loc.Y -= distY; c.camPos.X += distX; c.camPos.Y += distY; input.Close(); }
public virtual Projectile Shoot(ContentManager content, Character p, Camera c, int tileSize, Character e) { timeSinceLastShot = 0; if (CheckAmmo()) { Projectile proj = null; if (name.Equals("Rifle")) { proj = new Projectile(content, p.Loc.X, p.Loc.Y, p.Direction + GetSpread() * (Math.PI / 180.0), 3, "BulletTwo", true, new Rectangle((int)((c.camPos.X + p.Loc.X) * tileSize), (int)((c.camPos.Y + p.Loc.Y) * tileSize), tileSize, tileSize), range, true, e.IsPlayer); } else if (name.Equals("Pistol")) { proj = new Projectile(content, p.Loc.X, p.Loc.Y, p.Direction + GetSpread() * (Math.PI / 180.0), 3, "Bullet", true, new Rectangle((int)((c.camPos.X + p.Loc.X) * tileSize), (int)((c.camPos.Y + p.Loc.Y) * tileSize), tileSize, tileSize), range, false, e.IsPlayer); } else { proj = new Projectile(content, p.Loc.X, p.Loc.Y, p.Direction + GetSpread() * (Math.PI / 180.0), 3, "BulletTwo", true, new Rectangle((int)((c.camPos.X + p.Loc.X) * tileSize), (int)((c.camPos.Y + p.Loc.Y) * tileSize), tileSize, tileSize), range, false, e.IsPlayer); } ammo[0]--; return proj; } else { return null; } }
public string CheckArea(Entity e, Camera c) { for (int i = (int)e.Loc.X - 2; i < (int)e.Loc.X + 2; i++) { //Loops through nearby y values for (int j = (int)e.Loc.Y - 2; j < (int)e.Loc.Y + 2; j++) { //Checks if the spot is a valid spot to check if (i > -1 && j > -1 && i < objectMap.GetLength(0) && j < objectMap.GetLength(1)) { //Checks to see if the object is null if (objectMap[i, j] != null) { bool left = false, right = false, top = false, bot = false; //Checks if there is an object to the left if ((i - 1) > -1 && objectMap[i - 1, j] != null && objectMap[i - 1, j].collision) { left = true; } //Checks if there is an object to the right if ((i + 1) < objectMap.GetLength(0) && objectMap[i + 1, j] != null && objectMap[i + 1, j].collision) { right = true; } //Checks if there is an object to the top if ((j - 1) > -1 && objectMap[i, j - 1] != null && objectMap[i, j - 1].collision) { top = true; } //Checks if there is an object to the bot if ((j + 1) < objectMap.GetLength(1) && objectMap[i, j + 1] != null && objectMap[i, j + 1].collision) { bot = true; } //Checks to see if the object collides if (!objectMap[i, j].CheckCollide(e, c, left, right, top, bot).Equals("none")) { //If the entity is a projectile then set the first index has a hit in it if (e is Projectile) { return "hit"; //Else is a character and puts the side with the collsion in the array } else { return objectMap[i, j].CheckCollide(e, c, left, right, top, bot); } } } } } } //Return the sides return "none"; }
//Shoots the player's current gun public static void ShootWeapon(Character player, MouseState mState, MouseState oldMState, List<Projectile> projectiles, bool temp, Camera c, ContentManager Content, Queue<SoundEffect> curSounds, Dictionary<string, SoundEffect> soundEffects, ref Map m) { if (!player.IsMeleeing && !(SkillSystem.skills[2].Active && player.IsSprinting)) { if (player.Weapon.Auto) { if (oldMState.LeftButton == ButtonState.Pressed) { if (temp) { SoundEffect TempSound; //enqueue gunshot sound //only shoot if not a null projectile Projectile p = player.Weapon.Shoot(Content, player, c, m.TileSize, player); if (p != null) { //create new projectile projectiles.Add(p); //load and enqueue sound soundEffects.TryGetValue("gunshot", out TempSound); curSounds.Enqueue(player.Weapon.ShootSound); m.sounds.Add(player.Loc); //add the player's sound to the map's sound queue for AI to detect m.sounds.Add(player.Loc); c.screenShake = true; } else { player.Weapon.Shoot(Content, player, c, m.TileSize, player); //enqueue gun click sound if empty soundEffects.TryGetValue("emptyClick", out TempSound); curSounds.Enqueue(TempSound); } } } } else { if (oldMState.LeftButton == ButtonState.Pressed && mState.LeftButton != ButtonState.Pressed) { if (temp) { if (player.Weapon.Name.Equals("Shotgun")) { SoundEffect TempSound; //enqueue gunshot sound //only shoot if not a null projectile Projectile p = player.Weapon.Shoot(Content, player, c, m.TileSize, player); if (p != null) { player.Weapon.Ammo[0] += 2; Projectile p2 = player.Weapon.Shoot(Content, player, c, m.TileSize, player); Projectile p3 = player.Weapon.Shoot(Content, player, c, m.TileSize, player); projectiles.Add(p); projectiles.Add(p2); projectiles.Add(p3); soundEffects.TryGetValue("shotgunSound.wav", out TempSound); curSounds.Enqueue(player.Weapon.ShootSound); m.sounds.Add(player.Loc); c.screenShake = true; } else { player.Weapon.Shoot(Content, player, c, m.TileSize, player); //enqueue gun click sound if empty soundEffects.TryGetValue("emptyClick", out TempSound); curSounds.Enqueue(TempSound); } } else { SoundEffect TempSound; //enqueue gunshot sound //only shoot if not a null projectile Projectile p = player.Weapon.Shoot(Content, player, c, m.TileSize, player); if (p != null) { projectiles.Add(p); soundEffects.TryGetValue("gunshot", out TempSound); curSounds.Enqueue(player.Weapon.ShootSound); m.sounds.Add(player.Loc); c.screenShake = true; } else { player.Weapon.Shoot(Content, player, c, m.TileSize, player); //enqueue gun click sound if empty soundEffects.TryGetValue("emptyClick", out TempSound); curSounds.Enqueue(TempSound); } } } } } } }
//Method to use the player's knife public static void Stab(Character player, KeyboardState state, KeyboardState oldState, ContentManager content, Camera c, int tileSize, List<Projectile> projectiles, Queue<SoundEffect> curSounds) { //If the player presses 'V' then does a melee attack if (state.IsKeyDown(Keys.Space) && oldState.IsKeyUp(Keys.Space) && (!SkillSystem.skills[2].Active && !player.IsSprinting) && !player.Weapon.isReloading) { projectiles.Add(weapons[0].Shoot(content, player, c, tileSize, player)); curSounds.Enqueue(weapons[0].ShootSound); //Sets the player to a melee state player.IsMeleeing = true; //Sets animation values player.NumOfFrames = 9; player.Frame = 0; player.FrameLevel = 0; player.TimePerFrame = 100; } }
//Creates a single riot enemy public static void CreateRiotEnemy(ref List<Enemy> enemies, ContentManager Content, Camera c, Map m, double x, double y, float dir) { enemies.Add(new RiotEnemy(Content, x, y, "NoTexture", PlayerPos.CalcRectangle(c.camPos.X, c.camPos.Y, x, y, m.TileSize, c.xOffset, c.yOffset), dir)); }
//Creates a single normal enemy public static void CreateNormalEnemy(ref List<Enemy> enemies, ContentManager Content, Camera c, Map m, double x, double y, float dir) { Enemy e = new Enemy(Content, x, y, "NoTexture", PlayerPos.CalcRectangle(c.camPos.X, c.camPos.Y, x, y, m.TileSize, c.xOffset, c.yOffset), dir); e.EntTexture = Content.Load<Texture2D>("Enemy" + rng.Next(1, 4)); enemies.Add(e); }