private void SetActiveAnimationState(AnimationState animationState)
        {
            if (_activeAnimationState == animationState ||
                _activeModelDrawConditionState == null)
            {
                return;
            }

            var modelInstance = _activeModelDrawConditionState.Model;

            var firstAnimationBlock = animationState.Animations
                                      .FirstOrDefault();

            if (firstAnimationBlock != null)
            {
                foreach (var animation in firstAnimationBlock.Animations)
                {
                    var anim = animation.Value;
                    //Check if the animation does really exist
                    if (anim != null)
                    {
                        var animationInstance = new AnimationInstance(modelInstance, anim);
                        modelInstance.AnimationInstances.Add(animationInstance);
                        animationInstance.Play();
                        break;
                    }
                }
            }
        }
Пример #2
0
        protected virtual bool SetActiveAnimationState(AnimationState animationState, Random random)
        {
            if (animationState == _activeAnimationState && (_activeModelDrawConditionState?.StillActive() ?? false))
            {
                return(false);
            }

            if (animationState == null ||
                animationState.Animations.Count == 0 ||
                _activeModelDrawConditionState == null)
            {
                return(true);
            }

            PreviousAnimationState = _activeAnimationState;

            if (_activeModelDrawConditionState?.Model != null)
            {
                foreach (var animationInstance in _activeModelDrawConditionState.Model.AnimationInstances)
                {
                    animationInstance.Stop();
                }
            }

            var modelInstance = _activeModelDrawConditionState.Model;

            modelInstance.AnimationInstances.Clear();
            _activeAnimationState = animationState;

            var animationBlock = animationState.Animations[random.Next(0, animationState.Animations.Count - 1)];

            if (animationBlock != null)
            {
                var anim = animationBlock.Animation.Value;
                //Check if the animation does really exist
                if (anim != null)
                {
                    var flags             = animationState.Flags;
                    var mode              = animationBlock.AnimationMode;
                    var animationInstance = new AnimationInstance(modelInstance.ModelBoneInstances, anim, mode, flags, GameObject, _context.Random);
                    modelInstance.AnimationInstances.Add(animationInstance);
                    animationInstance.Play(animationBlock.AnimationSpeedFactorRange.GetValue(random));
                }
            }

            if (animationState != null && animationState.Script != null)
            {
                _context.Scene3D.Game.Lua.ExecuteDrawModuleLuaCode(this, animationState.Script);
            }

            NLog.LogManager.GetCurrentClassLogger().Info($"Set active animation state for {GameObject.Definition.Name}");

            return(true);
        }
Пример #3
0
        private void SetActiveAnimationState(AnimationState animationState, Random random)
        {
            if (animationState == null ||
                animationState.Animations.Count == 0 ||
                _activeModelDrawConditionState == null)
            {
                return;
            }

            if (_activeModelDrawConditionState.StillActive() == false)
            {
                _activeAnimationState = null;
            }
            else if (animationState == _activeAnimationState)
            {
                return;
            }

            _activeAnimationState = animationState;

            var modelInstance = _activeModelDrawConditionState.Model;

            modelInstance.AnimationInstances.Clear();

            var animationBlock = animationState.Animations[random.Next(0, animationState.Animations.Count - 1)];

            if (animationBlock != null)
            {
                foreach (var animation in animationBlock.Animations)
                {
                    var anim = animation.Value;
                    //Check if the animation does really exist
                    if (anim != null)
                    {
                        var flags             = animationState.Flags;
                        var mode              = animationBlock.AnimationMode;
                        var animationInstance = new AnimationInstance(modelInstance, anim, mode, flags, GameObject);
                        modelInstance.AnimationInstances.Add(animationInstance);
                        animationInstance.Play();
                        break;
                    }
                }
            }
        }
