Example #1
0
 /// <summary>
 /// Move the position or the heading of the car depending on current direction.
 /// </summary>
 /// <param name="x"></param>
 /// <param name="y"></param>
 public override void Move(SimulationCommand command)
 {
     if (command.GetType() == typeof(CarCommand)){
         CarCommand carCommand = (CarCommand)command;
         if (_isLogging) Console.WriteLine("Car: Moving " + carCommand.Direction.ToString());
         switch (carCommand.Direction)
         {
             case Enums.Direction.forward:
                 this.Position.MovePosition(_forward.X, _forward.Y);
                 break;
             case Enums.Direction.back:
                 this.Position.MovePosition(_backward.X, _backward.Y);
                 break;
             case Enums.Direction.left:
                 TurnLeft();
                 break;
             case Enums.Direction.right:
                 TurnRight();
                 break;
             default:
                 throw new IsNotEnumException("Car: " + "\"" + Convert.ToChar(carCommand.Direction) + "\"" + " is not a valid command.");
         }
     }
     else {
         throw new IsNotCarException("Command given is not of type CarCommand.");
     }
 }
Example #2
0
        /// <summary>
        ///     When we get a message from the server, we handle the message here
        ///     and perform any necessary tasks.
        /// </summary>
        private void ListenerOnNetworkReceiveEvent(NetPeer peer, NetDataReader reader)
        {
            try
            {
                // The message type is the first byte, (255 message types)
                var messageType = reader.Data[0];

                // Skip the first byte
                var message = reader.Data.Skip(1).ToArray();

                // Switch between all the messages
                switch (messageType)
                {
                case CommandBase.ConnectionResultCommandId:
                    // We only want this message while connecting
                    if (Status != ClientStatus.Connecting)
                    {
                        break;
                    }

                    // Get the result
                    var connectionResult = ConnectionResultCommand.Deserialize(message);

                    if (connectionResult.Success)
                    {
                        // Log and set that we are connected.
                        CSM.Log($"Successfully connected to server at {peer.EndPoint.Host}:{peer.EndPoint.Port}.");
                        Status = ClientStatus.Connected;
                    }
                    else
                    {
                        CSM.Log($"Could not connect to server at {peer.EndPoint.Host}:{peer.EndPoint.Port}. Disconnecting... Error Message: {connectionResult.Reason}");
                        ConnectionMessage = $"Could not connect to server at {peer.EndPoint.Host}:{peer.EndPoint.Port}. Disconnecting... Error Message: {connectionResult.Reason}";
                        Disconnect();
                    }
                    break;

                // Handle ping commands by returning the ping
                case CommandBase.PingCommandId:
                    // Update the last server ping
                    _lastServerPing = DateTime.UtcNow;
                    // Send back a ping event
                    peer.Send(ArrayHelpers.PrependByte(CommandBase.PingCommandId, new PingCommand().Serialize()), SendOptions.ReliableOrdered);
                    break;

                case CommandBase.SimulationCommandID:
                    var simulation = SimulationCommand.Deserialize(message);

                    SimulationManager.instance.SimulationPaused        = simulation.SimulationPaused;
                    SimulationManager.instance.SelectedSimulationSpeed = simulation.SelectedSimulationSpeed;
                    break;
                }
            }
            catch (Exception ex)
            {
                CSM.Log($"Received an error from {peer.EndPoint.Host}:{peer.EndPoint.Port}. Message: {ex.Message}");
            }
        }
