Exemplo n.º 1
0
 /// <summary>
 /// Calls the Draw function of each of the GameObjects in the Level
 /// </summary>
 /// <param name="spriteBatch">Used to Draw all the sprites</param>
 public void DrawLevel(SpriteBatch spriteBatch)
 {
     foreach (GameObject gameObject in GameObjects)
     {
         if (gameObject is Tangible) //Makes sure only objects that are (partly) on-screen are drawed to improve framerate
         {
             Tangible tangible = (Tangible)gameObject;
             if (tangible.Hitbox.Intersects(Global.Instance.MainGame.camera.GetBounds()))
             {
                 gameObject.Draw(spriteBatch);
             }
         }
         else
         {
             gameObject.Draw(spriteBatch);
             //Resets the FallingSpike if it is out of view
             FallingSpike fallingSpike = (FallingSpike)gameObject;
             if (gameObject is FallingSpike)
             {
                 Rectangle rect = new Rectangle((int)fallingSpike.StartPosition.X, (int)fallingSpike.StartPosition.Y, 32, 32);
                 if (!rect.Intersects(Global.Instance.MainGame.camera.GetBounds()))
                 {
                     fallingSpike.ResetSpike();
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        public bool IsColliding(Level lvl, int offsetX, int offsetY, out Tangible collObject) //TODO: Improve collision (collider class?)
        {
            bool collidesWithSolid = false;

            collObject = null;
            foreach (GameObject gameObject in lvl.GameObjects)
            {
                if (gameObject is Tangible)
                {
                    Tangible  tangibleObject = (Tangible)gameObject;
                    Rectangle testRect       = new Rectangle((int)tangibleObject.Position.X - offsetX, (int)tangibleObject.Position.Y - offsetY, tangibleObject.Hitbox.Width, tangibleObject.Hitbox.Height);

                    if (testRect.Intersects(Hitbox) && tangibleObject != this)
                    {
                        if (tangibleObject.IsSolid)
                        {
                            collidesWithSolid = true;
                            collObject        = tangibleObject;
                        }
                        if (this is Mario)
                        {
                            Mario mario = (Mario)this;
                            if (tangibleObject is Coin)
                            {
                                Coin coin = (Coin)tangibleObject;
                                coin.AddCoin((Mario)this);
                            }
                            else if (tangibleObject is MysteryBlock)
                            {
                                MysteryBlock mysteryBlock = (MysteryBlock)tangibleObject;
                                mysteryBlock.Eject(mario);
                            }
                            else if (tangibleObject is CoinBlock)
                            {
                                CoinBlock coinBlock = (CoinBlock)tangibleObject;
                                coinBlock.Eject(mario);
                            }
                            else if (tangibleObject is Mushroom)
                            {
                                Mushroom mushroom = (Mushroom)tangibleObject;
                                mushroom.CollectMushroom(mario);
                            }
                            else if (tangibleObject is OneUpMushroom)
                            {
                                OneUpMushroom oneUpMushroom = (OneUpMushroom)tangibleObject;
                                oneUpMushroom.CollectMushroom(mario);
                            }
                            else if (tangibleObject is Shell)
                            {
                                Shell shell = (Shell)tangibleObject;
                                shell.CheckHit(mario);
                            }
                            else if (tangibleObject is Koopa)
                            {
                                Koopa koopa = (Koopa)tangibleObject;
                                koopa.CheckDeath(mario);
                            }
                            else if (tangibleObject is Muncher)
                            {
                                mario.GetHit();
                            }
                            else if (tangibleObject is Goomba)
                            {
                                Goomba goomba = (Goomba)tangibleObject;
                                goomba.CheckDeath(mario);
                            }
                        }
                        else if (this is Shell)
                        {
                            if (tangibleObject is Goomba)
                            {
                                Goomba goomba = (Goomba)tangibleObject;
                                goomba.Die();
                            }
                            if (tangibleObject is Koopa)
                            {
                                Koopa koopa = (Koopa)tangibleObject;
                                koopa.DieWithoutShell();
                                collidesWithSolid = false;
                            }
                        }
                    }
                }
            }
            return(collidesWithSolid);
        }