Пример #4
0
        public void OnceBackwardsTest()
        {
            var modelBoneInstances = NewDefaultModelBoneInstances();
            var animation          = NewBasicAnimationInstance();

            var instance = new AnimationInstance(modelBoneInstances, animation, AnimationMode.OnceBackwards,
                                                 AnimationFlags.StartFrameLast, null, new QuoteUnquoteRandom());

            instance.Play();
            var time = TimeInterval.Zero;

            for (var i = 4; i >= 0; i--)
            {
                instance.Update(time);
                time = new TimeInterval(time.TotalTime + TimeSpan.FromSeconds(1),
                                        TimeSpan.FromSeconds(1));

                Assert.Equal(i, modelBoneInstances[0].AnimatedOffset.Translation.X);
            }
        }
Пример #5
0
        public void StartAtEndManualTest()
        {
            var modelBoneInstances = NewDefaultModelBoneInstances();
            var animation          = NewBasicAnimationInstance();

            var instance = new AnimationInstance(modelBoneInstances, animation, AnimationMode.Manual,
                                                 AnimationFlags.StartFrameLast, null, new QuoteUnquoteRandom());

            instance.Play();
            var time = TimeInterval.Zero;

            for (var i = 0; i < 5; i++)
            {
                instance.Update(time);
                time = new TimeInterval(time.TotalTime + TimeSpan.FromSeconds(1),
                                        TimeSpan.FromSeconds(1));

                // always show the last frame
                Assert.Equal(4, modelBoneInstances[0].AnimatedOffset.Translation.X);
            }
        }
Пример #6
0
        protected virtual bool SetActiveAnimationState(AnimationState animationState, Random random)
        {
            if (animationState == _activeAnimationState && (_activeModelDrawConditionState?.StillActive() ?? false))
            {
                return(false);
            }

            if (animationState == null ||
                animationState.Animations.Count == 0 ||
                _activeModelDrawConditionState == null)
            {
                return(true);
            }

            var modelInstance = _activeModelDrawConditionState.Model;

            modelInstance.AnimationInstances.Clear();
            _activeAnimationState = animationState;

            var animationBlock = animationState.Animations[random.Next(0, animationState.Animations.Count - 1)];

            if (animationBlock != null)
            {
                foreach (var animation in animationBlock.Animations)
                {
                    var anim = animation.Value;
                    //Check if the animation does really exist
                    if (anim != null)
                    {
                        var flags             = animationState.Flags;
                        var mode              = animationBlock.AnimationMode;
                        var animationInstance = new AnimationInstance(modelInstance, anim, mode, flags, GameObject);
                        modelInstance.AnimationInstances.Add(animationInstance);
                        animationInstance.Play();
                        break;
                    }
                }
            }
            return(true);
        }
    IEnumerator StartAnimCoroutine(AnimationInstance animInstance)
    {
        if (animInstance.startTime > 0)
        {
            yield return(new WaitForSeconds(animInstance.startTime));
        }
        // animInstance.objectData = new ObjectStateData(items[animInstance.itemIndex]);
        print("Start Animation instance " + animInstance.animationData.name);
        yield return(StartCoroutine(animInstance.Play()));

        nbCoroutinesCompleted++;
        if (nbCoroutinesCompleted == nbCoroutines)
        {
            print("Sequence completed");
            //restore items' state
            if (replayType == SequenceReplayType.Reset)
            {
                Reset();
                RestoreItemStates();
            }

            //restore items' state and play again
            else if (replayType == SequenceReplayType.Loop)
            {
                Reset();
                RestoreItemStates();
                Play();
            }
            //Don't restore items' state and play again
            else if (replayType == SequenceReplayType.Accumulate)
            {
                Play();
            }
            //Don't restore items' state
            else if (replayType == SequenceReplayType.Stop)
            {
                Reset();
            }
        }
    }
