Path: blob/main/public/games/files/hextris/js/Block.js
1036 views
function Block(fallingLane, color, iter, distFromHex, settled) {1// whether or not a block is rested on the center hex or another block2this.settled = (settled === undefined) ? 0 : 1;3this.height = settings.blockHeight;4//the lane which the block was shot from5this.fallingLane = fallingLane;67this.checked=0;8//the angle at which the block falls9this.angle = 90 - (30 + 60 * fallingLane);10//for calculating the rotation of blocks attached to the center hex11this.angularVelocity = 0;12this.targetAngle = this.angle;13this.color = color;14//blocks that are slated to be deleted after a valid score has happened15this.deleted = 0;16//blocks slated to be removed from falling and added to the hex17this.removed = 0;18//value for the opacity of the white blcok drawn over falling block to give it the glow as it attaches to the hex19this.tint = 0;20//value used for deletion animation21this.opacity = 1;22//boolean for when the block is expanding23this.initializing = 1;24this.ict = MainHex.ct;25//speed of block26this.iter = iter;27//number of iterations before starting to drop28this.initLen = settings.creationDt;29//side which block is attached too30this.attachedLane = 0;31//distance from center hex32this.distFromHex = distFromHex || settings.startDist * settings.scale ;3334this.incrementOpacity = function() {35if (this.deleted) {36//add shakes37if (this.opacity >= 0.925) {38var tLane = this.attachedLane - MainHex.position;39tLane = MainHex.sides - tLane;40while (tLane < 0) {41tLane += MainHex.sides;42}4344tLane %= MainHex.sides;45MainHex.shakes.push({lane:tLane, magnitude:3 * (window.devicePixelRatio ? window.devicePixelRatio : 1) * (settings.scale)});46}47//fade out the opacity48this.opacity = this.opacity - 0.075 * MainHex.dt;49if (this.opacity <= 0) {50//slate for final deletion51this.opacity = 0;52this.deleted = 2;53if (gameState == 1 || gameState==0) {54localStorage.setItem("saveState", exportSaveState());55}56}57}58};5960this.getIndex = function (){61//get the index of the block in its stack62var parentArr = MainHex.blocks[this.attachedLane];63for (var i = 0; i < parentArr.length; i++) {64if (parentArr[i] == this) {65return i;66}67}68};6970this.draw = function(attached, index) {71this.height = settings.blockHeight;72if (Math.abs(settings.scale - settings.prevScale) > 0.000000001) {73this.distFromHex *= (settings.scale/settings.prevScale);74}7576this.incrementOpacity();77if(attached === undefined)78attached = false;7980if(this.angle > this.targetAngle) {81this.angularVelocity -= angularVelocityConst * MainHex.dt;82}83else if(this.angle < this.targetAngle) {84this.angularVelocity += angularVelocityConst * MainHex.dt;85}8687if (Math.abs(this.angle - this.targetAngle + this.angularVelocity) <= Math.abs(this.angularVelocity)) { //do better soon88this.angle = this.targetAngle;89this.angularVelocity = 0;90}91else {92this.angle += this.angularVelocity;93}9495this.width = 2 * this.distFromHex / Math.sqrt(3);96this.widthWide = 2 * (this.distFromHex + this.height) / Math.sqrt(3);97//this.widthWide = this.width + this.height + 3;98var p1;99var p2;100var p3;101var p4;102if (this.initializing) {103var rat = ((MainHex.ct - this.ict)/this.initLen);104if (rat > 1) {105rat = 1;106}107p1 = rotatePoint((-this.width / 2) * rat, this.height / 2, this.angle);108p2 = rotatePoint((this.width / 2) * rat, this.height / 2, this.angle);109p3 = rotatePoint((this.widthWide / 2) * rat, -this.height / 2, this.angle);110p4 = rotatePoint((-this.widthWide / 2) * rat, -this.height / 2, this.angle);111if ((MainHex.ct - this.ict) >= this.initLen) {112this.initializing = 0;113}114} else {115p1 = rotatePoint(-this.width / 2, this.height / 2, this.angle);116p2 = rotatePoint(this.width / 2, this.height / 2, this.angle);117p3 = rotatePoint(this.widthWide / 2, -this.height / 2, this.angle);118p4 = rotatePoint(-this.widthWide / 2, -this.height / 2, this.angle);119}120121if (this.deleted) {122ctx.fillStyle = "#FFF";123} else if (gameState === 0) {124if (this.color.charAt(0) == 'r') {125ctx.fillStyle = rgbColorsToTintedColors[this.color];126}127else {128ctx.fillStyle = hexColorsToTintedColors[this.color];129}130}131else {132ctx.fillStyle = this.color;133}134135ctx.globalAlpha = this.opacity;136var baseX = trueCanvas.width / 2 + Math.sin((this.angle) * (Math.PI / 180)) * (this.distFromHex + this.height / 2) + gdx;137var baseY = trueCanvas.height / 2 - Math.cos((this.angle) * (Math.PI / 180)) * (this.distFromHex + this.height / 2) + gdy;138ctx.beginPath();139ctx.moveTo(baseX + p1.x, baseY + p1.y);140ctx.lineTo(baseX + p2.x, baseY + p2.y);141ctx.lineTo(baseX + p3.x, baseY + p3.y);142ctx.lineTo(baseX + p4.x, baseY + p4.y);143//ctx.lineTo(baseX + p1.x, baseY + p1.y);144ctx.closePath();145ctx.fill();146147if (this.tint) {148if (this.opacity < 1) {149if (gameState == 1 || gameState==0) {150localStorage.setItem("saveState", exportSaveState());151}152153this.iter = 2.25;154this.tint = 0;155}156157ctx.fillStyle = "#FFF";158ctx.globalAlpha = this.tint;159ctx.beginPath();160ctx.moveTo(baseX + p1.x, baseY + p1.y);161ctx.lineTo(baseX + p2.x, baseY + p2.y);162ctx.lineTo(baseX + p3.x, baseY + p3.y);163ctx.lineTo(baseX + p4.x, baseY + p4.y);164ctx.lineTo(baseX + p1.x, baseY + p1.y);165ctx.closePath();166ctx.fill();167this.tint -= 0.02 * MainHex.dt;168if (this.tint < 0) {169this.tint = 0;170}171}172173ctx.globalAlpha = 1;174};175}176177function findCenterOfBlocks(arr) {178var avgDFH = 0;179var avgAngle = 0;180for (var i = 0; i < arr.length; i++) {181avgDFH += arr[i].distFromHex;182var ang = arr[i].angle;183while (ang < 0) {184ang += 360;185}186187avgAngle += ang % 360;188}189190avgDFH /= arr.length;191avgAngle /= arr.length;192193return {194x:trueCanvas.width/2 + Math.cos(avgAngle * (Math.PI / 180)) * avgDFH,195y:trueCanvas.height/2 + Math.sin(avgAngle * (Math.PI / 180)) * avgDFH196};197}198199200