/// <summary> Sets the state at a given index and keyframe. </summary>
        /// <param name="index"> the index to get the state of. </param>
        /// <param name="keyframe"> the keyframe index to ge the state of </param>
        /// <param name="value"> the value to set it to. </param>
        /// <exception cref="ArgumentNullException"> <paramref name="index" /> or
        /// <paramref name="keyframe" /> are out of bounds. </exception>
        public void SetState(int index, int keyframe, Hitbox.Type value)
        {
            Argument.Check(Check.Range(index, IDs));
            Argument.Check(Check.Range(keyframe, Keyframes));
#if UNITY_EDITOR
            Undo.RecordObject(this, "Change Hitbox State");
#endif
            Keyframes[keyframe].States[index] = value;
            InternalOnChange();
        }
 /// <summary> Adds a new ID with all keyframes with a given type. </summary>
 /// <param name="id"> the ID to add </param>
 /// <param name="type"> the type to set each keyframe to </param>
 /// <returns> whether the element was added or not </returns>
 public bool AddID(int id, Hitbox.Type type)
 {
     if (IDs.Contains(id))
     {
         return(false);
     }
     IDs.Add(id);
     foreach (HitboxKeyframe hitboxKeyframe in Keyframes)
     {
         hitboxKeyframe.States.Add(type);
     }
     return(true);
 }
        public IEnumerable <KeyValuePair <float, Hitbox.Type> > GetProgression(int index)
        {
            if (Keyframes == null)
            {
                throw new InvalidOperationException();
            }
            Argument.Check(Check.Range(index, IDs));
            Hitbox.Type type = GetTypeOrDefault(index);
            foreach (HitboxKeyframe hitboxKeyframe in Keyframes)
            {
                yield return(new KeyValuePair <float, Hitbox.Type>(hitboxKeyframe.Time, type));

                type = hitboxKeyframe.States[index];
            }
            yield return(new KeyValuePair <float, Hitbox.Type>(1.0f, type));
        }
        static void AddHitbox(Hitbox.Type type)
        {
            var hitboxes = new List <Hitbox>();
            var rootMap  = new Dictionary <GameObject, List <Hitbox> >();
            var idGen    = new Random();

            Undo.IncrementCurrentGroup();
            foreach (GameObject go in Selection.gameObjects)
            {
                var hbGo = new GameObject();
                Undo.RegisterCreatedObjectUndo(hbGo, "Create Hitbox GameObject");
                var collider = Undo.AddComponent <SphereCollider>(hbGo);
                var hb       = Undo.AddComponent <Hitbox>(hbGo);
                hb.DefaultType = type;
                hb.ID          = idGen.Next();
                hitboxes.Add(hb);
                Undo.SetTransformParent(hb.transform, go.transform, "Parent Hitbox");
                hb.transform.Reset();
                Undo.RecordObject(collider, "Edit Collider Size");
                collider.radius = 1f / ((Vector3)(hb.transform.localToWorldMatrix * Vector3.one)).Max();
                var        character = hbGo.GetComponentInParent <Character>();
                GameObject rootGo    = character != null ? character.gameObject : hb.transform.root.gameObject;
                if (!rootMap.ContainsKey(rootGo))
                {
                    rootMap[rootGo] = new List <Hitbox>();
                }
                rootMap[rootGo].Add(hb);
            }
            foreach (KeyValuePair <GameObject, List <Hitbox> > set in rootMap)
            {
                Hitbox[] allHitboxes = set.Key.GetComponentsInChildren <Hitbox>();
                int      i           = allHitboxes.Length - set.Value.Count;
                Undo.RecordObjects(set.Value.ToArray(), "Name Changes");
                foreach (Hitbox hitbox in set.Value)
                {
                    hitbox.name = string.Format("{0}_hb_{1}_{2}", set.Key.name, type, i).ToLower();
                    i++;
                }
            }
            Selection.objects = hitboxes.GetGameObject().ToArray();
            Undo.SetCurrentGroupName(string.Format("Generate {0} Hitbox{1}",
                                                   type,
                                                   hitboxes.Count > 0 ? "es" : string.Empty));
            EventsEditorWindow.GetWindow().Repaint();
        }
        public static Color GetHitboxColor(Hitbox.Type type)
        {
            switch (type)
            {
            case Hitbox.Type.Offensive:
                return(Config.OffensiveHitboxColor);

            case Hitbox.Type.Damageable:
                return(Config.DamageableHitboxColor);

            case Hitbox.Type.Invincible:
                return(Config.IntangibleHitboxColor);

            case Hitbox.Type.Intangible:
                return(Config.InvincibleHitboxColor);

            default:
                return(Color.white);
            }
        }
