class Board { constructor(stage) { this.stage = stage; this.grid = { A:{1:[],2:[],3:[],4:[],5:[]}, //Plateau de jeu B:{1:[],2:[],3:[],4:[],5:[],6:[]}, C:{1:[],2:[],3:[],4:[],5:[],6:[],7:[]}, D:{1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[]}, E:{1:[],2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[]}, F:{2:[],3:[],4:[],5:[],6:[],7:[],8:[],9:[]}, G:{3:[],4:[],5:[],6:[],7:[],8:[],9:[]}, H:{4:[],5:[],6:[],7:[],8:[],9:[]}, I:{5:[],6:[],7:[],8:[],9:[]} }; this.colors = settings.colors.board; this.colors_hole = settings.colors.hole; this.colors_p0 = settings.colors.piece_0; this.colors_p1 = settings.colors.piece_1; this.layer = this.stage.find('#board'); this.layer_pieces = this.stage.find('#pieces'); this.x = this.stage.width() / 2; this.y = this.stage.height() / 2; this.radius = this.stage.height() / 2; this.draw(); this.draw_slots(); } draw() { console.log('Board draw'); var hexagon = new Kinetic.RegularPolygon({ sides : 6, radius : this.radius, stroke : this.colors.stroke, strokeWidth : 5, fill : this.colors.bg, x : this.x, y : this.y }) hexagon.rotate(Math.PI/6) this.layer.add(hexagon) return hexagon } draw_slots() { var grid = this.grid; var colors = this.colors_hole var x = this.x; var y = this.y; var radius = this.radius; var radius_slot = radius / 10 - radius / 50; var hor = ["A","B","C","D","E","F","G","H","I"]; var dia = ["1","2","3","4","5","6","7","8","9"]; var h = 4; var d = 4; var rad = -1; var off = -1; var offx = -1; var radx = -1; var slot = false; // center circle slot = new Slot(x, y, radius_slot, 0, 0, { x: hor[h], y: dia[d] }); grid[hor[h]][dia[d]].push(slot); // we turn around the center, each "i" // is a sixth of PI/2 for (var i = 0; i < 6; i++) { rad = i * Math.PI / 3; // center coordinates h = 4 d = 4 for (var j = 0; j < 4; j++) { off = (j + 1) * (2 * radius_slot + (radius / 20)); switch (i) { case 1: case 2: h--; break; case 4: case 5: h++; break; } switch (i) { case 0: case 1: d--; break; case 3: case 4: d++; break; } slot = new Slot(x, y, radius_slot, rad, off, { x: hor[h], y: dia[d] }); grid[hor[h]][dia[d]].push(slot); var h_ = h var d_ = d for (var k = 0; k < j; k++) { offx = Math.sqrt(Math.pow(j, 2) + j + 1); radx = Math.atan(Math.sqrt(3) / (j * 2 + 1)); if (k >= (j / 2)) { radx = Math.PI/3 - radx; } else if (k == 1 && j == 3) { offx = 2 * Math.sqrt(3); radx = Math.atan(Math.sqrt(3) / 3); } radx += (i * Math.PI / 3); switch (i) { case 2: case 3: h_++; break; case 0: case 5: h_--; break; } switch (i) { case 1: case 2: d_++; break; case 4: case 5: d_--; break; } slot = new Slot(x, y, radius_slot, radx, offx * off / (j + 1), { x: hor[h_], y: dia[d_] }); grid[hor[h_]][dia[d_]].push(slot); } } } for (let g in grid) { for (let row in grid[g]) { this.layer.add(grid[g][row][0].draw()); } } } draw_pieces(pieces) { let grid = this.grid; let coord = false; let slot = false; for (let i in pieces) { coord = pieces[i].coord; grid[coord.x][coord.y][0].element.setZIndex(1); grid[coord.x][coord.y][0].element.draw(); grid[coord.x][coord.y].push(pieces[i]); grid[coord.x][coord.y][1].draw(); this.layer_pieces.add(grid[coord.x][coord.y][1].element); grid[coord.x][coord.y][1].element.setZIndex(10); grid[coord.x][coord.y][1].element.draw(); } } get_coord(coord) { if (this.grid.hasOwnProperty(coord.x) == false) { console.log('Undefined line :' + coord.x); return false; } if (this.grid[coord.x].hasOwnProperty(coord.y) == false) { console.log('Undefined column of line ' + coord.x + ' :' + coord.y); return false; } if (!(this.grid[coord.x][coord.y] instanceof Array)) { console.log(this.grid[coord.x][coord.y]); console.log('is not array'); return false; } return this.grid[coord.x][coord.y]; } pop_coord(coord) { if (this.get_coord(coord) !== false) { return this.get_coord(coord).pop(); } return false; } push_coord(coord, slot) { if (this.get_coord(coord) !== false) { return this.get_coord(coord).push(slot); } return false; } get_slot(coord) { if (this.get_coord(coord) !== false) { return this.get_coord(coord)[0]; } return false; } }