-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameObject.js
117 lines (111 loc) · 5.48 KB
/
GameObject.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
108
109
110
111
112
113
114
115
116
117
function GameObject(program, x, y, z, degrees, bounding_cir_rad) {
this.health = 3;
this.x = x;
this.y = y;
this.z = z;
this.degrees = degrees;
this.normalDegrees = degrees
this.xdir = Math.cos(this.degrees * Math.PI / 180.0);
this.zdir = Math.sin(this.degrees * Math.PI / 180.0);
this.bounding_cir_rad = bounding_cir_rad;
GameObject.prototype.turn = function (degreesRotation) {
this.degrees = (this.degrees + degreesRotation) % 360;
this.xdir = Math.cos(this.degrees * Math.PI / 180.0);
this.zdir = Math.sin(this.degrees * Math.PI / 180.0);
};
// Pass in negative speed for backward motion
GameObject.prototype.move = function (speed) {
this.x = this.x + speed * this.xdir;
this.z = this.z + speed * this.zdir;
};
}
;
// more standarized game object
function GameObj2(x, y, z, degrees, bounding_cir_rad) {
GameObject.call(this, G.program, x, y, z, degrees, bounding_cir_rad);
const _ = this;
_.scale = 1;
_.textureMatrix = mat4();
_.textureMatrix[3][3] = 0; // set to non 0 for cylindrical texture mapping
_.isVisible = true;
_.glow = [0, 0, 0, 0]; // no glow
_.cullFace = gl.BACK;
}
GameObj2.prototype = Object.create(GameObject.prototype);
GameObj2.prototype.show = function () {
const _ = this;
if (!_.isVisible) {
return;
}
bindMesh(_.mesh);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, _.texture);
let m = G.program.viewMatrix;
m = mult(m, translate(_.x, _.y, _.z));
m = mult(m, rotateY(_.degrees));
m = mult(m, _.scaleMatrix || scalem(_.scale, _.scale, _.scale));
gl.uniformMatrix4fv(G.program.u.modelViewMatrix, false, flatten(m));
m = transpose(inverse(m));
gl.uniformMatrix4fv(G.program.u.normalMatrix, false, flatten(m));
gl.uniformMatrix4fv(G.program.u.textureMatrix, false, flatten(_.textureMatrix));
gl.uniform4fv(G.program.u.glow, _.glow);
if (_.cullFace) {
gl.enable(gl.CULL_FACE);
gl.cullFace(_.cullFace);
} else {
gl.disable(gl.CULL_FACE);
}
gl.drawElements(gl.TRIANGLES, _.mesh.indicesLength, gl.UNSIGNED_SHORT, 0);
if (_.cullFace) {
gl.disable(gl.CULL_FACE);
}
gl.disableVertexAttribArray(G.program.a.position);
gl.disableVertexAttribArray(G.program.a.normal);
gl.disableVertexAttribArray(G.program.a.texCoord);
};
GameObj2.prototype.isCollide = function (that, cloudIndex) {
if (this.isVisible && that && that.isVisible) {
if (this == G.objects.hero && that == G.objects.target && that.captured == false) {
if (that.scale == 100) {
return (length([that.x - this.x, that.z - this.z]) < that.bounding_cir_rad + this.bounding_cir_rad) && that.y <= -3;
} else {
return (length([that.x - this.x, that.z - this.z]) < that.bounding_cir_rad + this.bounding_cir_rad) && that.y <= 0;
}
} else if (this == G.objects.villain && that == G.objects.target && that.captured == false) {
if (that.scale == 100) {
return (length([that.x - this.x, that.z - this.z]) < that.bounding_cir_rad + this.bounding_cir_rad) && that.y <= -3;
} else {
return (length([that.x - this.x, that.z - this.z]) < that.bounding_cir_rad + this.bounding_cir_rad) && that.y <= 0;
}
} else if (this == G.objects.target && that == G.objects.hero && this.captured == false) {
if (that.scale == 100) {
return (length([that.x - this.x, that.z - this.z]) < that.bounding_cir_rad + this.bounding_cir_rad) && this.y <= -3;
} else {
return (length([that.x - this.x, that.z - this.z]) < that.bounding_cir_rad + this.bounding_cir_rad) && this.y <= 0;
}
} else if (this == G.objects.target && that == G.objects.villain && this.captured == false) {
if (that.scale == 100) {
return (length([that.x - this.x, that.z - this.z]) < that.bounding_cir_rad + this.bounding_cir_rad) && this.y <= -3;
} else {
return (length([that.x - this.x, that.z - this.z]) < that.bounding_cir_rad + this.bounding_cir_rad) && this.y <= 0;
}
}
if (this == G.objects.rock.rock && that == G.objects.rocket.rocket) {
return (length([that.x - this.x, that.z - this.z]) < that.bounding_cir_rad + this.bounding_cir_rad) && that.y >= 9;
}
if (that == G.objects.booster0 || that == G.objects.booster1 || that == G.objects.booster2 || that == G.objects.booster3 || that == G.objects.booster4) {
const cMax = that.y + that.height/2;
const cMin = that.y - that.height/2;
return (length([that.x - this.x, that.z - this.z]) < that.bounding_cir_rad + this.bounding_cir_rad) && that.y >= -0.5;
}
if (this == G.objects.target && that == G.objects.clouds[cloudIndex]) {
const cMax = that.y + that.height/2;
const cMin = that.y - that.height/2;
return (length([that.x - this.x, that.z - this.z]) < that.bounding_cir_rad + this.bounding_cir_rad) && this.y >= cMin && this.y <= cMax;
}
}
return this.isVisible && that && that.isVisible && (length([that.x - this.x, that.z - this.z]) <= that.bounding_cir_rad + this.bounding_cir_rad);
};
function distance(x, y, a, b) {
return Math.sqrt((x - a) * (x - a) + (y - b) * (y - b));
}