예제 #1
0
        public void ProcessAnimationAction(AnimationEventTemplate aeTemplate, ActionTemplate action, string animEvent)
        {
            var character = aeTemplate.Entity.FindTemplate <CharacterTemplate>();
            var ae        = new ActionEvent(character, character, aeTemplate.AnimEvent.Position,
                                            aeTemplate.AnimEvent.Rotation, AnimationEvents.ToStateEvent(animEvent));

            if (ae.State == ActionState.Activate)
            {
                Debug.DrawLine(
                    aeTemplate.AnimEvent.Position, aeTemplate.AnimEvent.Rotation.GetPosition(aeTemplate.AnimEvent.Position, 2.5f),
                    Color.red, 5f);
            }
            var animationList = action.Config.GetEventHandler(animEvent);

            if (animationList != null)
            {
                for (int i = 0; i < animationList.Count; i++)
                {
                    animationList[i].Trigger(ae, animEvent);
                }
            }
            if (ae.State != ActionState.None)
            {
                aeTemplate.Entity.Post(ae);
                action.Entity.PostNoGlobal(ae);
            }
            if (ae.State == ActionState.Start)
            {
                for (int i = 0; i < action.Config.Costs.Count; i++)
                {
                    action.Config.Costs[i].ProcessCost(ae.Origin, action.Entity);
                }
            }
        }
예제 #2
0
        public void PostAnimationEvent(ActionUsingNode node, string eventName)
        {
            if (Events.TryGetValue(eventName, out var animationEvent))
            {
                animationEvent.Trigger(node, eventName);
            }
            ActionStateEvents state = AnimationEvents.ToStateEvent(eventName);

            if (state == ActionStateEvents.Activate)
            {
                node.Entity.Post(new ActionStateEvent(node.Entity, node.Entity, node.Animator.GetEventPosition, node.Animator.GetEventRotation, ActionStateEvents.Activate));
            }
            if (state == ActionStateEvents.None)
            {
                return;
            }
            var stateEvent = new ActionStateEvent(node.Entity, Action.GetEntity(), node.Animator.GetEventPosition, node.Animator.GetEventRotation, state);

            if (Action.Fx != null)
            {
                Action.Fx.TriggerEvent(stateEvent);
            }
            for (int i = 0; i < ScriptedEvents.Count; i++)
            {
                if (ScriptedEvents[i].Event != state)
                {
                    continue;
                }
                PostAnimationEvent(stateEvent);
                break;
            }
        }
예제 #3
0
        public override void DrawEditorGui()
        {
#if UNITY_EDITOR
            var animationLabels = AnimationEvents.GetValues();
            var index           = System.Array.IndexOf(animationLabels, EventName);
            var newIndex        = UnityEditor.EditorGUILayout.Popup("Event", index, animationLabels);
            if (newIndex >= 0)
            {
                EventName = animationLabels[newIndex];
                UnityEditor.EditorUtility.SetDirty(this);
            }
#endif
        }
예제 #4
0
        public override bool DrawGui(GUIStyle textStyle, GUIStyle buttonStyle)
        {
            bool changed = false;

#if UNITY_EDITOR
            GUILayout.BeginHorizontal();
            GUILayout.Space(20);
            var animationLabels = AnimationEvents.GetValues();
            var index           = System.Array.IndexOf(animationLabels, EventName);
            var newIndex        = UnityEditor.EditorGUILayout.Popup("Event", index, animationLabels);
            if (newIndex >= 0)
            {
                EventName = animationLabels[newIndex];
                UnityEditor.EditorUtility.SetDirty(this);
            }
            if (!Loop)
            {
                if (GUILayout.Button("Add Loop Condition", buttonStyle))
                {
                    Loop          = true;
                    LoopCondition = new EventCondition();
                    changed       = true;
                }
            }
            GUILayout.Space(20);
            GUILayout.EndHorizontal();
            if (Loop && LoopCondition != null)
            {
                LoopCondition.DrawGui(this, textStyle, buttonStyle);
            }
            if (changed)
            {
                UnityEditor.EditorUtility.SetDirty(this);
            }
#endif
            return(changed);
        }
