Skip to content

Commit

Permalink
Convert player to class
Browse files Browse the repository at this point in the history
  • Loading branch information
zerebos committed Jul 28, 2023
1 parent e0bb213 commit 8a9c8cc
Showing 1 changed file with 61 additions and 120 deletions.
181 changes: 61 additions & 120 deletions Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,129 +5,70 @@ Zack Rauen
*/

var playerVShaderID = "vertexShader";
var playerFShaderID = "green";
var numOfPoints = 0;
const playerVShaderID = "vertexShader";
const playerFShaderID = "green";

var Player = function (gl) {
this.gl = gl;
this.points = [vec2( -.15,-.925),vec2( .15,-.925),vec2( .15,-.975),vec2( -.15,-.975)];
this.leftTopMax = 0;
this.rightBottomMax = 2;
numOfPoints = this.points.length;
this.shiftX = 0;
this.shiftY = 0;
this.deltaTrans = 0.03;
this.buffer = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, this.buffer );
gl.bufferData( gl.ARRAY_BUFFER, flatten(this.points), gl.STATIC_DRAW );
this.velocity = 0;


this.attachShaders();
}
class Player {
constructor(webgl) {
this.webgl = webgl;
this.points = [vec2(-.15, -.925), vec2(.15, -.925), vec2(.15,-.975), vec2(-.15,-.975)];
this.leftTopMax = 0;
this.rightBottomMax = 2;
this.shiftX = 0;
this.shiftY = 0;
this.deltaTrans = 0.03;
this.buffer = this.webgl.createBuffer();
this.webgl.bindBuffer(this.webgl.ARRAY_BUFFER, this.buffer);
this.webgl.bufferData(this.webgl.ARRAY_BUFFER, flatten(this.points), this.webgl.STATIC_DRAW);
this.velocity = 0;

this.attachShaders();
}

Player.prototype.attachShaders = function() {
var vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, document.getElementById(playerVShaderID).text);
gl.compileShader(vertexShader);
var fragShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragShader, document.getElementById(playerFShaderID).text);
gl.compileShader(fragShader);
this.shaderProgram = gl.createProgram();
this.gl.attachShader(this.shaderProgram, vertexShader);
this.gl.attachShader(this.shaderProgram, fragShader);
this.gl.linkProgram(this.shaderProgram);
}
attachShaders() {
var vertexShader = this.webgl.createShader(this.webgl.VERTEX_SHADER);
this.webgl.shaderSource(vertexShader, document.getElementById(playerVShaderID).text);
this.webgl.compileShader(vertexShader);
var fragShader = this.webgl.createShader(this.webgl.FRAGMENT_SHADER);
this.webgl.shaderSource(fragShader, document.getElementById(playerFShaderID).text);
this.webgl.compileShader(fragShader);
this.shaderProgram = this.webgl.createProgram();
this.webgl.attachShader(this.shaderProgram, vertexShader);
this.webgl.attachShader(this.shaderProgram, fragShader);
this.webgl.linkProgram(this.shaderProgram);
}

Player.prototype.attachVariables = function() {
var myPosition = this.gl.getAttribLocation(this.shaderProgram, "myPosition");
this.gl.vertexAttribPointer( myPosition, 2, this.gl.FLOAT, false, 0, 0 );
this.gl.enableVertexAttribArray( myPosition );
this.xshiftLoc = this.gl.getUniformLocation(this.shaderProgram,"xshift");
this.gl.uniform1f(this.xshiftLoc,this.shiftX);
this.yshiftLoc = this.gl.getUniformLocation(this.shaderProgram,"yshift");
}
attachVariables() {
var myPosition = this.webgl.getAttribLocation(this.shaderProgram, "myPosition");
this.webgl.vertexAttribPointer( myPosition, 2, this.webgl.FLOAT, false, 0, 0 );
this.webgl.enableVertexAttribArray( myPosition );
this.xshiftLoc = this.webgl.getUniformLocation(this.shaderProgram,"xshift");
this.webgl.uniform1f(this.xshiftLoc,this.shiftX);
this.yshiftLoc = this.webgl.getUniformLocation(this.shaderProgram,"yshift");
}

