Skip to content

Commit

Permalink
Merge pull request #223 from tsg-ut/refactor-panel
Browse files Browse the repository at this point in the history
Panel#takeAndPlaceをStageに移動
  • Loading branch information
hakatashi authored Jan 9, 2017
2 parents 8f52887 + 41026a5 commit 5561ecf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 87 deletions.
104 changes: 25 additions & 79 deletions lib/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,106 +2,55 @@ const $ = require('jquery');
const assert = require('assert');

class Panel {
constructor(stage) {
this.stage = stage;
constructor(partsLimit, $panel) {
this.parts = new Map();
Object.keys(stage.config.parts).forEach((name, index) => {
const count = stage.config.parts[name];
Object.keys(partsLimit).forEach((name, index) => {
const count = partsLimit[name];
this.parts.set(name, count);
if (index === 0) {
this.selected = name;
}
});
this.$panel = this.stage.$stage.find('.panel');
this.$panel = $panel;
this.update();
}

/**
* panel上にblockを追加する
* @param {string} itemName - 追加するblock名
* @param {number} count - 追加する量
* @return {bool} 処理が成功したかどうか
* @param {string} blockName - 追加するblock名
* @return {undefined}
*/
addItem(itemName, count) {
if (!this.parts.has(itemName)) {
this.parts.set(itemName, count);
return true;
push(blockName) {
let currentCount = 0;
if (this.parts.has(blockName)) {
currentCount = this.parts.get(blockName);
}
const currentCount = this.parts.get(itemName);
if (currentCount === null) { // null means infinity
return true;
if (currentCount !== null) { // null means infinity
this.parts.set(blockName, currentCount + 1);
}
this.parts.set(itemName, currentCount + count);
return true;
this.update();
}

/**
* panel上のblockを減らす
* @param {string} itemName - 消費するblock名
* @param {number} count - 消費する量
* @return {bool} 処理が成功したかどうか
* @param {string} blockName - 消費するblock名
* @return {undefined}
*/
removeItem(itemName, count) {
if (!this.parts.has(itemName)) {
return false;
}
const currentCount = this.parts.get(itemName);
if (currentCount === null) { // null means infinity
return true;
}
if (currentCount - count > 0) {
this.parts.set(itemName, currentCount - count);
return true;
}
if (currentCount - count < 0) { // if panel doesn't have enough item
return false;
}
this.parts.delete(itemName);
return true;
}

takeAndPlace(x, y, blockName) {
const oldBlock = this.stage.board.getBlock(x, y);
assert(oldBlock, 'oldBlock is invalid');
if (blockName === 'empty') { // just take the block
if (oldBlock.config.name !== 'empty') {
// take the block from the board
this.addItem(oldBlock.config.name, 1);
this.stage.boardElement.placeBlock({x, y, type: blockName, rotate: 0});
this.update();
}
return;
}

if (this.parts.has(blockName)) {
let rotate = 0;
take(blockName) {
assert(this.parts.has(blockName), 'try to take non-existent block');

if (oldBlock.config.name === 'empty') {
// just place the block
this.removeItem(blockName, 1);
} else if (oldBlock.config.name === blockName && oldBlock.config.rotatable) {
rotate = (oldBlock.rotate + 1) % 4;
} else {
// take and place
this.removeItem(blockName, 1);
this.addItem(oldBlock.config.name, 1);
}
const currentCount = this.parts.get(blockName);
if (currentCount !== null) { // null means infinity
assert(currentCount > 0, 'the block isn\'t remaining');

if (!this.parts.has(blockName)) {
if (currentCount - 1 === 0) { // take the last block
this.selected = null;
this.parts.delete(blockName);
} else {
this.parts.set(blockName, currentCount - 1);
}

this.stage.boardElement.placeBlock({x, y, type: blockName, rotate});
this.update();
} else if (
!this.selected &&
oldBlock.config.name !== 'empty' &&
oldBlock.config.rotatable
) {
const rotate = (oldBlock.rotate + 1) % 4;
this.stage.boardElement.placeBlock({x, y, type: oldBlock.config.name, rotate});
this.update();
}
this.update();
}

update() {
Expand All @@ -119,16 +68,13 @@ class Panel {
})));
});

this.selected = this.$panel.find('.block[selected]').first().data('type');

this.$panel.find('.block').click((event) => {
const $block = $(event.target);

this.$panel.find('.block').attr('selected', false);
$block.attr('selected', true);

this.selected = $block.data('type');
this.stage.$selectedBlock = $block;
});
}
}
Expand Down
39 changes: 31 additions & 8 deletions lib/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Stage {

this.board = new Board(this.config, this.blockSize);
this.boardElement = new BoardElement(this, this.board);
this.panel = new Panel(this);
this.panel = new Panel(this.config.parts, this.$stage.find('.panel'));

this.$ranking = this.$stage.siblings('.result-layer').find('.ranking');
this.$result = this.$stage.siblings('.result-layer').find('.result');
Expand All @@ -35,20 +35,18 @@ class Stage {

this.$stage.find('.statement').text(config.statement);

this.$selectedBlock = this.$stage.find('.panel .block[selected]').first();

this.$stage.find('.board .block').click((event) => {
const $block = $(event.target);
if (this.board.status === 'stop') {
const type = this.$selectedBlock.data('type');
this.panel.takeAndPlace($block.data('x'), $block.data('y'), type);
const type = this.panel.selected;
this.takeAndPlace($block.data('x'), $block.data('y'), type);
}
});

this.$stage.find('.board .block').bind('contextmenu', (event) => {
const $block = $(event.target);
if (this.board.status === 'stop') {
this.panel.takeAndPlace($block.data('x'), $block.data('y'), 'empty');
this.takeAndPlace($block.data('x'), $block.data('y'), 'empty');
}
return false;
});
Expand Down Expand Up @@ -376,14 +374,15 @@ class Stage {
clearBoard() {
this.board.blocks.forEach((row, x) => {
row.forEach((block, y) => {
this.panel.takeAndPlace(x, y, 'empty');
this.takeAndPlace(x, y, 'empty');
});
});
}

makeBoard(board) {
board.forEach((block) => {
for (let i = 0; i < block.rotate + 1; i++) {
this.panel.takeAndPlace(block.x, block.y, block.type);
this.takeAndPlace(block.x, block.y, block.type);
}
});
}
Expand All @@ -396,6 +395,30 @@ class Stage {
$register.removeClass('success error');
$register.attr('disabled', false);
}

takeAndPlace(x, y, blockName) {
const oldBlock = this.board.getBlock(x, y);
assert(oldBlock, 'oldBlock is invalid');

if (!blockName || blockName === oldBlock.config.name) { // rotate the block
if (oldBlock.config.rotatable) {
const rotate = (oldBlock.config.rotate + 1) % 4;
this.boardElement.placeBlock({x, y, type: oldBlock.config.name, rotate});
}
} else { // replace the block
// take the block from panel
if (blockName !== 'empty') {
this.panel.take(blockName);
}

// push the old block into panel
if (oldBlock.config.name !== 'empty') {
this.panel.push(oldBlock.config.name);
}

this.boardElement.placeBlock({x, y, type: blockName, rotate: 0});
}
}
}

module.exports = Stage;

0 comments on commit 5561ecf

Please sign in to comment.