protected override void Update(GameTime gameTime) { gameState.Update(); List <IGameObject> allGameObjects = currentRoom.Blocks.Concat <IGameObject>(currentRoom.Items).Concat(currentRoom.Enemies).Concat(currentRoom.NPCs).Concat(currentRoom.Doors).Concat(player.ActiveItems).Concat(new List <IGameObject>() { player, player.Sword }).ToList(); CollisionDetection.DetectCollisions(allGameObjects); base.Update(gameTime); }
protected override void Draw(GameTime gameTime) { screen.Draw(spriteBatch); gameState.Draw(spriteBatch); // For testing, set showCollisions to true to show an outline around all colliders: if (showCollisions) { spriteBatch.Begin(); foreach (IGameObject g in currentRoom.Blocks.Concat <IGameObject>(currentRoom.Items).Concat(currentRoom.Enemies).Concat(currentRoom.NPCs).Concat(player.ActiveItems).Concat(new List <IGameObject>() { player, player.Sword })) { Rectangle rec = CollisionDetection.GetColliderRectangle(g, currentRoom.Position); spriteBatch.Draw(squareOutline, rec, new Color(Color.LimeGreen, 1)); } /*foreach (IGameObject g in currentRoom.Walls) * { * Rectangle rec = CollisionDetection.GetColliderRectangle(g, currentRoom.Position); * spriteBatch.Draw(squareOutline, rec, new Color(Color.Coral, .5f)); * }*/ foreach (IGameObject g in currentRoom.Doors) { Rectangle rec = CollisionDetection.GetColliderRectangle(g, currentRoom.Position); spriteBatch.Draw(squareOutline, rec, Color.Blue); } spriteBatch.End(); } if (reversingTime) { spriteBatch.Begin(); spriteBatch.Draw(rect, new Rectangle(new Point(0, 0), new Point(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight)), new Color(.1f, .2f, .1f, .1f)); spriteBatch.End(); } if (notificationsQueue.Count() > 0) { spriteBatch.Begin(); spriteBatch.DrawString(font, notificationsQueue.Peek().notificationText, new Vector2(700, 8), Color.White); spriteBatch.End(); } base.Draw(gameTime); }
public static void HandleCollision(Game1 game, Koopa koopa, IGameObject target) { Rectangle koopaRectangle = koopa.GetSizeRectangle(); koopa.isOnGround = false; if (target is Block block) { Rectangle blockRectangle = block.GetSizeRectangle(); CollisionDirection direction = CollisionDetection.DetectCollisionDirection(koopaRectangle, blockRectangle); if (koopa.state is FlipedKoopaState) { return; } if (!(direction is CollisionDirection.NoCollision) && !(block.state is HiddenBlockState)) { if (direction is CollisionDirection.Left) { koopa.position = new Vector2(block.GetSizeRectangle().X - koopa.GetSizeRectangle().Width - 5, koopa.position.Y); koopa.TurnLeft(); koopa.moveLeft = true; } else if (direction is CollisionDirection.Right) { koopa.position = new Vector2(block.GetSizeRectangle().X + block.GetSizeRectangle().Width, koopa.position.Y); koopa.TurnRight(); koopa.moveLeft = false; } else if (direction is CollisionDirection.Top) { koopa.position = new Vector2(koopa.position.X, block.GetSizeRectangle().Y - koopa.GetSizeRectangle().Height); koopa.isOnGround = true; } } direction = CollisionDetection.DetectCollisionDirection(blockRectangle, koopaRectangle); if (!(direction is CollisionDirection.NoCollision)) { if ((direction is CollisionDirection.Bottom) && ((block.state is BumpedBrickBlockState) || (block.state is BumpedUsedBlockState) || (block.state is ExplodingBlockState))) { game.HUD.GetScore(ConstantNumber.SCORE_100); koopa.Flip(); } } } }
public static void HandleCollision(Koopa koopa, IGameObject target) { Rectangle koopaRectangle = koopa.GetSizeRectangle(); if (koopa.state is FlipedKoopaState) { return; } if (target is IEnemy enemy) { Rectangle enemyRectangle = enemy.GetSizeRectangle(); CollisionDirection direction = CollisionDetection.DetectCollisionDirection(koopaRectangle, enemyRectangle); if (!(direction is CollisionDirection.NoCollision)) { if (direction is CollisionDirection.Left) { koopa.position = new Vector2(enemy.GetSizeRectangle().X - koopa.GetSizeRectangle().Width, koopa.position.Y); if (koopa.state is StompedKoopaState) { enemy.Disappear(); } else { koopa.TurnLeft(); koopa.moveLeft = true; } } else if (direction is CollisionDirection.Right) { koopa.position = new Vector2(enemy.GetSizeRectangle().X + enemy.GetSizeRectangle().Width, koopa.position.Y); if (koopa.state is StompedKoopaState) { enemy.Disappear(); } else { koopa.TurnRight(); koopa.moveLeft = false; } } } } }
public static void HandleCollision(Mario mario, Pipe collidingPipe, ref Collection <IGameObject> collidingGameObjects) { if (mario.state.isDead()) { return; } Rectangle marioRectangle = mario.GetSizeRectangle(); Rectangle marioLargeRectangle = mario.GetLargeSizeRectangle(); if (collidingPipe is IPipe pipe) { Rectangle pipeRectangle = pipe.GetSizeRectangle(); CollisionDirection direction = CollisionDetection.DetectCollisionDirection(marioRectangle, pipeRectangle); CollisionDirection directionLargeRectangle = CollisionDetection.DetectCollisionDirection(marioLargeRectangle, pipeRectangle); if (directionLargeRectangle != CollisionDirection.NoCollision) { collidingGameObjects.Add(collidingPipe); } if (!(direction is CollisionDirection.NoCollision)) { if (direction is CollisionDirection.Top) { mario.Position = new Vector2(mario.Position.X, pipe.GetSizeRectangle().Y - mario.GetSizeRectangle().Height); collidingGameObjects.Add(pipe); mario.state.ToIdle(); } else if (direction is CollisionDirection.Left) { mario.Position = new Vector2(pipe.GetSizeRectangle().X - mario.GetSizeRectangle().Width, mario.Position.Y); } else if (direction is CollisionDirection.Right) { mario.Position = new Vector2(pipe.GetSizeRectangle().X + pipe.GetSizeRectangle().Width, mario.Position.Y); } else if (direction is CollisionDirection.Bottom) { mario.Position = new Vector2(mario.Position.X, pipe.GetSizeRectangle().Y + pipe.GetSizeRectangle().Height); } } } }
public static void HandleCollision(StarItem star, IGameObject target) { Rectangle starRectangle = star.GetSizeRectangle(); if (target is Block block) { Rectangle blockRectangle = block.GetSizeRectangle(); CollisionDirection direction = CollisionDetection.DetectCollisionDirection(starRectangle, blockRectangle); if (!(direction is CollisionDirection.NoCollision)) { if (star.triggered) { if (direction is CollisionDirection.Left) { star.position = new Vector2(block.GetSizeRectangle().X - star.GetSizeRectangle().Width, star.position.Y); star.moveLeft = true; } else if (direction is CollisionDirection.Right) { star.position = new Vector2(block.GetSizeRectangle().X + block.GetSizeRectangle().Width, star.position.Y); star.moveLeft = false; } else if (direction is CollisionDirection.Top) { star.position = new Vector2(star.position.X, block.GetSizeRectangle().Y - star.GetSizeRectangle().Height); star.isOnGround = true; star.initialY = star.position.Y; star.Rise(); } } } else { star.isOnGround = false; } } }
protected override void Draw(GameTime gameTime) { screen.Draw(spriteBatch); gameState.Draw(spriteBatch); // For testing, set showCollisions to true to show an outline around all colliders: if (showCollisions) { spriteBatch.Begin(); foreach (IGameObject g in currentRoom.Blocks.Concat <IGameObject>(currentRoom.Items).Concat(currentRoom.Enemies).Concat(currentRoom.NPCs).Concat(currentRoom.Doors).Concat(player.ActiveItems).Concat(new List <IGameObject>() { player, player.Sword })) { Rectangle rec = CollisionDetection.GetColliderRectangle(g); spriteBatch.Draw(squareOutline, rec, new Color(Color.LimeGreen, 1)); } spriteBatch.End(); } base.Draw(gameTime); }
public static void HandleCollision(StarItem star, IGameObject target) { Rectangle starRectangle = star.GetSizeRectangle(); if (target is IPipe pipe) { Rectangle pipeRectangle = pipe.GetSizeRectangle(); CollisionDirection direction = CollisionDetection.DetectCollisionDirection(starRectangle, pipeRectangle); if (!(direction is CollisionDirection.NoCollision)) { if (direction is CollisionDirection.Left) { star.position = new Vector2(pipe.GetSizeRectangle().X - star.GetSizeRectangle().Width, star.position.Y); star.moveLeft = true; } else if (direction is CollisionDirection.Right) { star.position = new Vector2(pipe.GetSizeRectangle().X + pipe.GetSizeRectangle().Width, star.position.Y); star.moveLeft = false; } } } }
public static void HandleCollision(PoisonMushroomItem poisonMushroom, IGameObject target) { Rectangle poisonMushroomRectangle = poisonMushroom.GetSizeRectangle(); //Problem: check if goomba is above blocks, which then determines whether apply Fall method poisonMushroom.isOnGround = false; if (target is Block block) { Rectangle blockRectangle = block.GetSizeRectangle(); CollisionDirection direction = CollisionDetection.DetectCollisionDirection(poisonMushroomRectangle, blockRectangle); if (!(direction is CollisionDirection.NoCollision)) { if (poisonMushroom.triggered) { if (direction is CollisionDirection.Left) { poisonMushroom.position = new Vector2(block.GetSizeRectangle().X - poisonMushroom.GetSizeRectangle().Width, poisonMushroom.position.Y); poisonMushroom.moveLeft = true; } else if (direction is CollisionDirection.Right) { poisonMushroom.position = new Vector2(block.GetSizeRectangle().X + block.GetSizeRectangle().Width, poisonMushroom.position.Y); poisonMushroom.moveLeft = false; } else if (direction is CollisionDirection.Top) { poisonMushroom.position = new Vector2(poisonMushroom.position.X, block.GetSizeRectangle().Y - poisonMushroom.GetSizeRectangle().Height); poisonMushroom.isOnGround = true; } } } } }
public static void HandleCollision(MushroomItem mushroom, IGameObject target) { Rectangle mushroomRectangle = mushroom.GetSizeRectangle(); if (target is IPipe pipe) { Rectangle pipeRectangle = pipe.GetSizeRectangle(); CollisionDirection direction = CollisionDetection.DetectCollisionDirection(mushroomRectangle, pipeRectangle); if (!(direction is CollisionDirection.NoCollision)) { if (direction is CollisionDirection.Left) { mushroom.position = new Vector2(pipe.GetSizeRectangle().X - mushroom.GetSizeRectangle().Width, mushroom.position.Y); mushroom.moveLeft = true; } else if (direction is CollisionDirection.Right) { mushroom.position = new Vector2(pipe.GetSizeRectangle().X + pipe.GetSizeRectangle().Width, mushroom.position.Y); mushroom.moveLeft = false; } } } }
public static void HandleCollision(Game1 game, Mario mario, IItem collidingItem, ref Collection <IGameObject> collidingGameObjects) { if (mario.state.isDead()) { return; } Rectangle marioRectangle = mario.GetSizeRectangle(); Rectangle marioLargeRectangle = mario.GetLargeSizeRectangle(); if (collidingItem is IItem item) { Rectangle itemRectangle = item.GetSizeRectangle(); CollisionDirection direction = CollisionDetection.DetectCollisionDirection(marioRectangle, itemRectangle); CollisionDirection directionLargeRectangle = CollisionDetection.DetectCollisionDirection(marioLargeRectangle, itemRectangle); if (!(direction is CollisionDirection.NoCollision) || !(directionLargeRectangle is CollisionDirection.NoCollision)) { if (item is FireFlowerItem) { FireFlowerItem temp = (FireFlowerItem)item; if (temp.triggered) { game.HUD.GetScore(ConstantNumber.SCORE_1000); mario.Evolve(); SoundFactory.Instance.playPowerUpSoundEffect(); item.Disappear(); } else { if (direction is CollisionDirection.Bottom) { item.GetTrigger(); SoundFactory.Instance.playVineSoundEffect(); } } } if (item is MushroomItem) { MushroomItem temp = (MushroomItem)item; if (temp.triggered) { game.HUD.GetScore(ConstantNumber.SCORE_1000); mario.Evolve(); SoundFactory.Instance.playPowerUpSoundEffect(); item.Disappear(); } else { if (direction is CollisionDirection.Bottom) { item.GetTrigger(); SoundFactory.Instance.playVineSoundEffect(); } } } if (item is PoisonMushroomItem) { PoisonMushroomItem temp = (PoisonMushroomItem)item; if (temp.triggered) { mario.TakeDamage(); SoundFactory.Instance.playTakeDamageSoundEffect(); item.Disappear(); } else { item.GetTrigger(); SoundFactory.Instance.playVineSoundEffect(); } } if (item is StarItem) { StarItem temp = (StarItem)item; if (temp.triggered) { game.HUD.GetScore(ConstantNumber.SCORE_1000); mario.ToStar(); SoundFactory.Instance.playStarMaioSong(); item.Disappear(); } else { if (direction is CollisionDirection.Bottom) { item.GetTrigger(); SoundFactory.Instance.playVineSoundEffect(); } } } if (item is CoinItem) { CoinItem temp = (CoinItem)item; if (!temp.consumed) { if (temp.inBlock) { if (direction is CollisionDirection.Bottom) { item.GetTrigger(); SoundFactory.Instance.playCoinSoundEffect(); game.HUD.GetCoin(); } } else { item.GetTrigger(); SoundFactory.Instance.playCoinSoundEffect(); game.HUD.GetCoin(); } } } if (item is FlagPole flag) { flag.GetTrigger(); Game1.Instance.DisableUserControl(); Game1.Instance.LoadBasicCommand(); mario.Position = new Vector2(flag.GetSizeRectangle().X - mario.GetSizeRectangle().Width, mario.Position.Y); mario.ToCreepDown(); SoundFactory.Instance.playFlagpoleSoundEffect(); if (flag.flagFallTime < 0 || collidingGameObjects.Count > 0) { mario.Position = new Vector2(flag.GetSizeRectangle().X - ConstantNumber.FlAG_POSITION_X_ADJUST + mario.GetSizeRectangle().Width, mario.Position.Y); mario.ToLeft(); if (mario.standingTime < 0) { mario.ToRight(); mario.ReachDestination = true; } mario.standingTime--; } } } } }