public Room1(ImageHandler images) { sizeX = 1600; sizeY = 1000; boxList = new List<Box>(0); laserShooters = new List<LaserShooter>(); laserCatchers = new List<LaserCatcher>(); player = new Player(200, 100, 4); AddBoxesAround(); screenX = Screen.X; screenY = Screen.Y; bluePortal = new Portal(); orangePortal = new Portal(); boxList.Add(new Box(300, 100, true, false)); boxList.Add(new Box(300, 200, true, false)); boxList.Add(new Box(300, 300, false, true)); boxList.Add(new Box(300, 400, false, true)); boxList.Add(new Box(300, 500, false, true)); boxList.Add(new Box(300, 600, false, true)); boxList.Add(new Box(300, 700, false, true)); boxList.Add(new Box(300, 800, false, true)); boxList.Add(new Box(100, 800, true, false)); boxList.Add(new Box(200, 800, true, false)); boxList.Add(new Box(600, 100, true, false)); boxList.Add(new Box(600, 200, true, false)); boxList.Add(new Box(600, 300, true, false)); boxList.Add(new Box(600, 400, true, false)); boxList.Add(new Box(600, 500, true, false)); boxList.Add(new Box(600, 600, true, false)); boxList.Add(new Box(600, 700, true, false)); laserShooters.Add(new LaserShooter(100, 500, Math.PI / 2, images.laserSprites)); //laserShooters.Add(new LaserShooter(100, 800, 0, images.laserSprites)); player.sprite = images.playerSprite; player.blueBullet.sprite = images.blueBullet; player.orangeBullet.sprite = images.orangeBullet; background = images.background; screenSplitter = images.splitScreen; bluePortal.sprites = images.bluePortalSprites; orangePortal.sprites = images.orangePortalSprites; setBoxSprites(images.wall, images.portalWall, images.emptyBox); setAllLaserSprites(images.laserShooterSprites, images.laserCatcherSprites); }
public void Update(List<Box> boxList, Portal bluePortal, Portal orangePortal) { bool collided = false; for (int i = 0; i < boxList.Count; i++) { if (!boxList[i].empty) if (Collision.TestCoordinate(roomX + xReach, roomY + yReach, boxList[i])) { collided = true; } } if (!collided) { xReach += sprite.Width * (int)Math.Sin(direction); yReach -= sprite.Height * (int)Math.Cos(direction); } Portal tempPortal = null; if (bluePortal.exists && Collision.TestCoordinate(roomX + xReach, roomY + yReach, bluePortal)) { tempPortal = orangePortal; } else if (orangePortal.exists && Collision.TestCoordinate(roomX + xReach, roomY + yReach, orangePortal)) { tempPortal = bluePortal; } else { ported = false; xReachPorted = 0; yReachPorted = 0; } if (tempPortal != null) { ported = true; portedDirection = tempPortal.portalDirection; if (portedDirection == 0 || portedDirection == Math.PI) { portedSprite = textures[0]; if (portedDirection == 0) { if (yReachPorted <= 0 || !Collision.TestCoordinate(portedRoomX, portedRoomY, tempPortal)) { yReachPorted = portedSprite.Height * (int)Math.Cos(portedDirection); } portedRoomY = tempPortal.roomY + tempPortal.sprite.Bounds.Center.Y; } if (portedDirection == Math.PI) { if (yReachPorted > 0 || !Collision.TestCoordinate(portedRoomX, portedRoomY + portedSprite.Height, tempPortal)) { yReachPorted = portedSprite.Height * (int)Math.Cos(portedDirection); } portedRoomY = tempPortal.roomY + tempPortal.sprite.Bounds.Center.Y - portedSprite.Height; } portedRoomX = tempPortal.roomX + tempPortal.sprite.Bounds.Center.X - portedSprite.Bounds.Center.X; if (xReachPorted != 0) { xReachPorted = 0; } } if (portedDirection == Math.PI / 2 || portedDirection == -Math.PI / 2) { portedSprite = textures[1]; if (portedDirection == Math.PI / 2) { if (xReachPorted < 0 || !Collision.TestCoordinate(portedRoomX, portedRoomY, tempPortal)) { xReachPorted = 0; } portedRoomX = tempPortal.roomX + tempPortal.sprite.Bounds.Center.X + 1; } if (portedDirection == -Math.PI / 2) { if (xReachPorted > 0 || !Collision.TestCoordinate(portedRoomX + portedSprite.Width, portedRoomY, tempPortal)) { xReachPorted = 0; } portedRoomX = tempPortal.roomX + tempPortal.sprite.Bounds.Center.X - portedSprite.Width; } portedRoomY = tempPortal.roomY + tempPortal.sprite.Bounds.Center.Y - portedSprite.Bounds.Center.Y; if (yReachPorted != 0) { yReachPorted = 0; } } bool portCollided = false; for (int i = 0; i < boxList.Count; i++) { if (!boxList[i].empty) if (Collision.TestCoordinate(portedRoomX + xReachPorted, portedRoomY + yReachPorted, boxList[i])) { portCollided = true; } } if (!portCollided) { try { xReachPorted += portedSprite.Width * (int)Math.Sin(portedDirection); yReachPorted += portedSprite.Height * (int)Math.Cos(portedDirection); } catch (Exception NullReferenceException) { } } } }
/// <summary> /// Updates the logic for the element. This is specifically for the inherited member of Bullet. /// </summary> /// <param name="boxList">The list of boxes in the current room.</param> /// <param name="portal">The portal that the bullet will create if it collides with a box that /// can hold a portal.</param> /// <param name="otherPortal">The portal that the bullet cannot create.</param> public virtual void Update(List<Element> boxList, ref Portal portal, ref Portal otherPortal) { }
/// <summary> /// Updates the logic for the element. This is specifically for the inherited member of Player. /// </summary> /// <param name="newKeyboardState">The current KeyboardState.</param> /// <param name="oldKeyboardState">The previous KeyboardState.</param> /// <param name="newMouseState">The current MouseState.</param> /// <param name="oldMouseState">The previous MouseState.</param> /// <param name="boxList">The list of boxes in the current room.</param> /// <param name="bluePortal">The blue portal in the room.</param> /// <param name="orangePortal">The orange portal in the room.</param> public virtual void Update(KeyboardState newKeyboardState, KeyboardState oldKeyboardState, MouseState newMouseState, MouseState oldMouseState, List<Box> boxList, List<LaserShooter> laserList, ref Portal bluePortal, ref Portal orangePortal) { }
/// <summary> /// Finds the part of the box the bullet collided with. /// </summary> /// <param name="portalBox">The box the bullet collided with. This box must be able to hold /// portals.</param> /// <param name="portal">The portal that will be made.</param> /// <param name="bulletExists">The bool that says whether the bullet exists.</param> private void portalMade(Box portalBox, ref Portal portal, ref bool bulletExists) { double tempDouble = Collision.TestVertical(this, portalBox); if (tempDouble == 10) { tempDouble = Collision.TestHorizontal(this, portalBox); } portal.Created(portalBox, tempDouble, ref bulletExists); }
/// <summary> /// Updates the logic for the player. Determines collisions, movement, and portal logic. /// This is by far the largest update method in the game. As such it has been broken down and /// summarized as well. /// </summary> /// <param name="newKeyboardState">The current KeyboardState.</param> /// <param name="oldKeyboardState">The previous KeyboardState.</param> /// <param name="newMouseState">The current MouseState.</param> /// <param name="oldMouseState">The previous MouseState.</param> /// <param name="boxList">The list of boxes in the current room.</param> /// <param name="bluePortal">The blue portal in the room.</param> /// <param name="orangePortal">The orange portal in the room.</param> public override void Update(KeyboardState newKeyboardState, KeyboardState oldKeyboardState, MouseState newMouseState, MouseState oldMouseState, List<Box> boxList, List<LaserShooter> laserList, ref Portal bluePortal, ref Portal orangePortal) { // Summary: // These four bools determine the directions possible. bool up = true, down = true, left = true, right = true; // Summary: // The programming behind collision testing is to test for a collision // after each individual X and Y change. for (int count = 0; count < speed; count++) { for (int i = 0; i < boxList.Count; i++) { // Summary: // The collision testing for detecting where the collision // happened is split into vertical and horizontal tests. if (!boxList[i].empty) { // The direction is represented in radians. double tempDouble = Collision.TestVertical(this, boxList[i]); if (tempDouble == 0) { up = false; } if (tempDouble == Math.PI) { down = false; } tempDouble = Collision.TestHorizontal(this, boxList[i]); if (tempDouble == -Math.PI / 2) { left = false; } if (tempDouble == Math.PI / 2) { right = false; } } // Summary: // When a collision with a hole happens, the player dies. else { if (Collision.TestCompletelyInsideForAll(this, boxList)) { dead = true; } } } for (int i = 0; i < laserList.Count && laserList[i].laser != null; i++) { for (int j = 0; j < Math.Abs(laserList[i].laser.xReach) / 10; j++) { if (Collision.TestCoordinate(laserList[i].laser.roomX + 10 * j * Math.Abs(laserList[i].laser.xReach) / laserList[i].laser.xReach, laserList[i].laser.roomY, this)) { dead = true; } } for (int j = 0; j < Math.Abs(laserList[i].laser.yReach) / 10; j++) { if (Collision.TestCoordinate(laserList[i].roomX, laserList[i].roomY + 10 * j * Math.Abs(laserList[i].laser.yReach) / laserList[i].laser.yReach, this)) { dead = true; } } for (int j = 0; j < Math.Abs(laserList[i].laser.xReachPorted) / 10; j++) { if (Collision.TestCoordinate(laserList[i].laser.portedRoomX + 10 * j * Math.Abs(laserList[i].laser.xReachPorted) / laserList[i].laser.xReachPorted, laserList[i].laser.portedRoomY, this)) { dead = true; } } for (int j = 0; j < Math.Abs(laserList[i].laser.yReachPorted) / 10; j++) { if (Collision.TestCoordinate(laserList[i].laser.portedRoomX, laserList[i].laser.portedRoomY + 10 * j * Math.Abs(laserList[i].laser.yReachPorted) / laserList[i].laser.yReachPorted, this)) { dead = true; } } } // Temporary bools to detect collisions with portals. bool bluePortalporting = false; bool orangePortalporting = false; // Both portals must exist for there to be a collision. if (bluePortal.exists && orangePortal.exists) { bluePortal.Porting(this, ref roomX, ref roomY, ref bluePortalporting, ref up, ref down, ref left, ref right); } if (bluePortal.exists && orangePortal.exists) { orangePortal.Porting(this, ref roomX, ref roomY, ref orangePortalporting, ref up, ref down, ref left, ref right); } // The temporary bools are set up to tell which portal the player collided with. if (bluePortalporting || orangePortalporting) { ported = true; } else { ported = false; } // Summary: // The otherPortal is used to abstractly refer to the portal not currently being collided with. Portal otherPortal = new Portal(); if (bluePortalporting) { otherPortal = orangePortal; } else { otherPortal = bluePortal; } // Summary: // Gets the position of the player at the other portal. // Remember: ported is for collisions, porting is for moving through portals. if (ported && !porting) { if (otherPortal.portalDirection == 0) { portedRoomX = otherPortal.roomX + otherPortal.sprite.Bounds.Center.X - sprite.Bounds.Center.X; portedRoomY = otherPortal.roomY - sprite.Height + otherPortal.sprite.Height; } if (otherPortal.portalDirection == Math.PI) { portedRoomX = otherPortal.roomX + otherPortal.sprite.Bounds.Center.X - sprite.Bounds.Center.X; portedRoomY = otherPortal.roomY; } if (otherPortal.portalDirection == Math.PI / 2) { portedRoomX = otherPortal.roomX - sprite.Width + otherPortal.sprite.Width; portedRoomY = otherPortal.roomY + otherPortal.sprite.Bounds.Center.Y - sprite.Bounds.Center.Y; } if (otherPortal.portalDirection == -Math.PI / 2) { portedRoomX = otherPortal.roomX; portedRoomY = otherPortal.roomY + otherPortal.sprite.Bounds.Center.Y - sprite.Bounds.Center.Y; } } // When the spacebar is pressed, the player will be moving through portals. if (ported && !porting && newKeyboardState.IsKeyDown(Keys.Space) && !oldKeyboardState.IsKeyDown(Keys.Space)) { porting = true; } // If there is no collision with a portal, then the player is not moving through a portal. if (!ported) { porting = false; } // Summary: // Sets up the player's direction while moving through the portal, and // moves it through the portal far enough to no longer be colliding with // the other portal. if (porting) { if (otherPortal.portalDirection == 0) { portedRoomY += 1; direction = otherPortal.portalDirection + Math.PI; if (portedRoomY > otherPortal.roomY + otherPortal.sprite.Height) { roomX = portedRoomX; roomY = portedRoomY; } } if (otherPortal.portalDirection == Math.PI) { portedRoomY -= 1; direction = otherPortal.portalDirection + Math.PI; if (portedRoomY + sprite.Height < otherPortal.roomY) { roomX = portedRoomX; roomY = portedRoomY; } } if (otherPortal.portalDirection == Math.PI / 2) { portedRoomX += 1; direction = otherPortal.portalDirection; if (portedRoomX > otherPortal.roomX + otherPortal.sprite.Width + 1) { roomX = portedRoomX; roomY = portedRoomY; } } if (otherPortal.portalDirection == -Math.PI / 2) { portedRoomX -= 1; direction = otherPortal.portalDirection; if (portedRoomX + sprite.Width < otherPortal.roomX - 1) { roomX = portedRoomX; roomY = portedRoomY; } } } // If the player is not moving through a portal. else { // Summary: // The key being pressed and corresponding directional bool must both be true. if (newKeyboardState.IsKeyDown(Keys.W) && up) { roomY -= 1; } if (newKeyboardState.IsKeyDown(Keys.S) && down) { roomY += 1; } if (newKeyboardState.IsKeyDown(Keys.A) && left) { roomX -= 1; } if (newKeyboardState.IsKeyDown(Keys.D) && right) { roomX += 1; } // Prevents the sprite from moving when opposite directions are pressed. if (newKeyboardState.IsKeyDown(Keys.A) && newKeyboardState.IsKeyDown(Keys.D)) { if (left) { roomX += 1; } if (right) { roomX -= 1; } } if (newKeyboardState.IsKeyDown(Keys.W) && newKeyboardState.IsKeyDown(Keys.S)) { if (up) { roomY += 1; } if (down) { roomY -= 1; } } // Finds the slope of the line from the screen positions of the player and the mouse. direction = Math.Atan2(newMouseState.Y - screenY, newMouseState.X - screenX) + Math.PI / 2; } } // Summary: // Fires bullets if the rate of fire counter has caught up. // The two MouseStates prevent rapid fire from holding down the button. if (newMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton != ButtonState.Pressed && !blueBullet.exists && !ported && rateOfFire == -1) { blueBullet.Fired(roomX + sprite.Bounds.Center.X, roomY + sprite.Bounds.Center.Y, direction); rateOfFire = 0; } if (newMouseState.RightButton == ButtonState.Pressed && oldMouseState.RightButton != ButtonState.Pressed && !orangeBullet.exists && !ported && rateOfFire == -1) { orangeBullet.Fired(roomX + sprite.Bounds.Center.X, roomY + sprite.Bounds.Center.Y, direction); rateOfFire = 0; } // Adds to the rate of fire after a bullet has been fired. if (rateOfFire > -1) { rateOfFire += 1; if (rateOfFire == 20) { rateOfFire = -1; } } // Bullet logic can only happen if the bullets exist. if (blueBullet.exists) { blueBullet.Update(boxList, ref bluePortal, ref orangePortal); } if (orangeBullet.exists) { orangeBullet.Update(boxList, ref orangePortal, ref bluePortal); } }
/// <summary> /// Updates the logic for the bullet. Uses similar logic for collisions as the Player class. /// Checks for collisions with boxes and portals. Makes a portal if the bullet collides with /// a box that can hold a portal. /// </summary> /// <param name="boxList">The list of boxes in the current room.</param> /// <param name="portal">The portal that the bullet will create if it collides with a box that /// can hold a portal.</param> /// <param name="otherPortal">The portal that the bullet cannot create.</param> public void Update(List<Box> boxList, ref Portal portal, ref Portal otherPortal) { doubleX = roomX; doubleY = roomY; double XSpeed = 10 * Math.Cos(direction - Math.PI / 2); double YSpeed = 10 * Math.Sin(direction - Math.PI / 2); for (int count = 0; count < 10; count++) { for (int i = 0; i < boxList.Count && exists; i++) { if (otherPortal.exists && Collision.Test(this, otherPortal)) { exists = false; } if (Collision.Test(this, boxList[i]) && exists) { if (boxList[i].portalUse) { portalMade(boxList[i], ref portal, ref exists); } else if (!boxList[i].empty) { exists = false; } } } doubleX += XSpeed / 10.0; doubleY += YSpeed / 10.0; roomX = (int)doubleX; roomY = (int)doubleY; } }
public void Update(List<Box> boxList, Portal bluePortal, Portal orangePortal) { if (laser == null) { laser = new Laser(this, laserSprites); } }