예제 #5
0
        public bool DrawType(StateGraph graph, GUIStyle textStyle, GUIStyle buttonStyle)
        {
            bool changed = false;

#if UNITY_EDITOR
            switch (Type)
            {
            case ConditionType.Trigger:
            case ConditionType.EntityTag:
                break;

            case ConditionType.IsAttacking:
                GUILayout.Label("IsAttacking", textStyle);
                break;

            default:
                var graphLabels = GraphVariables.GetValues();
                var index       = System.Array.IndexOf(graphLabels, VariableName);
                var newVar      = UnityEditor.EditorGUILayout.Popup(index, graphLabels, buttonStyle, new [] { GUILayout.MaxWidth
                                                                                                                  (StateGraphNode.DefaultNodeSize.x * 0.5f) });
                if (newVar != index)
                {
                    VariableName = graphLabels[newVar];
                }
                break;
            }
            switch (Type)
            {
            case ConditionType.IsAttacking:
                bool.TryParse(Value, out bool oldBool);
                if (GUILayout.Button(oldBool.ToString(), buttonStyle))
                {
                    Value = (!oldBool).ToString();
                }
                break;

            case ConditionType.Trigger:
                var labels   = graph.GlobalTriggers.Select(t => t.Key).ToArray();
                var index    = System.Array.IndexOf(labels, Value);
                var newIndex = UnityEditor.EditorGUILayout.Popup(index, labels, buttonStyle);
                if (newIndex >= 0)
                {
                    Value   = labels[newIndex];
                    changed = true;
                }
                break;

            case ConditionType.StringVariable:
                Value = GUILayout.TextField(Value, buttonStyle);
                break;

            case ConditionType.BoolVariable:
                bool.TryParse(Value, out bool oldValue);
                if (GUILayout.Button(oldValue.ToString()))
                {
                    Value = (!oldValue).ToString();
                }
                break;

            case ConditionType.EntityTag:
                int.TryParse(Value, out int oldTagInt);
                var animationLabels = AnimationEvents.GetValues();
                var newTag          = UnityEditor.EditorGUILayout.Popup(oldTagInt, animationLabels);
                if (newTag != oldTagInt)
                {
                    Value = newTag.ToString();
                }
                break;

            case ConditionType.FloatVariable:
                float.TryParse(Value, out float oldFloat);
                var newFloatStr = GUILayout.TextField(Value, buttonStyle);
                if (float.TryParse(newFloatStr, out var newFloat) && Math.Abs(newFloat - oldFloat) > 0.001f)
                {
                    Value = newFloatStr;
                }
                break;

            case ConditionType.IntVariable:
                int.TryParse(Value, out int oldInt);
                var newIntStr = GUILayout.TextField(Value, buttonStyle);
                if (int.TryParse(newIntStr, out var newInt) && newInt != oldInt)
                {
                    Value = newIntStr;
                }
                break;
            }
#endif
            return(changed);
        }
예제 #6
0
 private ValueDropdownList<string> SignalsList() {
     return AnimationEvents.GetDropdownList();
 }
