This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.js
107 lines (93 loc) · 3.07 KB
/
Block.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Created by:
Victor Williams
*/
var BlockVShaderID = "vertexShader";
//data structures
var BlockData = {
red: {
color: "red",
speed: 1,
size: 2.5,
points: 1
},
blue: {
color: "blue",
speed: 2,
size: 1.5,
points: 5
},
white: {
color: "white",
speed: 3,
size: 1,
points: 25
},
key: function(n) {
return this[Object.keys(this)[n-1]];
}
};
var minSize = 0.1;
//the main stuff
var Block = function (gl) {
this.gl = gl;
this.blockID = Math.floor((Math.random() * 3) + 1);
this.blockData = BlockData.key(this.blockID);
this.startx = (Math.random() * (1-(minSize*this.blockData.size)+1))-1;
this.points = [vec2(this.startx,1.01+(minSize*this.blockData.size)),vec2(this.startx+(minSize*this.blockData.size),1.01+(minSize*this.blockData.size)),vec2(this.startx+(minSize*this.blockData.size),1.01),vec2(this.startx,1.01)];
this.leftTopMax = 0;
this.rightBottomMax = 2;
this.shiftX = 0;
this.shiftY = 0;
this.deltaTrans = 0.010*this.blockData.speed;
this.buffer = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, this.buffer );
gl.bufferData( gl.ARRAY_BUFFER, flatten(this.points), gl.STATIC_DRAW );
this.attachShaders();
}
Block.prototype.attachShaders = function() {
var vertexShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vertexShader, document.getElementById(BlockVShaderID).text);
gl.compileShader(vertexShader);
var fragShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fragShader, document.getElementById(this.blockData.color).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);
}
Block.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.yshiftLoc = this.gl.getUniformLocation(this.shaderProgram,"yshift");
}
Block.prototype.moveX = function(forward) {
var change = forward ? this.deltaTrans : -this.deltaTrans;
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;
}
}
}
Block.prototype.moveY = function(forward, timediff) {
var change = forward ? this.deltaTrans : -this.deltaTrans;
change *= timediff/25;
//console.log(change);
this.shiftY += change;
for (var i=0; i<this.points.length;i++) {
this.points[i][1] = this.points[i][1]+change;
}
}
Block.prototype.render = function(timediff) {
gl.useProgram(this.shaderProgram);
gl.bindBuffer( gl.ARRAY_BUFFER, this.buffer );
this.attachVariables();
this.moveY(false, timediff);
this.gl.uniform1f(this.yshiftLoc,this.shiftY);
this.gl.drawArrays(this.gl.TRIANGLE_FAN, 0, this.points.length);
//window.requestAnimFrame(this.render);
}