public void CheckForCollision(Hero Player, Zombie Zombie, int NumberOfZombies, int NumberOfZombiesKilled, Vector2 scrollOffset) { collision = false; #region Bullets to Zombies Matrix bulletTransform = Matrix.CreateTranslation(new Vector3(-center, 0.0f)) * Matrix.CreateRotationZ(rotation) * Matrix.CreateTranslation(new Vector3(position + scrollOffset, 0.0f)); Rectangle bulletRectangle = CalculateBoundingRectangle( new Rectangle(0, 0, sprite.Width, sprite.Height), bulletTransform); Matrix zombieTransform = Matrix.CreateTranslation(new Vector3(-Zombie.center, 0.0f)) * Matrix.CreateRotationZ(Zombie.rotation) * Matrix.CreateTranslation(new Vector3(Zombie.position + scrollOffset, 0.0f)); Rectangle zombieRectangle = CalculateBoundingRectangle( new Rectangle(0, 0, Zombie.sprite.Width, Zombie.sprite.Height), zombieTransform); if (bulletRectangle.Intersects(zombieRectangle)) { if (IntersectPixels(bulletTransform, sprite.Width, sprite.Height, textureData, zombieTransform, Zombie.sprite.Width, Zombie.sprite.Height, Zombie.textureData)) { Zombie.health -= 100; collision = true; if (Zombie.health <= 0) { NumberOfZombiesKilled++; NumberOfZombies--; Zombie.alive = false; Zombie.health = Zombie.fullHealth; } alive = false; if (Player.playerCharacter == Character.Sir_Edward) { Player.score += 10; } } } #endregion numberOfZombies = NumberOfZombies; numberOfZombiesKilled = NumberOfZombiesKilled; }
public void Update(M4Bullet[] M4Bullets, Hero Player, int NumberOfPlayersLeft, Zombie[] Zombies, int NumberOfZombies, int NumberOfZombiesKilled, Vector2 scrollOffset) { numberOfZombies = NumberOfZombies; numberOfZombiesKilled = NumberOfZombiesKilled; foreach (M4Bullet m4Bullet in M4Bullets) { if (m4Bullet.alive) { //this actually moves the bullet across the screen m4Bullet.position += m4Bullet.velocity; if (Vector2.Distance(Player.position + scrollOffset, m4Bullet.position + scrollOffset) > 1200.0f) { m4Bullet.alive = false; continue; } else { foreach (Zombie zombie in Zombies) { if (zombie.alive) { m4Bullet.CheckForCollision(Player, zombie, NumberOfZombies, NumberOfZombiesKilled, scrollOffset); if (m4Bullet.collision) { numberOfZombies = m4Bullet.numberOfZombies; numberOfZombiesKilled = m4Bullet.numberOfZombiesKilled; break; } } } } } } }
public void Update(ShotgunBullet[] ShotgunBullets, Hero Player, int NumberOfPlayersLeft, Zombie[] Zombies, int NumberOfZombies, int NumberOfZombiesKilled, Vector2 scrollOffset) { numberOfZombies = NumberOfZombies; numberOfZombiesKilled = NumberOfZombiesKilled; foreach(ShotgunBullet shotgunBullet in ShotgunBullets) { if (shotgunBullet.alive) { //this actually moves the bullet across the screen shotgunBullet.position += shotgunBullet.velocity; if (Vector2.Distance(Player.position + scrollOffset, shotgunBullet.position + scrollOffset) > Player.sprite.Width * 3) { shotgunBullet.alive = false; continue; } else { foreach (Zombie zombie in Zombies) { if (zombie.alive) { shotgunBullet.CheckForCollision(Player, zombie, NumberOfZombies, NumberOfZombiesKilled, scrollOffset); if (shotgunBullet.collision) { numberOfZombies = shotgunBullet.numberOfZombies; numberOfZombiesKilled = shotgunBullet.numberOfZombiesKilled; break; } } } } } } }
public override void LoadContent() { if (ScreenManager.Game.Content == null) ScreenManager.Game.Content = new ContentManager(ScreenManager.Game.Services, "Content"); redRectangle = ScreenManager.Game.Content.Load<Texture2D>("Sprites\\Misc\\redRectangle"); players = new Hero[NumberOfPlayers]; playerHalos = new OtherObject[NumberOfPlayers]; shotgunBullets = new ShotgunBullet[numberOfshotgunBullets]; miniUziBullets = new MiniUziBullet[numberOfminiUziBullets]; m4Bullets = new M4Bullet[numberOfm4Bullets]; g36cBullets = new G36CBullet[numberOfg36cBullets]; zombies = new Zombie[MaxZombies]; for (int i = 0; i < NumberOfPlayers; i++) { //Load the texture data from the Sprite folder depending on who the player is #region Texture Data if (PlayerCharacters[i] == Character.Sayid) { players[i] = new Hero(ScreenManager.Game.Content.Load<Texture2D>("Sprites\\Players\\Sayid\\Images\\Sayid")); playerHalos[i] = new OtherObject(ScreenManager.Game.Content.Load<Texture2D>("Sprites\\Players\\Sayid\\Halo\\Sayid-Halo")); } if (PlayerCharacters[i] == Character.Sir_Edward) { players[i] = new Hero(ScreenManager.Game.Content.Load<Texture2D>("Sprites\\Players\\Sir_Edward\\Images\\Sir_Edward")); playerHalos[i] = new OtherObject(ScreenManager.Game.Content.Load<Texture2D>("Sprites\\Players\\Sir_Edward\\Halo\\Sir_Edward-Halo")); } if (PlayerCharacters[i] == Character.Wilhelm) { players[i] = new Hero(ScreenManager.Game.Content.Load<Texture2D>("Sprites\\Players\\Wilhelm\\Images\\Wilhelm")); playerHalos[i] = new OtherObject(ScreenManager.Game.Content.Load<Texture2D>("Sprites\\Players\\Wilhelm\\Halo\\Wilhelm-Halo")); } if (PlayerCharacters[i] == Character.Juan) { players[i] = new Hero(ScreenManager.Game.Content.Load<Texture2D>("Sprites\\Players\\Juan\\Images\\Juan")); playerHalos[i] = new OtherObject(ScreenManager.Game.Content.Load<Texture2D>("Sprites\\Players\\Juan\\Halo\\Juan-Halo")); } #endregion //give a position for the player to start out at #region Position //bottom left first players[i].position = new Vector2(players[i].sprite.Width * xSignedPosition + (totalFieldWidth / 2), players[i].sprite.Height * ySignedPosition + (totalFieldHeight / 2)); if (xSignedPosition == -1 && ySignedPosition == -1) { //then bottom right xSignedPosition *= -1; } else if (xSignedPosition == 1 && ySignedPosition == -1) { //then top right ySignedPosition *= -1; } else if (xSignedPosition == 1 && ySignedPosition == 1) { //then top left xSignedPosition *= -1; } #endregion //give the correct health for the player depending on the handicap #region Health if (players[i].playerHandicap == Handicap.Easy) { players[i].health = 20; } else if (players[i].playerHandicap == Handicap.Medium) { players[i].health = 15; } else if (players[i].playerHandicap == Handicap.Hard) { players[i].health = 10; } else if (players[i].playerHandicap == Handicap.VeryHard) { players[i].health = 5; } //reference giving origina2 health for sca2e factoring 2ater players[i].fullHealth = players[i].health; #endregion players[i].playerCharacter = PlayerCharacters[i]; players[i].playerHandicap = PlayerHandicaps[i]; players[i].triggerSensitivity = sen[i]; LoadBullets(players[i].playerCharacter); players[i].LoadUpBulletManager(); } #region Zombies for (int i = 0; i < MaxZombies; i++) { zombies[i] = new Zombie(ScreenManager.Game.Content.Load<Texture2D>("Sprites\\Zombies\\Images\\zombie1(1)")); } #endregion healthBar = ScreenManager.Game.Content.Load<Texture2D>("Sprites\\Misc\\HealthBar"); gameBackgroundTexture = ScreenManager.Game.Content.Load<Texture2D>("Background\\GrassBackground"); Active = true; //sleep for a little so the game can load Thread.Sleep(1000); //resets so xbox doesnt try to catch up with lost time in thread ScreenManager.Game.ResetElapsedTime(); }
private void UpdateWeapon(InputState input, Character playerCharacter, int playerNumber) { switch (playerCharacter) { //seems fine #region Sayid Weapon Timer case Character.Sayid: //if pressed down if (input.CurrentGamePadStates[playerNumber].Triggers.Right > players[playerNumber].triggerSensitivity) { //if there is allowed for a new timer to be created if (sayidNewTimer && !sayidTimerActive) { TimerCallback tcb = SayidFireWeapon; sayidsPlayer = players[playerNumber]; sayidTimer = new Timer(tcb, autoEvent, 0, 330); sayidNewTimer = false; sayidTimerActive = true; } } //if let go if (input.CurrentGamePadStates[playerNumber].Triggers.Right <= players[playerNumber].triggerSensitivity && sayidTimerActive) { if (sayidTimer != null) { sayidTimer.Dispose(); } //allow for a new timer to be created sayidNewTimer = true; sayidTimerActive = false; } break; #endregion //seems fine #region Juan Weapon Timer case Character.Juan: if (input.CurrentGamePadStates[playerNumber].Triggers.Right > players[playerNumber].triggerSensitivity) { if (juanNewTimer && !juanTimerActive) { TimerCallback tcb = JuanFireWeapon; juansPlayer = players[playerNumber]; juanTimer = new Timer(tcb, autoEvent, 0, 1000); juanNewTimer = false; } } if (input.CurrentGamePadStates[playerNumber].Triggers.Right <= players[playerNumber].triggerSensitivity && juanTimerActive) { if (juanTimer != null) { juanTimer.Dispose(); } juanNewTimer = true; juanTimerActive = false; } break; #endregion //seems fine #region Sir Edward Weapon Timer case Character.Sir_Edward: if (input.CurrentGamePadStates[playerNumber].Triggers.Right > players[playerNumber].triggerSensitivity) { if (sir_EdwardNewTimer && !sir_EdwardTimerActive) { TimerCallback tcb = SirEdwardFireWeapon; sirEdwardsPlayer = players[playerNumber]; sir_EdwardTimer = new Timer(tcb, autoEvent, 0, 500); sir_EdwardNewTimer = false; } } if (input.CurrentGamePadStates[playerNumber].Triggers.Right <= players[playerNumber].triggerSensitivity && sir_EdwardTimerActive) { if (sir_EdwardTimer != null) { sir_EdwardTimer.Dispose(); } sir_EdwardNewTimer = true; sir_EdwardTimerActive = false; } break; #endregion //seems fine #region Wilhelm Weapon Timer case Character.Wilhelm: if (input.CurrentGamePadStates[playerNumber].Triggers.Right > players[playerNumber].triggerSensitivity) { if (wilhelmNewTimer && !wilhelmTimerActive) { TimerCallback tcb = WilhelmFireWeapon; wilhelmsPlayer = players[playerNumber]; wilhelmTimer = new Timer(tcb, autoEvent, 0, 500); wilhelmNewTimer = false; } } if (input.CurrentGamePadStates[playerNumber].Triggers.Right <= players[playerNumber].triggerSensitivity && wilhelmTimerActive) { if (wilhelmTimer != null) { wilhelmTimer.Dispose(); } wilhelmNewTimer = true; wilhelmTimerActive = false; } break; #endregion } }
public void CheckForCollision(Hero Player, int NumberOfPlayersLeft, int NumberOfZombies, Vector2 scrollOffset) { numberOfZombies = NumberOfZombies; numberOfPlayersLeft = NumberOfPlayersLeft; #region Transformations and Rectangles Matrix zombieTransform = Matrix.CreateTranslation(new Vector3(-center, 0.0f)) * Matrix.CreateRotationZ(rotation) * Matrix.CreateTranslation(new Vector3(position + scrollOffset, 0.0f)); Rectangle zombieRectangle = CalculateBoundingRectangle( new Rectangle(0, 0, sprite.Width, sprite.Height), zombieTransform); Matrix rectangleTransform = Matrix.CreateTranslation(new Vector3(-Player.center, 0.0f)) * Matrix.CreateRotationZ(Player.rotation) * Matrix.CreateTranslation(new Vector3(Player.position + scrollOffset, 0.0f)); Rectangle gunRect = CalculateBoundingRectangle( new Rectangle((int)3.5, (int)47.4, 2, 2), rectangleTransform); Rectangle rightForearmRect = CalculateBoundingRectangle( new Rectangle((int)28.9, (int)34.1, 2, 2), rectangleTransform); Rectangle rightElbowRect = CalculateBoundingRectangle( new Rectangle(46, (int)21.5, 2, 2), rectangleTransform); Rectangle rightShoulderRect = CalculateBoundingRectangle( new Rectangle((int)82.2, (int)28.2, 2, 2), rectangleTransform); Rectangle headRect = CalculateBoundingRectangle( new Rectangle((int)94.7, (int)47.4, 2, 2), rectangleTransform); Rectangle leftShoulderRect = CalculateBoundingRectangle( new Rectangle(84, (int)69.4, 2, 2), rectangleTransform); Rectangle leftElbowRect = CalculateBoundingRectangle( new Rectangle((int)45.6, (int)74.3, 2, 2), rectangleTransform); Rectangle leftForearmRect = CalculateBoundingRectangle( new Rectangle((int)29.3, (int)66.6, 2, 2), rectangleTransform); #endregion if (zombieRectangle.Intersects(gunRect) || zombieRectangle.Intersects(rightForearmRect) || zombieRectangle.Intersects(rightElbowRect) || zombieRectangle.Intersects(rightShoulderRect) || zombieRectangle.Intersects(headRect) || zombieRectangle.Intersects(leftShoulderRect) || zombieRectangle.Intersects(leftElbowRect) || zombieRectangle.Intersects(leftForearmRect)) { alive = false; NumberOfZombies--; Player.health--; if (Player.health <= 0) { Player.alive = false; NumberOfPlayersLeft--; } } numberOfZombies = NumberOfZombies; numberOfPlayersLeft = NumberOfPlayersLeft; zombie = this; player = Player; }
public void UpdateZombies(bool? player1Alive, bool? player2Alive, bool? player3Alive, bool? player4Alive, Vector2 screenCenter, Vector2 scrollOffset, Hero[] players, int NumberOfPlayersLeft) { //the threshold of which the zombie is either chasing or actually caught the player(s) //for example if the zombie is within 10 pixels of the player he is considered caught and the 10 pixels is the threshold // and everything else is within the chase threshold float zombieChaseThreshold = ZombieChaseDistance; float zombieCaughtThreshold = ZombieCaughtDistance; if (zombieState == ZombieAiState.Chasing) { zombieChaseThreshold += ZombieHysteresis / 2; zombieCaughtThreshold -= ZombieHysteresis / 2; } else if (zombieState == ZombieAiState.Caught) { zombieCaughtThreshold += ZombieHysteresis / 2; } UncheckPlayers(); //gives a position for the moving zombies...towards the closest player by giving a heading, and speed if (alive) { #region Players 1 if (NumberOfPlayersLeft == 1) { int j = GetPlayerValues(player1Alive, player2Alive, player3Alive, player4Alive); UncheckPlayers(); //gives the distance from zombie to player float distanceFromPlayer1 = Vector2.Distance(position + scrollOffset, players[j].position + scrollOffset); //if the distance from player is greater than the caught threshold... if (distanceFromPlayer1 > zombieCaughtThreshold) { //then the zombie must still be chasing the player... zombieState = ZombieAiState.Chasing; } else { //otherwise the player must be caught! zombieState = ZombieAiState.Caught; } // the zombie's speed, either 0 or the max speed (then its added to the position vector) float currentZombieSpeed; //while the zombie is chasing he needs to know which way to chase if (zombieState == ZombieAiState.Chasing) { //so towards the closest player he turns to face the player through the TurnToFace method further below zombieOrientation = TurnToFace(position + scrollOffset, players[j].position + scrollOffset, zombieOrientation, maxZombieTurning); currentZombieSpeed = maxZombieSpeed; } else //if the zombie isn't chasing then he has caught the player and there is no need to move further { currentZombieSpeed = 0.0f; } //the zombie's new heading is given by the sin and cos given above (heading is created through cos and sin vectors, // because it is a certain angle one faces in reference to a previous direction Vector2 heading = new Vector2( (float)Math.Cos(zombieOrientation), (float)Math.Sin(zombieOrientation)); position += heading * currentZombieSpeed; } #endregion #region Players 2 if (NumberOfPlayersLeft == 2) { int j = GetPlayerValues(player1Alive, player2Alive, player3Alive, player4Alive); int k = GetPlayerValues(player1Alive, player2Alive, player3Alive, player4Alive); UncheckPlayers(); float distanceFromPlayer1 = Vector2.Distance(position + scrollOffset, players[j].position + scrollOffset); float distanceFromPlayer2 = Vector2.Distance(position + scrollOffset, players[k].position + scrollOffset); if (distanceFromPlayer1 > zombieCaughtThreshold || distanceFromPlayer2 > zombieCaughtThreshold) { zombieState = ZombieAiState.Chasing; } else { zombieState = ZombieAiState.Caught; } float currentZombieSpeed; if (zombieState == ZombieAiState.Chasing) { if (players[j].alive) { if (distanceFromPlayer1 < distanceFromPlayer2) { zombieOrientation = TurnToFace(position + scrollOffset, players[j].position + scrollOffset, zombieOrientation, maxZombieTurning); } } if (players[k].alive) { if (distanceFromPlayer2 < distanceFromPlayer1) { zombieOrientation = TurnToFace(position + scrollOffset, players[k].position + scrollOffset, zombieOrientation, maxZombieTurning); } } currentZombieSpeed = maxZombieSpeed; } else { currentZombieSpeed = 0.0f; } Vector2 heading = new Vector2( (float)Math.Cos(zombieOrientation), (float)Math.Sin(zombieOrientation)); position += heading * currentZombieSpeed; } #endregion #region Players 3 if (NumberOfPlayersLeft == 3) { int j = GetPlayerValues(player1Alive, player2Alive, player3Alive, player4Alive); int k = GetPlayerValues(player1Alive, player2Alive, player3Alive, player4Alive); int l = GetPlayerValues(player1Alive, player2Alive, player3Alive, player4Alive); UncheckPlayers(); float distanceFromPlayer1 = Vector2.Distance(position + scrollOffset, players[j].position + scrollOffset); float distanceFromPlayer2 = Vector2.Distance(position + scrollOffset, players[k].position + scrollOffset); float distanceFromPlayer3 = Vector2.Distance(position + scrollOffset, players[l].position + scrollOffset); if (distanceFromPlayer1 > zombieCaughtThreshold || distanceFromPlayer2 > zombieCaughtThreshold || distanceFromPlayer3 > zombieCaughtThreshold) { zombieState = ZombieAiState.Chasing; } else { zombieState = ZombieAiState.Caught; } float currentZombieSpeed; if (zombieState == ZombieAiState.Chasing) { if (players[0].alive) { if (distanceFromPlayer1 < distanceFromPlayer2 && distanceFromPlayer1 < distanceFromPlayer3) { zombieOrientation = TurnToFace(position + scrollOffset, players[j].position + scrollOffset, zombieOrientation, maxZombieTurning); } } if (players[1].alive) { if (distanceFromPlayer2 < distanceFromPlayer1 && distanceFromPlayer2 < distanceFromPlayer3) { zombieOrientation = TurnToFace(position + scrollOffset, players[k].position + scrollOffset, zombieOrientation, maxZombieTurning); } } if (players[2].alive) { if (distanceFromPlayer3 < distanceFromPlayer1 && distanceFromPlayer3 < distanceFromPlayer2) { zombieOrientation = TurnToFace(position + scrollOffset, players[l].position + scrollOffset, zombieOrientation, maxZombieTurning); } } currentZombieSpeed = maxZombieSpeed; } else { currentZombieSpeed = 0.0f; } Vector2 heading = new Vector2( (float)Math.Cos(zombieOrientation), (float)Math.Sin(zombieOrientation)); position += heading * currentZombieSpeed; } #endregion #region Players 4 if (NumberOfPlayersLeft == 4) { int j = GetPlayerValues(player1Alive, player2Alive, player3Alive, player4Alive); int k = GetPlayerValues(player1Alive, player2Alive, player3Alive, player4Alive); int l = GetPlayerValues(player1Alive, player2Alive, player3Alive, player4Alive); int m = GetPlayerValues(player1Alive, player2Alive, player3Alive, player4Alive); UncheckPlayers(); float distanceFromPlayer1 = Vector2.Distance(position + scrollOffset, players[j].position + scrollOffset); float distanceFromPlayer2 = Vector2.Distance(position + scrollOffset, players[k].position + scrollOffset); float distanceFromPlayer3 = Vector2.Distance(position + scrollOffset, players[l].position + scrollOffset); float distanceFromPlayer4 = Vector2.Distance(position + scrollOffset, players[m].position + scrollOffset); if (distanceFromPlayer1 > zombieCaughtThreshold || distanceFromPlayer2 > zombieCaughtThreshold || distanceFromPlayer3 > zombieCaughtThreshold || distanceFromPlayer4 > zombieCaughtThreshold) { zombieState = ZombieAiState.Chasing; } else { zombieState = ZombieAiState.Caught; } float currentZombieSpeed; if (zombieState == ZombieAiState.Chasing) { if (players[0].alive) { if (distanceFromPlayer1 < distanceFromPlayer2 && distanceFromPlayer1 < distanceFromPlayer3 && distanceFromPlayer1 < distanceFromPlayer4) { zombieOrientation = TurnToFace(position + scrollOffset, players[j].position + scrollOffset, zombieOrientation, maxZombieTurning); } } if (players[1].alive) { if (distanceFromPlayer2 < distanceFromPlayer1 && distanceFromPlayer2 < distanceFromPlayer3 && distanceFromPlayer2 < distanceFromPlayer4) { zombieOrientation = TurnToFace(position + scrollOffset, players[k].position + scrollOffset, zombieOrientation, maxZombieTurning); } } if (players[2].alive) { if (distanceFromPlayer3 < distanceFromPlayer1 && distanceFromPlayer3 < distanceFromPlayer2 && distanceFromPlayer3 < distanceFromPlayer4) { zombieOrientation = TurnToFace(position + scrollOffset, players[l].position + scrollOffset, zombieOrientation, maxZombieTurning); } } if (players[3].alive) { if (distanceFromPlayer4 < distanceFromPlayer1 && distanceFromPlayer4 < distanceFromPlayer2 && distanceFromPlayer4 < distanceFromPlayer3) { zombieOrientation = TurnToFace(position + scrollOffset, players[m].position + scrollOffset, zombieOrientation, maxZombieTurning); } } currentZombieSpeed = maxZombieSpeed; } else { currentZombieSpeed = 0.0f; } Vector2 heading = new Vector2( (float)Math.Cos(zombieOrientation), (float)Math.Sin(zombieOrientation)); position += heading * currentZombieSpeed; } #endregion } }