CBlock.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. function CBlock(iType, oSprite, oParentContainer) {
  2. var _iType = iType;
  3. var _iOrientation = 0;
  4. var _iRow;
  5. var _iCol;
  6. var _bReplace = false;
  7. var _aSpriteBlock;
  8. var _aBlockType = BLOCKS_TYPE[_iType];
  9. var _oParentContainer = oParentContainer;
  10. this._init = function (oSprite) {
  11. _iCol = Math.floor(GRID_X_HALF - this.getWidth() / 2);
  12. _iRow = 0;
  13. _aSpriteBlock = this.createSpriteBlock(oSprite);
  14. this.refreshCellPosition();
  15. this.orderCellsChildIndex();
  16. };
  17. this.getHeight = function () {
  18. return _aBlockType.length;
  19. };
  20. this.getWidth = function () {
  21. return _aBlockType[0].length;
  22. };
  23. this.createSpriteBlock = function (oSprite) {
  24. var aSpriteBlock = new Array();
  25. var iCol = _iCol;
  26. var iRow = _iRow;
  27. var iX = s_oGameField.getXByCol(iCol);
  28. var iY = s_oGameField.getYByRow(iRow);
  29. var iChildID = (_aBlockType.length * _aBlockType[0].length);
  30. var iID = 0;
  31. for (var i = 0; i < _aBlockType.length; i++) {
  32. for (var j = 0; j < _aBlockType[i].length; j++) {
  33. if (_aBlockType[i][j] === 1) {
  34. aSpriteBlock[iID] = this.createCell(iX, iY, oSprite, iRow, iCol, 0);
  35. iID++;
  36. }
  37. iCol++;
  38. iX = s_oGameField.getXByCol(iCol);
  39. iChildID--;
  40. }
  41. iRow++;
  42. iCol = _iCol;
  43. iY = s_oGameField.getYByRow(iRow);
  44. iX = s_oGameField.getXByCol(iCol);
  45. }
  46. return aSpriteBlock;
  47. };
  48. this.createCell = function (iX, iY, oSprite, iRow, iCol, iOffsetRegY) {
  49. var oCell = new CCell(iX, iY, iOffsetRegY, oSprite, _oParentContainer);
  50. oCell.setRow(iRow);
  51. oCell.setCol(iCol);
  52. return oCell;
  53. };
  54. this.orderCellsChildIndex = function () {
  55. var iID = _oParentContainer.numChildren - 1;
  56. for (var i = 0; i < _aSpriteBlock.length; i++) {
  57. _aSpriteBlock[i].setChildIndex(iID);
  58. iID--;
  59. }
  60. };
  61. this.updateRenderOffsets = function (oRenderOffsets) {
  62. var oOffset = oRenderOffsets;
  63. oOffset.x++;
  64. if (oOffset.x >= this.getWidth()) {
  65. oOffset.y++;
  66. oOffset.x = 0;
  67. }
  68. return oOffset;
  69. };
  70. this.pieceFilled = function (iStep) {
  71. var oCoord = this.stepToCooordinates(iStep);
  72. var iFilled = _aBlockType[oCoord.y][oCoord.x];
  73. return !!iFilled;
  74. };
  75. this.stepToCooordinates = function (step) {
  76. var oCoords = {x: 0, y: 0};
  77. oCoords.x = step % this.getWidth();
  78. oCoords.y = Math.floor(step / this.getWidth());
  79. return oCoords;
  80. };
  81. this.down = function () {
  82. s_oGameField.checkHitBottom(this, DOWN);
  83. if (_bReplace !== true) {
  84. _iRow += 1;
  85. this.refreshCellPosition();
  86. }
  87. };
  88. this.refreshCellPosition = function () {
  89. var iCol = _iCol;
  90. var iRow = _iRow;
  91. var iID = 0;
  92. var iX = s_oGameField.getXByCol(iCol);
  93. var iY = s_oGameField.getYByRow(iRow);
  94. for (var i = 0; i < _aBlockType.length; i++) {
  95. for (var j = 0; j < _aBlockType[i].length; j++) {
  96. if (_aBlockType[i][j] === 1) {
  97. _aSpriteBlock[iID].setPosition(iX, iY);
  98. _aSpriteBlock[iID].setRow(iRow);
  99. _aSpriteBlock[iID].setCol(iCol);
  100. _aSpriteBlock[iID].setChildIndex(s_oGameField.getChildID(iRow, iCol));
  101. iID++;
  102. }
  103. iCol++;
  104. if (iCol < GRID_X) {
  105. iX = s_oGameField.getXByCol(iCol);
  106. }
  107. }
  108. if (iRow + 1 > GRID_Y - 1) {
  109. break;
  110. }
  111. iRow++;
  112. iCol = _iCol;
  113. iY = s_oGameField.getYByRow(iRow);
  114. iX = s_oGameField.getXByCol(iCol);
  115. }
  116. };
  117. this.setReplace = function (bVal) {
  118. _bReplace = bVal;
  119. };
  120. this.getReplace = function () {
  121. return _bReplace;
  122. };
  123. this.getCol = function () {
  124. return _iCol;
  125. };
  126. this.getRow = function () {
  127. return _iRow;
  128. };
  129. this.getBlock = function () {
  130. return _aSpriteBlock;
  131. };
  132. this.setRow = function (iVal) {
  133. _iRow = iVal;
  134. };
  135. this.setCol = function (iVal) {
  136. _iCol = iVal;
  137. };
  138. this.__rotateBlock = function (aOriginalBlock) {
  139. var aRotateBlock = new Array;
  140. for (var j = aOriginalBlock[0].length - 1; j > -1; j--) {
  141. var aRow = new Array();
  142. for (var i = 0; i < aOriginalBlock.length; i++) {
  143. aRow.push(aOriginalBlock[i][j]);
  144. }
  145. aRotateBlock.push(aRow);
  146. }
  147. return aRotateBlock;
  148. };
  149. this.getOrientation = function () {
  150. return _iOrientation;
  151. };
  152. this.setOrientation = function (iStartRotation) {
  153. if (iStartRotation === 360) {
  154. iStartRotation = 0;
  155. }
  156. var iNewRotation = iStartRotation;
  157. var aRotateBlock = _aBlockType;
  158. for(var i=0; i<4; i++){
  159. //TEST ALL ROTATIONS
  160. aRotateBlock = this.__rotateBlock(aRotateBlock);
  161. //console.log(aRotateBlock)
  162. var iResult = s_oGameField.checkBadRotation(aRotateBlock, this);
  163. if(iResult === 0){
  164. break;
  165. } else{
  166. iNewRotation += ROTATION_AMOUNT;
  167. }
  168. }
  169. _iOrientation = iNewRotation;
  170. _aBlockType = aRotateBlock;
  171. };
  172. this.unload = function () {
  173. for (var i = 0; i < _aSpriteBlock.length; i++) {
  174. _aSpriteBlock[i].unload();
  175. }
  176. _aSpriteBlock = null;
  177. };
  178. this._init(oSprite);
  179. return this;
  180. }