public void Execute() { // take the receiver method of Fire state change if (!Mario.IsFire() && !Mario.IsDied() && Sprint5Main.Coins >= 70) { Mario.MoveFire(); Sprint5Main.Coins -= 70; SoundFactory.Instance.PowerUp(); } }
public void Collide() { // tell mario what he collide with by check object's type and tell the object mario collide with it. if (Character1.Type == Sprint5Main.CharacterType.Mario) { MarioCharacter MarioCharacters = (MarioCharacter)Character1; if (Character2.Type == Sprint5Main.CharacterType.Block) { Character2.MarioCollide((Time == yTime) && (relativeVelocity.Y < 0)); } else if (Character2 is PlantEnemyCharacter || Character2 is CloudEnemyCharacter) { Character2.MarioCollide(false); } else { Character2.MarioCollide((Time == yTime) && (relativeVelocity.Y > 0)); } if (!MarioCharacters.IsDied()) { MarioCharacters.OnPipe = false; MarioCharacters.CollideWith(Character2, Time == yTime, relativeVelocity.Y > 0); } } else if (Character1.Type == Sprint5Main.CharacterType.Fireball) { Character1.Parameters.IsHidden = true; // fireball will hide no matter what it hit //only mario has response when collide with fireball if (Character2.Type == Sprint5Main.CharacterType.Enemy) { Character2.MarioCollide(true); } } else //items and enemies { if (Character1.Parameters.Velocity.X * relativeVelocity.X >= 0) { Character1.BlockCollide((Time == yTime) && (relativeVelocity.Y > 0)); } if (Character1 is BossEnemyCharacter && Character2 is BlockCharacter) { BlockCharacter block = (BlockCharacter)Character2; block.BossEnemyCollide(); } } }
public void Update() { //check whether they are out of screen foreach (ICharacter character in CharacterList) { if (Mario.Parameters.Position.X <= (Stage.Boundary.X / 2)) { character.Parameters.InScreen = character.Parameters.Position.X <= Stage.Boundary.X; } else if (Mario.Parameters.Position.X >= (Stage.MapBoundary.X - Stage.Boundary.X / 2)) { character.Parameters.InScreen = character.GetMaxPosition.X >= Stage.MapBoundary.X - Stage.Boundary.X; } else { character.Parameters.InScreen = character.GetMaxPosition.X >= (Mario.Parameters.Position.X - 800 / 2) && character.Parameters.Position.X <= Mario.Parameters.Position.X + 800 / 2; } if (character is BossEnemyCharacter) { character.Parameters.InScreen = true; } if (Mario.IsDied() || Mario.Win) { character.Parameters.InScreen = false; } } int insurance = 0; // an insurance for unstop loop float timeOfFrame = 1; // total time for collision while (timeOfFrame > 0) { insurance++; if (insurance > 50) { Console.WriteLine("It looks the loop will not stop. Check! The rest of Time = " + timeOfFrame); Sprint5Main.Game.Exit(); break; } Map.UpdateMovingCharacters(); List <CollidePair> firstContactPairs = new List <CollidePair>(); /* * Generate collide pair for mario, moving objects and fireball. */ if (!Mario.IsDied() && (Mario.GetAction != MarioState.ActionType.Other || Mario.Win)) { CreateCollidePairs(Mario); } foreach (ICharacter character in MovingCharacters) { if (!character.Parameters.IsHidden) { CreateCollidePairs(character); } } foreach (ICharacter character in FireBallCharacters) { if (!character.Parameters.IsHidden) { CreateCollidePairs(character); } } CollidePair[] pairs = CollidePairs.ToArray(); float shortestTime = timeOfFrame; // find the smallest first contact time. for (int i = 0; i < pairs.Length; i++) { if (CollidePairs[i].Time <= shortestTime && CollidePairs[i].Time >= 0) { shortestTime = CollidePairs[i].Time; firstContactPairs.Insert(0, CollidePairs[i]); } } Mario.Update(shortestTime); // Update with smallest first contact time. //Update all objects foreach (ICharacter character in FireBallCharacters) { character.Update(shortestTime); } foreach (ICharacter character in CharacterList) { character.Update(shortestTime); } //Do collision response. Use List since we don't know whether more than one objects collide with mario at the same time. CollidePair[] cp = firstContactPairs.ToArray(); for (int i = 0; i < cp.Length && cp[i].Time == shortestTime; i++) { cp[i].Collide(); } CollidePairs.Clear(); // clear collide pairs firstContactPairs.Clear(); //clear sorted collide pairs timeOfFrame -= shortestTime; // change the rest of time. } }