public override ProgrammingElement Clone() { TouchGestureFilter clone = new TouchGestureFilter(); CopyTo(clone); return(clone); }
public override bool MatchAction(Reflex reflex, out object param) { bool result = false; param = null; TouchGestureFilter touchFilt = reflex.Data.GetFilterByType(typeof(TouchGestureFilter)) as TouchGestureFilter; if (touchFilt != null) { float rotation = touchFilt.DeltaRotation; for (int i = 0; i < reflex.Filters.Count; ++i) { Filter filter = reflex.Filters[i]; if (!(filter is RotationFilter)) { continue; } RotationFilter rotateFilt = filter as RotationFilter; // does the rotation direction violate any of the rotation filters? if (!rotateFilt.IsRotationValid(rotation)) { return(false); } } // If we've gotten here we're good. The rotation is in the // right direction for the filter so return true. result = true; } return(result); }
public override void ComposeSensorTargetSet(GameActor gameActor, Reflex reflex) { if (terrainSensor != null) { terrainSensor.ComposeSensorTargetSet(gameActor, reflex); } else { List <Filter> filters = reflex.Filters; // The sensor will use the first TouchGestureFilter it finds. Since any given reflex // can only have a single one of these, this works well. TouchGestureFilter gestureFilter = null; TouchButtonFilter buttonFilter = null; GUIButtonFilter guiButtonFilter = null; for (int i = 0; i < filters.Count; i++) { if (filters[i] is TouchGestureFilter) { Debug.Assert(null == gestureFilter); gestureFilter = filters[i] as TouchGestureFilter; } if (filters[i] is TouchButtonFilter) { Debug.Assert(null == buttonFilter); buttonFilter = filters[i] as TouchButtonFilter; } if (filters[i] is GUIButtonFilter) { Debug.Assert(null == guiButtonFilter); guiButtonFilter = filters[i] as GUIButtonFilter; } } //if we don't have a filter, do nothing if (null == gestureFilter && null == buttonFilter && null == guiButtonFilter) { //Debug.Assert(false, "Did not find a touch filter for touch sensor."); return; } // Cause the filter to detect what is under the touch cursor and set that information on the reflex. // MatchAction will be true if the TouchFilter passes but then we still need to filter on any other // filters including the ever annoying "not", "me" and "anything" filters. Object param = null; bool matchAction = false; bool notFilter = reflex.Data.FilterExists("filter.not"); bool anythingFilter = reflex.Data.FilterExists("filter.anything"); if (gestureFilter != null) { matchAction = gestureFilter.MatchAction(reflex, out param); } if (buttonFilter != null) { // Make the actual on screen button visible if a reflex contains the touch button filter. //TouchButtons.MakeVisible(buttonFilter.button); matchAction = buttonFilter.MatchAction(reflex, out param); } if (guiButtonFilter != null) { matchAction = guiButtonFilter.MatchAction(reflex, out param); } // Give the anythingFilter a chance to kill the action. if (anythingFilter && reflex.TouchActor == null) { matchAction = false; } // Handle the "Not" filter if (notFilter) { if (matchAction) { matchAction = false; reflex.TouchActor = null; reflex.TouchPosition = null; } else { matchAction = true; } } bool touchCursorTarget = false; // We are moving towards a remembered target position if we're the movement actuator. // We do not require a match on the current frame to continue the movement. // Note, in the presence of a "Not" filter, there is no target to "not move" to, so this // is negated. // Also, support turning toward a remembered position. bool movingToTouchPos = (reflex.actuatorUpid == "actuator.movement" && !notFilter); movingToTouchPos |= (reflex.actuatorUpid == "actuator.turn" && !notFilter); if (movingToTouchPos && (reflex.TouchActor == reflex.Task.GameActor)) { // When moving with touch, going towards oneself doesn't make sense. By discarding // the touch actor and using the terrain position, we have a better definition of this // movement. This allows for small positional corrections around the target's bounding // box as well as letting slide movements that begin on oneself to work. reflex.TouchActor = null; reflex.TouchPosition = TouchEdit.HitInfo.TerrainPosition; } if (matchAction || movingToTouchPos) { bool validTarget = false; SensorTarget target = SensorTargetSpares.Alloc(); // If we have an actor touch target, check if it passes the filters. // If not, null it out. if (reflex.TouchActor != null) { // Create a target. target.Init(reflex.Task.GameActor, reflex.TouchActor); validTarget = true; } else { target.Init(reflex.Task.GameActor, reflex.Task.GameActor); // If we've got a me filter and not TouchActor we've failed. if (reflex.Data.FilterExists("filter.me")) { matchAction = false; } // NOTE: For classification purposes, if the user doesn't touch a particular actor, // we create an 'empty' classification. This is because we need a classification object // in order to do reflexes such as 'tap move blimp -> shoot'. If you don't tap an actor, // it needs to compare the absence of an actor to the blimp. if (reflex.TouchPosition != null) { // We need to add the touch position to the targetSet. target.Classification = new Classification(); target.GameThing = senseSet.Nearest.GameThing; target.Position = reflex.TouchPosition.Value; target.Direction = target.Position - reflex.Task.GameActor.Movement.Position; target.Range = target.Direction.Length(); // If we've arrived at the target position, clear MousePosition. Vector2 dir2d = new Vector2(target.Direction.X, target.Direction.Y); if (dir2d.LengthSquared() < 0.1f) { reflex.TouchPosition = null; } target.Direction /= target.Range; touchCursorTarget = true; validTarget = true; } else { // We don't have an actor or a position. Only with an action match should we // create a sensor target. if (matchAction) { // Calc a fake position for this target. GameActor actor = reflex.Task.GameActor; Vector3 pos = actor.Movement.Position; target.Classification = new Classification(); target.GameThing = senseSet.Nearest.GameThing; target.Position = pos; target.Direction = target.Position - reflex.Task.GameActor.Movement.Position; target.Range = target.Direction.Length(); target.Direction /= target.Range; validTarget = true; } } } // Test the target against the filters. if (validTarget) { for (int i = 0; i < filters.Count; i++) { if (!(filters[i] is TouchGestureFilter)) { if (!filters[i].MatchTarget(reflex, target) || !filters[i].MatchAction(reflex, out param)) { if (!notFilter) { // Failed a match on one of the filters and we don't have a negation // so null things out and short circuit. reflex.TouchActor = null; validTarget = false; matchAction = false; touchCursorTarget = false; break; } } } } } // If we still have a target, add it to the target set. if (validTarget) { reflex.targetSet.Add(target); } else { // Don't really need this target so free it. SensorTargetSpares.Free(target); } } reflex.targetSet.Action = reflex.targetSet.Count > 0; // && TestObjectSet(reflex); //reflex.targetSet.Action |= matchAction; // This forces the movement to keep going toward the clicked position/bot // even if the touch is no longer clicked. reflex.targetSet.ActionMouseTarget = touchCursorTarget; if (matchAction) { bool postProcessCheck = PostProcessAction(matchAction, reflex); if (!postProcessCheck) { // We matched the action, but it failed the post process check (used by "Once" modifier), // so we clear the target set. reflex.targetSet.Clear(); } } } }
public override ActionSet ComposeActionSet(Reflex reflex, GameActor gameActor) { ClearActionSet(actionSet); //UpdateCanBlend(reflex); if (!reflex.targetSet.AnyAction) { return(actionSet); } TurnModifier turnMod = reflex.GetModifierByType(typeof(TurnModifier)) as TurnModifier; GamePadStickFilter stickFilt = reflex.Data.GetFilterByType(typeof(GamePadStickFilter)) as GamePadStickFilter; GamePadTriggerFilter triggerFilt = reflex.Data.GetFilterByType(typeof(GamePadTriggerFilter)) as GamePadTriggerFilter; MouseFilter mouseFilt = reflex.Data.GetFilterByType(typeof(MouseFilter)) as MouseFilter; TouchGestureFilter touchFilt = reflex.Data.GetFilterByType(typeof(TouchGestureFilter)) as TouchGestureFilter; KeyBoardKeyFilter keyBoardKeyFilt = reflex.Data.GetFilterByType(typeof(KeyBoardKeyFilter)) as KeyBoardKeyFilter; // Will be modified depending on stick or trigger position. float speedModifier = 1.0f; // // Start by looking at the input filters to attenuate speed. // if (stickFilt != null) { // Note that we take the sign into account further below. // For now, just get the magnitude. speedModifier *= (float)Math.Abs(stickFilt.stickPosition.X); } else if (triggerFilt != null) { speedModifier *= triggerFilt.triggerValue; } else if (mouseFilt != null) { // Note that we take the sign into account further below. // For now, just get the magnitude. speedModifier *= Math.Abs(mouseFilt.ScreenPosition.X); } else if (touchFilt != null) { if (touchFilt.type == TouchGestureFilterType.Rotate) { reflex.Task.GameActor.Chassis.WasTouchRotated = true; } } // Decide if we're turning toward a particular direction or // setting a turn rate. Note, this will miss some cases // that we clean up further down. The failure is rooted // in TurnModifier if you want to go fix it. :-) Vector3 desiredHeading = Vector3.Zero; float targetHeading = 0; // See if the modifiers want to give us an absolute direction. reflex.ModifyHeading(gameActor, Modifier.ReferenceFrames.World, ref desiredHeading); targetHeading = MyMath.ZRotationFromDirection(desiredHeading); // If we have a desiredHeading, use that, else assume we're just turning. if (desiredHeading != Vector3.Zero) { // We were given an absolute direction, use it. actionSet.AddActionTarget(Action.AllocHeadingAction(reflex, targetHeading, speedModifier)); } else { // Determine which relative direction we should use. TurnModifier.TurnDirections direction = TurnModifier.TurnDirections.None; if (turnMod != null) { // If a turn modifier was specified, use its direction. direction = turnMod.direction; } else if (stickFilt != null) { // No turn modifier but we have a gamepad stick, use it to determine turn direction. direction = stickFilt.stickPosition.X > 0 ? TurnModifier.TurnDirections.Right : TurnModifier.TurnDirections.Left; } else if (triggerFilt != null) { // No turn modifier but we have a gamepad trigger, use it to determine turn direction. // Basically this automatically maps left trigger to left turn and right trigger to right turn. direction = triggerFilt.trigger == GamePadTriggerFilter.GamePadTrigger.LeftTrigger ? TurnModifier.TurnDirections.Left : TurnModifier.TurnDirections.Right; } else if (mouseFilt != null) { // No turn modifier but we do have mouse input. direction = mouseFilt.ScreenPosition.X > 0 ? TurnModifier.TurnDirections.Right : TurnModifier.TurnDirections.Left; // This scaling by 2 has no real justification except // that it matches the magnitude of the original code // so this is more compatible with older games. speedModifier *= 2.0f; } else if (touchFilt != null) { // No turn modifier but we do have touch input. if (touchFilt.type == TouchGestureFilterType.Rotate) { if (touchFilt.DeltaRotation < 0) { direction = TurnModifier.TurnDirections.Left; } else if (touchFilt.DeltaRotation > 0) { direction = TurnModifier.TurnDirections.Right; } } else if (touchFilt.type == TouchGestureFilterType.Tap) { direction = TurnModifier.TurnDirections.Toward; } } else if (keyBoardKeyFilt != null) { if (reflex.targetSet != null) { Vector2 dir = (Vector2)(reflex.targetSet.Param); if (dir.X == 1) { direction = TurnModifier.TurnDirections.Right; } else if (dir.X == -1) { direction = TurnModifier.TurnDirections.Left; } } } else { // Nothing exists that would help us decide which way we should turn, choose left. // This could be the user programming WHEN DO Turn direction = TurnModifier.TurnDirections.Left; } // Calculate the new direction we want to face. switch (direction) { // Turn to our left. case TurnModifier.TurnDirections.Left: { float angle = 0.0f; if (touchFilt != null && touchFilt.type == TouchGestureFilterType.Rotate) { angle = gameActor.Movement.RotationZ + Math.Abs(touchFilt.DeltaRotation); } else { angle = gameActor.Movement.RotationZ + MathHelper.PiOver2; } desiredHeading = new Vector3((float)Math.Cos(angle), (float)Math.Sin(angle), 0); actionSet.AddActionTarget(Action.AllocTurnSpeedAction(reflex, speedModifier), 0); } break; // Turn to our right. case TurnModifier.TurnDirections.Right: { float angle = 0.0f; if (touchFilt != null && touchFilt.type == TouchGestureFilterType.Rotate) { angle = gameActor.Movement.RotationZ - Math.Abs(touchFilt.DeltaRotation); } else { angle = gameActor.Movement.RotationZ - MathHelper.PiOver2; } desiredHeading = new Vector3((float)Math.Cos(angle), (float)Math.Sin(angle), 0); actionSet.AddActionTarget(Action.AllocTurnSpeedAction(reflex, -speedModifier), 0); } break; // Turn to face the direction we are moving. case TurnModifier.TurnDirections.Forward: { float angle = MyMath.ZRotationFromDirection(gameActor.Movement.Velocity); desiredHeading = new Vector3((float)Math.Cos(angle), (float)Math.Sin(angle), 0); actionSet.AddActionTarget(Action.AllocHeadingAction(reflex, targetHeading, speedModifier)); } break; // Turn to face the sensed object. case TurnModifier.TurnDirections.Toward: // We're probably here because of WHEN Mouse Left DO Turn Toward. // This is another case where we want a Heading Action. if (reflex.targetSet.Nearest != null) { speedModifier = 1; // If we're doing mouse-look (WHEN Mouse Over DO Turn Toward) try // and create a bit of a dead-zone in the middle. bool mouseOver = false; foreach (Filter f in reflex.Filters) { MouseFilter mf = f as MouseFilter; if (mf != null && mf.type == MouseFilterType.Hover) { mouseOver = true; break; } } desiredHeading = reflex.targetSet.Nearest.Direction; targetHeading = MyMath.ZRotationFromDirection(desiredHeading); // If mouseOver, calc damping to slow down turning when already looking // in the direction we want. This helps make it less twitchy. float dampingFactor = 1.0f; if (mouseOver) { float dot = Vector3.Dot(desiredHeading, gameActor.Movement.Heading); float deadzoneAngle = 0.96f; float dampzoneAngle = 0.9f; // If very close to center, just treat as zero. if (dot > deadzoneAngle) { dampingFactor = 0; } else if (dot > dampzoneAngle) { // Not in center, but close so damp turning. // The closer we are to already looking in the correct direction, increase damping. dampingFactor = MyMath.RemapRange(dot, deadzoneAngle, dampzoneAngle, 0, 1); // Smooth the result to flatten out the center. dampingFactor = MyMath.SmoothStep(0, 1, dampingFactor); } } actionSet.AddActionTarget(Action.AllocHeadingAction(reflex, targetHeading, dampingFactor * speedModifier)); } break; } } return(actionSet); } // end of ComposeActionSet()
protected void CopyTo(TouchGestureFilter clone) { base.CopyTo(clone); clone.type = this.type; }
public override bool MatchAction(Reflex reflex, out object param) { bool result = false; param = null; GamePadStickFilter stickFilt = reflex.Data.GetFilterByType(typeof(GamePadStickFilter)) as GamePadStickFilter; TouchGestureFilter touchFilt = reflex.Data.GetFilterByType(typeof(TouchGestureFilter)) as TouchGestureFilter; int directFiltCount = 0; Directions combinedDirection = 0; if (stickFilt != null) { Vector2 stick = stickFilt.stickPosition; stick.Normalize(); for (int i = 0; i < reflex.Filters.Count; ++i) { Filter filter = reflex.Filters[i]; if (!(filter is DirectionFilter)) { continue; } DirectionFilter directFilt = filter as DirectionFilter; // The first filter will handle all direction filters present, so if we are not // the first filter, just bail with a "true" result. if (directFiltCount > 0 && directFilt == this) { return(true); } // Is the stick in the correct quadrant for this direction? if (!directFilt.IsStickPositionValid(stick)) { return(false); } combinedDirection |= directFilt.direction; directFiltCount += 1; } // If we've gotten here we're good. The stick is in the // right quadrant for the filter so return true. result = true; } else if (touchFilt != null) { for (int i = 0; i < reflex.Filters.Count; ++i) { Filter filter = reflex.Filters[i]; if (!(filter is DirectionFilter)) { continue; } DirectionFilter directFilt = filter as DirectionFilter; // The first filter will handle all direction filters present, so if we are not // the first filter, just bail with a "true" result. if (directFiltCount > 0 && directFilt == this) { return(true); } combinedDirection |= directFilt.direction; directFiltCount += 1; } if (combinedDirection == touchFilt.Direction) { result = true; } } return(result); }