/// <summary> /// Constructors a new player. /// </summary> public Bobo(GameScreen screen, Level level, Vector2 position) { this.level = level; LoadContent(screen); Reset(position); }
/// <summary> /// Constructs a new level. /// </summary> /// <param name="serviceProvider"> /// The service provider that will be used to construct a ContentManager. /// </param> /// <param name="fileStream"> /// A stream containing the tile data. /// </param> public Level(GameScreen screen, IServiceProvider serviceProvider, Stream fileStream) { // Create a new content manager to load content used just by this level. content = new ContentManager(serviceProvider, "Content"); mainGameScreen = screen; background = Content.Load<Texture2D>("background/sunnysky"); LoadTiles(fileStream); }
public void AddScreen(GameScreen screen) { screen.IsExiting = false; screen.ScreenManager = this; if (isInitialized) { screen.LoadContent(); } screens.Add(screen); }
public void Fire(GameScreen screen, Vector2 thePosition, Vector2 theSpeed, Vector2 theDirection, string fireballname) { Position = thePosition; mSpeed = theSpeed; mDirection = theDirection; LoadContent(screen, fireballname); }
/// <summary> /// Animates each enemy and allow them to kill the player. /// </summary> private void UpdateEnemies(GameScreen screen, GameTime gameTime, List<Fireball> fireballs) { foreach (Enemy enemy in enemies) { enemy.Update(screen, gameTime, fireballs, player); // Touching an enemy instantly hurts the player if (enemy.BoundingRectangle.Intersects(Player.BoundingRectangle) && enemy.IsAlive) { health -= 10; } if (health == 0) { Lose(enemy, screen); } } }
private bool Lose(Enemy enemy, GameScreen screen) { screen.isPaused = true; isKilled = true; OnPlayerKilled(enemy); screen.ScreenManager.AddScreen(new LossScreen()); return true; }
/// <summary> /// Handles input, performs physics, and animates the player sprite. /// </summary> /// <remarks> /// We pass in all of the input states so that our game is only polling the hardware /// once per frame. We also pass the game's orientation because when using the accelerometer, /// we need to reverse our motion when the orientation is in the LandscapeRight orientation. /// </remarks> public void Update(GameScreen screen, GameTime gameTime, KeyboardState keyboardState, List<Enemy> enemies) { keyboardState = Keyboard.GetState(); GetInput(keyboardState); ApplyPhysics(gameTime); if (IsAlive && IsOnGround) { if (Math.Abs(Velocity.X) - 0.02f > 0) { sprite.PlayAnimation(runAnimation); } else { sprite.PlayAnimation(idleAnimation); } } // Clear input. movement = 0.0f; isJumping = false; if (isOnGround) numberOfJumps = 0; wasStunned = false; //Firing component for Bobo UpdateFireball(screen, gameTime, keyboardState); DoStun(screen, enemies); previousKeyboardState = keyboardState; }
public Fireball(GameScreen screen ,Level level, string fireballname) { this.level = level; LoadContent(screen, fireballname); }
private void ShootFireball(GameScreen screen) { if (isFiring) { sprite.PlayAnimation(runAnimation); Fireball f = new Fireball(screen, Level, "enemyfire"); //f.loadContent(); if ((int)direction == 1) f.Fire(screen, new Vector2(Position.X + 10, Position.Y - 25), new Vector2(200, 200), new Vector2(1, 0), "enemyfire"); else f.Fire(screen, new Vector2(Position.X - 25, Position.Y - 25), new Vector2(200, 200), new Vector2(-1, 0), "enemyfire"); mFireballs.Add(f); } }
public void Update(GameScreen screen, GameTime gameTime, List<Fireball> fireballs, Bobo player) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; if (IsAlive) { // Calculate tile position based on the side we are walking towards. float posX = Position.X + localBounds.Width / 2 * (int)direction; int tileX = (int)Math.Floor(posX / Tile.width) - (int)direction; int tileY = (int)Math.Floor(Position.Y / Tile.height); if (waitTime > 0) { // Wait for some amount of time. waitTime = Math.Max(0.0f, waitTime - (float)gameTime.ElapsedGameTime.TotalSeconds); if (waitTime <= 0.0f) { // Then turn around. direction = (FaceDirection)(-(int)direction); } } else { // If we are about to run into a wall or off a cliff, start waiting. if (Level.GetCollision(tileX + (int)direction, tileY - 1) == TileCollision.Impassable || Level.GetCollision(tileX + (int)direction, tileY) == TileCollision.Passable) { waitTime = MaxWaitTime; } else { if (enemy == "enemy") { // Move in the current direction. Vector2 velocity = new Vector2((int)direction * MoveSpeed * elapsed, 0.0f); position = position + velocity; } else { waitTime = Math.Min(0.0f, waitTime - elapsed); if (-1.3f < waitTime && waitTime < -1.0f) { if (flip == 1) direction = (FaceDirection)(-(int)direction); flip = 0; } else { if (flip == 0) { flip = 1; direction = (FaceDirection)(-(int)direction); } // Move in the current direction. Vector2 velocity = new Vector2((int)direction * MoveSpeed * elapsed, 0.0f); position = position + velocity; } } } foreach (Fireball f in fireballs) if (this.BoundingRectangle.Intersects(f.BoundingRectangle) && !f.isDead) { OnEnemyKilled(f); level.AddScore(100); f.Die(); } } if(enemy == "enemy") UpdateFireball(screen, gameTime, player); } }
public void Draw(GameTime gameTime, SpriteBatch spriteBatch, GameScreen screen) { // Stop running when the game is paused or before turning around. if (screen.isPaused) { sprite.PlayAnimation(idleAnimation); } else { sprite.PlayAnimation(runAnimation); } // Draw facing the way the enemy is moving. if (IsAlive) { SpriteEffects flip = direction > 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None; sprite.Draw(gameTime, spriteBatch, Position, flip); foreach (Fireball f in mFireballs) if (!f.isDead) f.Draw(gameTime, spriteBatch); } else if (waitDieTime > 0) { // Wait for some amount of time. waitDieTime = Math.Max(0.0f, waitDieTime - (float)gameTime.ElapsedGameTime.TotalSeconds); SpriteEffects flip = direction > 0 ? SpriteEffects.FlipHorizontally : SpriteEffects.None; sprite.Draw(gameTime, spriteBatch, Position, flip); } }
private void UpdateFireball(GameScreen screen, GameTime gameTime, KeyboardState keyboardState) { foreach (Fireball f in mFireballs) f.Update(gameTime); if (keyboardState.IsKeyDown(Keys.Space) == true && previousKeyboardState.IsKeyDown(Keys.Space) == false) { isFiring = true; ShootFireball(screen); } }
private void ShootFireball(GameScreen screen) { if (isFiring && (numFireBalls > 0)) { sprite.PlayAnimation(fireAnimation); Fireball f = new Fireball(screen,Level, ""); //f.loadContent(); if (flip == SpriteEffects.FlipHorizontally) f.Fire(screen, new Vector2(Position.X + 10, Position.Y - 25), new Vector2(200, 200), new Vector2(1, 0), ""); else f.Fire(screen, new Vector2(Position.X - 25, Position.Y - 25), new Vector2(200, 200), new Vector2(-1, 0), ""); mFireballs.Add(f); numFireBalls--; } }
private void DoStun(GameScreen screen, List<Enemy> Enemies) { foreach (Enemy E in Enemies){ if (this.BoundingRectangle.Intersects(E.BoundingRectangle) && E.IsAlive) { sprite.PlayAnimation(dieAnimation); //this.velocity = new Vector2(-200, 8000); wasStunned = true; } } }
/// <summary> /// Allows the game component to perform any initialization it needs to before starting /// to run. This is where it can query for any required services and load content. /// </summary> public void loadContent(GameScreen screen) { this.LoadContent(); }
private void UpdateFireball(GameScreen screen, GameTime gameTime, Bobo player) { foreach (Fireball f in mFireballs) { f.Update(gameTime); if (player.BoundingRectangle.Intersects(f.BoundingRectangle) && !f.isDead) { f.Die(); level.Health -= 10; } } if (waitFire > 0) { waitFire = Math.Max(0.0f, waitFire - (float)gameTime.ElapsedGameTime.TotalSeconds); } else { if ((((int)direction == 1 && Position.X < player.Position.X) || (int)direction == -1 && Position.X > player.Position.X) && ((int)Math.Floor(Position.Y / Tile.height) == (int)Math.Floor(player.Position.Y / Tile.height))) { isFiring = true; ShootFireball(screen); } waitFire = 0.8f; } }
public void LoadContent(GameScreen screen, string fireballname) { // Calculate bounds within texture size. /*int width = (int)(sprite.Width * 0.35); int left = (sprite.Width - width) / 2; int height = (int)(sprite.Height * 0.7); int top = sprite.Height - height; localBounds = new Rectangle(0, 0, sprite.Width, sprite.Height);*/ //------------------------end if (fireballname == "enemyfire") { sprite = Level.Content.Load<Texture2D>("Sprites/Enemy/enemyfire"); } else { if (mDirection.X < 0) sprite = Level.Content.Load<Texture2D>("Sprites/Bobo/fireball"); else sprite = Level.Content.Load<Texture2D>("Sprites/Bobo/fireball2"); } localBounds = new Rectangle(0, 0, sprite.Width, sprite.Height); }
public void Draw(GameTime gameTime, SpriteBatch spriteBatch, GameScreen screen) { spriteBatch.Begin(); spriteBatch.Draw(background, Vector2.Zero, null, Color.White, 0.0f, Vector2.Zero, 1.5f, SpriteEffects.None, 0.0f); spriteBatch.End(); float start = (float)(Height * Tile.height / 1.0365); //1x = 2 Matrix cameraTransform = Matrix.CreateTranslation(0.0f, cameraPosition-start, 0f); if (!screen.isPaused) { if (isBeginning == true || gameTime.TotalGameTime.TotalSeconds < 11 || !player.ScrollON) { //cameraTransform = Matrix.CreateTranslation(0.0f, -start, 0.0f); isBeginning = false; } else { ScrollCamera(spriteBatch.GraphicsDevice.Viewport, gameTime); cameraTransform = Matrix.CreateTranslation(0.0f, cameraPosition - start, 0.0f); } } spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, cameraTransform); DrawTiles(spriteBatch); foreach (PowerUp pu in powerups) pu.Draw(gameTime, spriteBatch); Player.Draw(gameTime, spriteBatch); // Draw enemys foreach (Enemy enemy in enemies) { if (enemy.IsAlive == true) enemy.Draw(gameTime, spriteBatch, screen); } spriteBatch.End(); }
public void RemoveScreen(GameScreen screen) { if (isInitialized) { screen.UnloadContent(); } screens.Remove(screen); screensToUpdate.Remove(screen); }
public void Update(GameScreen screen, GameTime gameTime, KeyboardState keyboardState) { keyboardState = Keyboard.GetState(); mainGameScreen = screen; if (isKilled == false) { Player.Update(screen, gameTime, keyboardState, enemies); UpdateEnemies(screen, gameTime, player.fireballs); UpdatePowers(gameTime); } if (Player.Position.Y > Height * Tile.height - cameraPosition) { if (!lost) lost = Lose(null, screen); } if (ReachedExit) { // Animate the time being converted into points. int seconds = (int)Math.Round(gameTime.ElapsedGameTime.TotalSeconds * 100.0f) - 11; score += seconds * 2; } //// The player has reached the exit if they are standing on the ground and //// his bounding rectangle contains the center of the exit tile. They can only //// exit when they have collected all of the gems. //if (Player.IsAlive && // Player.IsOnGround && // Player.BoundingRectangle.Contains(exit)) //{ // OnExitReached(screen); //} }
public void QuitToScreen(GameScreen screen) { int length = screens.Count; for (int i = length - 1; i >= 0; --i) { if (screens[i].bgSong != null) MediaPlayer.Stop(); screens.Remove(screens[i]); } length = screensToUpdate.Count; UnloadContent(); for (int i = length - 1; i >= 0; --i) { screensToUpdate.Remove(screensToUpdate[i]); } AddScreen(screen); }
/// <summary> /// Loads the player sprite sheet and sounds. /// </summary> public void LoadContent(GameScreen screen) { // Load animated textures. //spriteBatch = new SpriteBatch(screen.ScreenManager.GraphicsDevice); idleAnimation = new Animation(screen.ScreenManager.Game.Content.Load<Texture2D>("Sprites/Bobo/idle"), 0.1f, true); runAnimation = new Animation(screen.ScreenManager.Game.Content.Load<Texture2D>("Sprites/Bobo/running"), 0.1f, true); jumpAnimation = new Animation(screen.ScreenManager.Game.Content.Load<Texture2D>("Sprites/Bobo/jumping"), 0.1f, false); dieAnimation = new Animation(screen.ScreenManager.Game.Content.Load<Texture2D>("Sprites/Bobo/dead"), 0.1f, false); fireAnimation = new Animation(screen.ScreenManager.Game.Content.Load<Texture2D>("Sprites/Bobo/firing"), 0.1f, false); // Calculate bounds within texture size. int width = (int)(idleAnimation.FrameWidth * 1); int left = (idleAnimation.FrameWidth - width) / 2; int height = (int)(idleAnimation.FrameWidth * 1); int top = idleAnimation.FrameHeight - height; localBounds = new Rectangle(left, top, width, height); // Load Fireballs foreach (Fireball f in mFireballs) f.loadContent(screen); }