Added treat special card

This commit is contained in:
Eunchong Kim 2021-07-24 22:16:21 +09:00
parent b37a809516
commit cee411b6e5
5 changed files with 107 additions and 68 deletions

File diff suppressed because one or more lines are too long

View File

@ -57,6 +57,7 @@ export default class BasicCanvas {
this._y = y; this._y = y;
this._canvas.style.left = x + 'px'; this._canvas.style.left = x + 'px';
this._canvas.style.top = y + 'px'; this._canvas.style.top = y + 'px';
this.refresh();
} }
scale(scale) { scale(scale) {

View File

@ -86,16 +86,10 @@ export default class Card extends BasicCanvas {
mouseEffect() { mouseEffect() {
this._canvas.addEventListener('mouseenter', () => { this._canvas.addEventListener('mouseenter', () => {
console.log(this._canvas.style.top)
console.log(this._y)
console.log(this._h/4)
this._canvas.style.top = this._y - this._h/4 + 'px'; this._canvas.style.top = this._y - this._h/4 + 'px';
console.log(this._canvas.style.top)
}); });
this._canvas.addEventListener('mouseleave', () => { this._canvas.addEventListener('mouseleave', () => {
console.log(this._canvas.style.top)
this._canvas.style.top = this._y + 'px'; this._canvas.style.top = this._y + 'px';
console.log(this._canvas.style.top)
}); });
} }

View File

@ -51,21 +51,23 @@ export default class Player extends BasicCanvas {
} }
isEmpty() { isEmpty() {
return (this._cards.length === 0) ? true : false; return (this._cards.length === 0);
} }
refreshCards() { ellipticalFormula(x, a, b) {
return b * ( 1 - Math.sqrt( 1 - (x/a - 1)**2 ) );
}
reDeployCards() {
for (let i=0; i<this._cards.length; i++) { for (let i=0; i<this._cards.length; i++) {
let x0, y0, dx, dy;
if (this._id === 0) { if (this._id === 0) {
x0 = global.uno_game_w/4; const x0 = global.uno_game_w/4;
dx = global.uno_game_w/2 / (this._cards.length+1); const dx = global.uno_game_w/2 / (this._cards.length+1);
y0 = global.uno_game_h*4/5; const y0 = global.uno_game_h*4/5;
dy = 0; this._cards[i].move(x0 + dx*(i+1), y0);
this._cards[i].move(x0 + dx*(i+1), y0 + dy*(i+1));
} else { } else {
x0 = global.uno_game_w*(this._id-1)/3; const x0 = global.uno_game_w*(this._id-1)/3;
dx = global.uno_game_w/3 / (this._cards.length+2); const dx = global.uno_game_w/3 / (this._cards.length+2);
this._cards[i].move(x0 + dx*(i+1), this.ellipticalFormula(x0 + dx*(i+1), global.uno_game_w/2, global.uno_game_h)); this._cards[i].move(x0 + dx*(i+1), this.ellipticalFormula(x0 + dx*(i+1), global.uno_game_w/2, global.uno_game_h));
} }
} }
@ -73,12 +75,7 @@ export default class Player extends BasicCanvas {
sortCards() { sortCards() {
this._cards.sort( (a, b) => (a.num > b.num) ? 1 : -1 ); this._cards.sort( (a, b) => (a.num > b.num) ? 1 : -1 );
this.refreshCards(); this.reDeployCards();
}
ellipticalFormula(x, a, b) {
return b * ( 1 - Math.sqrt( 1 - (x/a - 1)**2 ) );
} }
} }

View File

@ -63,41 +63,39 @@ export default class Room extends BasicCanvas {
async startGame() { async startGame() {
console.log('Game start'); console.log('Game start');
// Init
this._turn_count = 0; this._turn_count = 0;
this._skip = false;
this._reverse = false;
this._draw2 = false;
this._draw4 = false;
this._current_player = this._players[0];
await( this.changeTopCard( this._cards.pop() ) ); await( this.changeTopCard( this._cards.pop() ) );
await( this._current_player = this._players[0] ); this.initTurn();
if (this._current_player.type === 'bot') {
this.botPlay();
} else {
this.humanTurn();
}
// if (player.isEmpty()) {
// console.log('player: ' + player.name + ' has no card left. Game end');
// break
// }
} }
processCard() { initTurn() {
// check draw cards
} if (this._draw2) {
this._draw2 = false;
async finishTurn() { for (let i=0; i<2; i++) this._current_player.addCard( this._cards.pop() );
await( this._current_player.refreshCards() ); this.finishTurn();
} else if (this._draw4) {
this._turn_count++; this._draw4 = false;
this._current_player = this.getNextPlayer(); for (let i=0; i<4; i++) this._current_player.addCard( this._cards.pop() );
if (this._current_player.type === 'human') { this.finishTurn();
this.humanTurn();
} else { } else {
this.botPlay(); if (this._current_player.type === 'bot') {
this.botTurn();
} else {
this.humanTurn();
}
} }
} }
async botPlay() { async botTurn() {
console.log('Turn count: ' + this._turn_count + ', current player: ' + this._current_player.name); console.log('Turn count: ' + this._turn_count + ', current player: ' + this._current_player.name);
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise(resolve => setTimeout(resolve, 1000));
@ -105,6 +103,7 @@ export default class Room extends BasicCanvas {
if (card) { if (card) {
console.log('played card num: ' + card.num + ', color: ' + card.color_n); console.log('played card num: ' + card.num + ', color: ' + card.color_n);
this.changeTopCard(card); this.changeTopCard(card);
this.treatCard(card);
} else { } else {
const card = this._cards.pop(); const card = this._cards.pop();
console.log('drawed card num: ' + card.num + ', color: ' + card.color_n); console.log('drawed card num: ' + card.num + ', color: ' + card.color_n);
@ -118,13 +117,16 @@ export default class Room extends BasicCanvas {
console.log('Turn count: ' + this._turn_count + ', current player: ' + this._current_player.name); console.log('Turn count: ' + this._turn_count + ', current player: ' + this._current_player.name);
this._top_back_card = this._cards[ this._cards.length-1 ]; this._top_back_card = this._cards[ this._cards.length-1 ];
this._top_back_card.mouseEffect();
// Select card event
this._current_player.cards.forEach( (card) => { this._current_player.cards.forEach( (card) => {
if (this._top_card.isMatch(card)) { if (this._top_card.isMatch(card)) {
card.mouseEffect(); card.mouseEffect();
card.canvas.addEventListener('click', () => { card.canvas.addEventListener('click', () => {
console.log('played card num: ' + card.num + ', color: ' + card.color_n); console.log('played card num: ' + card.num + ', color: ' + card.color_n);
this._current_player.removeCard(card);
// Remove event listener // Remove event listener
this._top_back_card.resetEventListener(); this._top_back_card.resetEventListener();
@ -133,15 +135,13 @@ export default class Room extends BasicCanvas {
}); });
this.changeTopCard(card); this.changeTopCard(card);
this.treatCard(card);
this._current_player.removeCard(card);
this.finishTurn(); this.finishTurn();
}); });
} }
}); });
// Draw card // Draw card event
this._top_back_card.canvas.addEventListener('click', () => { this._top_back_card.canvas.addEventListener('click', () => {
const card = this._cards.pop(); const card = this._cards.pop();
console.log('drawed card num: ' + card.num + ', color: ' + card.color_n); console.log('drawed card num: ' + card.num + ', color: ' + card.color_n);
@ -153,28 +153,75 @@ export default class Room extends BasicCanvas {
}); });
this._current_player.addCard(card); this._current_player.addCard(card);
this.finishTurn(); this.finishTurn();
}); });
} }
getNextPlayer() { treatCard(card) {
let current_player_id = this._current_player.id; console.log('treat card num:' + card.num)
if (this._reverse) { switch (card.num) {
if (current_player_id === 0) { case 10: // skip card
current_player_id = this._players.length - 1; this._skip = true;
} else { break;
current_player_id--; case 11: // reverse card
} this._reverse = (this._reverse) ? false : true;
break;
case 12: // +2 card
this._draw2 = true;
break;
case 13: // change color card
card.color_n = 0; // TODO
break;
case 14: // +4 card
this._draw4 = true;
break;
default:
break;
}
}
async finishTurn() {
console.log('finish turn')
// re-deploy player's cards
await( this._current_player.reDeployCards() );
// Check empty
if (this._current_player.isEmpty()) {
console.log('player: ' + this._current_player.name + ' has no card left. Game end');
} else { } else {
if (current_player_id === this._players.length-1) { this._turn_count++;
current_player_id = 0; await ( this.decideNextPlayer() );
this.initTurn();
}
}
decideNextPlayer() {
console.log('decide next player')
let current_player_id = this._current_player.id;
let loop_cnt = 1;
if (this._skip) {
this._skip = false;
loop_cnt++;
}
for (let i=0; i<loop_cnt; i++) {
if (this._reverse) {
if (current_player_id === 0) {
current_player_id = this._players.length - 1;
} else {
current_player_id--;
}
} else { } else {
current_player_id++; if (current_player_id === this._players.length-1) {
current_player_id = 0;
} else {
current_player_id++;
}
} }
} }
return this._players[current_player_id]; this._current_player = this._players[current_player_id];
} }
changeTopCard(card) { changeTopCard(card) {