/// <summary> /// Check what a projectile hit. The possibilities are: /// Nothing hit, Hit enemy, Hit self, hit own/enemy's crate. /// </summary> /// <param name="projectile">The projectile for which to /// perform the check.</param> /// <returns>A result inidicating what, if anything, was hit</returns> private HitCheckResult CheckHit(Projectile projectile) { HitCheckResult hitRes = HitCheckResult.Nothing; // Build a sphere around a projectile Vector3 center = new Vector3(projectile.ProjectilePosition, 0); BoundingSphere sphere = new BoundingSphere(center, Math.Max(projectile.ProjectileTexture.Width / 2, projectile.ProjectileTexture.Height / 2)); // Check Self-Hit - create a bounding box around self Vector3 min = new Vector3(catapultPosition, 0); Vector3 max = new Vector3(catapultPosition + new Vector2(animations["Fire"].FrameSize.X, animations["Fire"].FrameSize.Y), 0); BoundingBox selfBox = new BoundingBox(min, max); // Check enemy - create a bounding box around the enemy min = new Vector3(enemy.Catapult.Position, 0); max = new Vector3(enemy.Catapult.Position + new Vector2(animations["Fire"].FrameSize.X, animations["Fire"].FrameSize.Y), 0); BoundingBox enemyBox = new BoundingBox(min, max); // Check self-crate - Create bounding box around own crate min = new Vector3(crate.Position, 0); max = new Vector3(crate.Position + new Vector2(crate.Width, crate.Height), 0); BoundingBox selfCrateBox = new BoundingBox(min, max); // Check enemy-crate - Create bounding box around enemy crate min = new Vector3(enemy.Catapult.crate.Position, 0); max = new Vector3(enemy.Catapult.crate.Position + new Vector2(enemy.Catapult.crate.Width, enemy.Catapult.crate.Height), 0); BoundingBox enemyCrateBox = new BoundingBox(min, max); // Check self hit if (sphere.Intersects(selfBox) && currentState != CatapultState.HitKill) { AudioManager.PlaySound("catapultExplosion"); // Launch hit animation sequence on self UpdateHealth(self, sphere, selfBox); if (self.Health <= 0) { Hit(true); enemy.Score++; } hitRes = HitCheckResult.SelfCatapult; } // Check if enemy was hit else if (sphere.Intersects(enemyBox) && enemy.Catapult.CurrentState != CatapultState.HitKill && enemy.Catapult.CurrentState != CatapultState.Reset) { AudioManager.PlaySound("catapultExplosion"); // Launch enemy hit animaton UpdateHealth(enemy, sphere, enemyBox); if (enemy.Health <= 0) { enemy.Catapult.Hit(true); if (self.IsActive) { self.Score++; if (App.g_isTwoHumanPlayers) { Dictionary <string, object> scoreProperties = new Dictionary <string, object>(); if (GlobalContext.PlayerIsFirstOnAppWarp) { GlobalContext.tableProperties["Player1Score"] = self.Score; scoreProperties.Add("Player1Score", self.Score); } else { scoreProperties.Add("Player2Score", self.Score); GlobalContext.tableProperties["Player2Score"] = self.Score; } WarpClient.GetInstance().UpdateRoomProperties(GlobalContext.GameRoomId, scoreProperties, null); } } } hitRes = HitCheckResult.EnemyCatapult; currentState = CatapultState.Reset; } // Check if own crate was hit else if (sphere.Intersects(selfCrateBox)) { hitRes = HitCheckResult.SelfCrate; } // Check if enemy crate was hit else if (sphere.Intersects(enemyCrateBox)) { hitRes = HitCheckResult.EnemyCrate; } return(hitRes); }
/// <summary> /// Allows the page to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> private void OnUpdate(object sender, GameTimerEventArgs e) { // TODO: Add your update logic here // Check it one of the players reached 5 and stop the game if (mNavigationMode == NavigationMode.Back) { mNavigationMode = NavigationMode.Refresh; NavigationService.GoBack(); return; } float elapsed = (float)e.ElapsedTime.TotalSeconds; mInputState.Update(); HandleInput(mInputState); if ((playerOne.Catapult.GameOver || playerTwo.Catapult.GameOver) && (gameOver == false)) { gameOver = true; if (isTwoHumanPlayers) { if ((playerOne.Score > playerTwo.Score) && GlobalContext.PlayerIsFirstOnAppWarp) { AudioManager.PlaySound("gameOver_Win"); } else { AudioManager.PlaySound("gameOver_Lose"); } } else { if (playerOne.Score > playerTwo.Score) { AudioManager.PlaySound("gameOver_Win"); } else { AudioManager.PlaySound("gameOver_Lose"); } } return; } // If Reset flag raised and both catapults are not animating - // active catapult finished the cycle, new turn! if ((playerOne.Catapult.CurrentState == CatapultState.Reset || playerTwo.Catapult.CurrentState == CatapultState.Reset) && !(playerOne.Catapult.AnimationRunning || playerTwo.Catapult.AnimationRunning)) { if (!isTwoHumanPlayers) { if (playerOne.IsActive == true) //Last turn was a left player turn? { playerOne.IsActive = false; playerTwo.IsActive = true; playerOne.Catapult.CurrentState = CatapultState.Idle; if (!isTwoHumanPlayers) { playerTwo.Catapult.CurrentState = CatapultState.Aiming; } else { playerTwo.Catapult.CurrentState = CatapultState.Idle; } } else //It was an right player turn { playerOne.IsActive = true; playerTwo.IsActive = false; playerTwo.Catapult.CurrentState = CatapultState.Idle; playerOne.Catapult.CurrentState = CatapultState.Idle; } } else { playerTwo.Catapult.CurrentState = CatapultState.Idle; playerOne.Catapult.CurrentState = CatapultState.Idle; } changeTurn = true; isFirstPlayerTurn = !isFirstPlayerTurn; } if (!isTwoHumanPlayers) { if (changeTurn) { // Update wind wind = new Vector2(random.Next(-1, 2), random.Next(minWind, maxWind + 1)); // Set new wind value to the players and playerOne.Catapult.Wind = playerTwo.Catapult.Wind = wind.X > 0 ? wind.Y : -wind.Y; changeTurn = false; } } else if (changeTurn && ((isFirstPlayerTurn && GlobalContext.PlayerIsFirstOnAppWarp) || (!isFirstPlayerTurn && !GlobalContext.PlayerIsFirstOnAppWarp))) { // Update wind wind = new Vector2(random.Next(-1, 2), random.Next(minWind, maxWind + 1)); // Set new wind value to the players and playerOne.Catapult.Wind = playerTwo.Catapult.Wind = wind.X > 0 ? wind.Y : -wind.Y; Dictionary <string, object> windProperties = new Dictionary <string, object>(); GlobalContext.tableProperties["WindX"] = wind.X; GlobalContext.tableProperties["WindY"] = wind.Y; windProperties.Add("WindX", wind.X); windProperties.Add("WindY", wind.Y); WarpClient.GetInstance().UpdateRoomProperties(GlobalContext.GameRoomId, windProperties, null); changeTurn = false; } else { wind.X = (float)Convert.ToDouble(GlobalContext.tableProperties["WindX"]); wind.Y = (float)Convert.ToDouble(GlobalContext.tableProperties["WindY"]); playerOne.Catapult.Wind = playerTwo.Catapult.Wind = wind.X > 0 ? wind.Y : -wind.Y; } if (isTwoHumanPlayers) { if (GlobalContext.joinedUsers.Length == 2) { //both the players are on game so we need to update opponent's score from Appwarp server if (GlobalContext.PlayerIsFirstOnAppWarp) { playerTwo.Score = Convert.ToInt32(GlobalContext.tableProperties["Player2Score"]); } else { playerOne.Score = Convert.ToInt32(GlobalContext.tableProperties["Player1Score"]); } } else { /*Game is not being played now,so update both players scores from AppWarp server * It is needed because for e.g. if oppnent left the room when host player score!=0 * then for new opponent we need to reset the score of host player */ playerOne.Health = 100; playerTwo.Health = 100; playerOne.Score = Convert.ToInt32(GlobalContext.tableProperties["Player1Score"]); playerTwo.Score = Convert.ToInt32(GlobalContext.tableProperties["Player2Score"]); } } // Update the players playerOne.Update(e); playerTwo.Update(e); // Updates the clouds position UpdateClouds(elapsed); }
public void Update(GameTimerEventArgs gameTime) { bool startStall; CatapultState postUpdateStateChange = 0; if (gameTime == null) { throw new ArgumentNullException("gameTime"); } // The catapult is inactive, so there is nothing to update if (!IsActive) { // base.Update(gameTime); return; } switch (currentState) { case CatapultState.Idle: // Nothing to do break; case CatapultState.Aiming: if (lastUpdateState != CatapultState.Aiming) { AudioManager.PlaySound("ropeStretch", true); AnimationRunning = true; if (isLeftSide == true && !isHuman) { animations["Aim"].PlayFromFrameIndex(0); stallUpdateCycles = 20; startStall = false; } } // Progress Aiming "animation" if (isHuman) { UpdateAimAccordingToShotStrength(); } else if (isLeftSide && !isHuman) { animations["Aim"].Update(); startStall = AimReachedShotStrength(); currentState = (startStall) ? CatapultState.Stalling : CatapultState.Aiming; } break; case CatapultState.Stalling: if (stallUpdateCycles-- <= 0) { // We've finished stalling, fire the projectile postUpdateStateChange = CatapultState.Firing; } break; case CatapultState.Firing: // Progress Fire animation if (lastUpdateState != CatapultState.Firing) { AudioManager.StopSound("ropeStretch"); AudioManager.PlaySound("catapultFire"); StartFiringFromLastAimPosition(); } animations["Fire"].Update(); // If in the "split" point of the animation start // projectile fire sequence if (animations["Fire"].FrameIndex == splitFrames["Fire"]) { Fire(ShotVelocity, ShotAngle); } if (animations["Fire"].IsActive == false) { postUpdateStateChange = CatapultState.ProjectilesFalling; } break; case CatapultState.ProjectilesFalling: // End turn if all projectiles have been destroyed if (activeProjectiles.Count == 0) { postUpdateStateChange = CatapultState.Reset; } break; case CatapultState.HitDamage: if (animations["hitSmoke"].IsActive == false) { postUpdateStateChange = CatapultState.Reset; } animations["hitSmoke"].Update(); break; case CatapultState.HitKill: // Progress hit animation if ((animations["Destroyed"].IsActive == false) && (animations["hitSmoke"].IsActive == false)) { if (enemy.Score >= winScore) { GameOver = true; break; } self.Health = 100; postUpdateStateChange = CatapultState.Reset; } animations["Destroyed"].Update(); animations["hitSmoke"].Update(); break; case CatapultState.Reset: AnimationRunning = false; break; default: break; } lastUpdateState = currentState; if (postUpdateStateChange != 0) { currentState = postUpdateStateChange; } // Update active projectiles destroyedProjectiles.Clear(); // Clean swap list activeProjectilesCopy.Clear(); // Copy the projectile list so that it may be modified while updating activeProjectilesCopy.AddRange(activeProjectiles); foreach (var projectile in activeProjectilesCopy) { projectile.Update(gameTime); // If the projectile hit the ground if ((projectile.State == ProjectileState.HitGround) && (projectile.HitHandled == false)) { HandleProjectileHit(projectile); } if (projectile.State == ProjectileState.Destroyed) { destroyedProjectiles.Add(projectile); } } // Filter out destroyed projectiles foreach (var projectile in destroyedProjectiles) { activeProjectiles.Remove(projectile); } // Update crate crate.Update(gameTime); }