Пример #8
0
        public void OnceBackwardsRandomStartTest(int rngSeed)
        {
            var modelBoneInstances = NewDefaultModelBoneInstances();
            var animation          = NewBasicAnimationInstance();
            var random             = new QuoteUnquoteRandom(rngSeed);

            var instance = new AnimationInstance(modelBoneInstances, animation, AnimationMode.OnceBackwards,
                                                 AnimationFlags.RandomStart, null, random);

            instance.Play();

            var   time     = TimeInterval.Zero;
            float?previous = null;

            for (var i = 5; i > 0; i--)
            {
                instance.Update(time);
                time = new TimeInterval(time.TotalTime + TimeSpan.FromSeconds(1),
                                        TimeSpan.FromSeconds(1));
                if (previous is null)
                {
                    previous = modelBoneInstances[0].AnimatedOffset.Translation.X;
                    continue;
                }

                var target = previous.Value - 1;
                if (target < 0)
                {
                    target = 0;
                }

                Assert.Equal(target, modelBoneInstances[0].AnimatedOffset.Translation.X, new ToleranceEqualityComparer(0.001f));
                previous = modelBoneInstances[0].AnimatedOffset.Translation.X;
            }
            instance.Stop();
        }
        private W3dModelDrawConditionState CreateModelDrawConditionStateInstance(ModelConditionState conditionState)
        {
            // Load model, fallback to default model.
            var model = conditionState.Model?.Value ?? _defaultConditionState.Model?.Value;

            ModelInstance modelInstance = null;

            if (model != null)
            {
                modelInstance = model.CreateInstance(_loadContext);
            }

            if (modelInstance != null)
            {
                // TODO: Multiple animations. Shouldn't play all of them. I think
                // we should randomly choose one of them?
                // And there is also IdleAnimation.
                var firstAnimation = conditionState.ConditionAnimations
                                     .Concat(conditionState.IdleAnimations)
                                     .LastOrDefault();
                if (firstAnimation != null)
                {
                    var animation = firstAnimation.Animation.Value;

                    if (animation != null)
                    {
                        var animationInstance = new AnimationInstance(modelInstance, animation);
                        modelInstance.AnimationInstances.Add(animationInstance);
                        animationInstance.Play();
                    }
                }
            }

            var particleSystems = new List <ParticleSystem>();

            if (modelInstance != null)
            {
                foreach (var particleSysBone in conditionState.ParticleSysBones)
                {
                    var particleSystemTemplate = _loadContext.AssetStore.FXParticleSystemTemplates.GetByName(particleSysBone.ParticleSystem);
                    if (particleSystemTemplate == null)
                    {
                        particleSystemTemplate = _loadContext.AssetStore.ParticleSystemTemplates.GetByName(particleSysBone.ParticleSystem)?.ToFXParticleSystemTemplate();

                        if (particleSystemTemplate == null)
                        {
                            throw new InvalidOperationException("Missing referenced particle system: " + particleSysBone.ParticleSystem);
                        }
                    }

                    var bone = modelInstance.Model.BoneHierarchy.Bones.FirstOrDefault(x => string.Equals(x.Name, particleSysBone.BoneName, StringComparison.OrdinalIgnoreCase));
                    if (bone == null)
                    {
                        // TODO: Should this ever happen?
                        continue;
                    }

                    particleSystems.Add(new ParticleSystem(
                                            particleSystemTemplate,
                                            _loadContext,
                                            () => ref modelInstance.AbsoluteBoneTransforms[bone.Index]));
                }
            }

            return(modelInstance != null
               ? new W3dModelDrawConditionState(modelInstance, particleSystems)
               : null);
        }
Пример #10
0
 public override void Activate()
 {
     _animationInstance.Play();
 }
