Ejemplo n.º 1
0
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (mario.CollidesWith(pauline))
            {
                GameEnvironment.GameStateManager.SwitchTo("WinState");
                WinState.FinalScore = totalScore;
                GameEnvironment.GameStateManager.Reset();
            }

            if (mario.Acceleration.X < 0)
            {
                mario.Mirror = true;
            }
            else if (mario.Acceleration.X > 0)
            {
                mario.Mirror = false;
            }

            foreach (HammerPowerup hammer in hammers.Children)
            {
                if (!hammer.Visible)
                {
                    this.Remove(hammer);
                }
                if (hammer.IsActive)
                {
                    hammer.Position = mario.Position + mario.Center + new Vector2(0, 20);
                    hammer.Mirror   = mario.Mirror;
                    if (hammer.Mirror)
                    {
                        hammer.Origin = new Vector2(hammer.Sprite.Width, hammer.Sprite.Height);
                    }
                    else
                    {
                        hammer.Origin = new Vector2(0, hammer.Sprite.Height);
                    }
                }
                if (hammer.CollidesWith(mario))
                {
                    if (!hammer.IsActive)
                    {
                        hammer.IsActive         = true;
                        hammer.PowerupActivated = DateTime.UtcNow;
                    }
                }

                //if barrel hits active hammer, remove it
                for (int iBarrel = 0; iBarrel < barrels.Children.Count; iBarrel++)
                {
                    Barrel barrel = (Barrel)barrels.Children.ElementAt(iBarrel);
                    if (barrel.CollidesWith(hammer) && hammer.IsActive)
                    {
                        barrels.Remove(barrel);
                        ScoreText _scoreText = new ScoreText(barrel.Position);
                        scoreText.Add(_scoreText);
                        totalScore += ScoreText.ScoreIncrease;
                        break;
                    }
                }
            }

            foreach (Floor floor in floors.Children)
            {
                if (mario.Position.Y + mario.Sprite.Height - 10 <= floor.Position.Y)
                {
                    floor.MarioHasBeenAbove = true;
                }
                else
                {
                    floor.MarioHasBeenAbove = false;
                }

                //floor with mario collision
                if (floor.CollidesWith(mario) &&
                    floor.MarioHasBeenAbove == true &&
                    mario.Velocity.Y >= 0)
                {
                    mario.Velocity         = new Vector2(mario.Velocity.X, 0);
                    mario.Acceleration     = new Vector2(mario.Acceleration.X, 0);
                    mario.MovementStrategy = new MarioOnFloor();
                    mario.Position         = new Vector2(mario.Position.X, -mario.Sprite.Height + floor.Position.Y + 1);
                    break;
                }
                //reset movementstrategy if mario's movementstrategy is still "OnFloor"
                else if (!floor.CollidesWith(mario) &&
                         mario.MovementStrategy.GetType() == typeof(MarioOnFloor))
                {
                    mario.MovementStrategy = new MarioNormalMovement();
                }
            }



            bool breakflag = false;

            foreach (GameObjectList ladderList in ladders.Children)
            {
                foreach (Ladder ladder in ladderList.Children)
                {
                    //ladder with mario collision
                    if (ladder.CollidesWith(mario) && Math.Abs((ladder.Position.X + ladder.Center.X) - (mario.Position.X + mario.Center.X)) < ladderDistanceTrigger)
                    {
                        mario.MovementStrategy = new MarioOnLadder();
                        breakflag = true;
                        break;
                    }
                    else if (mario.MovementStrategy.GetType() == typeof(MarioOnLadder))
                    {
                        mario.MovementStrategy = new MarioNormalMovement();
                    }
                }
                if (breakflag)
                {
                    break;
                }
            }


            foreach (Barrel barrel in barrels.Children)
            {
                //floor collision
                foreach (Floor floor in floors.Children)
                {
                    if (floor.CollidesWith(barrel) &&
                        barrel.Velocity.Y >= 0 &&
                        barrel.MovementStrategy.GetType() != typeof(BarrelGoingDown))
                    {
                        barrel.Velocity = new Vector2(barrel.Velocity.X, -barrel.Velocity.Y);
                        if (barrel.MovementStrategy.GetType() != typeof(BarrelOnFloor) && Math.Abs(barrel.Velocity.Y) <= 50)
                        {
                            barrel.Velocity         = new Vector2(barrel.Velocity.X, 0);
                            barrel.MovementStrategy = new BarrelOnFloor(barrel.Velocity);
                        }
                        break;
                    }

                    if (!floor.CollidesWith(barrel) &&
                        barrel.MovementStrategy.GetType() == typeof(BarrelOnFloor))
                    {
                        barrel.MovementStrategy = new BarrelNormalMovement(barrel.Velocity);
                    }
                }

                //Bounce off walls

                if ((barrel.Position.X - barrel.Sprite.Width / 2.0f < 0 && barrel.Velocity.X <= 0 && barrel.Acceleration.X <= 0) ||
                    (barrel.Position.X + barrel.Sprite.Width / 2.0f > GameEnvironment.Screen.X && barrel.Velocity.X >= 0 && barrel.Acceleration.X >= 0))
                {
                    if (!(barrel.Position.Y - BarrelOffScreenDifference >= mario.Position.Y))
                    {
                        barrel.Velocity     = new Vector2(-barrel.Velocity.X, barrel.Velocity.Y);
                        barrel.Acceleration = new Vector2(-barrel.Acceleration.X, barrel.Acceleration.Y);
                    }
                }

                //remove if outside of screen
                if (barrel.Position.X + barrel.Center.X < -despawnArea || barrel.Position.X - barrel.Center.X > GameEnvironment.Screen.X + despawnArea)
                {
                    this.Remove(barrel);
                }

                //Barrels have a chance to go down the stairs
                barrel.Position = new Vector2(barrel.Position.X, barrel.Position.Y + barrel.Sprite.Height);
                Dictionary <Ladder, bool> _ladderDict = new Dictionary <Ladder, bool>(barrel.LadderDict);
                foreach (KeyValuePair <Ladder, bool> kvp in barrel.LadderDict)
                {
                    if (barrel.CollidesWith(kvp.Key) &&
                        kvp.Value != true &&
                        Math.Abs((barrel.Position.X) - (kvp.Key.Position.X + kvp.Key.Center.X)) < 5)
                    {
                        _ladderDict.Remove(kvp.Key);
                        _ladderDict.Add(kvp.Key, true);
                        if (random.NextDouble() < 0.2)
                        {
                            barrel.MovementStrategy = new BarrelGoingDown(barrel.Velocity);
                        }
                    }
                }
                barrel.LadderDict = _ladderDict;
                barrel.Position   = new Vector2(barrel.Position.X, barrel.Position.Y - barrel.Sprite.Height);

                //Mario loses when he touches a barrel
                if (barrel.CollidesWith(mario))
                {
                    GameEnvironment.GameStateManager.SwitchTo("GameOverState");
                    GameOverState.FinalScore = totalScore;
                    GameEnvironment.GameStateManager.Reset();
                }
            }

            //Donkey kong keeps throwing barrels
            if (random.NextDouble() <= barrelSpawnChance)
            {
                //create ladderDict
                ladderDict = new Dictionary <Ladder, bool>();
                foreach (GameObjectList ladderList in ladders.Children)
                {
                    if (ladderList.Id != "1")
                    {
                        ladderDict.Add((Ladder)ladderList.Children[0], false);
                    }
                }

                barrels.Add(new Barrel(
                                new Vector2(kdankyDang.Position.X, kdankyDang.Position.Y - 20),
                                Barrel.BarrelStartVelocity,
                                ladderDict));
            }
        }