protected override void Update(GameTime gameTime) { networkHelper.Update(); KeyboardState keyboardState = Keyboard.GetState(); if (keyboardState.IsKeyDown(Keys.Escape)) // If ESC is pressed { this.Exit(); // Exit the game } if (!networkHelper.gameStart) { if (keyboardState.IsKeyDown(Keys.F1)) { networkHelper.SignInGamer(); } if (keyboardState.IsKeyDown(Keys.F2)) { networkHelper.CreateSession(); Host = true; camera = new Camera(this, hostPos); Components.Add(camera); // Add camera to components Player2 = new Vector3(100, 100, 100); playerModel = new BasicModel(Content.Load <Model>("spaceship")); } if (keyboardState.IsKeyDown(Keys.F3)) { networkHelper.FindSession(); Guest = true; camera = new Camera(this, guestPos); Components.Add(camera); // Add camera to components Player2 = new Vector3(100, 100, 100); playerModel = new BasicModel(Content.Load <Model>("spaceship")); } if (keyboardState.IsKeyDown(Keys.F4)) { networkHelper.AsyncFindSession(); Guest = true; camera = new Camera(this, guestPos); Components.Add(camera); // Add camera to components Player2 = new Vector3(100, 100, 100); playerModel = new BasicModel(Content.Load <Model>("spaceship")); } if (Mouse.GetState().LeftButton == ButtonState.Pressed && (gameTime.TotalGameTime.TotalSeconds - shotTimer) >= 1) { shotTimer = gameTime.TotalGameTime.TotalSeconds; bulletPos.Add(camera.cameraPosition); bulletVel.Add(camera.forward); x = true; } if (bulletPos.Count > 0) { bulletModel.Update(bulletPos[0]); } playerModel.Update(Player2); jumpPadModel1.Update(jumpPadLocation1); if (camera.angle != pAngle) { Vector3 newgunPosition = gunPosition - camera.cameraPosition; newgunPosition = Vector3.Transform(newgunPosition, Matrix.CreateRotationY(pAngle.Y - camera.angle.Y)); gunPosition = newgunPosition + camera.cameraPosition; } if (pAngle.X != camera.angle.X) { float dotprod, angle, degree; Vector3 tempAngle = gunPosition - camera.cameraPosition; tempAngle = Vector3.Transform(tempAngle, Matrix.CreateRotationY(0 - camera.angle.Y)); dotprod = Vector3.Dot(camera.angle, tempAngle); angle = (float)Math.Acos(dotprod); degree = angle * 180 / MathHelper.Pi; Console.WriteLine(degree); tempAngle = Vector3.Transform(tempAngle, Matrix.CreateRotationX(pAngle.X - camera.angle.X)); tempAngle = Vector3.Transform(tempAngle, Matrix.CreateRotationY(camera.angle.Y - 0)); tempAngle = tempAngle + camera.cameraPosition; } /*gunPosition = camera.cameraPosition; * gunPosition.Z -= 11; * gunPosition.X += 6; * gunPosition.Y -= 2;*/ gunPosition.Y = camera.cameraPosition.Y - 2; //gunPosition.X = camera.cameraPosition.X + 6; //gunPosition.Z = camera.cameraPosition.Z - 11; gun.Update(gunPosition); pAngle = camera.angle; } else { if (Mouse.GetState().LeftButton == ButtonState.Pressed && (gameTime.TotalGameTime.TotalSeconds - shotTimer) >= 1) { shotTimer = gameTime.TotalGameTime.TotalSeconds; bulletPos.Add(camera.cameraPosition); bulletVel.Add(camera.forward); x = true; } if (bulletPos.Count > 0) { bulletModel.Update(bulletPos[0]); } jumpPadModel1.Update(jumpPadLocation1); if (networkHelper.p2hit) { ++p2kills; ++p1deaths; if (Host) { camera.cameraPosition = hostPos; } else { camera.cameraPosition = guestPos; } } } if (networkHelper.SessionState == NetworkSessionState.Playing) { // Send any key pressed to the remote player networkHelper.SendMessage(camera.cameraPosition); // Recieve the keys from the remote player networkHelper.ReceiveMessage(); Player2 = networkHelper.Player2; playerModel.rot = networkHelper.rot; playerModel.Update(Player2); } bulletMove(gameTime); BulletCollision(); //check collision PlayerCollision(); if (camera.gameStart != networkHelper.gameStart) { camera.gameStart = networkHelper.gameStart; } base.Update(gameTime); }
public override void Update(GameTime gameTime) { networkHelper.Update(); if (gameStart) { Console.WriteLine(speed); raymove.Position = cameraPosition; // Set the ray positions to the camera position rayfall.Position = cameraPosition; if (jumping) { cameraPosition.Y += jump; jump -= .08f; if (jump <= -2f) { jumping = false; } } if (jumpPad) { cameraPosition -= jumpPadDirection1 * jumpPadSpeed * delta; cameraPosition.Y -= jumpPading; if (jumpPading >= -5f && jumpPadinc) { jumpPading -= .08f; } else { jumpPadinc = false; jumpPading = 0f; } } if (Game1.terrain.Intersects(rayfall) > 20) { // If the ray intersect distance is greater than 20... cameraPosition += fallingvel; // Move the camera down if (fallingvel.Y > maxfallvel.Y) { fallingvel *= 1.1f; // Increase falling speed } } else { // If the ray intersect distance is less than 20... fallingvel = new Vector3(0, -.03f, 0); // Reset gravity jumping = false; // No longer jumping jumpPad = false; heightmax = .45f; // Reset maximum height climbable jumpPading = 0f; } delta = (float)gameTime.ElapsedGameTime.TotalSeconds; MouseState mouse = Mouse.GetState(); KeyboardState kbs = Keyboard.GetState(); int centerX = Game.Window.ClientBounds.Width / 2; int centerY = Game.Window.ClientBounds.Width / 2; Mouse.SetPosition(centerX, centerY); angle.X += MathHelper.ToRadians((mouse.Y - centerY) * speed2 * 0.01f); angle.Y += MathHelper.ToRadians((mouse.X - centerX) * speed2 * 0.01f); forward = Vector3.Normalize(new Vector3((float)Math.Sin(-angle.Y), (float)Math.Sin(angle.X), (float)Math.Cos(-angle.Y))); Vector3 left = Vector3.Normalize(new Vector3((float)Math.Cos(angle.Y), 0f, (float)Math.Sin(angle.Y))); if (!jumpPad) { Console.WriteLine(cameraPosition); if (kbs.IsKeyDown(Keys.W)) { // When W is pressed go forward height = Game1.terrain.getHeight(cameraPosition); Vector3 newcameraPosition = cameraPosition; Vector3 newcameraDirection = forward; newcameraDirection.Y = 0; newcameraPosition -= newcameraDirection * speed * delta; float newheight = Game1.terrain.getHeight(newcameraPosition); height = newheight - height; if (Keyboard.GetState().IsKeyDown(Keys.LeftShift) && oldkeyboard.IsKeyDown(Keys.LeftShift)) { cameraPosition -= newcameraDirection * (speed * 1.7f) * delta; } else { cameraPosition -= newcameraDirection * speed * delta; } if (!jumping && height > 0) { cameraPosition.Y += height; } } if (kbs.IsKeyDown(Keys.S)) { // When S is pressed go forward height = Game1.terrain.getHeight(cameraPosition); Vector3 newcameraPosition = cameraPosition; Vector3 newcameraDirection = forward; newcameraDirection.Y = 0; newcameraPosition += newcameraDirection * speed * delta; float newheight = Game1.terrain.getHeight(newcameraPosition); height = newheight - height; cameraPosition += newcameraDirection * speed * delta; if (!jumping) { cameraPosition.Y += height; } } if (kbs.IsKeyDown(Keys.A)) { // When A is pressed go forward height = Game1.terrain.getHeight(cameraPosition); Vector3 newcameraPosition = cameraPosition; Vector3 newcameraDirection = left; newcameraDirection.Y = 0; newcameraPosition -= newcameraDirection * speed * delta; float newheight = Game1.terrain.getHeight(newcameraPosition); height = newheight - height; cameraPosition -= left * speed * delta; if (!jumping) { cameraPosition.Y += height; } } if (kbs.IsKeyDown(Keys.D)) { // When D is pressed go forward height = Game1.terrain.getHeight(cameraPosition); Vector3 newcameraPosition = cameraPosition; Vector3 newcameraDirection = left; newcameraDirection.Y = 0; newcameraPosition += newcameraDirection * speed * delta; float newheight = Game1.terrain.getHeight(newcameraPosition); height = newheight - height; cameraPosition += newcameraDirection * speed * delta; if (!jumping) { cameraPosition.Y += height; } } if (Keyboard.GetState().IsKeyDown(Keys.Space)) { // If Space Bar is pressed... timeSinceLastJump = gameTime.TotalGameTime.TotalSeconds; speed *= 1.01f; if (speed >= 130) //cap speed at 130 { speed = 130; } if (!jumping) { // If not already jumping... jump = 2f; // Increase jumping variable heightmax *= 2; // Increase the maximum height climeable jumping = true; // Set it to now jumping } } if (!jumping && (gameTime.TotalGameTime.TotalSeconds - timeSinceLastJump) >= 1) { //cameraPosition.Y += height; speed = 50f; timeSinceLastJump = 0; } } view = Matrix.Identity; view *= Matrix.CreateTranslation(-cameraPosition); view *= Matrix.CreateRotationZ(angle.Z); view *= Matrix.CreateRotationY(angle.Y); view *= Matrix.CreateRotationX(angle.X); cameraDirection = Vector3.Transform(cameraDirection, Matrix.CreateFromAxisAngle(Vector3.Up, (-MathHelper.PiOver4 / 150) * (mouse.X - centerX))); cameraDirection = Vector3.Transform(cameraDirection, // Update camera Direction Matrix.CreateFromAxisAngle(Vector3.Cross(Vector3.Up, cameraDirection), (MathHelper.PiOver4 / 150) * (Mouse.GetState().Y - centerY))); cameraDirection.Normalize(); oldkeyboard = Keyboard.GetState(); } base.Update(gameTime); }