Changed to use multiple canvas for each item with z-index
This commit is contained in:
parent
9afc0e1be2
commit
be5a129b14
68
src/index.js
68
src/index.js
@ -1,55 +1,59 @@
|
|||||||
/* Class */
|
/* Class */
|
||||||
|
import BasicCanvas from './js/basic_canvas.js';
|
||||||
import Room from './js/room.js';
|
import Room from './js/room.js';
|
||||||
import BoxText from './js/box_text.js';
|
import BoxText from './js/box_text.js';
|
||||||
/* SCSS */
|
/* SCSS */
|
||||||
import './styles/uno-game.scss';
|
import './styles/uno-game.scss';
|
||||||
|
|
||||||
const canvas = document.createElement("CANVAS");
|
// Global variables
|
||||||
const ctx = canvas.getContext('2d');
|
global.canvas_count = 0; // increment number to avoid the same canvas id
|
||||||
let start_game_box_text;
|
|
||||||
|
|
||||||
|
// uno-game-div must be found
|
||||||
|
global.uno_game_div;
|
||||||
|
global.uno_game_w = window.innerWidth;
|
||||||
|
global.uno_game_h = window.innerHeight;
|
||||||
|
|
||||||
/* Main view */
|
/* Main view */
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
// Add div class
|
// Get uno-game-div
|
||||||
const div = document.getElementById('uno-game');
|
global.uno_game_div = document.getElementById('uno-game');
|
||||||
div.classList.add('uno-game-div');
|
global.uno_game_div.classList.add('uno-game-div');
|
||||||
|
|
||||||
const inner_w = window.innerWidth;
|
// Create canvas
|
||||||
const inner_h = window.innerHeight;
|
const basic_canvas = new BasicCanvas();
|
||||||
// Add canvas
|
|
||||||
canvas.classList.add('uno-game-canv');
|
|
||||||
canvas.width = inner_w;
|
|
||||||
canvas.height = inner_h;
|
|
||||||
div.appendChild( canvas );
|
|
||||||
|
|
||||||
// Add game start buttono
|
// Set canvas
|
||||||
const btn_width = inner_w/8;
|
basic_canvas.canvas.classList.add('uno-game-canv-main');
|
||||||
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');
|
|
||||||
|
// Add game start button with bitmap
|
||||||
|
const btn_width = global.uno_game_w/8;
|
||||||
|
const start_game_box_text = new BoxText(basic_canvas.ctx, global.uno_game_w*5/12, global.uno_game_h*3/4,
|
||||||
|
global.uno_game_w/7, global.uno_game_w/8/1.6, 'Start Game');
|
||||||
|
|
||||||
|
/* Canvas click */
|
||||||
|
basic_canvas.canvas.addEventListener("click", e => {
|
||||||
|
const rect = basic_canvas.canvas.getBoundingClientRect();
|
||||||
|
const point = {
|
||||||
|
x: e.clientX - rect.left,
|
||||||
|
y: e.clientY - rect.top
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log(point);
|
||||||
|
if ( start_game_box_text.isClicked(point) ) {
|
||||||
|
basic_canvas.clear();
|
||||||
|
startGame();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Canvas click */
|
|
||||||
canvas.addEventListener("click", e => {
|
|
||||||
const rect = canvas.getBoundingClientRect();
|
|
||||||
const point = {
|
|
||||||
x: e.clientX - rect.left,
|
|
||||||
y: e.clientY - rect.top
|
|
||||||
};
|
|
||||||
|
|
||||||
console.log(point);
|
|
||||||
if ( start_game_box_text.isClicked(point) ) {
|
|
||||||
start_game_box_text.clear();
|
|
||||||
startGame();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const room = new Room('room1', ctx);
|
|
||||||
|
|
||||||
/* Game start */
|
/* Game start */
|
||||||
function startGame() {
|
function startGame() {
|
||||||
console.info('Game start');
|
console.info('Game start');
|
||||||
|
|
||||||
|
const room = new Room('room1');
|
||||||
|
|
||||||
room.addHuman('newini');
|
room.addHuman('newini');
|
||||||
room.addBot();
|
room.addBot();
|
||||||
room.addBot();
|
room.addBot();
|
||||||
|
|||||||
31
src/js/basic_canvas.js
Normal file
31
src/js/basic_canvas.js
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
export default class BasicCanvas {
|
||||||
|
constructor() {
|
||||||
|
this._canvas = document.createElement("CANVAS");
|
||||||
|
global.uno_game_div.appendChild( this._canvas );
|
||||||
|
global.canvas_count++;
|
||||||
|
console.debug(global.canvas_count);
|
||||||
|
this._canvas.width = global.uno_game_w;
|
||||||
|
this._canvas.height = global.uno_game_h;
|
||||||
|
this._canvas.classList.add('uno-game-canv-default');
|
||||||
|
this._canvas.style += 'z-index: ' + global.canvas_count + ';';
|
||||||
|
this._ctx = this._canvas.getContext('2d');
|
||||||
|
}
|
||||||
|
|
||||||
|
get canvas() {
|
||||||
|
return this._canvas;
|
||||||
|
}
|
||||||
|
set canvas(canvas) {
|
||||||
|
this._canvas = canvas;
|
||||||
|
}
|
||||||
|
|
||||||
|
get ctx() {
|
||||||
|
return this._ctx;
|
||||||
|
}
|
||||||
|
set ctx(ctx) {
|
||||||
|
this._ctx = ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
clear() {
|
||||||
|
this._ctx.clearRect(0, 0, global.uno_game_w, global.uno_game_h);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -21,8 +21,4 @@ export default class BoxText {
|
|||||||
return ( (this._x <= point.x && point.x <= this._x + this._w)
|
return ( (this._x <= point.x && point.x <= this._x + this._w)
|
||||||
&& (this._y <= point.y && point.y <= this._y + this._h) )
|
&& (this._y <= point.y && point.y <= this._y + this._h) )
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
|
||||||
this._ctx.clearRect(this._x, this._y, this._w, this._h);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,22 @@
|
|||||||
|
import BasicCanvas from './basic_canvas.js';
|
||||||
|
/* Images */
|
||||||
|
import cards_img from '../images/cards.svg';
|
||||||
|
import card_back from '../images/card_back.svg';
|
||||||
|
|
||||||
export default class Card {
|
export default class Card extends BasicCanvas {
|
||||||
constructor(num, color_n) {
|
constructor(num, color_n) {
|
||||||
|
super();
|
||||||
|
|
||||||
this._num = num;
|
this._num = num;
|
||||||
this._color_n = color_n;
|
this._color_n = color_n;
|
||||||
|
|
||||||
this._card_w = 240;
|
this._c_w = 240;
|
||||||
this._card_h = 360;
|
this._c_h = 360;
|
||||||
|
|
||||||
|
this._cards_img = new Image();
|
||||||
|
this._cards_img.src = cards_img;
|
||||||
|
this._card_back_img = new Image();
|
||||||
|
this._card_back_img.src = card_back;
|
||||||
}
|
}
|
||||||
|
|
||||||
get num() {
|
get num() {
|
||||||
@ -31,24 +42,22 @@ export default class Card {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
drawImageFront(ctx, img, x, y) {
|
drawImageFront(x, y) {
|
||||||
|
this.clear();
|
||||||
this._x = x;
|
this._x = x;
|
||||||
this._y = y;
|
this._y = y;
|
||||||
this._w = ctx.canvas.width/16;
|
this._w = global.uno_game_w/16;
|
||||||
this._h = this._w * this._card_h / this._card_w;
|
this._h = this._w * this._c_h / this._c_w;
|
||||||
ctx.drawImage(img, 1+this._card_w*this._num, 1+this._card_h*this._color_n, this._card_w, this._card_h,
|
this._ctx.drawImage(this._cards_img, 1+this._c_w*this._num, 1+this._c_h*this._color_n, this._c_w, this._c_h,
|
||||||
x, y, this._w, this._h);
|
x, y, this._w, this._h);
|
||||||
}
|
}
|
||||||
|
|
||||||
drawImageBack(ctx, img, x, y) {
|
drawImageBack(x, y) {
|
||||||
|
this.clear();
|
||||||
this._x = x;
|
this._x = x;
|
||||||
this._y = y;
|
this._y = y;
|
||||||
this._w = ctx.canvas.width/16;
|
this._w = global.uno_game_w/16;
|
||||||
this._h = this._w * this._card_h / this._card_w;
|
this._h = this._w * this._c_h / this._c_w;
|
||||||
ctx.drawImage(img, x, y, this._w, this._h);
|
this._ctx.drawImage(this._card_back_img, x, y, this._w, this._h);
|
||||||
}
|
|
||||||
|
|
||||||
eraseImage(ctx) {
|
|
||||||
ctx.clearRect(this._x, this._y, this._w, this._h);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,9 @@
|
|||||||
export default class Player {
|
import BasicCanvas from './basic_canvas.js';
|
||||||
|
|
||||||
|
export default class Player extends BasicCanvas {
|
||||||
constructor(name, id) {
|
constructor(name, id) {
|
||||||
|
super();
|
||||||
|
|
||||||
this._name = name;
|
this._name = name;
|
||||||
this._id = id;
|
this._id = id;
|
||||||
this._cards = [];
|
this._cards = [];
|
||||||
|
|||||||
@ -1,25 +1,21 @@
|
|||||||
|
import BasicCanvas from './basic_canvas.js';
|
||||||
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 */
|
/* Images */
|
||||||
import cards_img from '../images/cards.svg';
|
|
||||||
import card_back from '../images/card_back.svg';
|
|
||||||
|
|
||||||
export default class Room {
|
export default class Room extends BasicCanvas {
|
||||||
constructor(name, ctx) {
|
constructor(name) {
|
||||||
|
super();
|
||||||
|
|
||||||
this._name = name;
|
this._name = name;
|
||||||
this._players = [];
|
this._players = [];
|
||||||
this._cards = [];
|
this._cards = [];
|
||||||
this._used_cards = [];
|
this._used_cards = [];
|
||||||
this._ctx = ctx;
|
|
||||||
|
|
||||||
|
// Fill room name
|
||||||
this._ctx.font = "32px Arial";
|
this._ctx.font = "32px Arial";
|
||||||
this._ctx.fillText(name, 10, 10);
|
this._ctx.fillText(name, 10, 10);
|
||||||
|
|
||||||
this._cards_img = new Image();
|
|
||||||
this._cards_img.src = cards_img;
|
|
||||||
this._card_back_img = new Image();
|
|
||||||
this._card_back_img.src = card_back;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addHuman(name) {
|
addHuman(name) {
|
||||||
@ -41,8 +37,7 @@ export default class Room {
|
|||||||
if ( (x === 13) && (y >= 4) ) { // +4 cards
|
if ( (x === 13) && (y >= 4) ) { // +4 cards
|
||||||
x = 14;
|
x = 14;
|
||||||
}
|
}
|
||||||
const card = new Card(x, y%4);
|
this._cards.push( new Card(x, y%4) );
|
||||||
this._cards.push(card);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log(this._cards);
|
console.log(this._cards);
|
||||||
@ -67,17 +62,17 @@ export default class Room {
|
|||||||
const inner_h = window.innerHeight;
|
const inner_h = window.innerHeight;
|
||||||
|
|
||||||
// Drw top card
|
// Drw top card
|
||||||
this._top_card.drawImageFront(this._ctx, this._cards_img, inner_w/2, inner_h/2);
|
this._top_card.drawImageFront(inner_w/2, inner_h/2);
|
||||||
|
|
||||||
// Draw players card
|
// Draw players card
|
||||||
this._players.forEach( (player) => {
|
this._players.forEach( (player) => {
|
||||||
for (let i=0; i<player.cards.length; i++) {
|
for (let i=0; i<player.cards.length; i++) {
|
||||||
const card = player.cards[i];
|
const card = player.cards[i];
|
||||||
card.eraseImage(this._ctx);
|
card.clear();
|
||||||
if (player.type == 'bot') {
|
if (player.type == 'bot') {
|
||||||
card.drawImageBack(this._ctx, this._card_back_img, inner_w*(i+1)/16, inner_h*player.id/5);
|
card.drawImageBack(inner_w*(i+1)/16, inner_h*player.id/5);
|
||||||
} else {
|
} else {
|
||||||
card.drawImageFront(this._ctx, this._cards_img, inner_w*(i+1)/16, inner_h*4/5);
|
card.drawImageFront(inner_w*(i+1)/16, inner_h*4/5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -100,7 +95,7 @@ export default class Room {
|
|||||||
const card = await( player.playCard(this._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);
|
||||||
card.eraseImage(this._ctx);
|
card.clear();
|
||||||
this._used_cards.push(this._top_card);
|
this._used_cards.push(this._top_card);
|
||||||
this._top_card = card;
|
this._top_card = card;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.uno-game-canv {
|
.uno-game-canv-default {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
display: block;
|
display: block;
|
||||||
top: 0;
|
top: 0;
|
||||||
@ -13,6 +13,8 @@
|
|||||||
bottom: 0;
|
bottom: 0;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.uno-game-canv-main {
|
||||||
background-image: url('../images/green_table.jpg');
|
background-image: url('../images/green_table.jpg');
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user