<commit_before>﻿using OpenSage.Mathematics;
using System;
using System.Numerics;

namespace OpenSage.Graphics.Animation
{
    public sealed class AnimationComponent : EntityComponent
    {
        private TransformComponent[] _bones;

        private bool _playing;

        private TimeSpan _currentTimeValue;

        private bool[] _originalVisibilities;

        private Transform[] _boneTransforms;

        private Animation _animation;

        public Animation Animation
        {
            get { return _animation; }
            set
            {
                _animation = value;

                _currentTimeValue = TimeSpan.Zero;
            }
        }

        public void Play()
        {
            if (_playing)
            {
                return;
            }

            var modelComponent = Entity.GetComponent<ModelComponent>();
            if (modelComponent.Bones != _bones)
            {
                _bones = modelComponent.Bones;
                _originalVisibilities = new bool[_bones.Length];
                _boneTransforms = new Transform[_bones.Length];
            }

            ResetBoneTransforms();

            for (var i = 0; i < _bones.Length; i++)
            {
                _originalVisibilities[i] = _bones[i].Entity.Visible;
            }

            _playing = true;
        }

        public void Stop()
        {
            if (!_playing)
            {
                return;
            }

            // TODO: Reset to original transforms and visibilities?

            _playing = false;
        }

        private void ResetBoneTransforms()
        {
            for (var i = 0; i < _boneTransforms.Length; i++)
            {
                _boneTransforms[i].Translation = Vector3.Zero;
                _boneTransforms[i].Rotation = Quaternion.Identity;

                _bones[i].Entity.Visible = true;
            }
        }

        internal void Update(GameTime gameTime)
        {
            if (!_playing)
            {
                return;
            }

            UpdateBoneTransforms(gameTime);

            for (var i = 0; i < _boneTransforms.Length; i++)
            {
                _bones[i].LocalPosition = _boneTransforms[i].Translation;
                _bones[i].LocalRotation = _boneTransforms[i].Rotation;
            }
        }

        private void UpdateBoneTransforms(GameTime gameTime)
        {
            var time = _currentTimeValue + gameTime.ElapsedGameTime;

            // If we reached the end, loop back to the start.
            while (time >= _animation.Duration)
            {
                time -= _animation.Duration;
            }

            // If we've just moved backwards, reset the keyframe indices.
            if (time < _currentTimeValue)
            {
                ResetBoneTransforms();
            }

            _currentTimeValue = time;

            for (var i = 0; i < _animation.Clips.Length; i++)
            {
                Keyframe? previous = null;
                Keyframe? next = null;

                var clip = _animation.Clips[i];

                for (var j = 0; j < clip.Keyframes.Length; j++)
                {
                    var keyframe = clip.Keyframes[j];

                    if (keyframe.Time > _currentTimeValue)
                    {<commit_msg>Don't crash when an animation channel references a non-existent bone<commit_after>﻿using OpenSage.Mathematics;
using System;
using System.Numerics;

namespace OpenSage.Graphics.Animation
{
    public sealed class AnimationComponent : EntityComponent
    {
        private TransformComponent[] _bones;

        private bool _playing;

        private TimeSpan _currentTimeValue;

        private bool[] _originalVisibilities;

        private Transform[] _boneTransforms;

        private Animation _animation;

        public Animation Animation
        {
            get { return _animation; }
            set
            {
                _animation = value;

                _currentTimeValue = TimeSpan.Zero;
            }
        }

        public void Play()
        {
            if (_playing)
            {
                return;
            }

            var modelComponent = Entity.GetComponent<ModelComponent>();
            if (modelComponent.Bones != _bones)
            {
                _bones = modelComponent.Bones;
                _originalVisibilities = new bool[_bones.Length];
                _boneTransforms = new Transform[_bones.Length];
            }

            ResetBoneTransforms();

            for (var i = 0; i < _bones.Length; i++)
            {
                _originalVisibilities[i] = _bones[i].Entity.Visible;
            }

            _playing = true;
        }

        public void Stop()
        {
            if (!_playing)
            {
                return;
            }

            // TODO: Reset to original transforms and visibilities?

            _playing = false;
        }

        private void ResetBoneTransforms()
        {
            for (var i = 0; i < _boneTransforms.Length; i++)
            {
                _boneTransforms[i].Translation = Vector3.Zero;
                _boneTransforms[i].Rotation = Quaternion.Identity;

                _bones[i].Entity.Visible = true;
            }
        }

        internal void Update(GameTime gameTime)
        {
            if (!_playing)
            {
                return;
            }

            UpdateBoneTransforms(gameTime);

            for (var i = 0; i < _boneTransforms.Length; i++)
            {
                _bones[i].LocalPosition = _boneTransforms[i].Translation;
                _bones[i].LocalRotation = _boneTransforms[i].Rotation;
            }
        }

        private void UpdateBoneTransforms(GameTime gameTime)
        {
            var time = _currentTimeValue + gameTime.ElapsedGameTime;

            // If we reached the end, loop back to the start.
            while (time >= _animation.Duration)
            {
                time -= _animation.Duration;
            }

            // If we've just moved backwards, reset the keyframe indices.
            if (time < _currentTimeValue)
            {
                ResetBoneTransforms();
            }

            _currentTimeValue = time;

            for (var i = 0; i < _animation.Clips.Length; i++)
            {
                Keyframe? previous = null;
                Keyframe? next = null;

                var clip = _animation.Clips[i];

                if (clip.Bone >= _boneTransforms.Length)
                {
                    continue;
                }

                for (var j = 0; j < clip.Keyframes.Length; j++)
                {
                    var keyframe = clip.Keyframes[j];

                    if (keyframe.Time > _currentTimeValue)
                    {
{'fraction_non_alphanumeric': 0.08013527422437877, 'fraction_numerical': 0.00176444640494045, 'mean_word_length': 1.2833165491775764, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1561596', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1626, 'n_words': 395}