Esempio n. 1
0
 public CompletionAwaiter(Anim anim) => this.anim = anim;
Esempio n. 2
0
        public static Anim To(object target, float duration, object props,
                              bool autoKillSimilarTarget = true, bool autoKillNullifiedTarget = true, bool preRun = true)
        {
            if (target is IList list)
            {
                Anim anim = null;

                foreach (var subtarget in list)
                {
                    anim = To(subtarget, duration, props, autoKillSimilarTarget, autoKillNullifiedTarget, preRun);
                }

                return(anim);
            }

            Type targetType = target.GetType();

            List <Action <float> > actions = new List <Action <float> >();
            Action onUpdate = null, onComplete = null;

            var key = target;
            var ease = Ease.Out3;
            var delay = 0f;

            // speed
            float speed = float.NaN, distance = float.NaN;

            foreach (var(property, index) in props.GetType().GetProperties().ItemIndex())
            {
                var type = property.PropertyType;
                var name = property.Name;

                if (name == "duration")
                {
                    duration = Convert.ToSingle(property.GetValue(props));
                    continue;
                }

                if (name == "delay")
                {
                    delay = Convert.ToSingle(property.GetValue(props));
                    continue;
                }

                if (name == "ease")
                {
                    ease = property.GetValue(props) as Func <float, float>;
                    continue;
                }

                if (name == "speed")
                {
                    speed = (float)property.GetValue(props);
                    continue;
                }

                // by the default, 'key' is 'target', but that value can be overrided
                if (name == "key")
                {
                    key = property.GetValue(props);
                    continue;
                }

                if (name == "onComplete")
                {
                    onComplete = (Action)property.GetValue(props);
                    continue;
                }

                if (name == "onUpdate")
                {
                    onUpdate = (Action)property.GetValue(props);
                    continue;
                }

                var propertyTarget = targetType.GetProperty(name);

                if (propertyTarget == null)
                {
                    throw new Exception($"the property \"{name}\" does not exist on target ({target})!");
                }

                bool propertyTypesMatch = propertyTarget.PropertyType == type;

                object from = propertyTarget.GetValue(target);
                object to   = property.GetValue(props);

                if (!propertyTypesMatch)
                {
                    // auto cast int to float
                    if (from is float fromF && to is int toI)
                    {
                        float toF = (float)toI;
                        float d   = toF - fromF;
                        actions.Add(t => propertyTarget.SetValue(target, fromF + d * t));
                        continue;
                    }

                    throw new Exception($"properties \"{name}\" do not match: " +
                                        $"\nfrom: {type}, to: {propertyTarget.PropertyType}");
                }

                if (from.GetType() != to.GetType())
                {
                    throw new Exception($"oups, ({from.GetType()})\"from\" & ({to.GetType()})\"to\" do not match the same type!");
                }

                Action <float> action;

                if (from is Quaternion fromQ && to is Quaternion toQ)
                {
                    action = t => propertyTarget.SetValue(target,
                                                          Quaternion.SlerpUnclamped(fromQ, toQ, t));
                }
Esempio n. 3
0
 public AwaitableCompletion(Anim anim) => this.anim = anim;