Пример #11
0
        private W3dModelDrawConditionState CreateModelDrawConditionStateInstance(ModelConditionState conditionState)
        {
            ModelInstance modelInstance = null;

            if (!string.Equals(conditionState.Model, "NONE", StringComparison.OrdinalIgnoreCase))
            {
                var w3dFilePath = Path.Combine("Art", "W3D", conditionState.Model + ".W3D");
                var model       = _contentManager.Load <Model>(w3dFilePath);
                if (model != null)
                {
                    modelInstance = model.CreateInstance(_contentManager.GraphicsDevice);
                }
            }

            if (modelInstance != null)
            {
                // TODO: Multiple animations. Shouldn't play all of them. I think
                // we should randomly choose one of them?
                // And there is also IdleAnimation.
                var firstAnimation = conditionState.Animations
                                     .Concat(conditionState.IdleAnimations)
                                     .LastOrDefault();
                if (firstAnimation != null)
                {
                    var splitName = firstAnimation.Animation.Split('.');

                    var w3dFilePath = Path.Combine("Art", "W3D", splitName[0] + ".W3D");
                    var model       = _contentManager.Load <Model>(w3dFilePath);

                    if (model.Animations.Length == 0)
                    {
                        // TODO: What is the actual algorithm here?
                        w3dFilePath = Path.Combine("Art", "W3D", splitName[1] + ".W3D");
                        model       = _contentManager.Load <Model>(w3dFilePath);
                    }

                    if (model != null)
                    {
                        var animation = model.Animations.FirstOrDefault(x => string.Equals(x.Name, splitName[1], StringComparison.OrdinalIgnoreCase));
                        if (animation != null)
                        {
                            // TODO: Should this ever be null?

                            var animationInstance = new AnimationInstance(modelInstance, animation);

                            modelInstance.AnimationInstances.Add(animationInstance);

                            animationInstance.Play();
                        }
                    }
                }
            }

            var particleSystems = new List <ParticleSystem>();

            if (modelInstance != null)
            {
                foreach (var particleSysBone in conditionState.ParticleSysBones)
                {
                    var particleSystemDefinition = _contentManager.IniDataContext.ParticleSystems.First(x => x.Name == particleSysBone.ParticleSystem);
                    var bone = modelInstance.Model.Bones.FirstOrDefault(x => string.Equals(x.Name, particleSysBone.BoneName, StringComparison.OrdinalIgnoreCase));
                    if (bone == null)
                    {
                        // TODO: Should this ever happen?
                        continue;
                    }

                    particleSystems.Add(new ParticleSystem(
                                            _contentManager,
                                            particleSystemDefinition,
                                            () => ref modelInstance.AbsoluteBoneTransforms[bone.Index]));
                }
            }

            return(modelInstance != null
               ? new W3dModelDrawConditionState(modelInstance, particleSystems)
               : null);
        }