예제 #7
0
        public override void OnInspectorGUI()
        {
            _script = (ClipAnimator)target;
            if (_script.Animator == null)
            {
                _script.Animator = _script.gameObject.GetComponent <Animator>();
                if (_script.Animator == null)
                {
                    EditorGUILayout.LabelField("No Valid Animator");
                    DrawDefaultInspector();
                    return;
                }
            }
            EditorGUIUtility.labelWidth *= 0.5f;
            EditorGUI.BeginChangeCheck();
            var normal   = new GUIStyle("Box");
            var selected = new GUIStyle("Box");

            selected.normal.background = SelectedBackground;
            var animationLabels = PlayerAnimationIds.GetNames().ToArray();
            var events          = AnimationEvents.GetNames().ToArray();

            for (int i = 0; i < _script.Clips.Length; i++)
            {
                var state = _script.Clips[i];
                if (state.Clip != null)
                {
                    state.ClipName = state.Clip.name;
                }
                EditorGUILayout.BeginVertical(_currentAnimation == state ? selected : normal);
                EditorGUILayout.BeginHorizontal();
                var newClip = EditorGUILayout.ObjectField(state.Clip, typeof(AnimationClip), false);
                if (newClip != null && newClip is AnimationClip newAnimationClip && newAnimationClip != state.Clip)
                {
                    state.Clip = newAnimationClip;
                }
                //EditorGUILayout.LabelField(state.Clip.name, EditorStyles.boldLabel);
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                var index = System.Array.IndexOf(animationLabels, state.Id);
                //var index = labels.IndexOf(state.Id);
                var newIndex = EditorGUILayout.Popup(index, animationLabels);
                if (newIndex >= 0)
                {
                    state.Id = animationLabels[newIndex];
                }
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Fps:");
                state.Fps = EditorGUILayout.IntSlider((int)state.Fps, 1, 60);
                EditorGUILayout.EndHorizontal();

                //EditorGUILayout.BeginHorizontal();
                //EditorGUILayout.LabelField("Event:");
                //state.PercentEvent = EditorGUILayout.Slider(state.PercentEvent, 0, 1);
                //EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Length Multi:");
                state.PlaySpeedMultiplier = EditorGUILayout.Slider(state.PlaySpeedMultiplier, 0.1f, 5);
                EditorGUILayout.LabelField((state.ClipLength / state.PlaySpeedMultiplier).ToString("F1"));
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Loop: " + state.Clip?.isLooping);
                if (GUILayout.Button("Delete"))
                {
                    if (EditorUtility.DisplayDialog("", "Are you sure?", "Yes", "No"))
                    {
                        var list = _script.Clips.ToList();
                        list.RemoveAt(i);
                        _script.Clips = list.ToArray();
                        EditorGUILayout.EndHorizontal();
                        EditorGUILayout.EndVertical();
                        break;
                    }
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                if (_currentAnimation == null)
                {
                    if (GUILayout.Button("Play"))
                    {
                        TimeManager.StartUnscaled(PlayAnimation(_script, state));
                    }
                    EditorGUILayout.Space();
                    EditorGUILayout.EndHorizontal();
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField("Events: " + state.EventTotal());
                }
                else if (_currentAnimation == state)
                {
                    if (GUILayout.Button("Stop"))
                    {
                        Stop();
                    }
                    var inputEvent = Event.current;
                    if (inputEvent.type == EventType.KeyDown && inputEvent.shift)
                    {
                        switch (inputEvent.keyCode)
                        {
                        case KeyCode.Alpha1:
                            _playType = (PlayType)0;
                            KeyUsed(inputEvent);
                            break;

                        case KeyCode.Alpha2:
                            _playType = (PlayType)1;
                            KeyUsed(inputEvent);
                            break;

                        case KeyCode.Alpha3:
                            _playType = (PlayType)2;
                            KeyUsed(inputEvent);
                            break;

                        case KeyCode.Alpha4:
                            _playType = (PlayType)3;
                            KeyUsed(inputEvent);
                            break;
                        }
                    }
                    _playType = (PlayType)EditorGUILayout.EnumPopup(_playType);
                    switch (_playType)
                    {
                    case PlayType.Percent:
                        _forceTime = EditorGUILayout.Slider(_forceTime, 0, state.ClipLength);
                        EditorGUILayout.EndHorizontal();
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.LabelField("Frame " + _currentFrame);
                        break;

                    case PlayType.Frame:
                        state.CheckArraysLength();
                        _forceFrame = EditorGUILayout.IntSlider(_forceFrame, 0, state.RenderFrames.Length - 1);
                        EditorGUILayout.EndHorizontal();
                        EditorGUILayout.BeginHorizontal();
                        var width = EditorGUIUtility.labelWidth;
                        EditorGUIUtility.labelWidth = width * 0.2f;
                        var frameLayoutOptions = new GUILayoutOption[] {
                            GUILayout.ExpandWidth(false), GUILayout.Width(50)
                        };
                        state.RenderFrames[_forceFrame] = EditorGUILayout.Toggle("R: ", state.RenderFrames[_forceFrame], frameLayoutOptions);
                        state.FrameLengths[_forceFrame] = EditorGUILayout.FloatField("L: ", state.FrameLengths[_forceFrame], frameLayoutOptions);
                        EditorGUIUtility.labelWidth     = width;
                        var eventIndex    = System.Array.IndexOf(events, state.Events[_forceFrame]);
                        var newEventIndex = EditorGUILayout.Popup(eventIndex, events);
                        if (eventIndex != newEventIndex)
                        {
                            state.Events[_forceFrame] = events[newEventIndex];
                        }
                        var textBg = new GUIStyle();
                        if (!string.IsNullOrEmpty(state.Events[_forceFrame]))
                        {
                            textBg.normal.background = ErrorBackground;
                        }
                        state.Events[_forceFrame] = EditorGUILayout.TextArea(state.Events[_forceFrame], textBg);

                        break;

                    default:
                        EditorGUILayout.Space();
                        EditorGUILayout.Space();
                        EditorGUILayout.EndHorizontal();
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.LabelField("Frame " + _currentFrame);
                        break;
                    }
                    if (inputEvent.type == EventType.KeyDown && _playType == PlayType.Frame)
                    {
                        switch (inputEvent.keyCode)
                        {
                        case KeyCode.F:
                            state.RenderFrames[_forceFrame] = !state.RenderFrames[_forceFrame];
                            KeyUsed(inputEvent);
                            break;

                        case KeyCode.W:
                            _forceFrame++;
                            if (_forceFrame > state.RenderFrames.Length - 1)
                            {
                                _forceFrame = 0;
                            }
                            KeyUsed(inputEvent);
                            break;

                        case KeyCode.S:
                            _forceFrame--;
                            if (_forceFrame < 0)
                            {
                                _forceFrame = state.RenderFrames.Length - 1;
                            }
                            KeyUsed(inputEvent);
                            break;

                        case KeyCode.D:
                            state.FrameLengths[_forceFrame] += 0.25f;
                            KeyUsed(inputEvent);
                            break;

                        case KeyCode.A:
                            state.FrameLengths[_forceFrame] -= 0.25f;
                            KeyUsed(inputEvent);
                            break;

                        case KeyCode.X:
                            state.FrameLengths[_forceFrame] = 1;
                            KeyUsed(inputEvent);
                            break;
                        }
                    }
                    //EditorGUILayout.LabelField(_displayTime.ToString("F1"));
                }
                else
                {
                    if (GUILayout.Button("Play"))
                    {
                        Stop();
                        TimeManager.StartUnscaled(PlayAnimation(_script, state));
                    }
                    EditorGUILayout.Space();
                    EditorGUILayout.EndHorizontal();
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField("Playing " + _currentAnimation?.ClipName);
                }
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.EndVertical();
                //EditorGUILayout.Space();
                //EditorGUILayout.Space();
            }
            //if (GUILayout.Button("Add Animation State")) {
            //    _script.AnimationStates.Add(new AnimationState());
            //}
            //EditorGUILayout.Space();
            DropAreaGUI();

            if (EditorGUI.EndChangeCheck())
            {
                Undo.RecordObject(_script, _script.name);
                EditorUtility.SetDirty(_script);
            }
            EditorGUIUtility.labelWidth *= 2;
            if (GUILayout.Button("Save"))
            {
                var saveLocation = Application.dataPath + "/GameData";
                var path         = EditorUtility.SaveFilePanel("Save", saveLocation, "Layout", "json");
                if (!string.IsNullOrEmpty(path) && path.Length > 0)
                {
                    SaveFile(path);
                }
            }
            if (GUILayout.Button("Load"))
            {
                var saveLocation = Application.dataPath + "/GameData";
                var path         = EditorUtility.OpenFilePanel("Load", saveLocation, "json");
                if (!string.IsNullOrEmpty(path) && path.Length > 0)
                {
                    LoadFile(path);
                }
            }
            if (GUILayout.Button("Clear"))
            {
                if (EditorUtility.DisplayDialog("Are you sure?", "", "Yes", "No"))
                {
                    _script.Clips = new AnimationClipState[0];
                }
            }
            DrawDefaultInspector();
            if (AnimationMode.InAnimationMode())
            {
                if (GUILayout.Button("End Animation Mode"))
                {
                    AnimationMode.StopAnimationMode();
                    _canPlay = false;
                }
            }
        }
예제 #8
0
        private void DrawNodes()
        {
            if (_graph == null)
            {
                return;
            }
            var aeLabels  = AnimationEvents.GetNames().ToArray();
            var aeValues  = AnimationEvents.GetValues();
            var tagLabels = GraphNodeTags.GetNames().ToArray();
            var tagValues = GraphNodeTags.GetValues();

            for (int i = 0; i < _graph.Nodes.Count; i++)
            {
                var node       = _graph.Nodes[i];
                var maxWidth   = node.Rect.x * 0.8f;
                int styleIndex = 0;
                if (node == _graph.Default)
                {
                    styleIndex = StyleIndexDefault;
                }
                else if (node.IsGlobal)
                {
                    styleIndex = StyleIndexGlobal;
                }
                else if (node.HasConditions)
                {
                    styleIndex = node.AllowEarlyExit ? StyleHasEarlyExit : StyleHasConditions;
                }
                var style = node == _selected ? _nodeSelectedStyles[styleIndex] : _nodeStyles[styleIndex];
                GUILayout.BeginArea(node.Rect, style);
                EditorGUI.BeginChangeCheck();
                GUILayout.Space(10);
                GUILayout.Label(node.Title, StateGraphExtensions.NodeTextStyle, GUILayout.MaxWidth(maxWidth));
                GUILayout.BeginHorizontal();
                GUILayout.Space(20);
                var tagIndex = System.Array.IndexOf(tagValues, node.Tag);
                var newTag   = UnityEditor.EditorGUILayout.Popup(tagIndex, tagLabels, GUILayout.MaxWidth(maxWidth / 2));
                if (newTag != tagIndex)
                {
                    node.Tag = tagValues[newTag];
                }
                GUILayout.Label("ID: " + node.Id, StateGraphExtensions.NodeTextStyle, GUILayout.MaxWidth(maxWidth / 2));
                GUILayout.Space(20);
                GUILayout.EndHorizontal();
                if (node.DrawGui(StateGraphExtensions.NodeTextStyle, _nodeButtonStyle))
                {
                    Repaint();
                }
                for (int c = 0; c < node.Conditions.Count; c++)
                {
                    node.Conditions[c].DrawGui(node, StateGraphExtensions.NodeTextStyle, _nodeButtonStyle);
                    GUILayout.Space(10);
                }
                if (node.Conditions.Count > 0 && node.OutPoints.Count > 1)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Space(20);
                    GUILayout.Label("Else exit ", StateGraphExtensions.NodeTextStyle);
                    var indices = new string[node.OutPoints.Count];
                    for (int idx = 0; idx < indices.Length; idx++)
                    {
                        indices[idx] = idx.ToString();
                    }
                    node.DefaultExit = UnityEditor.EditorGUILayout.Popup(node.DefaultExit, indices, StateGraphExtensions.NodeTextStyle);
                    GUILayout.Space(20);
                    GUILayout.EndHorizontal();
                }
                GUILayout.BeginHorizontal();
                GUILayout.Space(20);
                var enterIndex = System.Array.IndexOf(aeValues, node.EnterEvent);
                var exitIndex  = System.Array.IndexOf(aeValues, node.ExitEvent);
                GUILayout.Label("Enter");
                var newEnter = UnityEditor.EditorGUILayout.Popup(enterIndex, aeLabels);
                if (newEnter != enterIndex)
                {
                    node.EnterEvent = aeValues[newEnter];
                }
                GUILayout.Label("Exit");
                var newExit = UnityEditor.EditorGUILayout.Popup(exitIndex, aeLabels);
                if (newExit != exitIndex)
                {
                    node.ExitEvent = aeValues[newExit];
                }
                GUILayout.Space(20);
                GUILayout.EndHorizontal();
                if (node.Conditions.Count < node.MaxConditions)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Space(20);
                    if (GUILayout.Button("Add Condition", _nodeButtonStyle))
                    {
                        node.Conditions.Add(new ConditionExit());
                        node.CheckSize();
                    }
                    GUILayout.Space(20);
                    GUILayout.EndHorizontal();
                }
                if (EditorGUI.EndChangeCheck())
                {
                    EditorUtility.SetDirty(node);
                }
                GUILayout.EndArea();
                var inSpacing = Mathf.Clamp(0.8f / node.InPoints.Count, 0.02f, 1);
                for (int c = 0; c < node.InPoints.Count; c++)
                {
                    if (node.InPoints[c] == null)
                    {
                        node.InPoints.RemoveAt(c);
                        break;
                    }
                    StateGraphExtensions.DrawConnectionPoint(node.InPoints[c], c, inSpacing, OnClickInPoint, RemoveConnectionPoint);
                }
                var outSpacing = Mathf.Clamp(0.8f / node.OutPoints.Count, 0.02f, 1);
                for (int c = 0; c < node.OutPoints.Count; c++)
                {
                    if (node.OutPoints[c] == null)
                    {
                        node.OutPoints.RemoveAt(c);
                        break;
                    }
                    StateGraphExtensions.DrawConnectionPoint(node.OutPoints[c], c, outSpacing, OnClickOutPoint, RemoveConnectionPoint);
                }
                if (node.InPoints.Count < node.InputMax)
                {
                    var addRect = new Rect(
                        node.Rect.x + ConnectionOutPoint.Width * 0.5f,
                        node.Rect.y + node.Rect.height - (ConnectionOutPoint.Height * 1.25f),
                        ConnectionOutPoint.Width, ConnectionOutPoint.Height);
                    if (GUI.Button(addRect, "+", StateGraphExtensions.NodeTextStyle))
                    {
                        node.InPoints.Add(new ConnectionInPoint(node, node.FindMinConnectionId()));
                        EditorUtility.SetDirty(node);
                    }
                }
                if (node.OutPoints.Count < node.OutputMax)
                {
                    var addRect = new Rect(
                        node.Rect.x + node.Rect.width - (ConnectionOutPoint.Width * 1.25f),
                        node.Rect.y + node.Rect.height - (ConnectionOutPoint.Height * 1.25f),
                        ConnectionOutPoint.Width, ConnectionOutPoint.Height);
                    if (GUI.Button(addRect, "+", StateGraphExtensions.NodeTextStyle))
                    {
                        node.OutPoints.Add(new ConnectionOutPoint(node, node.FindMinConnectionId()));
                        EditorUtility.SetDirty(node);
                    }
                }
            }
        }