/// <summary> /// Updates the player's state in the world /// </summary> /// <param name="time">The current gametime</param> /// <param name="enemyList">The current list onf enemies</param> /// <returns>new position of the player</returns> public Vector2 updateState(GameTime time, Enemy[] enemyList, Boss boss) { Vector2 update; //get a list of pressed keys KeyboardState KBstate = Keyboard.GetState(); //if not respawning, handle collisions if (!respawn) { foreach (Enemy enemy in enemyList) { if (enemy != null && hitBox.Intersects(enemy.hitBox)) { hit = true; enemy.alive = false; } } if (boss != null && hitBox.Intersects(boss.hitBox)) { boss.health = boss.health - 50; hit = true; } } if (hit) { update = new Vector2(xPos, yPos); respawn = true; respawnTimer.Start(); lives--; } else if (respawn) { keyboardMovement(KBstate); } else { keyboardMovement(KBstate); keyboardShoot(KBstate); } //update each shot, and clear it if it is out of bounds foreach(Shot shot in shotList) { shot.update(); if (shot.isOutOfPlay() || shot.hit) { removeList.Add(shot); } } foreach (Shot shot in removeList) { shotList.Remove(shot); } //update hitbox hitBox.X = xPos + 5; hitBox.Y = yPos + 10; update = new Vector2(xPos, yPos); //handle sprite animation sprite.handleMovement(currentXSpeed); return update; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { LoadContent(); Constants constants = new Constants(); activeList = new Enemy[20]; shotList = new EnemyShot[100000]; // Create Level Reader LevelReader reader = new LevelReader(); // Load Background ArrayList tmpRemoveLst = new ArrayList(); foreach (Enemy enemy in reader.enemyList) { if (enemy is Grunt) { enemy.init(enemyText, enemyShot, constants); enemyList.Add(enemy); } else if (enemy is Boss) { boss = (Boss)enemy; boss.init(enemyText, enemyShot, constants); } } foreach (Boss boss in tmpRemoveLst) { enemyList.Remove(boss); } // load background song if (reader.levelSong != "none") { Uri uri = new Uri(reader.levelSong,UriKind.Relative); try { bsong = Song.FromUri(reader.levelSong, uri); //MediaPlayer.Play(bsong); } catch (System.ArgumentException) { Console.Error.WriteLine("Error loading file: " + (string)reader.levelSong + " ... Ignoring"); } } // Load background if (reader.background != "none") { try { Stream str = File.OpenRead(reader.background); backgroundTexture = Texture2D.FromStream(GraphicsDevice, str); } catch (System.IO.DirectoryNotFoundException) { Console.Error.WriteLine("Could not locate file: " + (string)reader.background + " Using default."); backgroundTexture = singlePix; } } else { backgroundTexture = singlePix; } //create player humanAnimatedTexture = new AnimatedSprite(humanTexture, constants.HUMAN_NEUTRAL_FRAME, constants.HUMAN_NEUTRAL_FRAME, constants.MAX_HUMAN_FRAMES, constants.HUMAN_SPRITE_WIDTH, constants.HUMAN_SPRITE_HEIGHT); human = new Player(humanAnimatedTexture, shotTexture, constants.HUMAN_START_X, constants.HUMAN_START_Y, constants.MAX_HUMAN_SPEED); base.Initialize(); }
/// <summary> /// Gets the next level /// </summary> /// <returns></returns> public void getNextLevel() { // get current level from the ArrayList of levels and increment the currentLevel string level = (string)levelFiles[currentLevel]; currentLevel++; // Create reader from current level StreamReader reader = new StreamReader(File.OpenRead(level)); // Read the first line, and store it as the background this.background = reader.ReadLine(); // Read the second line, and store it as the song this.levelSong = reader.ReadLine(); // The rest of the lines will be enemy placements/appear times string line = reader.ReadLine(); while (line != null) { string[] output = line.Split(';'); //First entry will be the enemy type string type = output[0]; //Second entry will be the time; int appearTime = Convert.ToInt32(output[1]); // Third will be the appear x int appx = Convert.ToInt32(output[2]); // Forth will the the tar x int xpos = Convert.ToInt32(output[3]); // Fifth will be the tar y int ypos = Convert.ToInt32(output[4]); // Sixth will be the moveSpeed int speed = Convert.ToInt32(output[5]); // switch to switch statement if (type == "Grunt") { //Create a blank grunt Grunt grunt = new Grunt(); grunt.xPos = appx; grunt.yPos = -10; grunt.tarx = xpos; grunt.tary = ypos; grunt.appearTime = appearTime; grunt.moveSpeed = speed; //TODO figure out weird null exception enemyList.Add(grunt); } else if (type == "Boss") { int health = Convert.ToInt32(output[6]); Boss boss = new Boss(appx,-10,1000,xpos,ypos); boss.appearTime = appearTime; enemyList.Add(boss); } /* * Additional types go here else if { } */ else { // Do nothing and print error Console.Error.WriteLine("Error unrecognized enemy type: <" + type + ">"); } line = reader.ReadLine(); } }