<commit_before>import * as Cardinality from 'helpers/Cardinality';
import { scalePlayer, canJump } from 'physics-utils';
import {
    getCardinalityOfVector, getCameraDistanceToPlayer, lerp,
    playerV3ToFinishEntityV3Collision, playerToCircleCollision3dTo2d,
    playerToBoxCollision3dTo2d,
} from 'helpers/Utils';
import { Vector3, } from 'three';

// Warning: duplicated in playerScaleReducer
const scaleStartDelayMs = 250;

export default function entityInteractionReducer(
    keysDown:Object,
    actions:Object,
    gameData:Object,
    oldState:Object,
    currentState:Object,
    next:Function
) {

    const {
        currentLevelTouchyArray, playerRadius, playerDensity, playerScale,
        currentLevelId, nextChapters, previousChapterFinishEntity,
        previousChapterEntity, previousChapter, cameraFov,
        currentGameChapterId, currentChapterId, allChaptersArray,
        allEntities,
    } = gameData;

    const {
        cameraPosition, world, playerBody, textQueue, sideEffectQueue,
        moveQueue,
    } = oldState;

    const { playerPositionV3, time, } = currentState;

    let newState = {};

    for( let i = 0; i < currentLevelTouchyArray.length; i++ ) {

        const entity = currentLevelTouchyArray[ i ];

        const isSameScale = entity.scale.x === playerScale;

        if( entity.type === 'finish' ) {

            // This would make a good abstracted out function, probably
            if(
                playerV3ToFinishEntityV3Collision(
                    playerPositionV3,
                    playerRadius,
                    entity.position,
                    entity.scale,
                ) &&
                isSameScale
            ) {

                newState.isAdvancing = true;

                // this is almost certainly wrong to determine which way
                // the finish line element is facing
                const cardinality = getCardinalityOfVector(
                    Cardinality.RIGHT.clone().applyQuaternion( entity.rotation )
                );
                const isUp = cardinality === Cardinality.DOWN || cardinality === Cardinality.UP;

                let isNextChapterBigger;

                // Go to parent chapter of this one
                if( entity === previousChapterFinishEntity ) {

                    const previousChapterNextChapterData = previousChapter.nextChapters.find(
                        data => data.chapterId === currentChapterId
                    );
                    
                    isNextChapterBigger = previousChapterNextChapterData.scale.x < 1;
                    const multiplier = isNextChapterBigger ? 8 : 0.125;

                    // Data needed to correclty re-position the player
                    const nextChapter = {
                        position: previousChapterNextChapterData.position
                            .clone()
                            .multiplyScalar( -multiplier ),
                        scale: new Vector3( multiplier, multiplier, multiplier, ),
                    };

                    // Find who, if anyone, linked to the previous chapter
                    const previousPreviousChapter = allChaptersArray.find( chapter =>
                        chapter.nextChapters.find( data => data.chapterId === previousChapter.id )
                    );

                    newState.advanceAction = () =>
                        actions.advanceToPreviousChapter(
                            nextChapter, previousChapter.id, previousPreviousChapter ? previousPreviousChapter.id : null, isNextChapterBigger,
                        );

                // Go to child chapter of this one
                } else {

                    const nextChapter = [ ...nextChapters ].sort( ( a, b ) =>
                        a.position.distanceTo( entity.position ) -
                        b.position.distanceTo( entity.position )
                    )[ 0 ];

                    // If we're going forward then the nextChapter data will
                    // have the scale of the next level
                    isNextChapterBigger = nextChapter.scale.x > 1;

                    newState.advanceAction = () =>
                        actions.advanceChapter( nextChapter );
                
                }

                // Calculate where to tween the player to. *>2 to move
                // past the hit box for the level exit/entrance
                newState.startTransitionPosition = playerPositionV3;
                newState.currentTransitionPosition = playerPositionV3;

                const currentTransitionTarget = new Vector3(
                    lerp( playerPositionV3.x, entity.position.x, isUp ? 0 : 2.5 ),
                    playerPositionV3.y,
                    lerp( playerPositionV3.z, entity.position.z, isUp ? 2.5 : 0 ),
                );

                newState.currentTransitionTarget = currentTransitionTarget;
                newState.currentTransitionStartTime = time;
                newState.currentTransitionCameraTarget = new Vector3(
                    currentTransitionTarget.x,
                    getCameraDistanceToPlayer(
                        // todo: when going from small to big (like going
                        // backwards), the camera needs to zoom *out*, but we
                        // don't support scales bigger than 1, set to 1 for now
                        playerPositionV3.y, cameraFov, playerScale * ( isNextChapterBigger ? 1 : 0.125 )
                    ),
                    currentTransitionTarget.z,
                );

                newState.transitionCameraPositionStart = cameraPosition;

                return {<commit_msg>transiton test<commit_after>import * as Cardinality from 'helpers/Cardinality';
import { scalePlayer, canJump } from 'physics-utils';
import {
    getCardinalityOfVector, getCameraDistanceToPlayer, lerp,
    playerV3ToFinishEntityV3Collision, playerToCircleCollision3dTo2d,
    playerToBoxCollision3dTo2d,
} from 'helpers/Utils';
import { Vector3, } from 'three';

// Warning: duplicated in playerScaleReducer
const scaleStartDelayMs = 250;

export default function entityInteractionReducer(
    keysDown:Object,
    actions:Object,
    gameData:Object,
    oldState:Object,
    currentState:Object,
    next:Function
) {

    const {
        currentLevelTouchyArray, playerRadius, playerDensity, playerScale,
        currentLevelId, nextChapters, previousChapterFinishEntity,
        previousChapterEntity, previousChapter, cameraFov,
        currentGameChapterId, currentChapterId, allChaptersArray,
        allEntities,
    } = gameData;

    const {
        cameraPosition, world, playerBody, textQueue, sideEffectQueue,
        moveQueue,
    } = oldState;

    const { playerPositionV3, time, } = currentState;

    let newState = {};

    for( let i = 0; i < currentLevelTouchyArray.length; i++ ) {

        const entity = currentLevelTouchyArray[ i ];

        const isSameScale = entity.scale.x === playerScale;

        if( entity.type === 'finish' ) {

            // This would make a good abstracted out function, probably
            if(
                playerV3ToFinishEntityV3Collision(
                    playerPositionV3,
                    playerRadius,
                    entity.position,
                    entity.scale,
                ) &&
                isSameScale
            ) {

                newState.isAdvancing = true;

                // this is almost certainly wrong to determine which way
                // the finish line element is facing
                const cardinality = getCardinalityOfVector(
                    Cardinality.RIGHT.clone().applyQuaternion( entity.rotation )
                );
                const isUp = cardinality === Cardinality.DOWN || cardinality === Cardinality.UP;

                let isNextChapterBigger;

                // Go to parent chapter of this one
                if( entity === previousChapterFinishEntity ) {

                    const previousChapterNextChapterData = previousChapter.nextChapters.find(
                        data => data.chapterId === currentChapterId
                    );
                    
                    isNextChapterBigger = previousChapterNextChapterData.scale.x < 1;
                    const multiplier = isNextChapterBigger ? 8 : 0.125;

                    // Data needed to correclty re-position the player
                    const nextChapter = {
                        position: previousChapterNextChapterData.position
                            .clone()
                            .multiplyScalar( -multiplier ),
                        scale: new Vector3( multiplier, multiplier, multiplier, ),
                    };

                    // Find who, if anyone, linked to the previous chapter
                    const previousPreviousChapter = allChaptersArray.find( chapter =>
                        chapter.nextChapters.find( data => data.chapterId === previousChapter.id )
                    );

                    newState.advanceAction = () =>
                        actions.advanceToPreviousChapter(
                            nextChapter, previousChapter.id, previousPreviousChapter ? previousPreviousChapter.id : null, isNextChapterBigger,
                        );

                // Go to child chapter of this one
                } else {

                    const nextChapter = [ ...nextChapters ].sort( ( a, b ) =>
                        a.position.distanceTo( entity.position ) -
                        b.position.distanceTo( entity.position )
                    )[ 0 ];

                    // If we're going forward then the nextChapter data will
                    // have the scale of the next level
                    isNextChapterBigger = nextChapter.scale.x > 1;

                    newState.advanceAction = () =>
                        actions.advanceChapter( nextChapter );
                
                }

                // Calculate where to tween the player to. *>2 to move
                // past the hit box for the level exit/entrance
                newState.startTransitionPosition = playerPositionV3;
                newState.currentTransitionPosition = playerPositionV3;

                const directionToFinish = getCardinalityOfVector(
                    entity.position.clone().sub( playerPositionV3 )
                );

                const currentTransitionTarget = entity.position.clone().add(
                    directionToFinish.multiplyScalar( 1.25 * entity.scale.x )
                );

                newState.currentTransitionTarget = currentTransitionTarget;
                newState.currentTransitionStartTime = time;
                newState.currentTransitionCameraTarget = new Vector3(
                    currentTransitionTarget.x,
                    getCameraDistanceToPlayer(
                        // todo: when going from small to big (like going
                        // backwards), the camera needs to zoom *out*, but we
                        // don't support scales bigger than 1, set to 1 for now
                        playerPositionV3.y, cameraFov, playerScale * ( isNextChapterBigger ? 1 : 0.125 )
                    ),
                    currentTransitionTarget.z,
                );

                newState.transitionCameraPositionStart = cameraPosition;

                return {
{'fraction_non_alphanumeric': 0.07066982705644807, 'fraction_numerical': 0.00754982003335967, 'mean_word_length': 1.5663437711196215, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '817995', 'n_tokens_mistral': 2875, 'n_tokens_neox': 2509, 'n_words': 657}