Beispiel #1
0
    void SetThrottle(ref ControlProxy control)
    {
        float finalThrottleTime;
        float finalThrottleReleaseTime;

        if (car.slipVeloForward < -1.8f)
        {
            finalThrottleTime        = throttleTimeSlip;
            finalThrottleReleaseTime = throttleTimeSlip;
        }
        else
        {
            finalThrottleTime        = throttleTime;
            finalThrottleReleaseTime = throttleReleaseTime;
        }

        if (forwardInput)
        {
            control.throttle = Mathf.Clamp01(car.throttle + finalThrottleTime * Time.deltaTime);
        }
        else if (backInput)
        {
            control.throttle = Mathf.Clamp01(car.throttle - throttleReleaseTimeBraking * Time.deltaTime);
        }
        else
        {
            control.throttle = Mathf.Clamp01(car.throttle - finalThrottleReleaseTime * Time.deltaTime);
        }
    }
 public static ControlProxy eventFired_Add(this ControlProxy controlProxy, ControlProxyEventHandler controlProxyEventHandler)
 {
     if (controlProxy.notNull())
     {
         controlProxy.EventFired += controlProxyEventHandler;
     }
     return(controlProxy);
 }
Beispiel #3
0
 public void SetControls(ControlProxy control)
 {
     steer       = control.steer;
     throttle    = control.throttle;
     brake       = control.brake;
     reverse     = control.reverse;
     currentGear = control.gear;
     eBrakeOn    = control.eBrake;
 }
Beispiel #4
0
        public MainGui SetEventsListener(ControlProxy controlProxy)
        {
            controlProxy.unsubscribeAllEvents()
            .eventFired_Remove(new ControlProxyEventHandler(ProxyEventFired));

            controlProxy.subscribeToEvents(dialog)
            .eventFired_Add(new ControlProxyEventHandler(ProxyEventFired));
            return(this);
        }
Beispiel #5
0
 void SetBrakes(ref ControlProxy control)
 {
     if (backInput)
     {
         control.brake = Mathf.Clamp01(car.brake + brakeTime * Time.deltaTime);
     }
     else
     {
         control.brake = Mathf.Clamp01(car.brake - brakeReleaseTime * Time.deltaTime);
     }
 }
 public static ControlProxy unsubscribeAllEvents(this ControlProxy controlProxy)
 {
     //"[ControlProxy] Unsubscribing to All Events".info();
     if (controlProxy.notNull())
     {
         foreach (EventDescriptor ed in controlProxy.GetEvents())
         {
             controlProxy.UnsubscribeEvent(ed);
         }
     }
     return(controlProxy);
 }
 public static ControlProxy get_ControlProxy_for_MainWindowHandle(this Process process)
 {
     if (process.notNull())
     {
         try
         {
             return(ControlProxy.FromHandle(process.MainWindowHandle));
         }
         catch (Exception ex)
         {
             ex.log();
         }
     }
     return(null);
 }
 public static ControlProxy subscribeToEvents(this ControlProxy controlProxy, EventFilterDialog dialog)
 {
     if (controlProxy.notNull() && dialog.notNull() && dialog.EventList.notNull())
     {
         "[ControlProxy] Subscribing to Events".info();
         foreach (EventDescriptor ed in controlProxy.GetEvents())
         {
             if (dialog.EventList[ed.Name].Display)
             {
                 controlProxy.SubscribeEvent(ed);
             }
         }
     }
     return(controlProxy);
 }
Beispiel #9
0
    void SetGear(ref ControlProxy control)
    {
        int newGear = car.currentGear;

        if (car.currentGear == 0)
        {
            control.reverse = false;
            if (canDrive)
            {
                if (forwardInput)
                {
                    newGear = 1;
                }
                else if (backInput)
                {
                    newGear         = 1;
                    control.reverse = true;
                }
            }
        }
        else if (car.automaticTrans)
        {
            if (car.engineRPM > car.autoShiftUp)
            {
                if (car.currentGear < car.gearRatios.Length - 1)
                {
                    newGear = car.currentGear + 1;
                }
            }
            else if (car.engineRPM < car.autoShiftDown)
            {
                if (car.currentGear > 1)
                {
                    newGear = car.currentGear - 1;
                }
            }
            if (Mathf.Abs(car.speed) < 2.0f && !forwardInput && backInput)
            {
                newGear  = 0;
                canDrive = false;
            }
        }
        else
        {
            //TODO manual transmission
        }
        control.gear = newGear;
    }
