
function Player(){
    
    var name = "none";
    var actorID = null;
    var bufferID = null;
    
    var avatar = new Person( {fighterClass: "average"}  );
    
    var animation = new SpriteAnimation();
    var spriteMapLayout = null;
    var currentAnimationSequence = "bounce";
    var defaultAnimationSequence = "bounce";
    
    var direction = "east";
    
    var currentMovement = "nada";
    var previousMovement = "nada";
    var forceAnimationReload = false;
    
    this.origin = { x: 0, y: 0 };
    
    this.isJumping = false;
    this.jumpHeight = 80;
    this.jumpSpeed = 5;
    this.jumpGoingUp = false;
    this.lastJumpTime = 0;
    this.jumpTimer = 0;
    this.movingJump = 0;
   
    this.acceleration = 3000;                 //
    this.velocity = { x: 0, y: 0 };           //
    this.initialVelocity = { x: 0, y: 700};   //
    
    this.isAttacking = false;
    this.isMoving = false;
    this.isCrouching = false;
    this.isCrouchBlocking = false;
    this.isBlocking = false;
    
    this.init = function(o){
        
        name = (o.name != undefined ) ? o.name : name;
        
        actorID = o.actorID;
        bufferID = o.bufferID;
        
        if( o.animationInfo != undefined ){
            spriteMapLayout = (o.animationInfo.spriteMapLayout != undefined ) ? o.animationInfo.spriteMapLayout : spriteMapLayout;
            animation.init( { actorID: actorID, bufferID: bufferID, spriteMapLayout: spriteMapLayout }  );
        }
        
        direction = ( o.direction != undefined ) ? o.direction : direction;       
        
         if( o.position != undefined ){
            if( o.position.x != undefined && o.position.y != undefined){
                this.setPosition( o.position.x, o.position.y  );
                this.origin = o.position;
            }
        }
        
        if( o.width != undefined ){
            this.setWidth( o.width );
        }
        
        if( o.height != undefined ){
            this.setHeight( o.height );
        }
    }
    
    this.updateAction = function(i){
        
        if( i != currentMovement ){
            previousMovement = currentMovement;
            currentMovement = i;
            loop = false;
            
            switch( currentMovement ){
                case "left":
                    loop = true;
                    currentAnimationSequence = "walk";
                    break;
                case "right":
                    loop = true;
                    currentAnimationSequence = "walk";
                    break;
                case "jump":                     
                    currentAnimationSequence = "jump_up";
                    break;
                case "wait":
                    loop = true;
                    currentAnimationSequence = "bounce";
                    break;
                case "kick_weak":
                    currentAnimationSequence ="kick_weak";
                    break;
                default:
                    loop = true;
                    currentAnimationSequence = defaultAnimationSequence;
                    break;
            }
            
            this.resetAnimation( loop );
            
        }
    }
    
    this.animate = function(){
        animation.animate();
    }
    
    this.getDefaultAnimationSequence = function(){ return defaultAnimationSequence;  }
    
    this.updateAnimation = function(){
        animation.updateAnimationSequence();
    }
    
    this.resetAnimation = function(){
        animation.resetAnimation();
    }
    
    this.getBufferID = function(){
        return bufferID;
    }
    
    this.getSpriteMapLayout = function(){
        return spriteMapLayout;
    }
    
    this.setCurrentAnimationSequence = function(i){
        currentAnimationSequence = i;
        animation.updateAnimationSequence();
    }
    
    this.getCurrentAnimationSequence = function(){
        return currentAnimationSequence;
    }
    
    this.setDirection = function(i){
        direction = i;
    }
    
    this.getDirection = function(){
        return direction;
    }
    
    this.setPlayerName = function(i){ name = i;}    
    this.getPlayerName = function(){ return name;}
    
    this.setFighterClass = function(){ avatar.setFighterClass(); };       
    
    this.setWidth = function(i){ avatar.setWidth(i); }
    this.getWidth = function(){ return avatar.getWidth();  }
    
    this.setHeight = function(i){ avatar.setHeight(i); }
    this.getHeight = function(){ return avatar.getHeight();  }
    
    this.setPosition = function( x, y ){ avatar.setXY(x,y); }
    this.getPosition = function(){ return avatar.getXY();  }
    
    this.getSpeed = function(){ return avatar.getSpeed(); }
    
    this.move = function( x, y){
        
        // Move left and right, but make sure were not ALREADY jumping
        if( !this.isJumping ){
            if( x < 0 ){
                if( direction == "east" ){                    
                     if( y > 0 ) this.crouchBlock(); // moving backwards and down is only a blocking crouch
                    else this.moveLeft();
                }
                else if( direction == "west" ){
                    if( y > 0 ) this.crouch(); //moving forward and down is only a crouch
                    else this.moveLeft();
                }
            }
            if( x > 0 ){
                if( direction == "west" ){
                    if( y > 0 ){
                        console.log( "crouch block");
                        this.crouchBlock(); // moving backwards and down is only a blocking crouch
                    }
                    else this.moveRight();
                }
                else if( direction == "east" ){
                    if( y > 0 ){
                        this.crouch(); // moving forward and down is only a crouch
                        console.log("crouch");
                    }
                    else this.moveRight();
                }
            }
        } // if( !this.jumping )
        
        if( y < 0 ){
            this.jump(x);
        }
        
        if( y > 0 ){
            if( !this.isJumping && !this.isAttacking)
                this.crouch();
        }
       
        
        //avatar.setX( avatar.getXY().x + x  );
    }
    
    this.moveLeft = function(){       
        this.setCurrentAnimationSequence("walk");        
        avatar.moveLeft();
    }
    
    this.moveRight = function(){        
        this.setCurrentAnimationSequence("walk");         
        avatar.moveRight();
    }
    
     
    this.jump = function(x){
        var timer = tickTime - this.lastJumpTime;
        
        if( !this.isJumping && timer > 100 ){
            //this.setCurrentAnimationSequence("jump");                                
            this.isJumping = true;
            this.jumpGoingUp = true;
            this.movingJump = x;
            this.velocity = { x: 0, y: this.initialVelocity.y  };
        }        
    }
    
    this.setY = function(y){ avatar.setY(y);  }
    this.setX = function(x){ avatar.setX(x);  }
    
    this.doJump = function(){
       
        var maxHeight = this.origin.y - this.jumpHeight;
        var _y = avatar.getY();        
        
        if( _y <= maxHeight ){        
            this.jumpGoingUp = false;           
        }
       
        this.velocity.y -=  this.acceleration *  (gameSpeed / 1000 ) // NOW based on gamespeed which is millisenconds, so divide by 1000. .01 ; // Roughly 100 ticks per second   or .01 
        _y -= this.velocity.y  *   (gameSpeed / 1000 ); // now based on gamespeed which is millisecons, so divide by 1000... .01 ;
        
        
        if( _y >= this.origin.y ){            
            _y = this.origin.y;            
            this.isJumping = false;
            this.lastJumpTime = tickTime;
        }
        
        this.setY( _y );
        
        var _x = avatar.getX() + this.movingJump;
        if( _x <= 0 ) _x = 0;
        this.setX(_x);
        
    }
    
    this.moveUp = function(){
        this.setCurrentAnimationSequence("jump");     
        avatar.moveUp();
    }
    
    this.moveDown = function(){
        this.setCurrentAnimationSequence("crouch");     
        avatar.moveDown();
    }
    
    this.crouch = function(){
        this.setCurrentAnimationSequence("crouch");     
        avatar.crouch();
    }
    
    this.crouchBlock = function(){
        this.setCurrentAnimationSequence("crouch_block");     
        avatar.crouchBlock();
    }
    
    this.kick = function(i){
        this.setCurrentAnimationSequence("kick_weak");          
        avatar.kick(i);
    }
}

