/// <summary> /// Load the current level from the text file /// </summary> /// <param name="reader">The level file stream (after width and height have been read in)</param> private void loadLevel(StreamReader reader) { int currentTile = 0; currentTile = reader.Read(); bool swap = true; int x = 0; int y = 0; player = new Player(Game, new Vector2(x * 60, y * 40), this); while (!reader.EndOfStream) { switch (currentTile) { case 'w': // waypoint tiles[x][y] = new Tile(new Vector2(x * 60.0f, y * 40), Tile.TileType.Waypoint, 0.0f); break; case 'p': // player tile. Spawn the player here tiles[x][y] = new Tile(new Vector2(x * 60, y * 40), Tile.TileType.Player, 0.0f); spawn = new Point(x, y); break; case 'P': // platform tiles[x][y] = new Tile(new Vector2(x * 60.0f, y * 40), Tile.TileType.Platform, 0.0f); break; case 'W': tiles[x][y] = new Tile(textures["sewerWall"], new Vector2(x * 60.0f, y * 40), Tile.TileType.SewerWall, 0.0f); break; case 'S': tiles[x][y] = new Tile(textures["sewerInterior"], new Vector2(x * 60.0f, y * 40), Tile.TileType.SewerInterior, 0.0f); break; case 'V': tiles[x][y] = new Tile(new Vector2(x * 60.0f, y * 40.0f), Tile.TileType.Waypoint, 0.0f); waypoints.Add("spinner", new Waypoint(Waypoint.WaypointType.Spinner, player, new Vector2(x * 60, y * 40), 0.0f)); vehicle = new Vehicle(Game, player, Vehicle.VehicleType.Spinner, new Vector2(x * 60, y * 40), this); vehicleSpawn = new Point(x, y); break; case '.': // air tiles[x][y] = new Tile(new Vector2(x * 60.0f, y * 40), Tile.TileType.Air, 0.0f); break; case 'x': // road // swap between the two different textures if (swap) tiles[x][y] = new Tile(textures["road0"], new Vector2(x * 60.0f, y * 40), Tile.TileType.Road, 0.0f); else tiles[x][y] = new Tile(textures["road1"], new Vector2(x * 60.0f, y * 40), Tile.TileType.Road, 0.0f); break; case 'X': tiles[x][y] = new Tile(new Vector2(x * 60.0f, y * 40), Tile.TileType.Waypoint, 0.0f); waypoints.Add("X", new Waypoint(Waypoint.WaypointType.EndRide, vehicle, new Vector2(x * 60, y * 40), 0.0f)); break; case 'c': tiles[x][y] = new Tile(textures["concrete"], new Vector2(x * 60.0f, y * 40), Tile.TileType.Concrete, 0.0f); break; case 's': tiles[x][y] = new Tile(new Vector2(x * 60.0f, y * 40), Tile.TileType.Waypoint, 0.0f); waypoints.Add("s", new Waypoint(Waypoint.WaypointType.SavePoint, this, new Vector2(x * 60, y * 40), 0.0f)); break; case 'e': tiles[x][y] = new Tile(new Vector2(x * 60.0f, y * 40), Tile.TileType.Waypoint, 0.0f); waypoints.Add("e", new Waypoint(Waypoint.WaypointType.EndLevel, this, new Vector2(x * 60, y * 40), 0.0f)); break; case 'E': tiles[x][y] = new Tile(new Vector2(x * 60, y * 40), Tile.TileType.Boss, 0.0f); boss = new Boss(Game, x * 60, y * 40, this); break; case 'B': tiles[x][y] = new Tile(textures["buildingInterior"], new Vector2(x * 60.0f, y * 40), Tile.TileType.BuildingInterior, 0.0f); break; case 'b': tiles[x][y] = new Tile(textures["buildingWall"], new Vector2(x * 60.0f, y * 40), Tile.TileType.BuildingWall, 0.0f); break; case '-': tiles[x][y] = new Tile(textures["buildingPlatform"], new Vector2(x * 60.0f, y * 40), Tile.TileType.Platform, 0.0f); break; case '|': tiles[x][y] = new Tile(textures["ladder"], new Vector2(x * 60.0f, y * 40), Tile.TileType.Ladder, 0.0f); break; case 'l': tiles[x][y] = new Tile(textures["powerline"], new Vector2(x * 60.0f, y * 40.0f), Tile.TileType.Powerline, 0.0f); break; case 'L': tiles[x][y] = new Tile(textures["buildingInterior"], new Vector2(x * 60.0f, y * 40.0f), Tile.TileType.Waypoint, 0.0f); vehicle = new Vehicle(Game, player, Vehicle.VehicleType.Lift, new Vector2(x * 60, (y + 1) * 40), this); waypoints.Add("lift", new Waypoint(Waypoint.WaypointType.Lift, player, new Vector2(x * 60, y * 40), 0.0f)); if (waypoints["X"] != null) waypoints["X"].handler = vehicle; vehicleSpawn = new Point(x, y); break; case '\n': // newline or EOF, reset the x value and increment the y value y++; x = -1; break; default: // carriage return, reset the x-value once more x = -1; break; } x++; swap = !swap; currentTile = reader.Read(); } player.resetRectangle(spawn); }
/// <summary> /// Tests if the player is colliding with the supplied block /// </summary> /// <param name="tile">The block to test for collisions</param> /// <param name="side">The side where the collision is meant to occur</param> /// <param name="gameTime">The current game time. Used for arresting motion, or for calculating gravitational effects when the player is not colliding with a block below them</param> public void testCollisions(Tile tile, int side, GameTime gameTime) { if (BoundingRectangle.Intersects(tile.BoundingRectangle)) { // collision detected, process it if (tile.collisionType == Tile.CollisionType.Solid) { if (tile.tileType == Tile.TileType.Waypoint) { for (int i = 0; i < currentLevel.waypoints.Count; i++) currentLevel.waypoints.ElementAt(i).Value.handleEvent(); } else if (tile.tileType == Tile.TileType.Ladder) { canClimb = true; } // else if (tile.tileType == Tile.TileType.Bullet) // damage the player else { if (canClimb) canClimb = !canClimb; // block movement through the block // colliding with a block to your right if (side == 0) { BoundingRectangle.Min.X = tile.BoundingRectangle.Min.X - 60; BoundingRectangle.Max.X = tile.BoundingRectangle.Min.X; texRect.Location = new Point((int)BoundingRectangle.Min.X, (int)BoundingRectangle.Min.Y); } // colliding with a block to your left else if (side == 1) { BoundingRectangle.Min.X = tile.BoundingRectangle.Max.X; BoundingRectangle.Max.X = BoundingRectangle.Min.X + 60; texRect.Location = new Point((int)BoundingRectangle.Min.X, (int)BoundingRectangle.Min.Y); } // hitting a solid block from beneath, so block movement else if (side == 2) { BoundingRectangle.Min.Y = tile.BoundingRectangle.Max.Y; BoundingRectangle.Max.Y = BoundingRectangle.Min.Y + 80; texRect.Location = new Point((int)BoundingRectangle.Min.X, (int)BoundingRectangle.Min.Y); if (Physics.Velocity > 0.0f) Physics.Velocity = 0.0f; } else if (side == 3 && !ridingVehicle) { BoundingRectangle.Min.Y = tile.BoundingRectangle.Min.Y - 80; BoundingRectangle.Max.Y = tile.BoundingRectangle.Min.Y; texRect.Location = new Point((int)BoundingRectangle.Min.X, (int)BoundingRectangle.Min.Y); Physics.Velocity = jumpSpeed; } } } // otherwise, the block is passable, so do not test any collisions for it else if (tile.collisionType == Tile.CollisionType.Platform) { if (side == 3 && !ridingVehicle) { BoundingRectangle.Min.Y = tile.BoundingRectangle.Min.Y - 80; BoundingRectangle.Max.Y = tile.BoundingRectangle.Min.Y; texRect.Location = new Point((int)BoundingRectangle.Min.X, (int)BoundingRectangle.Min.Y); Physics.Velocity = jumpSpeed; } } else { climbing = false; canClimb = false; } } }
protected override void LoadContent() { textures.Clear(); waypoints.Clear(); // using the level x, load the relevant textures into the texture list switch (levelx) { case 0: // highway textures.Add("road0", Game.Content.Load<Texture2D>("Tiles/road-0")); textures.Add("road1", Game.Content.Load<Texture2D>("Tiles/road-1")); textures.Add("concrete", Game.Content.Load<Texture2D>("Tiles/concrete")); break; case 1: // sewer textures.Add("sewerWall", Game.Content.Load<Texture2D>("Tiles/sewerWall")); textures.Add("sewerInterior", Game.Content.Load<Texture2D>("Tiles/sewerInterior")); //textures.Add("water", Game.Content.Load<Texture2D>("Tiles/water")); //textures.Add("cannon", Game.Content.Load<Texture2D>("Tiles/cannon")); // the cannon is a waypoint block with a texture textures.Add("concrete",Game.Content.Load<Texture2D>("Tiles/concrete")); break; case 2: // tower textures.Add("sewerWall", Game.Content.Load<Texture2D>("Tiles/sewerWall")); textures.Add("sewerInterior", Game.Content.Load<Texture2D>("Tiles/sewerInterior")); textures.Add("buildingWall", Game.Content.Load<Texture2D>("Tiles/buildingWall")); textures.Add("buildingInterior", Game.Content.Load<Texture2D>("Tiles/buildingInterior")); textures.Add("concrete", Game.Content.Load<Texture2D>("Tiles/concrete")); textures.Add("buildingPlatform", Game.Content.Load<Texture2D>("Tiles/buildingPlatform")); textures.Add("ladder", Game.Content.Load<Texture2D>("Tiles/ladder")); //textures.Add("cannon", Game.Content.Load<Texture2D>("Tiles/cannon")); break; case 3: // rooftops textures.Add("buildingWall", Game.Content.Load<Texture2D>("Tiles/buildingWall")); textures.Add("buildingInterior", Game.Content.Load<Texture2D>("Tiles/buildingInterior")); textures.Add("buildingPlatform", Game.Content.Load<Texture2D>("Tiles/buildingPlatform")); textures.Add("ladder", Game.Content.Load<Texture2D>("Tiles/ladder")); //textures.Add("cannon", Game.Content.Load<Texture2D>("Tiles/cannon")); break; case 4: // rooftops with spinner textures.Add("buildingWall", Game.Content.Load<Texture2D>("Tiles/buildingWall")); textures.Add("buildingInterior", Game.Content.Load<Texture2D>("Tiles/buildingInterior")); textures.Add("powerline", Game.Content.Load<Texture2D>("Tiles/powerline")); break; case 5: // case 5 // boss level textures.Add("powerline",Game.Content.Load<Texture2D>("Tiles/powerline")); textures.Add("buildingWall", Game.Content.Load<Texture2D>("Tiles/buildingWall")); textures.Add("buildingInterior", Game.Content.Load<Texture2D>("Tiles/buildingInterior")); textures.Add("buildingPlatform", Game.Content.Load<Texture2D>("Tiles/buildingPlatform")); break; default: // end the game Game.Exit(); break; } if (levelx < 6) { backingTrack = Game.Content.Load<Song>("Music/" + levelx); MediaPlayer.IsRepeating = true; MediaPlayer.Play(backingTrack); StreamReader reader = new StreamReader("Content/Levels/" + levelx + ".txt"); width = int.Parse(reader.ReadLine()); height = int.Parse(reader.ReadLine()); tiles = new Tile[width][]; for (int i = 0; i < width; i++) tiles[i] = new Tile[height]; loadLevel(reader); base.LoadContent(); } }
public void testCollisions(Tile tile, int side, GameTime gameTime) { if (player.ridingVehicle) { if (BoundingRectangle.Intersects(tile.BoundingRectangle)) { // collision detected, process it if (tile.tileType == Tile.TileType.Waypoint) { for(int i = 0; i < level.waypoints.Values.Count; i++) { level.waypoints.ElementAt(i).Value.handleEvent(); } } if (tile.collisionType == Tile.CollisionType.Solid) { // if (tile.tileType == Tile.TileType.Bullet) // damage the player // block movement through the block // colliding with a block to your right if (side == 0) { BoundingRectangle.Min.X = tile.BoundingRectangle.Min.X - 60; BoundingRectangle.Max.X = tile.BoundingRectangle.Min.X; player.BoundingRectangle.Min.X = BoundingRectangle.Min.X; player.BoundingRectangle.Max.X = BoundingRectangle.Max.X; texRect.Location = new Point((int)BoundingRectangle.Min.X, (int)BoundingRectangle.Min.Y); player.texRect.Location = new Point((int)player.BoundingRectangle.Min.X, (int)player.BoundingRectangle.Min.Y); //BottomBoundingRectangle.Location = new Point(tile.BoundingRectangle.Left - 60, BottomBoundingRectangle.Location.Y); } // colliding with a block to your left if (side == 1) { BoundingRectangle.Min.X = tile.BoundingRectangle.Max.X; BoundingRectangle.Max.X = BoundingRectangle.Min.X + 60; player.BoundingRectangle.Max.X = BoundingRectangle.Max.X; player.BoundingRectangle.Min.X = BoundingRectangle.Min.X; texRect.Location = new Point((int)BoundingRectangle.Min.X, (int)BoundingRectangle.Min.Y); player.texRect.Location = new Point((int)player.BoundingRectangle.Min.X, (int)player.BoundingRectangle.Min.Y); } if (side == 3) { BoundingRectangle.Min.Y = tile.BoundingRectangle.Min.Y - 40; BoundingRectangle.Max.Y = tile.BoundingRectangle.Min.Y; player.BoundingRectangle.Min.Y = BoundingRectangle.Min.Y - 80; player.BoundingRectangle.Max.Y = BoundingRectangle.Min.Y; texRect.Location = new Point((int)BoundingRectangle.Min.X, (int)BoundingRectangle.Min.Y); player.texRect.Location = new Point((int)player.BoundingRectangle.Min.X, (int)player.BoundingRectangle.Min.Y); physics.Velocity = 200.0f; } } else if (tile.collisionType == Tile.CollisionType.Platform) { if (side == 3) { BoundingRectangle.Min.Y = tile.BoundingRectangle.Min.Y - 40; BoundingRectangle.Max.Y = tile.BoundingRectangle.Min.Y; player.BoundingRectangle.Min.Y = BoundingRectangle.Min.Y - 80; player.BoundingRectangle.Max.Y = BoundingRectangle.Min.Y; texRect.Location = new Point((int)BoundingRectangle.Min.X, (int)BoundingRectangle.Min.Y); player.texRect.Location = new Point((int)player.BoundingRectangle.Min.X, (int)player.BoundingRectangle.Min.Y); physics.Velocity = 200.0f; } } } } }