Beispiel #10
0
 /// <summary>
 /// Used to build the treeview as the user expands nodes.
 /// We always stay one step ahead of the user to get the expand state set correctly.
 /// So, for instance, when we just show processes, we have already calculated all the top level windows.
 /// When the user expands a process -- we calculate the children of all top level windows
 /// And so on...
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void treeWindow_BeforeExpand(object sender, TreeViewCancelEventArgs e)
 {
     foreach (TreeNode child in e.Node.Nodes)
     {
         child.Nodes.Clear();
         ControlProxy proxy = child.Tag as ControlProxy;
         if (proxy != null)
         {
             foreach (ControlProxy proxychild in proxy.Children)
             {
                 string name = String.IsNullOrEmpty(proxychild.GetComponentName()) ?
                               "<noname>" : proxychild.GetComponentName();
                 TreeNode node = child.Nodes.Add(proxychild.Handle.ToString(), name + "     [" +
                                                 proxychild.GetClassName() + "]");
                 node.Tag = proxychild;
             }
         }
     }
 }
Beispiel #11
0
 /// <summary>
 /// Starts event logging
 /// </summary>
 private void StartLogging()
 {
     if (tsButtonStartStop.Checked)
     {
         currentProxy = propertyGrid.SelectedObject as ControlProxy;
         if (currentProxy != null)
         {
             //unsubscribe from events.
             foreach (EventDescriptor ed in currentProxy.GetEvents())
             {
                 if (dialog.EventList[ed.Name].Display)
                 {
                     currentProxy.SubscribeEvent(ed);
                 }
             }
             currentProxy.EventFired += new ControlProxyEventHandler(ProxyEventFired);
         }
     }
 }
        public static TreeNode addChildControlProxies(this TreeNode treeNode, ControlProxy controlProxy)
        {
            if (treeNode.notNull() && controlProxy != null)
            {
                treeNode.add_Node("...loading data...");
                var children = controlProxy.Children;
                treeNode.clear();
                foreach (ControlProxy proxychild in children)
                {
                    var name = String.IsNullOrEmpty(proxychild.GetComponentName())
                                            ?   "<noname>"
                                            : proxychild.GetComponentName();
                    var nodeText    = name + "     [" + proxychild.GetClassName() + "]";//proxychild.Handle.ToString()
                    var hasChildren = proxychild.Children.size() > 0;
                    treeNode.add_Node(nodeText, proxychild, hasChildren);
                }
            }

            return(treeNode);
        }
        public static TreeNode addChildControlProxies(this TreeNode treeNode, ControlProxy controlProxy)
        {
            if (treeNode.notNull() && controlProxy != null)
            {
                treeNode.add_Node("...loading data...");
                var children = controlProxy.Children;
                treeNode.clear();
                foreach (ControlProxy proxychild in children)
                {
                    var name = String.IsNullOrEmpty(proxychild.GetComponentName())
                                            ?   "<noname>"
                                            : proxychild.GetComponentName();
                    var nodeText = name + "     [" +proxychild.GetClassName() + "]";//proxychild.Handle.ToString()
                    var hasChildren = proxychild.Children.size() > 0;
                    treeNode.add_Node(nodeText, proxychild,  hasChildren);
                }
            }

            return treeNode;
        }
Beispiel #14
0
    // Update is called once per frame
    void Update()
    {
        ControlProxy control = new ControlProxy();

        control.eBrake = Input.GetKey(KeyCode.Space);

        SetSteer(ref control);

        forwardInput = Input.GetKey(KeyCode.UpArrow);
        backInput    = Input.GetKey(KeyCode.DownArrow);

        /*this will temporarily disable the controls so that you must release forward and back
        * to put the car in gear.  Stops accidently putting the car in reverse while braking*/
        if (!canDrive)
        {
            if (!forwardInput && !backInput)
            {
                canDrive = true;
            }
            else
            {
                forwardInput = false;
                backInput    = false;
            }
        }

        control.reverse = car.reverse;
        if (car.reverse)
        {
            bool x = forwardInput;
            forwardInput = backInput;
            backInput    = x;
        }

        SetGear(ref control);
        SetThrottle(ref control);
        SetBrakes(ref control);

        car.SetControls(control);
    }
