/// <summary> /// Updates Main Gameplay Loop code here, this is affected by whether or not the scene is paused. /// </summary> /// <param name="gameTime"></param> /// <param name="otherScreenHasFocus"></param> /// <param name="coveredByOtherScreen"></param> public override void UpdateScene(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { character.Update((float)gameTime.ElapsedGameTime.TotalSeconds, vxEngine.InputManager.PreviousKeyboardState, vxEngine.InputManager.KeyboardState, vxEngine.InputManager.PreviousGamePadState, vxEngine.InputManager.GamePadState); if (this.IsActive) { vxEngine.InputManager.ShowCursor = false; } else { vxEngine.InputManager.ShowCursor = true; } //Update grabber if (vxEngine.InputManager.MouseState.RightButton == ButtonState.Pressed && !grabber.IsGrabbing) { //Find the earliest ray hit RayCastResult raycastResult; if (BEPUPhyicsSpace.RayCast(new Ray(Camera.Position, Camera.WorldMatrix.Forward), 500, rayCastFilter, out raycastResult)) { var entityCollision = raycastResult.HitObject as EntityCollidable; //If there's a valid ray hit, then grab the connected object! if (entityCollision != null && entityCollision.Entity.IsDynamic) { Console.WriteLine("GRABBING ITEM: {0}", entityCollision.Entity.GetType().ToString()); grabber.Setup(entityCollision.Entity, raycastResult.HitData.Location); //grabberGraphic.IsDrawing = true; grabDistance = raycastResult.HitData.T; } } } if (vxEngine.InputManager.MouseState.RightButton == ButtonState.Pressed && grabber.IsUpdating) { if (grabDistance < 4) { grabDistance = 3; grabber.GoalPosition = Camera.Position + Camera.WorldMatrix.Forward * grabDistance; } } else if (vxEngine.InputManager.MouseState.RightButton == ButtonState.Released && grabber.IsUpdating) { grabber.Release(); } base.UpdateScene(gameTime, otherScreenHasFocus, coveredByOtherScreen); }
/// <summary> /// Updates Main Gameplay Loop code here, this is affected by whether or not the scene is paused. /// </summary> /// <param name="gameTime"></param> /// <param name="otherScreenHasFocus"></param> /// <param name="coveredByOtherScreen"></param> public override void UpdateScene(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { ParentEntityPlaceHolder = null; if (vxEngine.InputManager.IsNewMouseButtonPress(MouseButtons.MiddleButton)) { vxEngine.Mouse_ClickPos = new Vector2(vxEngine.InputManager.MouseState.X, vxEngine.InputManager.MouseState.Y); } if (IsActive) { //If it's in Testing Mode, Update the Vehicle and Chase Camera if (SandboxGameState == vxEnumSandboxGameState.Running) { //drop it out of view workingPlane.Position = OutofSight; } //Update If In Edit Mode if (SandboxGameState == vxEnumSandboxGameState.EditMode) { MouseWorld = Matrix.Identity; //Reset to Negative One each loop Index = -1; AddMode = vxEnumAddMode.OnPlane; Ray Ray_Mouse = vxGeometryHelper.CalculateCursorRay(vxEngine, Camera.Projection, Camera.View); if (!grabber.IsGrabbing) { Cursor.Update(gameTime, Ray_Mouse); if (Cursor.IsMouseHovering == false) { //Next Find the earliest ray hit RayCastResult raycastResult; if (BEPUPhyicsSpace.RayCast(Ray_Mouse, 5000, rayCastFilter, out raycastResult)) { var entityCollision = raycastResult.HitObject as EntityCollidable; //Set the Index of the Highlited Item if (raycastResult.HitObject.Tag != null) { if (raycastResult.HitObject.Tag.GetType() == typeof(int)) { //Unselect The Previous Selection if (Index > -1) { if (Items[Index] != null) { Items[Index].SelectionState = vxEnumSelectionState.Unseleced; } } Vector3 pnt = raycastResult.HitData.Location; Vector3 nrml = raycastResult.HitData.Normal; nrml.Normalize(); //Get Index of Currently Selected Item Index = Convert.ToInt32(raycastResult.HitObject.Tag); if (Index < Items.Count) { if (Items[Index] != null) { Items[Index].SelectionState = vxEnumSelectionState.Hover; if (Items[Index].GetType() == typeof(vxSnapBox)) { if (temp_part != null && entityCollision != null) { temp_part.SetMesh(entityCollision.WorldTransform.Matrix, false, false); ParentEntityPlaceHolder = Items[Index]; Index = -2; vxDebugShapeRenderer.AddBoundingBox(raycastResult.HitObject.BoundingBox, Color.HotPink); } } else { if (temp_part != null && IsSurface(Items[Index])) { vxDebugShapeRenderer.AddBoundingSphere( new BoundingSphere(pnt, 1), Color.Blue); vxDebugShapeRenderer.AddLine( pnt, pnt + 5 * nrml, Color.LimeGreen); Vector3 fwd = Vector3.Cross(Vector3.UnitX + Vector3.UnitZ, nrml); fwd.Normalize(); AddMode = vxEnumAddMode.OnSurface; MouseWorld = Matrix.CreateWorld(pnt, fwd, nrml); } } } } } } } } } //If Index still equals -1, then it isn't over any elements, and a new element can be added. int_intersc_previous = int_intersc; if (SandboxGameState == vxEnumSandboxGameState.EditMode) { if (Ray_Mouse.Intersects(workingPlane.WrknPlane) != null) { Vector3 intersection = (float)Ray_Mouse.Intersects(workingPlane.WrknPlane) * Ray_Mouse.Direction + Ray_Mouse.Position; int_intersc = new Vector3((int)intersection.X, (int)intersection.Y, (int)intersection.Z); } if (temp_part != null && Index > -2) { if (AddMode == vxEnumAddMode.OnSurface && temp_part.CanBePlacedOnSurface == true) { temp_part.SetMesh(MouseWorld, false, false); } else { temp_part.Position = int_intersc; temp_part.SetMesh(false, false); } } } else { //Get it WAYYYY out of the scene int_intersc = OutofSight; } // Update Cursor //********************************************************** Vector3 CursorAverage = Vector3.Zero; for (int ind = 0; ind < SelectedItems.Count; ind++) { SelectedItems[ind].SelectionState = vxEnumSelectionState.Selected; CursorAverage += SelectedItems[ind].World.Translation; } //Only Set the Cursor Position if the Mouse is Up, otherwise, the cursor sets the entity positions if (SelectedItems.Count > 0 && vxEngine.InputManager.MouseState.LeftButton == ButtonState.Released) { CursorAverage /= SelectedItems.Count; Cursor.Position = CursorAverage; } } if (vxEngine.InputManager.MouseState.MiddleButton == ButtonState.Pressed) { Mouse.SetPosition((int)vxEngine.Mouse_ClickPos.X, (int)vxEngine.Mouse_ClickPos.Y); } } base.UpdateScene(gameTime, otherScreenHasFocus, coveredByOtherScreen); }