abalone/Slot.class.js

163 lines
4.4 KiB
JavaScript

class Slot {
constructor(x, y, r, rot, o, coord) {
this.colors = settings.colors.slot;
this.x = x;
this.y = y;
this.r = r;
this.rot = rot;
this.o = o;
this.coord = coord;
this.zIndex = 1;
this.player = false;
this.element = false;
this.piece = false;
}
draw() {
let _this = this;
let circle = new Kinetic.Circle({
layer : true,
stroke : this.colors.stroke,
strokeWidth : this.strokeWidth,
fill : this.colors.bg,
x : this.x,
y : this.y,
radius : this.r,
rotation : this.rot,
offset :{
x : this.o,
y : 0
}
}).on('click', function (e) {
_this.click();
}).on('mouseenter', function (e) {
_this.mouseenter();
}).on('mouseleave', function (e) {
_this.mouseleave(this);
}).on('mouseover', function (e) {
_this.over();
}, function(e) {
});
this.element = circle;
return this.element;
}
click() {
}
mouseenter() {
}
mouseleave() {
}
over() {
//console.log('Slot coord : ' + JSON.stringify(this.coord));
}
move(pos, rotation, offx) {
this.element.position(pos);
this.element.rotation(rotation);
this.element.offsetX(offx);
this.element.draw();
}
is_sibling(slot) {
let x0_val = this.coord.x.charCodeAt();
let x1_val = slot.coord.x.charCodeAt();
let y0_val = parseInt(this.coord.y);
let y1_val = parseInt(slot.coord.y);
let e_val = 'E'.charCodeAt();
console.log(x0_val + ':' + y0_val);
console.log(x1_val + ':' + y1_val);
if (x0_val === x1_val) {
return Math.abs(y0_val - y1_val) === 1;
}
else if (Math.abs(x0_val - x1_val) === 1) {
return (y0_val - y1_val) <= 0;
}
return false;
}
count_siblings(slots) {
let res = 0;
for (let slot of slots) {
if (this.is_sibling(slot)) {
res++;
}
}
console.log(res);
return res;
}
get_direction_coord(direction) {
let res = {'x': this.coord.x, 'y': this.coord.y};
let e_val = 'E'.charCodeAt();
let x_val = res.x.charCodeAt();
if (direction == 'e') {
res.y = (parseInt(res.y) + 1).toString();
}
else if (direction == 'w') {
res.y = (parseInt(res.y) - 1).toString();
}
else if (direction.charAt(0) == 'n') {
res.x = String.fromCharCode(x_val - 1);
if (direction.charAt(1) == 'w') {
res.y = (parseInt(res.y) - 1).toString();
}
}
else if (direction.charAt(0) == 's') {
res.x = String.fromCharCode(x_val + 1);
if (direction.charAt(1) == 'e') {
res.y = (parseInt(res.y) + 1).toString();
}
}
else {
res = false;
}
return res;
}
static are_aligned(slots, dir = false) {
if (slots.length == 1) {
return true;
}
let slot = slots.pop();
let x0 = slot.coord.x.charCodeAt();
let y0 = parseInt(slot.coord.y);
let next_slot = false;
if (dir === false) {
for (dir of ['ne', 'nw', 'w', 'sw', 'se', 'e']) {
console.log('trying dir : ' + dir);
next_slot = this.find_by_coord(slots,
slot.get_direction_coord(dir));
if (next_slot !== false) {
break;
}
}
}
else {
next_slot = this.find_by_coord(slots,
slot.get_direction_coord(dir));
}
if (next_slot) {
slots.slice(slots.indexOf(next_slot), 1);
return this.are_aligned(slots, dir);
}
return false;
}
static find_by_coord(slots, coord) {
let x0 = coord.x.charCodeAt();
let y0 = parseInt(coord.y);
for (let slot of slots) {
if (slot.coord.x.charCodeAt() == x0
&& parseInt(slot.coord.y) == y0) {
return slot;
}
}
return false;
}
}