Exemple #6
0
        public Color GetHitboxColor(Hitbox.Type type)
        {
            switch (type)
            {
            case Hitbox.Type.Offensive:
                return(OffensiveHitboxColor);

            case Hitbox.Type.Damageable:
                return(DamageableHitboxColor);

            case Hitbox.Type.Invincible:
                return(IntangibleHitboxColor);

            case Hitbox.Type.Intangible:
                return(InvincibleHitboxColor);

            default:
                return(Color.magenta);
            }
        }
Exemple #7
0
        private Hitbox ParseBox(Vector2u boxStart, Vector2u relativeZero)
        {
            Vector2u current = boxStart;

            current.X += 1;
            Color pixelColor = image.GetPixel(current.X, current.Y);
            Color boxColor   = pixelColor;

            while (pixelColor == boxColor)
            {
                current.X += 1;
                pixelColor = image.GetPixel(current.X, current.Y);
            }

            // Go back to where we last were ok.
            current.X -= 1;
            pixelColor = image.GetPixel(current.X, current.Y);

            while (pixelColor == boxColor)
            {
                current.Y += 1;
                pixelColor = image.GetPixel(current.X, current.Y);
            }

            // We want to end up at the +1, +1 relative to the
            // bottom right pixel of the hitbox.
            current.X += 1;

            IntRect rectangle = new IntRect(
                (int)(boxStart.X - relativeZero.X),
                (int)(boxStart.Y - relativeZero.Y),
                (int)(current.X - boxStart.X),
                (int)(current.Y - boxStart.Y));

            Hitbox.Type type = pixelColor == hitColor
                                ? Hitbox.Type.Hitbox
                                : Hitbox.Type.Hurtbox;

            return(new Hitbox(rectangle, type));
        }
        /// <summary> Adds a new HitboxKeyfram at the a specified time. </summary>
        /// <param name="time"> the time to add the keyframe at </param>
        /// <param name="defaultType"> the default type to use for every hitbox if no keyframes are currently present </param>
        /// <returns> the created keyframe </returns>
        /// <exception cref="ArgumentNullException"> </exception>
        public HitboxKeyframe AddKeyframe(float time = 0f, Hitbox.Type defaultType = Hitbox.Type.Inactive)
        {
            time = Mathf.Clamp01(time);
#if UNITY_EDITOR
            Undo.RecordObject(this, "Add Keyframe");
#endif
            HitboxKeyframe prevKeyframe = PrevKeyframe(time);
            var            keyframe     = new HitboxKeyframe {
                Time = time,
            };
            if (prevKeyframe != null)
            {
                keyframe.States = new List <Hitbox.Type>(prevKeyframe.States);
            }
            else
            {
                keyframe.States = Enumerable.Repeat(defaultType, IDs.Count).ToList();
            }
            Keyframes.Add(keyframe);
            InternalOnChange();
            return(keyframe);
        }
 void LabelGUI()
 {
     using (hGUI.Vertical(EditorStyles.helpBox.WithoutMargins().WithoutPadding(), GUILayout.Width(150))) {
         using (hGUI.Color(Config.Debug.GetHitboxColor(state))) {
             state =
                 (Hitbox.Type)EditorGUILayout.EnumPopup(state, EditorStyles.toolbarPopup, GUILayout.Height(18));
         }
         if (IDs != null)
         {
             for (hitboxIndex = 0; hitboxIndex < IDs.Count; hitboxIndex++)
             {
                 DrawHitboxLabel(hitboxIndex);
             }
         }
         hGUI.Space();
         Events =
             (EventData)
             EditorGUILayout.ObjectField(GUIContent.none,
                                         Events,
                                         typeof(EventData),
                                         false,
                                         GUILayout.Width(150));
     }
 }
