Skip to content

Commit 50d4f1a

Browse files
committed
Better UI/styling, added tile indicator and fixed offsets
1 parent cf9531b commit 50d4f1a

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

index.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ function get_allowed_moves(piece: [Piece, Color], from: Square, ignore_check: bo
248248
var allowed_movements: Square[] = [];
249249
var movement_dirs: FixedLengthArray<Square[], 8> = [[], [], [], [], [], [], [], []];
250250

251+
if (piece === null) {
252+
return [];
253+
}
254+
251255
console.log(from);
252256

253257
const max_dist = 8;
@@ -455,6 +459,14 @@ function in_check(color: Color, after_move: [Piece, Square, Square] | null = nul
455459
return moves.length > 0;
456460
}
457461

462+
function get_tile_squares(tile: Tile): FixedLengthArray<Square, 4> {
463+
const base: Square = [tile[0] * 2, tile[1] * 2];
464+
return [
465+
base, [base[0] + 1, base[1]],
466+
[base[0], base[1] + 1], [base[0] + 1, base[1] + 1]
467+
];
468+
}
469+
458470
function tile_to_offset(tile: Tile): CanvasPosition {
459471
return [tile[0] * 150, tile[1] * 150];
460472
}
@@ -496,19 +508,27 @@ function draw_piece(square: Square, piece: [Piece, Color]) {
496508

497509
var highlight: Square | null = null;
498510
var indicators: Square[] = [];
511+
var show_tile_indicator = false;
499512

500513
function draw_highlight(square: Square) {
501514
let pos = square_to_offset(square);
502515

503-
ctx.strokeStyle = "#e4e31d";
504-
ctx.strokeRect(pos[0] - 1, pos[1] + 1, 75, 75);
516+
ctx.strokeStyle = "#1f38c3ff";
517+
ctx.strokeRect(pos[0], pos[1], 75, 75);
505518
}
506519

507520
function draw_indicator(square: Square) {
508521
let pos = square_to_offset(square);
509522

510-
ctx.fillStyle = "#9d9d9d7c";
511-
ctx.fillRect(pos[0], pos[1], 75, 75);
523+
ctx.strokeStyle = "#981b1bff";
524+
ctx.strokeRect(pos[0], pos[1], 75, 75);
525+
}
526+
527+
function draw_tile_indicator(tile: Tile) {
528+
let pos = tile_to_offset(tile);
529+
530+
ctx.strokeStyle = "#981b1bff";
531+
ctx.strokeRect(pos[0], pos[1], 150, 150);
512532
}
513533

514534
function draw_turn() {
@@ -555,12 +575,16 @@ function draw_all() {
555575
draw_tile_outline(tile);
556576
}
557577

578+
if (!!highlight) {
579+
draw_highlight(highlight);
580+
}
581+
558582
for (const indicator of indicators) {
559583
draw_indicator(indicator);
560584
}
561585

562-
if (!!highlight) {
563-
draw_highlight(highlight);
586+
if (!!show_tile_indicator) {
587+
draw_tile_indicator(empty_location);
564588
}
565589

566590
draw_turn();
@@ -590,6 +614,7 @@ function switch_turn() {
590614
has_moved_piece = false;
591615
has_moved_tile = false;
592616
indicators = [];
617+
show_tile_indicator = false;
593618
}
594619

595620
tick(0);
@@ -598,7 +623,7 @@ board.addEventListener('click', function (event) {
598623
return;
599624
}
600625
const square = offset_to_square([Math.floor(event.pageX - canvasLeft), Math.floor(event.pageY - canvasTop)]);
601-
if (square[0] >= 64 || square[1] >= 64) {
626+
if (square[0] > 7 || square[1] > 7) {
602627
return;
603628
}
604629
const tile = get_tile(square);
@@ -609,10 +634,12 @@ board.addEventListener('click', function (event) {
609634
if (get_square(square) !== null && !has_moved_piece) {
610635
indicators = get_allowed_moves(get_square(square)!, square);
611636
}
637+
show_tile_indicator = !!move_dir(tile);
612638
} else if (squareToMove !== null && squares_equal(square, squareToMove)) {
613639
squareToMove = null;
614640
highlight = null;
615641
indicators = [];
642+
show_tile_indicator = false;
616643
} else if (squareToMove !== null && !squares_equal(tile, empty_location) && ((!has_moved_piece && move_piece_and_tile) || !move_piece_and_tile) && is_move_allowed(get_square(squareToMove)!, squareToMove, square)) {
617644
const piece = get_square(squareToMove);
618645

@@ -661,6 +688,7 @@ board.addEventListener('click', function (event) {
661688

662689
squareToMove = null;
663690
highlight = null;
691+
show_tile_indicator = false;
664692
//flip_board();
665693
has_moved_piece = true;
666694
indicators = [];
@@ -676,6 +704,7 @@ board.addEventListener('click', function (event) {
676704
}
677705
squareToMove = null;
678706
highlight = null;
707+
show_tile_indicator = false;
679708
//flip_board();
680709
has_moved_tile = true;
681710
if ((move_piece_and_tile && has_moved_piece) || !move_piece_and_tile) {

0 commit comments

Comments
 (0)