/*Player.prototype.moveX = function(forward) {
var change = 0.05;
if (forward) {
this.velocity += change;
if (this.velocity > 0.25)
this.velocity = 0.25;
}
else {
this.velocity -= change;
if (this.velocity < -0.25)
this.velocity = -0.25;
}
//this.velocity *= forward ? 1 : -1;
}
moveX(forward, timestep) {
var change = forward ? this.deltaTrans : -this.deltaTrans;
change *= timestep / 25;
if (this.points[this.leftTopMax][0] + change >= -1.01 && this.points[this.rightBottomMax][0] + change < 1.01) {
this.shiftX += change;
for (var i = 0; i < this.points.length;i++) {
this.points[i][0] = this.points[i][0] + change;
}
}
}

Player.prototype.movePlayer = function(forward) {
var change = forward ? this.deltaTrans : -this.deltaTrans;
change *= Math.abs(this.velocity);
if (this.points[this.leftTopMax][0] + change >= -1.01 && this.points[this.rightBottomMax][0] + change < 1.01) {
this.shiftX += change;
for (var i=0; i<this.points.length;i++) {
this.points[i][0] = this.points[i][0]+change;
}
}
}*/

Player.prototype.moveX = function(forward, timestep) {
var change = forward ? this.deltaTrans : -this.deltaTrans;
change *= timestep/25;
if (this.points[this.leftTopMax][0] + change >= -1.01 && this.points[this.rightBottomMax][0] + change < 1.01) {
this.shiftX += change;
for (var i=0; i<this.points.length;i++) {
this.points[i][0] = this.points[i][0]+change;
}
}
}

Player.prototype.moveY = function(forward) {
// var change = forward ? this.deltaTrans : -this.deltaTrans;
// if (this.points[this.leftTopMax][1] + change <= 1.01 && this.points[this.rightBottomMax][1] + change >= -1.01) {
// this.shiftY += change;
// for (var i=0; i<this.points.length;i++) {
// this.points[i][1] = this.points[i][1]+change;
// }
// this.gl.uniform1f(this.yshiftLoc,this.shiftY);
// }
// else {
// console.log(this.points);
// }
}

/*Player.prototype.render = function(timestep) {
var diff = 0.25*timestep/1000;
this.movePlayer(this.velocity > 0.0);
gl.useProgram(this.shaderProgram);
gl.bindBuffer( gl.ARRAY_BUFFER, this.buffer );
this.attachVariables();
this.gl.drawArrays(this.gl.TRIANGLE_FAN, 0, this.points.length);
if (this.velocity > 0.0 && this.velocity - diff > 0.0)
this.velocity = this.velocity - diff;
else if (this.velocity < 0.0 && this.velocity + diff < 0.0)
this.velocity = this.velocity + diff;
else
this.velocity=0.0;
//window.requestAnimFrame(this.render);
}*/


Player.prototype.render = function(left, right, timestep) {
gl.useProgram(this.shaderProgram);
gl.bindBuffer( gl.ARRAY_BUFFER, this.buffer );
if (left)
this.moveX(false, timestep);
if (right)
this.moveX(true, timestep);
this.attachVariables();
this.gl.drawArrays(this.gl.TRIANGLE_FAN, 0, this.points.length);
//window.requestAnimFrame(this.render);
render(left, right, timestep) {
this.webgl.useProgram(this.shaderProgram);
this.webgl.bindBuffer(this.webgl.ARRAY_BUFFER, this.buffer);
if (left)
this.moveX(false, timestep);
if (right)
this.moveX(true, timestep);
this.attachVariables();
this.webgl.drawArrays(this.webgl.TRIANGLE_FAN, 0, this.points.length);
}
}

0 comments on commit 8a9c8cc

Please sign in to comment.