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