Пример #12
0
        private W3dModelDrawConditionState CreateModelDrawConditionStateInstance(ModelConditionState conditionState)
        {
            ModelInstance modelInstance = null;

            if (!string.Equals(conditionState.Model, "NONE", StringComparison.OrdinalIgnoreCase))
            {
                var w3dFilePath = Path.Combine("Art", "W3D", conditionState.Model + ".W3D");
                var model       = _contentManager.Load <Model>(w3dFilePath);
                if (model != null)
                {
                    modelInstance = model.CreateInstance(_contentManager.GraphicsDevice);
                }
            }

            if (modelInstance != null)
            {
                // TODO: Multiple animations. Shouldn't play all of them. I think
                // we should randomly choose one of them?
                // And there is also IdleAnimation.
                var firstAnimation = conditionState.ConditionAnimations
                                     .Concat(conditionState.IdleAnimations)
                                     .LastOrDefault();
                if (firstAnimation != null)
                {
                    if (!_contentManager.DataContext.Animations.TryGetValue(firstAnimation.Animation, out var animation))
                    {
                        var splitName = firstAnimation.Animation.Split('.');

                        var w3dFilePath = Path.Combine("Art", "W3D", splitName[1] + ".W3D");
                        var w3dEntry    = _contentManager.FileSystem.GetFile(w3dFilePath);
                        var w3dFile     = W3dFile.FromFileSystemEntry(w3dEntry);

                        var animations = ModelLoader.LoadAnimations(w3dFile, _contentManager);
                        if (animations.Length != 1 || !string.Equals(animations[0].Name, firstAnimation.Animation, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new NotSupportedException();
                        }

                        animation = animations[0];
                    }

                    var animationInstance = new AnimationInstance(modelInstance, animation);
                    modelInstance.AnimationInstances.Add(animationInstance);
                    animationInstance.Play();
                }
            }

            var particleSystems = new List <ParticleSystem>();

            if (modelInstance != null)
            {
                foreach (var particleSysBone in conditionState.ParticleSysBones)
                {
                    var particleSystemDefinition = _contentManager.IniDataContext.ParticleSystems.First(x => x.Name == particleSysBone.ParticleSystem);
                    var bone = modelInstance.Model.BoneHierarchy.Bones.FirstOrDefault(x => string.Equals(x.Name, particleSysBone.BoneName, StringComparison.OrdinalIgnoreCase));
                    if (bone == null)
                    {
                        // TODO: Should this ever happen?
                        continue;
                    }

                    particleSystems.Add(new ParticleSystem(
                                            _contentManager,
                                            particleSystemDefinition,
                                            () => ref modelInstance.AbsoluteBoneTransforms[bone.Index]));
                }
            }

            return(modelInstance != null
               ? new W3dModelDrawConditionState(modelInstance, particleSystems)
               : null);
        }
Пример #13
0
        private W3dModelDrawConditionState CreateModelDrawConditionStateInstance(ModelConditionState conditionState)
        {
            // Load model, fallback to default model.
            var model = conditionState.Model?.Value ?? _defaultConditionState.Model?.Value;

            ModelInstance modelInstance = null;

            if (model != null)
            {
                modelInstance = model.CreateInstance(_loadContext);
            }

            if (modelInstance != null)
            {
                // TODO: Multiple animations. Shouldn't play all of them. I think
                // we should randomly choose one of them?
                // And there is also IdleAnimation.
                var firstAnimation = conditionState.ConditionAnimations
                                     .Concat(conditionState.IdleAnimations)
                                     .LastOrDefault();
                if (firstAnimation != null)
                {
                    var animation = firstAnimation.Animation.Value;

                    if (animation != null)
                    {
                        var mode              = conditionState.AnimationMode;
                        var flags             = conditionState.Flags;
                        var animationInstance = new AnimationInstance(modelInstance, animation, mode, flags);
                        modelInstance.AnimationInstances.Add(animationInstance);
                        animationInstance.Play();
                    }
                }
            }

            var particleSystems = new List <ParticleSystem>();

            if (modelInstance != null)
            {
                foreach (var particleSysBone in conditionState.ParticleSysBones)
                {
                    var particleSystemTemplate = particleSysBone.ParticleSystem.Value;
                    if (particleSystemTemplate == null)
                    {
                        throw new InvalidOperationException();
                    }

                    var bone = modelInstance.Model.BoneHierarchy.Bones.FirstOrDefault(x => string.Equals(x.Name, particleSysBone.BoneName, StringComparison.OrdinalIgnoreCase));
                    if (bone == null)
                    {
                        // TODO: Should this ever happen?
                        continue;
                    }

                    particleSystems.Add(new ParticleSystem(
                                            particleSystemTemplate,
                                            _loadContext,
                                            () => ref modelInstance.AbsoluteBoneTransforms[bone.Index]));
                }
            }

            if (modelInstance != null)
            {
                if (conditionState.HideSubObject != null)
                {
                    foreach (var hideSubObject in conditionState.HideSubObject)
                    {
                        var item = modelInstance.ModelBoneInstances.Select((value, i) => new { i, value }).FirstOrDefault(x => x.value.Name.EndsWith("." + hideSubObject.ToUpper()));
                        if (item != null)
                        {
                            modelInstance.BoneVisibilities[item.i] = false;
                        }
                    }
                }
                if (conditionState.ShowSubObject != null)
                {
                    foreach (var showSubObject in conditionState.ShowSubObject)
                    {
                        var item = modelInstance.ModelBoneInstances.Select((value, i) => new { i, value }).FirstOrDefault(x => x.value.Name.EndsWith("." + showSubObject.ToUpper()));
                        if (item != null)
                        {
                            modelInstance.BoneVisibilities[item.i] = true;
                        }
                    }
                }
            }

            return(modelInstance != null
               ? new W3dModelDrawConditionState(modelInstance, particleSystems)
               : null);
        }