function Actor(){
    
    var x = 0;
    var y = 0;    
    var width = 20;
    var height = 20;
    
    this.setXY = function( a, b ){
        x = ( a != null ) ? a : x;
        y = ( b != null ) ? b : y;
    }    
    this.getXY = function(){ return {x: x, y: y}; }
    
    this.setX = function( i ){ x = i;}
    this.getX = function(){ return x; }
    
    this.setY = function( i ){ y = i;}
    this.getY = function(){ return y; }
    
    this.setWidth = function(i){ width = i; }
    this.getWidth = function(){ return width; }
    
    this.setHeight = function(i){ height = i; }
    this.getHeight = function(){ return height; }
}

function BodyPart(){
    
}

function FighterClass(o){
    var name = (typeof o == "object" && o.type != undefined) ? o.type : "average";
    var speed = 3;
    
    this.setClass = function(i){
        name = i;
        
        switch( name ){
            case "tank":
                speed = 1;
                break;
            case "light":
                speed =3;
                break;
            case "average":
                // No break because average is the default and they flow together
            default:
                speed = 2;
                break;
        }
    }
    
    this.setSpeed = function(i){ speed = i;  }
    this.getSpeed = function(i){ return speed; }
    
}

function Person( o ){
    var actorObj = new Actor();
    var parts = { torso: new BodyPart() };
    var width = 20;
    var height = 20;
    var image = new Image();
    var imageId = null;
        
    // Name of our fighter class
    var fcName = (typeof o == "object" && o.fighterClass != undefined) ? o.fighterClass : "average";
    // Create our fighter class object
    var fighterClass = new FighterClass( { type: fcName } );
    
    this.setImageId = function(i){
        imageId = i;        
    }
    
    this.getImageId = function(){
        if( imageId == null ){
            outMessage( fcName + " image is not defined"  );
            return false;
        }
        
        return imageId;
    }
    
    this.setWidth = function(i){
        width = i;
        actorObj.setWidth(i);
    }
    this.getWidth = function(){ return actorObj.getWidth();  }
    
    this.setHeight = function(i){
        height = i;
        actorObj.setHeight(i);
    }
    this.getHeight = function(){ return actorObj.getHeight();  }
            
    this.setXY = function( a, b ){ actorObj.setXY( a, b); }
    this.getXY = function(){ return actorObj.getXY();  }
    this.getPosition = this.getXY();
    
    this.getX = function(){ return actorObj.getX(); }
    this.setX = function( x ){ actorObj.setX(x); }
    
    this.getY = function(){ return actorObj.getY(); }
    this.setY = function( y ){ actorObj.setY(y); }
    
    this.getSpeed = function(){
        return fighterClass.getSpeed();
    }
   
   this.moveForward = function(){
    
   }
   
   this.moveBackward = function(){
    
   }
   
   this.block = function(){
        
   }
   
    this.moveLeft = function(){
        var currentX = actorObj.getX();
        var newX = 0;
        
        if( currentX > 0 ){ newX = currentX - fighterClass.getSpeed(); }
        else{ newX = 0;}
        
        actorObj.setX( newX  );
        
    }

    this.moveRight = function(){
               
        var currentX = actorObj.getX();
        var newX = 0;
        var maxX = canvasWidth - width;
        
        if( currentX <= maxX ){ newX = currentX + fighterClass.getSpeed();  }
        else{  newX = maxX; }
           
        actorObj.setX( newX  );

    }
    
    this.jump = function( actorID ){
        
        var pid = window[actorID];
        var jump = pid.checkJumping();
        var maxHeight = pid.jumpStartY - 40;
        var startY = pid.jumpStartY;
        var _y = pid.jumpStartY;
        var newY = 0;
        var speed = 20;
       
        if( _y >= maxHeight ) _y = _y - speed;
        else _y = _y + speed;
            
        console.log("yup");   
                   
        if( _y >= startY ){
            jump = false;              
            pid.setJumping( false );
        }
        
    }
    
    this.moveUp = function(){
        
        var currentY = actorObj.getY();
        var newY = 0;
        
        if( currentY <= 0 ){ newY = 0;  }
        else{ newY = currentY - fighterClass.getSpeed();  }
        
        actorObj.setY( newY );
        
    }
    
    this.moveDown = function(){
        var currentY = actorObj.getY(); 
        var newY = 0;
        var maxY = canvasHeight - height;
        
        if( currentY >= maxY){ newY = maxY;  }
        else{ newY = currentY + fighterClass.getSpeed();  }
        
        actorObj.setY( newY );        
    }
    
    this.crouch = function(){
        
    }
    
    this.crouchBlock = function(){
        
    }
    
    this.kick = function(i){
        switch( i ){
            
        }
    }
    
    this.setFighterClass = function(i){ fighterClass = i;  }
    this.getFighterClass = function(){ return fighterClass;}
}

