Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/hextris/js/Block.js
1036 views
1
function Block(fallingLane, color, iter, distFromHex, settled) {
2
// whether or not a block is rested on the center hex or another block
3
this.settled = (settled === undefined) ? 0 : 1;
4
this.height = settings.blockHeight;
5
//the lane which the block was shot from
6
this.fallingLane = fallingLane;
7
8
this.checked=0;
9
//the angle at which the block falls
10
this.angle = 90 - (30 + 60 * fallingLane);
11
//for calculating the rotation of blocks attached to the center hex
12
this.angularVelocity = 0;
13
this.targetAngle = this.angle;
14
this.color = color;
15
//blocks that are slated to be deleted after a valid score has happened
16
this.deleted = 0;
17
//blocks slated to be removed from falling and added to the hex
18
this.removed = 0;
19
//value for the opacity of the white blcok drawn over falling block to give it the glow as it attaches to the hex
20
this.tint = 0;
21
//value used for deletion animation
22
this.opacity = 1;
23
//boolean for when the block is expanding
24
this.initializing = 1;
25
this.ict = MainHex.ct;
26
//speed of block
27
this.iter = iter;
28
//number of iterations before starting to drop
29
this.initLen = settings.creationDt;
30
//side which block is attached too
31
this.attachedLane = 0;
32
//distance from center hex
33
this.distFromHex = distFromHex || settings.startDist * settings.scale ;
34
35
this.incrementOpacity = function() {
36
if (this.deleted) {
37
//add shakes
38
if (this.opacity >= 0.925) {
39
var tLane = this.attachedLane - MainHex.position;
40
tLane = MainHex.sides - tLane;
41
while (tLane < 0) {
42
tLane += MainHex.sides;
43
}
44
45
tLane %= MainHex.sides;
46
MainHex.shakes.push({lane:tLane, magnitude:3 * (window.devicePixelRatio ? window.devicePixelRatio : 1) * (settings.scale)});
47
}
48
//fade out the opacity
49
this.opacity = this.opacity - 0.075 * MainHex.dt;
50
if (this.opacity <= 0) {
51
//slate for final deletion
52
this.opacity = 0;
53
this.deleted = 2;
54
if (gameState == 1 || gameState==0) {
55
localStorage.setItem("saveState", exportSaveState());
56
}
57
}
58
}
59
};
60
61
this.getIndex = function (){
62
//get the index of the block in its stack
63
var parentArr = MainHex.blocks[this.attachedLane];
64
for (var i = 0; i < parentArr.length; i++) {
65
if (parentArr[i] == this) {
66
return i;
67
}
68
}
69
};
70
71
this.draw = function(attached, index) {
72
this.height = settings.blockHeight;
73
if (Math.abs(settings.scale - settings.prevScale) > 0.000000001) {
74
this.distFromHex *= (settings.scale/settings.prevScale);
75
}
76
77
this.incrementOpacity();
78
if(attached === undefined)
79
attached = false;
80
81
if(this.angle > this.targetAngle) {
82
this.angularVelocity -= angularVelocityConst * MainHex.dt;
83
}
84
else if(this.angle < this.targetAngle) {
85
this.angularVelocity += angularVelocityConst * MainHex.dt;
86
}
87
88
if (Math.abs(this.angle - this.targetAngle + this.angularVelocity) <= Math.abs(this.angularVelocity)) { //do better soon
89
this.angle = this.targetAngle;
90
this.angularVelocity = 0;
91
}
92
else {
93
this.angle += this.angularVelocity;
94
}
95
96
this.width = 2 * this.distFromHex / Math.sqrt(3);
97
this.widthWide = 2 * (this.distFromHex + this.height) / Math.sqrt(3);
98
//this.widthWide = this.width + this.height + 3;
99
var p1;
100
var p2;
101
var p3;
102
var p4;
103
if (this.initializing) {
104
var rat = ((MainHex.ct - this.ict)/this.initLen);
105
if (rat > 1) {
106
rat = 1;
107
}
108
p1 = rotatePoint((-this.width / 2) * rat, this.height / 2, this.angle);
109
p2 = rotatePoint((this.width / 2) * rat, this.height / 2, this.angle);
110
p3 = rotatePoint((this.widthWide / 2) * rat, -this.height / 2, this.angle);
111
p4 = rotatePoint((-this.widthWide / 2) * rat, -this.height / 2, this.angle);
112
if ((MainHex.ct - this.ict) >= this.initLen) {
113
this.initializing = 0;
114
}
115
} else {
116
p1 = rotatePoint(-this.width / 2, this.height / 2, this.angle);
117
p2 = rotatePoint(this.width / 2, this.height / 2, this.angle);
118
p3 = rotatePoint(this.widthWide / 2, -this.height / 2, this.angle);
119
p4 = rotatePoint(-this.widthWide / 2, -this.height / 2, this.angle);
120
}
121
122
if (this.deleted) {
123
ctx.fillStyle = "#FFF";
124
} else if (gameState === 0) {
125
if (this.color.charAt(0) == 'r') {
126
ctx.fillStyle = rgbColorsToTintedColors[this.color];
127
}
128
else {
129
ctx.fillStyle = hexColorsToTintedColors[this.color];
130
}
131
}
132
else {
133
ctx.fillStyle = this.color;
134
}
135
136
ctx.globalAlpha = this.opacity;
137
var baseX = trueCanvas.width / 2 + Math.sin((this.angle) * (Math.PI / 180)) * (this.distFromHex + this.height / 2) + gdx;
138
var baseY = trueCanvas.height / 2 - Math.cos((this.angle) * (Math.PI / 180)) * (this.distFromHex + this.height / 2) + gdy;
139
ctx.beginPath();
140
ctx.moveTo(baseX + p1.x, baseY + p1.y);
141
ctx.lineTo(baseX + p2.x, baseY + p2.y);
142
ctx.lineTo(baseX + p3.x, baseY + p3.y);
143
ctx.lineTo(baseX + p4.x, baseY + p4.y);
144
//ctx.lineTo(baseX + p1.x, baseY + p1.y);
145
ctx.closePath();
146
ctx.fill();
147
148
if (this.tint) {
149
if (this.opacity < 1) {
150
if (gameState == 1 || gameState==0) {
151
localStorage.setItem("saveState", exportSaveState());
152
}
153
154
this.iter = 2.25;
155
this.tint = 0;
156
}
157
158
ctx.fillStyle = "#FFF";
159
ctx.globalAlpha = this.tint;
160
ctx.beginPath();
161
ctx.moveTo(baseX + p1.x, baseY + p1.y);
162
ctx.lineTo(baseX + p2.x, baseY + p2.y);
163
ctx.lineTo(baseX + p3.x, baseY + p3.y);
164
ctx.lineTo(baseX + p4.x, baseY + p4.y);
165
ctx.lineTo(baseX + p1.x, baseY + p1.y);
166
ctx.closePath();
167
ctx.fill();
168
this.tint -= 0.02 * MainHex.dt;
169
if (this.tint < 0) {
170
this.tint = 0;
171
}
172
}
173
174
ctx.globalAlpha = 1;
175
};
176
}
177
178
function findCenterOfBlocks(arr) {
179
var avgDFH = 0;
180
var avgAngle = 0;
181
for (var i = 0; i < arr.length; i++) {
182
avgDFH += arr[i].distFromHex;
183
var ang = arr[i].angle;
184
while (ang < 0) {
185
ang += 360;
186
}
187
188
avgAngle += ang % 360;
189
}
190
191
avgDFH /= arr.length;
192
avgAngle /= arr.length;
193
194
return {
195
x:trueCanvas.width/2 + Math.cos(avgAngle * (Math.PI / 180)) * avgDFH,
196
y:trueCanvas.height/2 + Math.sin(avgAngle * (Math.PI / 180)) * avgDFH
197
};
198
}
199
200