/// <summary> /// Update method /// </summary> /// <param name="gameTime">game time</param> protected override void Update(TimeSpan gameTime) { this.input = WaveServices.Input; KeyboardState currentKeyboardState = this.input.KeyboardState; if (currentKeyboardState.IsConnected) { if (currentKeyboardState.IsKeyPressed(Keys.O) && this.lastKeyboardState.IsKeyReleased(Keys.O)) { this.RenderManager.DebugLines = !this.RenderManager.DebugLines; } this.lastKeyboardState = currentKeyboardState; } if (this.input.TouchPanelState.IsConnected) { this.touchState = this.input.TouchPanelState; if (this.touchState.Count > 0 && this.mouseJoint == null) { this.TouchPosition = this.touchState[0].Position; this.vsm.ToVirtualPosition(ref this.TouchPosition); foreach (Entity entity in this.Owner.Scene.EntityManager.FindAllByTag("Draggable")) { Collider2D collider = entity.FindComponent <Collider2D>(false); if (collider != null) { if (collider.Contain(TouchPosition)) { RigidBody2D rigidBody = entity.FindComponent <RigidBody2D>(); if (rigidBody != null) { if (rigidBody.PhysicBodyType == WaveEngine.Common.Physics2D.RigidBodyType2D.Dynamic) { this.ConnectedEntity = entity; //Create Joint this.mouseJoint = new MouseJoint2D() { Target = this.TouchPosition, }; this.ConnectedEntity.AddComponent(mouseJoint); break; } } } } } } if (this.touchState.Count == 0 && this.mouseJoint != null) { if (!this.ConnectedEntity.IsDisposed) { this.ConnectedEntity.RemoveComponent(this.mouseJoint); } this.mouseJoint = null; } if (this.mouseJoint != null) { this.TouchPosition = this.touchState[0].Position; this.vsm.ToVirtualPosition(ref this.TouchPosition); this.mouseJoint.Target = this.TouchPosition; } } }
/// <summary> /// Update Method /// </summary> /// <param name="gameTime">Current Game Time</param> protected override void Update(TimeSpan gameTime) { this.input = WaveServices.Input; if (this.input.TouchPanelState.IsConnected) { this.touchState = this.input.TouchPanelState; // Checks Mouse Left Button Click and anyone entity linked if (this.touchState.Count > 0 && this.mouseJoint == null) { // Udpates Mouse Position this.touchPosition = this.touchState[0].Position; foreach (Entity entity in this.Scene.EntityManager.EntityGraph) { Collider2D collider = entity.FindComponent <Collider2D>(false); if (collider != null) { // Collider Test if (collider.Contain(touchPosition)) { RigidBody2D rigidBody = entity.FindComponent <RigidBody2D>(); if (rigidBody != null) { // Forbiden Mouse Joint of Kinematic Bodies if (rigidBody.PhysicBodyType != PhysicBodyType.Kinematic) { this.connectedEntity = entity; // Create Mouse Joint this.mouseJoint = new FixedMouseJoint2D(this.touchPosition); JointMap2D jointMap = this.connectedEntity.FindComponent <JointMap2D>(); jointMap.AddJoint("mouseJoint", this.mouseJoint); // We can break after collider test when true, but we'll miss overlapped entities if Physic entity is // under a non Physic entity. We are breaking here just for sample. break; } } } } } } // Checks Mouse Left Button Release if (this.touchState.Count == 0 && this.mouseJoint != null) { if (!this.connectedEntity.IsDisposed) { // Remove Fixed Joint JointMap2D jointMap2D = this.connectedEntity.FindComponent <JointMap2D>(); jointMap2D.RemoveJoint("mouseJoint"); } this.mouseJoint = null; } // If joint exists then update joint anchor position if (this.mouseJoint != null) { this.touchPosition = this.touchState[0].Position; this.mouseJoint.WorldAnchor = this.touchPosition; } } }
/// <summary> /// Update method /// </summary> /// <param name="gameTime"></param> protected override void Update(TimeSpan gameTime) { this.input = WaveServices.Input; var activeCamera2D = this.RenderManager.ActiveCamera2D; if (this.input.TouchPanelState.IsConnected) { this.touchState = this.input.TouchPanelState; if (this.touchState.Count > 0 && this.mouseJoint == null) { // Gets the virtual screen touch position this.TouchPosition = this.touchState[0].Position; this.vsm.ToVirtualPosition(ref this.TouchPosition); // Launchs a ray from the virtual screen position Ray r; activeCamera2D.CalculateRay(ref this.TouchPosition, out r); this.WorldPosition = r.IntersectionZPlane(0).ToVector2(); /// check collision with DRAGGABLE entities only foreach (Entity entity in this.Owner.Scene.EntityManager.FindAllByTag(GameConstants.TAGDRAGGABLE)) { Collider2D collider = entity.FindComponent <Collider2D>(false); if (collider != null) { if (collider.Contain(this.WorldPosition)) { RigidBody2D rigidBody = entity.FindComponent <RigidBody2D>(); if (rigidBody != null) { if (rigidBody.PhysicBodyType == WaveEngine.Common.Physics2D.RigidBodyType2D.Dynamic) { this.ConnectedEntity = entity; //Create Joint this.mouseJoint = new MouseJoint2D() { Target = this.WorldPosition, }; this.ConnectedEntity.AddComponent(mouseJoint); var audioService = WaveServices.GetService <AudioService>(); audioService.Play(Audio.Sfx.Rubber_wav); break; } } } } } } // Touch just released, remove joint if (this.touchState.Count == 0 && this.mouseJoint != null) { if (!this.ConnectedEntity.IsDisposed) { this.ConnectedEntity.RemoveComponent(this.mouseJoint); var playerComponent = this.ConnectedEntity.FindComponent <PlayerComponent>(); playerComponent.Launch(); } this.mouseJoint = null; } // update touch positions if (this.mouseJoint != null) { this.TouchPosition = this.touchState[0].Position; this.vsm.ToVirtualPosition(ref this.TouchPosition); Ray r; activeCamera2D.CalculateRay(ref this.TouchPosition, out r); this.WorldPosition = r.IntersectionZPlane(0).ToVector2(); this.mouseJoint.Target = this.WorldPosition; } } }