abalone/Piece.class.js

92 lines
2.2 KiB
JavaScript

class Piece extends Slot {
constructor(id, player, x, y, r, rot, o, coord, abalone) {
super(x, y, r, rot, o, coord)
this.id = id;
this.player = player;
this.player.pieces.push(this);
this.abalone = abalone;
this.zIndex = 10;
this.out = false;
this.colors = settings.colors['piece_'+player.id];
this.strokeWidth = 5;
this.selected = false;
this.hovered = false;
this.new_coord = false;
}
click() {
super.click();
if (typeof(this.abalone) == 'undefined' ||
this.abalone.state !== 1) {
alert('The game is not running!');
}
if (this.selected) {
// unselect
if (this.abalone.cur_move().del_piece(this)) {
this.set_selected();
return true;
}
}
else if (this.abalone.cur_move().add_piece(this)) {
console.log(this.abalone.cur_move());
this.set_selected();
return true;
}
return false;
}
mouseenter() {
if (typeof(this.abalone) == 'undefined' ||
this.abalone.state !== 1) {
alert('The game is not running!');
return;
}
if (!this.abalone.isplayerturn(this.player.id)) {
return;
}
this.hovered = true;
this.element.stroke(this.colors.selected);
this.element.draw();
}
mouseleave(element) {
if (typeof(this.abalone) == 'undefined' ||
this.abalone.state !== 1) {
alert('The game is not running!');
return;
}
if (this.selected || !this.hovered) {
return;
}
this.hovered = false;
element.stroke(this.colors.stroke);
element.draw();
}
set_coord(coord) {
this.coord = coord;
}
set_selected() {
if (this.selected != false) {
this.selected = false;
this.element.stroke(this.colors.stroke);
this.element.draw();
return;
}
this.selected = true;
this.element.stroke(this.colors.selected);
this.element.draw();
}
set_out() {
this.element.hide();
}
}