/// <summary>
        /// Remove passenger
        /// </summary>
        /// <param name="other"></param>
        private void RemovePassengerParent(GameObject other)
        {
            if (IsPassenger(other.tag))
            {
                if (_kinematicWhileMoving)
                {
                    TrainStationController trainStationController = other.transform.parent.GetComponent <TrainStationController>();

                    if (trainStationController != null)
                    {
                        int         objectId = other.gameObject.GetInstanceID();
                        UnityAction stopAction;
                        UnityAction leaveAction;

                        if (_onStopRuntimeEvents.TryGetValue(objectId, out stopAction))
                        {
                            trainStationController.onStop.RemoveListener(_onStopRuntimeEvents[objectId]);
                            _onStopRuntimeEvents.Remove(objectId);
                        }

                        if (_onLeaveRuntimeEvents.TryGetValue(objectId, out leaveAction))
                        {
                            trainStationController.onLeave.RemoveListener(_onLeaveRuntimeEvents[objectId]);
                            _onLeaveRuntimeEvents.Remove(objectId);
                        }
                    }
                }

                other.transform.parent = null;
            }
        }
        /// <summary>
        /// Set passenger parent transform
        /// </summary>
        /// <param name="other"></param>
        /// <param name="parent"></param>
        private void SetPassengerParent(GameObject other, Transform parent)
        {
            if (IsPassenger(other.tag))
            {
                other.transform.parent = parent;

                if (_kinematicWhileMoving)
                {
                    TrainStationController trainStationController = parent.GetComponent <TrainStationController>();

                    if (trainStationController != null)
                    {
                        int         objectId = other.gameObject.GetInstanceID();
                        UnityAction stopAction;
                        UnityAction leaveAction;

                        if (!_onStopRuntimeEvents.TryGetValue(objectId, out stopAction))
                        {
                            stopAction = new UnityAction((delegate { GameObject localGameObject = other; SetPassengerKinematic(localGameObject, false); }));

                            _onStopRuntimeEvents.Add(objectId, stopAction);
                            trainStationController.onStop.AddListener(_onStopRuntimeEvents[objectId]);
                        }

                        if (!_onLeaveRuntimeEvents.TryGetValue(objectId, out leaveAction))
                        {
                            leaveAction = new UnityAction((delegate { GameObject localGameObject = other; SetPassengerKinematic(localGameObject, true); }));

                            _onLeaveRuntimeEvents.Add(objectId, leaveAction);
                            trainStationController.onLeave.AddListener(_onLeaveRuntimeEvents[objectId]);
                        }
                    }
                }
            }
        }
Beispiel #3
0
        private void OnTriggerEnter(Collider other)
        {
            TrainStationController trainStationController = other.GetComponent <TrainStationController>();

            if (trainStationController != null)
            {
                switch (stopMode)
                {
                case StopMode.Always:
                    trainStationController.StopAtStation(stationBehaviour, stopTimeout, turnOffEngines);
                    break;

                case StopMode.Once:
                    if (!_alreadyStopped)
                    {
                        trainStationController.StopAtStation(stationBehaviour, stopTimeout, turnOffEngines);
                        _alreadyStopped = true;
                    }
                    break;

                case StopMode.Random:
                    if (Probability.RandomEvent(randomStopProbability))
                    {
                        trainStationController.StopAtStation(stationBehaviour, stopTimeout, turnOffEngines);
                    }
                    break;
                }

                ITrainDoorsController trainDoorsController = other.GetComponent <ITrainDoorsController>();

                if (trainDoorsController != null)
                {
                    trainDoorsController.StationDoorDirection = stationDoorDirection;
                }
            }
        }