Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/hextris/js/Hex.js
1036 views
1
function Hex(sideLength) {
2
this.playThrough = 0;
3
this.fillColor = [44,62,80];
4
this.tempColor = [44,62,80];
5
this.angularVelocity = 0;
6
this.position = 0;
7
this.dy = 0;
8
this.dt = 1;
9
this.sides = 6;
10
this.blocks = [];
11
this.angle = 180 / this.sides;
12
this.targetAngle = this.angle;
13
this.shakes = [];
14
this.sideLength = sideLength;
15
this.strokeColor = 'blue';
16
this.x = trueCanvas.width / 2;
17
this.y = trueCanvas.height / 2;
18
this.ct = 0;
19
this.lastCombo = this.ct - settings.comboTime;
20
this.lastColorScored = "#000";
21
this.comboTime = 1;
22
this.texts = [];
23
this.lastRotate = Date.now();
24
for (var i = 0; i < this.sides; i++) {
25
this.blocks.push([]);
26
}
27
28
this.shake = function(obj) { //lane as in particle lane
29
var angle = 30 + obj.lane * 60;
30
angle *= Math.PI / 180;
31
var dx = Math.cos(angle) * obj.magnitude;
32
var dy = Math.sin(angle) * obj.magnitude;
33
gdx -= dx;
34
gdy += dy;
35
obj.magnitude /= 2 * this.dt;
36
if (obj.magnitude < 1) {
37
for (var i = 0; i < this.shakes.length; i++) {
38
if (this.shakes[i] == obj) {
39
this.shakes.splice(i, 1);
40
}
41
}
42
}
43
};
44
45
this.addBlock = function(block) {
46
if (!(gameState == 1 || gameState === 0)) return;
47
block.settled = 1;
48
block.tint = 0.6;
49
var lane = this.sides - block.fallingLane;// -this.position;
50
this.shakes.push({lane:block.fallingLane, magnitude:4.5 * (window.devicePixelRatio ? window.devicePixelRatio : 1) * (settings.scale)});
51
lane += this.position;
52
lane = (lane + this.sides) % this.sides;
53
block.distFromHex = MainHex.sideLength / 2 * Math.sqrt(3) + block.height * this.blocks[lane].length;
54
this.blocks[lane].push(block);
55
block.attachedLane = lane;
56
block.checked = 1;
57
};
58
59
this.doesBlockCollide = function(block, position, tArr) {
60
if (block.settled) {
61
return;
62
}
63
64
if (position !== undefined) {
65
arr = tArr;
66
if (position <= 0) {
67
if (block.distFromHex - block.iter * this.dt * settings.scale - (this.sideLength / 2) * Math.sqrt(3) <= 0) {
68
block.distFromHex = (this.sideLength / 2) * Math.sqrt(3);
69
block.settled = 1;
70
block.checked = 1;
71
} else {
72
block.settled = 0;
73
block.iter = 1.5 + (waveone.difficulty/15) * 3;
74
}
75
} else {
76
if (arr[position - 1].settled && block.distFromHex - block.iter * this.dt * settings.scale - arr[position - 1].distFromHex - arr[position - 1].height <= 0) {
77
block.distFromHex = arr[position - 1].distFromHex + arr[position - 1].height;
78
block.settled = 1;
79
block.checked = 1;
80
}
81
else {
82
block.settled = 0;
83
block.iter = 1.5 + (waveone.difficulty/15) * 3;
84
}
85
}
86
} else {
87
var lane = this.sides - block.fallingLane;// -this.position;
88
lane += this.position;
89
90
lane = (lane+this.sides) % this.sides;
91
var arr = this.blocks[lane];
92
93
if (arr.length > 0) {
94
if (block.distFromHex + block.iter * this.dt * settings.scale - arr[arr.length - 1].distFromHex - arr[arr.length - 1].height <= 0) {
95
block.distFromHex = arr[arr.length - 1].distFromHex + arr[arr.length - 1].height;
96
this.addBlock(block);
97
}
98
} else {
99
if (block.distFromHex + block.iter * this.dt * settings.scale - (this.sideLength / 2) * Math.sqrt(3) <= 0) {
100
block.distFromHex = (this.sideLength / 2) * Math.sqrt(3);
101
this.addBlock(block);
102
}
103
}
104
}
105
};
106
107
this.rotate = function(steps) {
108
if(Date.now()-this.lastRotate<75 && !(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) ) return;
109
if (!(gameState === 1 || gameState === 0)) return;
110
this.position += steps;
111
if (!history[this.ct]) {
112
history[this.ct] = {};
113
}
114
115
if (!history[this.ct].rotate) {
116
history[this.ct].rotate = steps;
117
}
118
else {
119
history[this.ct].rotate += steps;
120
}
121
122
while (this.position < 0) {
123
this.position += 6;
124
}
125
126
this.position = this.position % this.sides;
127
this.blocks.forEach(function(blocks) {
128
blocks.forEach(function(block) {
129
block.targetAngle = block.targetAngle - steps * 60;
130
});
131
});
132
133
this.targetAngle = this.targetAngle - steps * 60;
134
this.lastRotate = Date.now();
135
};
136
137
this.draw = function() {
138
this.x = trueCanvas.width/2;
139
140
if (gameState != -2) {
141
this.y = trueCanvas.height/2;
142
}
143
this.sideLength = settings.hexWidth;
144
gdx = 0;
145
gdy = 0;
146
for (var i = 0; i < this.shakes.length; i++) {
147
this.shake(this.shakes[i]);
148
}
149
if (this.angle > this.targetAngle) {
150
this.angularVelocity -= angularVelocityConst * this.dt;
151
}
152
else if(this.angle < this.targetAngle) {
153
this.angularVelocity += angularVelocityConst * this.dt;
154
}
155
156
if (Math.abs(this.angle - this.targetAngle + this.angularVelocity) <= Math.abs(this.angularVelocity)) { //do better soon
157
this.angle = this.targetAngle;
158
this.angularVelocity = 0;
159
}
160
else {
161
this.angle += this.angularVelocity;
162
}
163
164
drawPolygon(this.x + gdx, this.y + gdy + this.dy, this.sides, this.sideLength, this.angle,arrayToColor(this.fillColor) , 0, 'rgba(0,0,0,0)');
165
};
166
}
167
168
function arrayToColor(arr){
169
return 'rgb(' + arr[0]+ ','+arr[1]+','+arr[2]+')';
170
}
171
172