Update view on each turn
This commit is contained in:
parent
c5c7468e3a
commit
5eba06e673
@ -16,7 +16,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
div.classList.add('uno-game-div');
|
div.classList.add('uno-game-div');
|
||||||
|
|
||||||
const inner_w = window.innerWidth;
|
const inner_w = window.innerWidth;
|
||||||
const inner_h = inner_w*9/16;
|
const inner_h = window.innerHeight;
|
||||||
// Add canvas
|
// Add canvas
|
||||||
canvas.classList.add('uno-game-canv');
|
canvas.classList.add('uno-game-canv');
|
||||||
canvas.width = inner_w;
|
canvas.width = inner_w;
|
||||||
@ -25,7 +25,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
|
|
||||||
// Add game start buttono
|
// Add game start buttono
|
||||||
const btn_width = inner_w/8;
|
const btn_width = inner_w/8;
|
||||||
start_game_box_text = new BoxText(ctx, inner_w*7/16, inner_h*3/4, inner_w/8, inner_w/8/1.6, 'Start Game');
|
start_game_box_text = new BoxText(ctx, inner_w*5/12, inner_h*3/4, inner_w/7, inner_w/8/1.6, 'Start Game');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
import Player from './player.js';
|
import Player from './player.js';
|
||||||
|
|
||||||
export default class Bot extends Player {
|
export default class Bot extends Player {
|
||||||
constructor() {
|
constructor(name, id) {
|
||||||
super('bot');
|
super(name+id, id);
|
||||||
|
this._type = 'bot';
|
||||||
}
|
}
|
||||||
|
|
||||||
playCard(top_card) {
|
playCard(top_card) {
|
||||||
|
|||||||
@ -10,7 +10,7 @@ export default class BoxText {
|
|||||||
ctx.lineWidth = 4;
|
ctx.lineWidth = 4;
|
||||||
ctx.fillStyle = "#abc";
|
ctx.fillStyle = "#abc";
|
||||||
ctx.fillRect(x, y, w, h);
|
ctx.fillRect(x, y, w, h);
|
||||||
ctx.font = Math.floor(h/4)+"px Arial";
|
ctx.font = Math.floor(h/3)+"px Arial";
|
||||||
ctx.textAlign="center";
|
ctx.textAlign="center";
|
||||||
ctx.textBaseline = "middle";
|
ctx.textBaseline = "middle";
|
||||||
ctx.fillStyle = "#000000";
|
ctx.fillStyle = "#000000";
|
||||||
|
|||||||
@ -1,7 +1,11 @@
|
|||||||
|
|
||||||
export default class Card {
|
export default class Card {
|
||||||
constructor(num, color_n) {
|
constructor(num, color_n) {
|
||||||
this._num = num;
|
this._num = num;
|
||||||
this._color_n = color_n;
|
this._color_n = color_n;
|
||||||
|
|
||||||
|
this._card_w = 240;
|
||||||
|
this._card_h = 360;
|
||||||
}
|
}
|
||||||
|
|
||||||
get num() {
|
get num() {
|
||||||
@ -26,4 +30,25 @@ export default class Card {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
drawImageFront(ctx, img, x, y) {
|
||||||
|
this._x = x;
|
||||||
|
this._y = y;
|
||||||
|
this._w = ctx.canvas.width/16;
|
||||||
|
this._h = this._w * this._card_h / this._card_w;
|
||||||
|
ctx.drawImage(img, 1+this._card_w*this._num, 1+this._card_h*this._color_n, this._card_w, this._card_h,
|
||||||
|
x, y, this._w, this._h);
|
||||||
|
}
|
||||||
|
|
||||||
|
drawImageBack(ctx, img, x, y) {
|
||||||
|
this._x = x;
|
||||||
|
this._y = y;
|
||||||
|
this._w = ctx.canvas.width/16;
|
||||||
|
this._h = this._w * this._card_h / this._card_w;
|
||||||
|
ctx.drawImage(img, x, y, this._w, this._h);
|
||||||
|
}
|
||||||
|
|
||||||
|
eraseImage(ctx) {
|
||||||
|
ctx.clearRect(this._x, this._y, this._w, this._h);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
import Player from './player.js';
|
import Player from './player.js';
|
||||||
|
|
||||||
export default class Human extends Player {
|
export default class Human extends Player {
|
||||||
constructor(name) {
|
constructor(name, id) {
|
||||||
super(name);
|
super(name, id);
|
||||||
|
this._type = 'human';
|
||||||
}
|
}
|
||||||
|
|
||||||
playCard(top_card) {
|
playCard(top_card) {
|
||||||
|
|||||||
@ -1,17 +1,39 @@
|
|||||||
export default class Player {
|
export default class Player {
|
||||||
constructor(name) {
|
constructor(name, id) {
|
||||||
this._name = name;
|
this._name = name;
|
||||||
|
this._id = id;
|
||||||
this._cards = [];
|
this._cards = [];
|
||||||
|
this._type = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
get id() {
|
||||||
|
return this._id;
|
||||||
|
}
|
||||||
|
set id(id) {
|
||||||
|
this._id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
get name() {
|
get name() {
|
||||||
return this._name;
|
return this._name;
|
||||||
}
|
}
|
||||||
|
|
||||||
set name(name) {
|
set name(name) {
|
||||||
this._name = name;
|
this._name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get type() {
|
||||||
|
return this._type;
|
||||||
|
}
|
||||||
|
set type(type) {
|
||||||
|
this._type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
get cards() {
|
||||||
|
return this._cards;
|
||||||
|
}
|
||||||
|
set cards(cards) {
|
||||||
|
this._cards = cards;
|
||||||
|
}
|
||||||
|
|
||||||
addCard(card) {
|
addCard(card) {
|
||||||
this._cards.push(card);
|
this._cards.push(card);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,35 +1,34 @@
|
|||||||
import Human from './human.js';
|
import Human from './human.js';
|
||||||
import Bot from './bot.js';
|
import Bot from './bot.js';
|
||||||
import Card from './card.js';
|
import Card from './card.js';
|
||||||
|
/* Images */
|
||||||
import cards_img from '../images/cards.svg';
|
import cards_img from '../images/cards.svg';
|
||||||
|
import card_back from '../images/card_back.svg';
|
||||||
|
|
||||||
export default class Room {
|
export default class Room {
|
||||||
constructor(name, ctx) {
|
constructor(name, ctx) {
|
||||||
this._name = name;
|
this._name = name;
|
||||||
this._players = [];
|
this._players = [];
|
||||||
this._cards = [];
|
this._cards = [];
|
||||||
|
this._used_cards = [];
|
||||||
this._ctx = ctx;
|
this._ctx = ctx;
|
||||||
|
|
||||||
this._ctx.font = "32px Arial";
|
this._ctx.font = "32px Arial";
|
||||||
this._ctx.fillText(name, 50, 50);
|
this._ctx.fillText(name, 10, 10);
|
||||||
//this._ctx.drawImage(back, canvas.width-cdWidth/2-60, canvas.height/2-cdHeight/4, cdWidth/2, cdHeight/2);
|
|
||||||
//this._ctx.fillText(playerName, 100, 390);
|
|
||||||
|
|
||||||
this._cdWidth = 240;
|
|
||||||
this._cdHeight = 360;
|
|
||||||
|
|
||||||
this._cards_img = new Image();
|
this._cards_img = new Image();
|
||||||
this._cards_img.src = cards_img;
|
this._cards_img.src = cards_img;
|
||||||
this._back_img = new Image();
|
this._card_back_img = new Image();
|
||||||
|
this._card_back_img.src = card_back;
|
||||||
}
|
}
|
||||||
|
|
||||||
addHuman(name) {
|
addHuman(name) {
|
||||||
this._players.push( new Human(name) );
|
this._players.push( new Human(name, this._players.length) );
|
||||||
console.log(this._players);
|
console.log(this._players);
|
||||||
}
|
}
|
||||||
|
|
||||||
addBot() {
|
addBot() {
|
||||||
this._players.push( new Bot() );
|
this._players.push( new Bot('bot', this._players.length) );
|
||||||
console.log(this._players);
|
console.log(this._players);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,13 +51,7 @@ export default class Room {
|
|||||||
shuffleDeck() {
|
shuffleDeck() {
|
||||||
this._players.forEach( (player) => {
|
this._players.forEach( (player) => {
|
||||||
for (let i=0; i<7; i++) {
|
for (let i=0; i<7; i++) {
|
||||||
const card = this.draw();
|
player.addCard( this.draw() );
|
||||||
const cdWidth = this._cdWidth;
|
|
||||||
const cdHeight = this._cdHeight;
|
|
||||||
this._ctx.drawImage(this._cards_img, 1+cdWidth*card.num, 1+cdHeight*card.color_n, cdWidth, cdHeight,
|
|
||||||
1+cdWidth*i/10, 100, cdWidth/10, cdHeight/10);
|
|
||||||
//(hand.length/112)*(cdWidth/3)+(canvas.width/(2+(hand.length-1)))*(i+1)-(cdWidth/4), 400, cdWidth/2, cdHeight/2);
|
|
||||||
player.addCard(card);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
console.log(this._players);
|
console.log(this._players);
|
||||||
@ -69,13 +62,34 @@ export default class Room {
|
|||||||
return this._cards.splice(card_i, 1)[0];
|
return this._cards.splice(card_i, 1)[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateView() {
|
||||||
|
const inner_w = window.innerWidth;
|
||||||
|
const inner_h = window.innerHeight;
|
||||||
|
|
||||||
|
// Drw top card
|
||||||
|
this._top_card.drawImageFront(this._ctx, this._cards_img, inner_w/2, inner_h/2);
|
||||||
|
|
||||||
|
// Draw players card
|
||||||
|
this._players.forEach( (player) => {
|
||||||
|
for (let i=0; i<player.cards.length; i++) {
|
||||||
|
const card = player.cards[i];
|
||||||
|
card.eraseImage(this._ctx);
|
||||||
|
if (player.type == 'bot') {
|
||||||
|
card.drawImageBack(this._ctx, this._card_back_img, inner_w*(i+1)/16, inner_h*player.id/5);
|
||||||
|
} else {
|
||||||
|
card.drawImageFront(this._ctx, this._cards_img, inner_w*(i+1)/16, inner_h*4/5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async startGame() {
|
async startGame() {
|
||||||
await( this.initDeck() );
|
await( this.initDeck() );
|
||||||
await( this.shuffleDeck() );
|
await( this.shuffleDeck() );
|
||||||
|
|
||||||
let count = 0;
|
let count = 0;
|
||||||
let current_turn = 0;
|
let current_turn = 0;
|
||||||
let top_card = this.draw();
|
this._top_card = this.draw();
|
||||||
|
|
||||||
console.log('Game start');
|
console.log('Game start');
|
||||||
|
|
||||||
@ -83,20 +97,29 @@ export default class Room {
|
|||||||
console.log('count: ' + count + ', current player: ' + this._players[current_turn].name);
|
console.log('count: ' + count + ', current player: ' + this._players[current_turn].name);
|
||||||
const player = this._players[current_turn];
|
const player = this._players[current_turn];
|
||||||
|
|
||||||
const card = await( player.playCard(top_card) );
|
const card = await( player.playCard(this._top_card) );
|
||||||
if (card) {
|
if (card) {
|
||||||
console.log('player: ' + player.name + ' played card num: ' + card.num + ', color: ' + card.color_n);
|
console.log('player: ' + player.name + ' played card num: ' + card.num + ', color: ' + card.color_n);
|
||||||
top_card = card;
|
card.eraseImage(this._ctx);
|
||||||
|
this._used_cards.push(this._top_card);
|
||||||
|
this._top_card = card;
|
||||||
} else {
|
} else {
|
||||||
const card = this.draw();
|
const card = this.draw();
|
||||||
console.log('player: ' + player.name + ' drawed card num: ' + card.num + ', color: ' + card.color_n);
|
console.log('player: ' + player.name + ' drawed card num: ' + card.num + ', color: ' + card.color_n);
|
||||||
player.addCard(card);
|
player.addCard(card);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.updateView();
|
||||||
|
//await Promise.all([
|
||||||
|
// timeout(1000)
|
||||||
|
//]);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||||
|
|
||||||
if (player.isEmpty()) {
|
if (player.isEmpty()) {
|
||||||
console.log('player: ' + player.name + ' has no card left. Game end');
|
console.log('player: ' + player.name + ' has no card left. Game end');
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
current_turn = (current_turn >= this._players.length-1) ? 0 : ++current_turn;
|
current_turn = (current_turn >= this._players.length-1) ? 0 : ++current_turn;
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user