/// <summary> /// Creates all the initial game objects /// </summary> private void CreateGameObjects() { ActiveGameObjects = new List<GameObject>(); int index = 0; foreach (PlayerInfo player in activePlayers) { //Create a Game Object for every active player GameObject playerShip; playerShip = new GameObject(); playerShip.Scale = new Vector3(0.25f, 0.25f, 0.25f); playerShip.Yaw = 1.5f; playerShip.Pitch = 0f; playerShip.Roll = 1.5f; playerShip.Health = 30; playerShip.UpdateRotationByYawPitchRoll(); playerShip.Player_Information = player; playerShip.Name = "Player Ship"; playerShip.shootingSound = soundBank.GetCue("missile"); playerShip.explosionSound = soundBank.GetCue("explosion"); Material shipMaterial = new Material(); shipMaterial.Diffuse = new Vector4(0, 0, 0, 1); shipMaterial.SpecularPower = 10; if (index == 0) { shipMaterial.Specular = Color.Red.ToVector4(); } else { shipMaterial.Specular = Color.Green.ToVector4(); } GeometryNode playerShipNode = new GeometryNode("Player Ship"); playerShipNode.Model = player.Player_Ship.Player_Ship_Model; playerShipNode.Material = shipMaterial; playerShip.Geometry = playerShipNode; playerShipNode.AddToPhysicsEngine = true; playerShipNode.Physics.Shape = ShapeType.Box; scene.RootNode.AddChild(playerShip); ActiveGameObjects.Add(playerShip); if (index == 0) { playerShip.Translation = new Vector3(-25, 20, -100); } else if (index == 1) { playerShip.Translation = new Vector3(25,-25, -100); playerShip.Pitch = 9.5f; playerShip.UpdateRotationByYawPitchRoll(); } index++; } }
//Physics Functions /// <summary> /// Checks for collission between two game objects /// </summary> /// <param name="obj1">Missile</param> /// <param name="obj2">Player Ship</param> /// <returns>True if collission is detected, False otherwise</returns> private bool CheckCollision(GameObject obj1, GameObject obj2) { Vector3 translation = obj2.Translation; Matrix rotate = Matrix.CreateFromYawPitchRoll(obj2.Yaw, obj2.Pitch, obj2.Roll); translation += (rotate.Forward * ((2) * 0.05f)); double distance = getDistance(obj1.Translation.X, obj1.Translation.Y, translation.X, translation.Y); //Console.WriteLine("Distance = " + distance); if (distance > 4) return false; else return true; }
/// <summary> /// Updates the rotation of an object based on it's target position /// </summary> /// <param name="player"></param> /// <param name="targetPosition"></param> private void UpdateRotation(GameObject player, Vector3 targetPosition) { Matrix rotation = Matrix.CreateFromYawPitchRoll(player.Yaw, player.Pitch, player.Roll); Vector3 pos = player.Translation + rotation.Backward; double slope = findSlope(player.Translation.X,player.Translation.Y,pos.X,pos.Y); double slopeDiff = findSlope(player.Translation.X, player.Translation.Y, targetPosition.X, targetPosition.Y); float angleDirection = (float)Math.Atan(slope); angleDirection = MathHelper.ToDegrees(angleDirection); float angleTarget = (float)Math.Atan(slopeDiff); angleTarget = MathHelper.ToDegrees(angleTarget); if (pos.X < player.Translation.X && pos.Y > player.Translation.Y) angleDirection += 180; if (pos.X < player.Translation.X && pos.Y < player.Translation.Y) angleDirection += 180; if (pos.X > player.Translation.X && pos.Y < player.Translation.Y) angleDirection += 360; if (targetPosition.X < player.Translation.X && targetPosition.Y > player.Translation.Y) angleTarget += 180; if (targetPosition.X < player.Translation.X && targetPosition.Y < player.Translation.Y) angleTarget += 180; if (targetPosition.X > player.Translation.X && targetPosition.Y < player.Translation.Y) angleTarget += 360; if (angleTarget < 1) angleTarget += 360; if (angleDirection < 1) angleDirection += 360; if ( Math.Abs(angleDirection - angleTarget) < 5) { player.turnCounter = 0; return; } else { if (player.turnCounter == 60 || player.turnCounter == -60) { player.turnCounter = 0; } if (player.turnCounter > 0) { player.Pitch += 0.1f; player.turnCounter++; } else if ( player.turnCounter < 0) { player.Pitch -= 0.1f; player.turnCounter--; } else if ( Math.Abs(angleDirection - angleTarget) <= 180.0f) { player.Pitch += 0.1f; player.turnCounter++; } else { player.Pitch -= 0.1f; player.turnCounter--; } } player.UpdateRotationByYawPitchRoll(); }
/// <summary> /// Creates a new missle in front of owner with their rotation /// </summary> /// <param name="owner">The owner of the object</param> private void Shoot(GameObject owner) { if (!owner.shootingSound.IsPlaying) owner.shootingSound.Play(); owner.shootingSound = soundBank.GetCue("missile"); GameObject missile = new GameObject(); missile.Rotation = owner.Rotation; missile.Name = "Missile"; missile.Yaw = owner.Yaw; missile.Pitch = owner.Pitch; missile.Roll = owner.Roll; Matrix rotation = Matrix.CreateFromYawPitchRoll(missile.Yaw, missile.Pitch, missile.Roll); missile.Translation = owner.Translation + (rotation.Backward * 6f); missile.Scale = new Vector3(0.025f, 0.025f, 0.025f); missile.Type = GameObjectType.Missle; missile.Player_Information = new PlayerInfo(owner.Player_Information.ToString()); missile.Player_Information.Speed_Level = 7; GeometryNode missileNode = new GeometryNode("Missile"); missileNode.Model = missileModel; missile.Geometry = missileNode; missile.Geometry.AddToPhysicsEngine = true; //missile.Geometry.Physics.Shape = ShapeType.Box; missile.Geometry.Material = owner.Geometry.Material; foreach (GameObject obj in ActiveGameObjects) { if (obj.Name != "Missile") { if (owner.Player_Information.PlayerName != obj.Player_Information.PlayerName) { if (ActiveGameObjects[0].Player_Information.PlayerName == missile.Player_Information.PlayerName) { //Console.WriteLine("Added collision callback player 1"); //AddCollisionCallbackPlayer2(obj, missile); } else if (ActiveGameObjects[1].Player_Information.PlayerName == missile.Player_Information.PlayerName) { //Console.WriteLine("Added collision callback player 2"); //AddCollisionCallbackPlayer1(obj, missile); } } } } ActiveGameObjects.Add(missile); scene.RootNode.AddChild(ActiveGameObjects[ActiveGameObjects.Count - 1]); }
/// <summary> /// Rotates the ship when it goes out of bounds /// </summary> /// <param name="player"></param> private void RotateAnimation(GameObject player) { Matrix rotation = Matrix.CreateFromYawPitchRoll(player.Yaw, player.Pitch, player.Roll); Vector3 pos = player.Translation + rotation.Backward; double slope = findSlope(player.Translation.X, player.Translation.Y, pos.X, pos.Y); float angleDirection = (float)Math.Atan(slope); angleDirection = MathHelper.ToDegrees(angleDirection); /* if (OutOfBounds(player) == true) { angleDirection += 180; player.Pitch += .1f; player.UpdateRotationByYawPitchRoll(); } */ }
/* /// <summary> /// Called whenever a collision occurs /// </summary> /// <param name="pair"></param> private void CollisionOccuredShips(NewtonPhysics.CollisionPair pair) { //ActiveGameObjects[0].Health -= 10 - ActiveGameObjects[0].Player_Information.Armour_Level; //ActiveGameObjects[1].Health -= 10 - ActiveGameObjects[1].Player_Information.Armour_Level; //Console.WriteLine("Collission betwen the Ships!"); } /// <summary> /// Adds a collision callback to a pair of GameObjects /// </summary> /// <param name="ob1">The first object to add to the collision</param> /// <param name="ob2">The second object to add to the collision</param> private void AddCollisionCallbackShips(GameObject ob1, GameObject ob2) { NewtonPhysics.CollisionPair pair = new NewtonPhysics.CollisionPair(ob1.Geometry.Physics, ob2.Geometry.Physics); //((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair, CollisionOccuredShips); } /// <summary> /// Called whenever a collision occurs /// </summary> /// <param name="pair"></param> private void CollisionOccuredPlayer1(NewtonPhysics.CollisionPair pair) { double distance = getDistance(pair.CollisionObject1.PhysicsWorldTransform.Translation.X, pair.CollisionObject1.PhysicsWorldTransform.Translation.Y, pair.CollisionObject2.PhysicsWorldTransform.Translation.X, pair.CollisionObject2.PhysicsWorldTransform.Translation.Y); Console.WriteLine("Distance = " + distance); if (distance > 8) return; Console.WriteLine("Hit!"); explosionSound.Play(); explosionSound = soundBank.GetCue("explosion"); scene.PhysicsEngine.RemovePhysicsObject(pair.CollisionObject2); ActiveGameObjects[0].Health -= 10 - ActiveGameObjects[0].Player_Information.Armour_Level; if (ActiveGameObjects[0].Health <= 0) { if (session != null) { if (session.IsHost) { SendGameOver(ActiveGameObjects[1].Player_Information.PlayerName); session.EndGame(); session.Update(); } } } Console.WriteLine("Player 1 Hit! Health is at " + ActiveGameObjects[0].Health); player1_hud.AddMessage(activePlayers[0].PlayerName + " was hit!"); } /// <summary> /// Adds a collision callback to a pair of GameObjects /// </summary> /// <param name="ob1">The first object to add to the collision</param> /// <param name="ob2">The second object to add to the collision</param> private void AddCollisionCallbackPlayer1(GameObject ob1, GameObject ob2) { NewtonPhysics.CollisionPair pair = new NewtonPhysics.CollisionPair(ob1.Geometry.Physics, ob2.Geometry.Physics); ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair, CollisionOccuredPlayer1); collisionPairsPlayer1.Add(pair); } /// <summary> /// Called whenever a collision occurs /// </summary> /// <param name="pair"></param> private void CollisionOccuredPlayer2(NewtonPhysics.CollisionPair pair) { double distance = getDistance(pair.CollisionObject1.PhysicsWorldTransform.Translation.X, pair.CollisionObject1.PhysicsWorldTransform.Translation.Y, pair.CollisionObject2.PhysicsWorldTransform.Translation.X, pair.CollisionObject2.PhysicsWorldTransform.Translation.Y); Console.WriteLine("Distance = " + distance); if (distance > 8) return; Console.WriteLine("Hit!"); explosionSound.Play(); explosionSound = soundBank.GetCue("explosion"); scene.PhysicsEngine.RemovePhysicsObject(pair.CollisionObject2); ActiveGameObjects[1].Health -= 10 - ActiveGameObjects[1].Player_Information.Armour_Level; if (ActiveGameObjects[1].Health <= 0) { if (session != null) { if (session.IsHost) { SendGameOver(ActiveGameObjects[0].Player_Information.PlayerName); session.EndGame(); session.Update(); } } } Console.WriteLine("Player 2 Hit! Health is at " + ActiveGameObjects[1].Health); player1_hud.AddMessage(activePlayers[1].PlayerName + " was hit!"); } /// <summary> /// Adds a collision callback to a pair of GameObjects /// </summary> /// <param name="ob1">The first object to add to the collision</param> /// <param name="ob2">The second object to add to the collision</param> private void AddCollisionCallbackPlayer2(GameObject ob1, GameObject ob2) { NewtonPhysics.CollisionPair pair = new NewtonPhysics.CollisionPair(ob1.Geometry.Physics, ob2.Geometry.Physics); ((NewtonPhysics)scene.PhysicsEngine).AddCollisionCallback(pair, CollisionOccuredPlayer2); collisionPairsPlayer2.Add(pair); } */ //********************Game Logic Functions********************************// /// <summary> /// Checks to see if a GameObject is out of bounds /// </summary> /// <param name="player">The GameObjecct to be checked</param> /// <returns>True if the object is out of bounds, false otherwise</returns> private bool OutOfBounds(GameObject player) { if (player.Translation.X > 38 || player.Translation.X < -38) { return true; } else if (player.Translation.Y > 22 || player.Translation.Y < -34) { return true; } else { return false; } }