Exemplo n.º 1
0
        public Simulation(SimulationCoordinateSpace coordSpace, INetworkDatabase network, IUnitDatabase units, LoggingManager log)
        {
            CoordSpace = coordSpace;
            Network    = network;
            Units      = units;
            _log       = log;

            _currentSegmentByUnitId = new int?[units.GetNumUnits()];
            _dirByUnitId            = new SegmentEndpoint[units.GetNumUnits()];
            _tByUnitId      = new float[units.GetNumUnits()];
            _unitProperties = new UnitProperties[units.GetNumUnits()];

            for (var unitIndex = 0; unitIndex < Units.GetNumUnits(); unitIndex++)
            {
                var unit = Units.GetUnitByIndex(unitIndex);

                var result = Network.FindSegmentAt(unit.Pos, unit.Orientation, 0.2f, (float)(Math.PI * 0.25));

                if (result == null)
                {
                    Console.Error.WriteLine($"Simulation: could not snap unit {unitIndex} to railroad network!");
                    continue;
                }

                var(segmentId, dir, t)             = result.Value;
                _currentSegmentByUnitId[unitIndex] = segmentId;
                _dirByUnitId[unitIndex]            = dir;
                _tByUnitId[unitIndex] = t;

                var eh = _log.GetEntityHandle(_unitProperties[unitIndex].GetType(), unitIndex);
                _unitProperties[unitIndex] = new UnitProperties {
                    Controller      = new TrainControlStack(unitIndex, _log, Network),
                    AccelerationPin = _log.GetSignalPin(eh, "acceleration"),
                    VelocityPin     = _log.GetSignalPin(eh, "velocity"),
                    SegmentIdPin    = _log.GetSignalPin(eh, "segmentId"),
                    TPin            = _log.GetSignalPin(eh, "t"),
                };

                _unitProperties[unitIndex].Controller.GoAutoSchedule();
            }
        }