コード例 #1
0
 /// <summary>
 /// Ejects the content of the MysterBlock
 /// </summary>
 /// <param name="mario">Used to check if the mysteryBlock is being hit from below</param>
 public void Eject(Mario mario)
 {
     if (mario.VelocityY < 0 && !HasBeenUsed && mario.Hitbox.Y >= Hitbox.Bottom)
     {
         if (MysteryObject == typeof(Coin))
         {
             Coin c = (Coin)Activator.CreateInstance(MysteryObject, (int)Position.X / Global.Instance.GridSize, ((int)Position.Y - Hitbox.Height) / Global.Instance.GridSize, CurrentLevel, _contentManager, true);
             c.AddCoin(mario);
             CurrentLevel.ToAddGameObject(c);
         }
         if (MysteryObject == typeof(Mushroom))
         {
             Mushroom m = (Mushroom)Activator.CreateInstance(MysteryObject, (int)Position.X / Global.Instance.GridSize, ((int)Position.Y - Hitbox.Height) / Global.Instance.GridSize, CurrentLevel, _contentManager);
             CurrentLevel.ToAddGameObject(m);
         }
         if (MysteryObject == typeof(LevelReader))
         {
             if (_levelNumber == 0)
             {
                 if (_courseClearSound != null)
                 {
                     MediaPlayer.Pause();
                     _courseClearSound.Play();
                     new Timer(Global.Instance.MainGame.sound.ResumeMusic).Change(8000, Timeout.Infinite);
                 }
             }
             LevelReader _lr = (LevelReader)Activator.CreateInstance(MysteryObject, _contentManager);
             Global.Instance.MainGame.ChangeCurrentLevel(_lr.ReadLevel(_levelNumber));
         }
         HasBeenUsed = true;
         _animator.GetTextures(64, 0, 16, 16, 1, 1);
         _animator.SetAnimationSpeed(0);
     }
 }
コード例 #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);
        }