예제 #1
0
        /// <summary>
        /// Update the state of the client vehicle
        /// </summary>
        /// <param name="worldState"></param>
        /// <param name="dt"></param>
        public override SimVehicleState Update(WorldState worldState, double dt)
        {
            try
            {
                sumDt += dt;
                // update world
                this.world.UpdateWorld(worldState);

                // update value held in sim client
                this.ClientVehicle.Update(worldState);

                // set vehicle speed check
                double speedLock = this.ClientVehicle.CurrentState.Speed;

                // vehicle state
                VehicleState vs = world.VehicleStateFromSim(this.ClientVehicle.CurrentState);
                vs.Timestamp = GetCurrentTimestamp.ts;

                // update in ai and all listeners
                this.communicator.Update(
                    vs,
                    world.VehiclesFromWorld(this.ClientVehicle.CurrentState.VehicleID, vs.Timestamp),
                    world.ObstaclesFromWorld(this.ClientVehicle.CurrentState.VehicleID, this.ClientVehicle.CurrentState.Position, this.ClientVehicle.CurrentState.Heading.ArcTan, vs.Timestamp),
                    this.ClientVehicle.CurrentState.Speed,
                    world.GetLocalRoadEstimate(this.ClientVehicle.CurrentState),
                    world.GetPathRoadEstimate(this.ClientVehicle.CurrentState));

                // check if we're in step mode
                lock (runControlClients) {
                    if (stepMode)
                    {
                        foreach (ClientRunControlFacade stepClient in runControlClients.Values)
                        {
                            stepClient.Step();
                        }
                    }
                }

                // save persistent info
                double      maxSpeed    = this.ClientVehicle.CurrentState.MaximumSpeed;
                bool        useMaxSpeed = this.ClientVehicle.CurrentState.UseMaximumSpeed;
                Coordinates pos         = this.ClientVehicle.CurrentState.Position;
                Coordinates heading     = this.ClientVehicle.CurrentState.Heading;
                bool        canMove     = this.ClientVehicle.CurrentState.canMove;

                // get next state
                DynamicsVehicle.VehicleState = this.ClientVehicle.CurrentState;
                SimVehicleState updatedState = DynamicsVehicle.Update(dt, this.world);

                // modify with persistent info
                if (useMaxSpeed)
                {
                    updatedState.Speed = maxSpeed;
                }
                if (!canMove)
                {
                    updatedState.Position = pos;
                    updatedState.Heading  = heading;
                }

                // set state
                this.ClientVehicle.CurrentState = updatedState;

                if (this.ClientVehicle.CurrentState.LockSpeed)
                {
                    this.ClientVehicle.CurrentState.Speed = speedLock;
                }

                // return updated to sim
                return(updatedState);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error updating: \n" + e.ToString());
                return(null);
            }
        }