{"size": 2608, "ext": "js", "max_stars_repo_path": "src/entities/Player/WeaponFSM.js", "max_stars_repo_name": "mohsenheydari/three-fps", "max_stars_repo_stars_event_min_datetime": "2021-05-30T16:12:15.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-12T16:03:25.000Z", "max_issues_repo_path": "src/entities/Player/WeaponFSM.js", "max_issues_repo_name": "mohsenheydari/three-fps", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/entities/Player/WeaponFSM.js", "max_forks_repo_name": "mohsenheydari/three-fps", "max_forks_repo_forks_event_min_datetime": "2021-06-06T15:31:55.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-24T15:29:39.000Z"}
{"max_stars_count": 35, "max_issues_count": null, "max_forks_count": 6, "avg_line_length": 24.1481481481, "max_line_length": 86, "alphanum_fraction": 0.5774539877}
import {FiniteStateMachine, State} from '../../FiniteStateMachine'
import * as THREE from 'three'

export default class WeaponFSM extends FiniteStateMachine{
    constructor(proxy){
        super();
        this.proxy = proxy;
        this.Init();
    }

    Init(){
        this.AddState('idle', new IdleState(this));
        this.AddState('shoot', new ShootState(this));
        this.AddState('reload', new ReloadState(this));
    }
}

class IdleState extends State{
    constructor(parent){
        super(parent);
    }

    get Name(){return 'idle'}
    get Animation(){return this.parent.proxy.animations['idle']; }

    Enter(prevState){
        const action = this.Animation.action;

        if(prevState){
            action.time = 0.0;
            action.enabled = true;
            action.setEffectiveTimeScale(1.0);
            action.crossFadeFrom(prevState.Animation.action, 0.1, true);
        }

        action.play();
    }

    Update(t){
        if(this.parent.proxy.shoot && this.parent.proxy.magAmmo > 0){
            this.parent.SetState('shoot');
        }
    }
}

class ShootState extends State{
    constructor(parent){
        super(parent);
    }

    get Name(){return 'shoot'}
    get Animation(){return this.parent.proxy.animations['shoot']; }

    Enter(prevState){
        const action = this.Animation.action;

        if(prevState){
            action.time = 0.0;
            action.enabled = true;
            action.setEffectiveTimeScale(1.0);
            action.crossFadeFrom(prevState.Animation.action, 0.1, true);
        }

        action.timeScale = 3.0;
        action.play();
    }

    Update(t){
        if(!this.parent.proxy.shoot || this.parent.proxy.magAmmo == 0){
            this.parent.SetState('idle');
        }
    }
}

class ReloadState extends State{
    constructor(parent){
        super(parent);

        this.parent.proxy.mixer.addEventListener( 'finished', this.AnimationFinished);
    }

    get Name(){ return 'reload'; }
    get Animation(){ return this.parent.proxy.animations['reload']; }

    AnimationFinished = e => {
        if(e.action != this.Animation.action){
            return;
        }

        this.parent.proxy.ReloadDone();
        this.parent.SetState('idle');
    }

    Enter(prevState){
        const action = this.Animation.action;
        action.loop = THREE.LoopOnce;

        if(prevState){
            action.time = 0.0;
            action.enabled = true;
            action.setEffectiveTimeScale(1.0);
            action.crossFadeFrom(prevState.Animation.action, 0.1, true);
        }

        action.play();
    }
}
