<commit_before>module Thralldom {
    export class StateMachineUtils {

        private static FallingGravityMultiplier = 0.3;
        private static StopFallingVelocityMultiplier = 0.6;
        private static JumpingErrorMargin = 1;

        private static dummyEventHandler = (state: number, object: DynamicObject) => { };
        private static dummyPredicate = (object: DynamicObject) => false;

        // MEMLEAK
        private static _jumpingImpulse: Ammo.btVector3;
        private static get JumpingImpulse(): Ammo.btVector3 {
            if (!StateMachineUtils._jumpingImpulse) {
                StateMachineUtils._jumpingImpulse = new Ammo.btVector3(0, Character.defaultSettings.jumpImpulse, 0);
            }
            return StateMachineUtils._jumpingImpulse;
        }

        private static getDummyState(index: number): State {
            return new State(index, State.emptyUpdate, StateMachineUtils.dummyEventHandler, StateMachineUtils.dummyEventHandler, StateMachineUtils.dummyPredicate);
        }

        private static ensureAnimationLoop(character: Character): boolean {
            var animation = character.animation;
            var animationData = character.animationData[character.stateMachine.current];

            var startTime = Utilities.convertFrameToTime(animationData.startFrame, animation);
            var endTime = Utilities.convertFrameToTime(animationData.endFrame, animation);

            if (animation.currentTime >= endTime) {
                animation.stop();
                animation.play(startTime);

                character.weapon.animation.stop();
                character.weapon.animation.play(startTime);

                return true;
            }
            return false;
        }

        private static pauseAnimationAfterEnd(character: Character): boolean {
            var animation = character.animation;
            var animationData = character.animationData[character.stateMachine.current];

            var startTime = Utilities.convertFrameToTime(animationData.startFrame, animation);
            var endTime = Utilities.convertFrameToTime(animationData.endFrame, animation);

            if (animation.currentTime >= endTime && !animation.isPaused) {
                animation.pause();
                character.weapon.animation.pause();

                return true;
            }
            return false;
        }

        private static restartAnimationIfNeeded(character: Character, previousState: number, currentState: number): void {
            if (previousState != currentState) {
                character.animation.stop();
                character.weapon.animation.stop();
                var startTime = Utilities.convertFrameToTime(character.animationData[currentState].startFrame, character.animation);
                character.animation.play(startTime);
                character.weapon.animation.play(startTime);
            }

        }

        private static getWalkingState(): State {
            var walking: State;

            var walkingEntry = (previous: number, hero: Character): void => {

                if (previous != CharacterStates.Walking) {
                    AudioManager.instance.playSound("Walking", hero.mesh, true, false);
                }

                StateMachineUtils.restartAnimationIfNeeded(hero, previous, CharacterStates.Walking);
                walking.data.isWalking = true;
            }

            var walkingUpdate = (delta: number, hero: Character): void => {
                StateMachineUtils.ensureAnimationLoop(hero);

                hero.setWalkingVelocity(delta);

                walking.data.isWalking = false;
            }

            var walkingExit = (next: number, hero: Character): void => {
                if (next != CharacterStates.Walking)
                    AudioManager.instance.stopSound("Walking", hero.mesh.id);
            }

            var walkingInterupt = (hero: Character): boolean => {
                var velocity = hero.rigidBody.getLinearVelocity();

                return !walking.data.isWalking// && GeometryUtils.almostZero(velocity.x()) && GeometryUtils.almostZero(velocity.z());
            }

            walking = new State(CharacterStates.Walking, walkingUpdate, walkingEntry, walkingExit, walkingInterupt);

            return walking;
        }

        private static getSprintingState(): State {
            var sprinting: State;

            var sprintingEntry = (previous: number, hero: Character): void => {

                if (previous != CharacterStates.Sprinting) {
                    AudioManager.instance.playSound("Sprinting", hero.mesh, true, false);
                }

                StateMachineUtils.restartAnimationIfNeeded(hero, previous, CharacterStates.Sprinting);

                sprinting.data.isSprinting = true;
            }

            var sprintingUpdate = (delta: number, hero: Character): void => {

                StateMachineUtils.ensureAnimationLoop(hero);

                var velocity = hero.setWalkingVelocity(delta, true);

                sprinting.data.isSprinting = false;
            }

            var sprintingExit = (next: number, hero: Character): void => {
                if (next != CharacterStates.Sprinting) {
                    AudioManager.instance.stopSound("Walking", hero.mesh.id);<commit_msg>Minor sound bug when sprinting<commit_after>module Thralldom {
    export class StateMachineUtils {

        private static FallingGravityMultiplier = 0.3;
        private static StopFallingVelocityMultiplier = 0.6;
        private static JumpingErrorMargin = 1;

        private static dummyEventHandler = (state: number, object: DynamicObject) => { };
        private static dummyPredicate = (object: DynamicObject) => false;

        // MEMLEAK
        private static _jumpingImpulse: Ammo.btVector3;
        private static get JumpingImpulse(): Ammo.btVector3 {
            if (!StateMachineUtils._jumpingImpulse) {
                StateMachineUtils._jumpingImpulse = new Ammo.btVector3(0, Character.defaultSettings.jumpImpulse, 0);
            }
            return StateMachineUtils._jumpingImpulse;
        }

        private static getDummyState(index: number): State {
            return new State(index, State.emptyUpdate, StateMachineUtils.dummyEventHandler, StateMachineUtils.dummyEventHandler, StateMachineUtils.dummyPredicate);
        }

        private static ensureAnimationLoop(character: Character): boolean {
            var animation = character.animation;
            var animationData = character.animationData[character.stateMachine.current];

            var startTime = Utilities.convertFrameToTime(animationData.startFrame, animation);
            var endTime = Utilities.convertFrameToTime(animationData.endFrame, animation);

            if (animation.currentTime >= endTime) {
                animation.stop();
                animation.play(startTime);

                character.weapon.animation.stop();
                character.weapon.animation.play(startTime);

                return true;
            }
            return false;
        }

        private static pauseAnimationAfterEnd(character: Character): boolean {
            var animation = character.animation;
            var animationData = character.animationData[character.stateMachine.current];

            var startTime = Utilities.convertFrameToTime(animationData.startFrame, animation);
            var endTime = Utilities.convertFrameToTime(animationData.endFrame, animation);

            if (animation.currentTime >= endTime && !animation.isPaused) {
                animation.pause();
                character.weapon.animation.pause();

                return true;
            }
            return false;
        }

        private static restartAnimationIfNeeded(character: Character, previousState: number, currentState: number): void {
            if (previousState != currentState) {
                character.animation.stop();
                character.weapon.animation.stop();
                var startTime = Utilities.convertFrameToTime(character.animationData[currentState].startFrame, character.animation);
                character.animation.play(startTime);
                character.weapon.animation.play(startTime);
            }

        }

        private static getWalkingState(): State {
            var walking: State;

            var walkingEntry = (previous: number, hero: Character): void => {

                if (previous != CharacterStates.Walking) {
                    AudioManager.instance.playSound("Walking", hero.mesh, true, false);
                }

                StateMachineUtils.restartAnimationIfNeeded(hero, previous, CharacterStates.Walking);
                walking.data.isWalking = true;
            }

            var walkingUpdate = (delta: number, hero: Character): void => {
                StateMachineUtils.ensureAnimationLoop(hero);

                hero.setWalkingVelocity(delta);

                walking.data.isWalking = false;
            }

            var walkingExit = (next: number, hero: Character): void => {
                if (next != CharacterStates.Walking)
                    AudioManager.instance.stopSound("Walking", hero.mesh.id);
            }

            var walkingInterupt = (hero: Character): boolean => {
                var velocity = hero.rigidBody.getLinearVelocity();

                return !walking.data.isWalking// && GeometryUtils.almostZero(velocity.x()) && GeometryUtils.almostZero(velocity.z());
            }

            walking = new State(CharacterStates.Walking, walkingUpdate, walkingEntry, walkingExit, walkingInterupt);

            return walking;
        }

        private static getSprintingState(): State {
            var sprinting: State;

            var sprintingEntry = (previous: number, hero: Character): void => {

                if (previous != CharacterStates.Sprinting) {
                    AudioManager.instance.playSound("Sprinting", hero.mesh, true, false);
                }

                StateMachineUtils.restartAnimationIfNeeded(hero, previous, CharacterStates.Sprinting);

                sprinting.data.isSprinting = true;
            }

            var sprintingUpdate = (delta: number, hero: Character): void => {

                StateMachineUtils.ensureAnimationLoop(hero);

                var velocity = hero.setWalkingVelocity(delta, true);

                sprinting.data.isSprinting = false;
            }

            var sprintingExit = (next: number, hero: Character): void => {
                if (next != CharacterStates.Sprinting) {
                    AudioManager.instance.stopSound("Sprinting", hero.mesh.id);
{'fraction_non_alphanumeric': 0.08933744509858892, 'fraction_numerical': 0.0018689842070834502, 'mean_word_length': 2.150426847218134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '2399431', 'n_tokens_mistral': 2648, 'n_tokens_neox': 2487, 'n_words': 527}