Exemplo n.º 1
0
        public void LoadEvents(StateMachineNode stateMachine, IReadOnlyDictionary <string, Action <float, State> > eventCallbacks, IReadOnlyDictionary <string, Action <State, StateContinousEvent> > continousEventCallbacks = null)
        {
            StatesEvents.ForEach(eventState =>
            {
                //Assert.IsTrue(stateMachine.States.ContainsKey(stateEvents.Name), $"The state machine {stateMachine.Name} from the graph {stateMachine.Graph.Name} doesn't contain the state {stateEvents.Name} required by the asset {name}");

                if (!stateMachine.States.TryGetValue(eventState.Name, out var state))
                {
                    return;
                }

                switch ((eventCallbacks, continousEventCallbacks))
                {
                case ({ }, null):
                    eventState.Events.ForEach(evt =>
                    {
                        if (evt.Type == EventType.Trigger)
                        {
                            if (eventCallbacks != null && eventCallbacks.TryGetValue(evt.Name, out var callback))
                            {
                                state.AddEvent(evt.TriggerTime, eventCallbacks[evt.Name]);
                            }
                        }
                    });
                    break;
Exemplo n.º 2
0
        public override BaseNode Copy()
        {
            StateMachineNode copy = new StateMachineNode()
            {
                Name = Name, Speed = Speed
            };

            var valueProviderCopyMap = new Dictionary <IValueProvider, IValueProvider>();

            Parameters.Values.ForEach(p =>
            {
                var parameterCopy = copy.AddParameter(p.Copy());
                valueProviderCopyMap.Add(p.ValueProvider, parameterCopy.ValueProvider);
            });

            var copiedStates                 = new Dictionary <BaseState, BaseState>();
            var copiedTransitions            = new Dictionary <Transition, Transition>();
            var transitionsToSetSource       = new List <(Transition copy, BaseState originalSource)>();
            var transitionsToSetDestination  = new List <(Transition copy, State originalDestination)>();
            var conditionsToSetValueProvider = new List <(TransitionCondition copy, IValueProvider originalValueProvider)>();

            Transition CopyTransition(Transition original)
            {
                if (copiedTransitions.ContainsKey(original))
                {
                    return(copiedTransitions[original]);
                }

                Transition transitionCopy = original.Copy(copiedStates, valueProviderCopyMap);

                copy.Transitions.Add(transitionCopy);

                copiedTransitions.Add(original, transitionCopy);

                if (transitionCopy.SourceState == null)
                {
                    transitionsToSetSource.Add((transitionCopy, original.SourceState));
                }

                if (transitionCopy.DestinationState == null)
                {
                    transitionsToSetDestination.Add((transitionCopy, original.DestinationState));
                }

                return(transitionCopy);
            }

            States.Values.ForEach(s =>
            {
                State stateCopy = (State)s.Copy(CopyTransition, valueProviderCopyMap);
                copy.AddState(stateCopy);
                copiedStates.Add(s, stateCopy);
            });
            AnyStates.Items.Values.ForEach(s =>
            {
                AnyState anyStateCopy = (AnyState)s.Copy(CopyTransition);
                s.StateFilters.ForEach(f => anyStateCopy.StateFilters.Add(new AnyStateFilter()
                {
                    State = (State)copiedStates[f.State], Mode = f.Mode
                }));
                copy.AnyStates.AddItem(s.Name, anyStateCopy);
                copiedStates.Add(s, anyStateCopy);
            });

            copy.EntryState = (State)copiedStates[EntryState];

            transitionsToSetSource.ForEach(t => t.copy.SourceState           = copiedStates[t.originalSource]);
            transitionsToSetDestination.ForEach(t => t.copy.DestinationState = (State)copiedStates[t.originalDestination]);

            return(copy);
        }