Exemple #10
0
 public Color GetHitboxColor(Hitbox.Type type)
 {
     return(_colorMap[type]);
 }
        /// <summary> Unity callback. Called when the EditorWindow is created. </summary>
        void OnEnable()
        {
            changes      = new List <Edit>();
            DragListener =
                new EventListener().AddListener(EventType.DragUpdated,
                                                delegate {
                DragAndDrop.visualMode = Behaviour && GetObjectHitboxes(DragAndDrop.objectReferences).Any()
                            ? DragAndDropVisualMode.Generic
                            : DragAndDropVisualMode.None;
            })
                .AddListeners(new[] { EventType.DragExited, EventType.DragPerform },
                              delegate {
                if (Events == null)
                {
                    return;
                }
                DragAndDrop.AcceptDrag();
                foreach (Hitbox hb in GetObjectHitboxes(DragAndDrop.objectReferences))
                {
                    Events.AddHitbox(hb);
                }
            });

            GeneralEventListener = new EventListener().AddListener(EventType.MouseDrag,
                                                                   delegate(Event evt) {
                if (dragStart == DragStart.SeekBar)
                {
                    SetSeekTime(evt);
                }
                if (dragStart == DragStart.Keyframe)
                {
                    SetKeyframeTime(evt, selectedKeyframe);
                }
            })
                                   .AddListener(EventType.MouseUp, delegate { dragStart = DragStart.None; })
                                   .AddListener(EventType.KeyDown,
                                                delegate(Event evt) {
                if (evt.keyCode == KeyCode.Delete && selectedKeyframe != null)
                {
                    Events.DeleteKeyframe(selectedKeyframe);
                    Repaint();
                }
            });

            SeekListener = new EventListener().AddListeners(new[] { EventType.MouseDown, EventType.MouseDrag },
                                                            delegate(Event evt) {
                if (evt.type == EventType.MouseDown)
                {
                    dragStart = DragStart.SeekBar;
                }
                if (evt.type == EventType.MouseDrag && dragStart != DragStart.SeekBar)
                {
                    return;
                }
                SetSeekTime(evt);
            });

            SetListener = new EventListener().AddListeners(new[] { EventType.MouseDown, EventType.MouseDrag },
                                                           delegate(Event evt) {
                if (evt.type == EventType.MouseDown)
                {
                    dragStart = DragStart.EditArea;
                }
                if (evt.type == EventType.MouseDrag && dragStart != DragStart.EditArea)
                {
                    return;
                }
                Hitbox.Type type = evt.button == 0 ? state : GetTypeOrDefault(hitboxIndex);
                if (Events == null || Events.GetState(hitboxIndex, keyframeIndex) == type)
                {
                    return;
                }
                changes.Add(new Edit {
                    index = hitboxIndex, keyframe = keyframeIndex, type = type
                });
            });

            KeyframeListener = new EventListener().AddListener(EventType.MouseDown,
                                                               delegate {
                dragStart        = DragStart.Keyframe;
                selectedKeyframe = Keyframes[keyframeIndex];
            });

            HitboxListener = new EventListener().AddListener(EventType.MouseDown,
                                                             delegate {
                Hitbox hitbox = GetHitbox(IDs[hitboxIndex]);
                if (hitbox != null)
                {
                    Selection.activeGameObject = hitbox.gameObject;
                }
            });

            OnAssetEdit += Repaint;

            Characters = new ObjectSelector <CharacterData>(c => c.name, c => c != null);

            Characters.SelectionChanged += UpdateCharacter;
            Refresh();
        }