Beispiel #15
0
        /// <summary>
        /// This uses ControlPaint.DrawReversibleFrame to highlight the given window
        /// </summary>
        private void FlashCurrentWindow()
        {
            ControlProxy proxy = propertyGrid.SelectedObject as ControlProxy;

            if (proxy != null && proxy.IsManaged && proxy.GetValue("Location") != null)
            {
                IntPtr handle  = proxy.Handle;
                Point  topleft = (Point)proxy.GetValue("Location");
                if (proxy.Parent != null)
                {
                    topleft = (Point)proxy.Parent.PointToScreen(topleft);
                }
                Size      size = (Size)proxy.GetValue("Size");
                Rectangle r    = new Rectangle(topleft, size);

                for (int i = 1; i <= 7; i++)
                {
                    ControlPaint.DrawReversibleFrame(r, Color.Red, FrameStyle.Thick);
                    Thread.Sleep(100);
                }
                Thread.Sleep(250);  //extra delay at the end.
                ControlPaint.DrawReversibleFrame(r, Color.Red, FrameStyle.Thick);
            }
        }
Beispiel #16
0
 /// <summary>
 /// Starts event logging
 /// </summary>
 private void StartLogging()
 {
     if (tsButtonStartStop.Checked) {
         currentProxy = propertyGrid.SelectedObject as ControlProxy;
         if (currentProxy != null) {
             //unsubscribe from events.
             foreach (EventDescriptor ed in currentProxy.GetEvents()) {
                 if (dialog.EventList[ed.Name].Display) {
                     currentProxy.SubscribeEvent(ed);
                 }
             }
             currentProxy.EventFired += new ControlProxyEventHandler(ProxyEventFired);
         }
     }
 }
        public MainGui SetEventsListener(ControlProxy controlProxy)
        {
            controlProxy.unsubscribeAllEvents()
                        .eventFired_Remove(new ControlProxyEventHandler(ProxyEventFired));

            controlProxy.subscribeToEvents(dialog)
                        .eventFired_Add(new ControlProxyEventHandler(ProxyEventFired));
            return this;
        }
Beispiel #18
0
    void SetSteer(ref ControlProxy control)
    {
        steerInput = 0;
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            steerInput = -1;
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            steerInput = 1;
        }

        // Steering
        Vector3 carDir          = transform.forward;
        float   fVelo           = rigidbody.velocity.magnitude;
        Vector3 veloDir         = rigidbody.velocity.normalized;
        float   angle           = -Mathf.Asin(Mathf.Clamp(Vector3.Cross(veloDir, carDir).y, -1, 1));
        float   optimalSteering = angle / (car.steerMax * Mathf.Deg2Rad);

        if (fVelo < 1)
        {
            optimalSteering = 0;
        }

        float steer             = steerInput;
        float adjustedSteerTime = 0;

        if (steerInput == 0)
        {
            adjustedSteerTime = 1 / (steerReleaseTime + (Mathf.Abs(car.speed) * steerReleaseVeloFactor));
            if (car.steer < 0)
            {
                steer = car.steer + adjustedSteerTime * Time.deltaTime;
                if (steer > 0)
                {
                    steer = 0;
                }
            }
            else
            {
                steer = car.steer - adjustedSteerTime * Time.deltaTime;
                if (steer < 0)
                {
                    steer = 0;
                }
            }
        }
        else
        {
            if (steerInput > car.steer)
            {
                if (car.steer < 0)
                {
                    adjustedSteerTime = 1 / (steerReleaseTime + (car.speed * steerReleaseVeloFactor));
                }
                else
                {
                    adjustedSteerTime = 1 / (steerTime + (car.speed * steerVeloFactor));
                }
                if (car.steer < optimalSteering)
                {
                    adjustedSteerTime *= 1 + (optimalSteering - car.steer) * steerCorrectionFactor;
                }
            }
            else if (steerInput < car.steer)
            {
                if (car.steer > 0)
                {
                    adjustedSteerTime = 1 / (steerReleaseTime + (car.speed * steerReleaseVeloFactor));
                }
                else
                {
                    adjustedSteerTime = 1 / (steerTime + (car.speed * steerVeloFactor));
                }
                if (car.steer > optimalSteering)
                {
                    adjustedSteerTime *= 1 + (car.steer - optimalSteering) * steerCorrectionFactor;
                }
            }
            steer = Mathf.Clamp(car.steer + steerInput * adjustedSteerTime * Time.deltaTime, -1.0f, 1.0f);
        }
        control.steer = steer;
    }