Example #3
0
        private SimulationCommand GetSimulationProject()
        {
            SimulationCommand simulationProject = new SimulationCommand()
            {
                FileName = arg
            };
            bool metUnknownOption = false;

            while (!metUnknownOption && Eat() is string option)
            {
                switch (option)
                {
                case "-o":
                case "--output":
                    string folder = GetString();
                    if (folder is null)
                    {
                        throw logger.Error(new OptionException(option, "output parameter need a string value."));
                    }
                    simulationProject.OutputFolder = folder;
                    break;

                case "-s":
                case "--step-size":
                    simulationProject.StepSize = GetDouble();
                    break;

                case "-t":
                case "--stop-time":
                    simulationProject.StopTime = GetDouble();
                    break;

                case "-j":
                case "--json":
                    simulationProject.IsJsonFile = GetBoolean();
                    break;

                case "-r":
                case "--real-time":
                    simulationProject.RealTimeSimulation = GetBoolean();
                    break;

                case "-c":
                case "--compile":
                    simulationProject.ToCompile = GetBoolean();
                    break;

                default:
                    metUnknownOption = true;
                    break;
                }
            }
            return(simulationProject);
        }
Example #4
0
        /// <summary>
        ///     Starts, stops or resets the V-REP EDU simulation according to the given command.
        /// </summary>
        /// <param name="command">The given command.</param>
        public void SetSimulationState(SimulationCommand command)
        {
            switch (command)
            {
            case SimulationCommand.Stop:
                StopSimulation();
                break;

            case SimulationCommand.Start:
                StartSimulation();
                break;

            case SimulationCommand.Reset:
                //StopSimulation();
                VREPWrapper.simxStopSimulation(_clientId, simx_opmode.oneshot_wait);
                Thread.Sleep(400);
                //StartSimulation();
                VREPWrapper.simxStartSimulation(_clientId, simx_opmode.oneshot_wait);
                break;
            }
        }
Example #5
0
        /// <summary>
        ///     When we get a message from a client, we handle the message here
        ///     and perform any necessary tasks.
        /// </summary>
        private void ListenerOnNetworkReceiveEvent(NetPeer peer, NetDataReader reader)
        {
            try
            {
                // The message type is the first byte, (255 message types)
                var messageType = reader.Data[0];

                // Skip the first byte
                var message = reader.Data.Skip(1).ToArray();

                // Switch between all the messages
                switch (messageType)
                {
                case CommandBase.ConnectionRequestCommandId:

                    var connectionResult = Commands.ConnectionRequestCommand.Deserialize(message);

                    CSM.Log($"Connection request from {peer.EndPoint.Host}:{peer.EndPoint.Port}. Version: {connectionResult.GameVersion}, ModCount: {connectionResult.ModCount}, ModVersion: {connectionResult.ModVersion}");

                    // TODO, check these values, but for now, just accept the request.
                    SendToClient(peer, CommandBase.ConnectionResultCommandId, new ConnectionResultCommand {
                        Success = true
                    });
                    break;

                case CommandBase.SimulationCommandID:
                    var simulation = SimulationCommand.Deserialize(message);

                    SimulationManager.instance.SimulationPaused        = simulation.SimulationPaused;
                    SimulationManager.instance.SelectedSimulationSpeed = simulation.SelectedSimulationSpeed;
                    break;
                }
            }
            catch (Exception ex)
            {
                CSM.Log($"Received an error from {peer.EndPoint.Host}:{peer.EndPoint.Port}. Message: {ex.Message}");
            }
        }
Example #6
0
 public abstract void Move(SimulationCommand command);
Example #7
0
 public void SetSimulationState(SimulationCommand command)
 {
     switch (command)
     {
         case SimulationCommand.Stop:
             StopSimulation();
             break;
         case SimulationCommand.Start:
             StartSimulation();
             break;
         case SimulationCommand.Reset:
             //StopSimulation();
             VREPWrapper.simxStopSimulation(_clientId, simx_opmode.oneshot_wait);
             Thread.Sleep(400);
             //StartSimulation();
             VREPWrapper.simxStartSimulation(_clientId, simx_opmode.oneshot_wait);
             break;
     }
 }
Example #8
0
 public void MoveEntity(SimulationCommand command)
 {
     _entity.Move(command);
     if (_isLogging) Console.WriteLine("Moved entity: " + _entity.ToString());
     DidEntityHitWall();
 }