view webGL/dandy/resources/Player.js @ 4:7f615f5f5220

upload javascript file
author NOBUYASU Oshiro
date Mon, 14 Jun 2010 15:01:02 +0900
parents
children 881478004f18
line wrap: on
line source

function Pad()
{
    this.right=0;
    this.left=0;
    this.up=0;
    this.down=0;
    this.count=0;
    this.state=0;
    return this;
}

function Player( object )
{
    this.x = 0;
    this.y = 0;
    this.z = 10;
    this.dx = 0;
    this.dy = 0;
    this.idle = object["player_idle"];
    this.rightmove = object["player_rightmove"];
    this.leftmove = object["player_leftmove"];
    this.turntoright = object["player_turntoright"];
    this.turntoleft = object["player_turntoleft"];
    this.image = this.idle;
    return this;
}
function movePlayer( player, pad )
{

    pad.right = Math.min(pad.right, 3);
    pad.left = Math.min(pad.left, 3);
    pad.up = Math.min(pad.up, 4);
    pad.down = Math.min(pad.down, 4);

    player.x += pad.right;
    player.x -= pad.left;
    player.y += pad.up;
    player.y -= pad.down;

    if( pad.count == 1) pad.state=0;
    if( pad.count > 0) pad.state=1;
    if( pad.count > 10 ) pad.state=2;

    if( pad.state == 0 ) player.image  = player.idle;
    if( pad.state == 1 ) {
	if( pad.right > 0 ) player.image = player.turntoleft;
        if( pad.left > 0 ) player.image  = player.turntoright;
    }
    if( pad.state == 2 ) {
	if( pad.right > 0 ) player.image = player.leftmove;
        if( pad.left > 0 ) player.image  = player.rightmove;
    }

    if ( pad.right > 0) pad.right -= 1.5;
    if ( pad.left > 0) pad.left -= 1.5;
    if ( pad.up > 0) pad.up -= 2;
    if ( pad.down > 0) pad.down -= 2;    

}
function drawPlayer(ctx, player, scale)
{
    // setup VBOs
    ctx.enableVertexAttribArray(0);
    ctx.enableVertexAttribArray(1);
    ctx.enableVertexAttribArray(2);
    
    ctx.bindBuffer(ctx.ARRAY_BUFFER, player.image.vertexObject);
    ctx.vertexAttribPointer(2, 3, ctx.FLOAT, false, 0, 0);
    
    ctx.bindBuffer(ctx.ARRAY_BUFFER, player.image.normalObject);
    ctx.vertexAttribPointer(0, 3, ctx.FLOAT, false, 0, 0);
    
    ctx.bindBuffer(ctx.ARRAY_BUFFER, player.image.texCoordObject);
    ctx.vertexAttribPointer(1, 2, ctx.FLOAT, false, 0, 0);
    
    ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, player.image.indexObject);

    // generate the model-view matrix
    var mvMatrix = new CanvasMatrix4();
    mvMatrix.scale(scale, scale, scale);
    mvMatrix.rotate(180, 0,0,1);
    mvMatrix.translate(player.x, player.y, player.z);

    // construct the normal matrix from the model-view matrix
    var normalMatrix = new CanvasMatrix4(mvMatrix);
    normalMatrix.invert();
    normalMatrix.transpose();
    ctx.uniformMatrix4fv(ctx.getUniformLocation(ctx.program, "u_normalMatrix"), false, normalMatrix.getAsWebGLFloatArray());
    
    // construct the model-view * projection matrix
    var mvpMatrix = new CanvasMatrix4(mvMatrix);
    mvpMatrix.multRight(ctx.perspectiveMatrix);
    ctx.uniformMatrix4fv(ctx.getUniformLocation(ctx.program, "u_modelViewProjMatrix"), false, mvpMatrix.getAsWebGLFloatArray());
    
    ctx.bindTexture(ctx.TEXTURE_2D, player.image.texture);
    ctx.drawElements(ctx.TRIANGLES, player.image.numIndices, ctx.UNSIGNED_SHORT, 0);
}