Beispiel #19
0
    void DriveActuator(AI.Goal goal)
    {
        // min and max refers to the speed of the car not the size of the angle
        float minForwardAngle = 120f * Mathf.Deg2Rad;
        float maxForwardAngle = car.steerMax * Mathf.Deg2Rad / 4;

        float minBackAngle = 120f * Mathf.Deg2Rad;
        float maxBackAngle = 170f * Mathf.Deg2Rad;

        float speedFactor = Mathf.Clamp01(car.speed * 0.036f - 0.2f);         // scales from 20 to 120 kph

        float   backAngle = Mathf.Lerp(minBackAngle, maxBackAngle, speedFactor);
        Vector3 backArc   = car.transform.TransformDirection(new Vector3(Mathf.Sin(backAngle), 0f, Mathf.Cos(backAngle)));

        Debug.DrawRay(car.transform.position, backArc, Color.cyan);

        float   forwardAngle = Mathf.Lerp(minForwardAngle, maxForwardAngle, speedFactor);
        Vector3 forwardArc   = car.transform.TransformDirection(new Vector3(Mathf.Sin(forwardAngle), 0f, Mathf.Cos(forwardAngle)));

        Debug.DrawRay(car.transform.position, forwardArc, Color.cyan);

        Vector2 forward2D = new Vector2(car.transform.forward.x, car.transform.forward.z).normalized;
        Vector2 carPos2D  = new Vector2(car.transform.position.x, car.transform.position.z);

        ControlProxy control = new ControlProxy();

        // New steer based on direction
        Vector2 targetDir = (goal.position - carPos2D).normalized;
        float   goalAngle = Vector2.Angle(goal.direction, forward2D) * Mathf.Deg2Rad;

        if (goalAngle < forwardAngle)
        {
            // targetAngle will try to match position with the goal
            float   targetAngle = Vector2.Angle(targetDir, forward2D) * Mathf.Deg2Rad;
            Vector3 localTarget = car.transform.InverseTransformDirection(new Vector3(targetDir.x, 0f, targetDir.y));
            control.steer    = Mathf.Clamp(targetAngle * Mathf.Sign(localTarget.x) / (car.steerMax * Mathf.Deg2Rad), -1f, 1f);          // note Mathf.Sign() is not Mathf.Sin()
            control.throttle = Mathf.Max(0.25f, Mathf.Abs(localTarget.z));
            if (car.speed < 0)
            {
                control.steer *= -1;
            }
        }
        else if (goalAngle > backAngle)
        {
            if (debugUseBreak)
            {
                Debug.Break();
            }
            float   targetAngle = Vector2.Angle(targetDir, forward2D) * Mathf.Deg2Rad;
            Vector3 localTarget = car.transform.InverseTransformDirection(new Vector3(targetDir.x, 0f, targetDir.y));
            control.steer = Mathf.Clamp(targetAngle * Mathf.Sign(localTarget.x) / (car.steerMax * Mathf.Deg2Rad), -1f, 1f);             // note Mathf.Sign() is not Mathf.Sin()
            if (car.speed < 5.0f)
            {
                control.steer   *= -1;
                control.throttle = 1.0f;
                control.reverse  = true;
            }
            else
            {
                control.throttle = 0.0f;
                control.brake    = 1.0f;
            }
        }
        else
        {
            // steep turns will try to match direction with the goal not position
            Vector2 velo2D           = new Vector2(car.rigidbody.velocity.x, car.rigidbody.velocity.z);
            float   approachVelo     = Vector2.Dot(velo2D, targetDir);
            float   distToNextCorner = (goal.cornerPosition - carPos2D).magnitude;
            float   turnScale        = approachVelo / distToNextCorner;
            Vector3 localTarget      = car.transform.InverseTransformDirection(new Vector3(goal.direction.x, 0f, goal.direction.y));
            control.steer    = Mathf.Clamp(goalAngle * Mathf.Sign(localTarget.x) / (car.steerMax * Mathf.Deg2Rad) * turnScale, -1f, 1f);
            control.throttle = 1f;
        }

        control.gear = car.currentGear;
        if (car.engineRPM > car.autoShiftUp)
        {
            if (car.currentGear < car.gearRatios.Length - 1)
            {
                car.currentGear += 1;
            }
        }
        else if (car.engineRPM < car.autoShiftDown)
        {
            if (car.currentGear > 1)
            {
                car.currentGear -= 1;
            }
        }

        if (!debugUseAI)
        {
            return;
        }
        car.SetControls(control);
    }
Beispiel #20
0
 public SUIDotNetCheckBox(IntPtr hWnd)
     : base(hWnd)
 {
     btn = new SUICheckBox(hWnd);
     ControlProxy proxy = ControlProxy.FromHandle(WindowHandle);
 }