From d3838afbdd5f92cf0399b097273f74f5901c48a6 Mon Sep 17 00:00:00 2001 From: yukirij Date: Wed, 5 Feb 2025 12:42:44 -0800 Subject: [PATCH] Add alt button; add alt move for king. --- www/js/game.js | 32 ++++++- www/js/game_config_const.js | 46 +++++---- www/js/interface.js | 179 +++++++++++++++++++++--------------- www/js/scene.js | 1 + 4 files changed, 165 insertions(+), 93 deletions(-) diff --git a/www/js/game.js b/www/js/game.js index 54db2db..ab763af 100644 --- a/www/js/game.js +++ b/www/js/game.js @@ -433,7 +433,8 @@ GAME.Game = class { // Handle alt moves. if(play.source == 2) { switch(moves.alt) { - case 1: { + case 1: + case 3: { piece.promoted = false; } break; } @@ -691,7 +692,7 @@ GAME.Game = class { case 1: { // Check all tiles if not blocking. if(block_directions == 0) { - for(let i = 0; i < GAME_DATA.board.tiles.length; ++i) { + for(let i = 0; i < this.board.tiles.length; ++i) { if(this.placable_tile(piece, i, {check:false, extent:false})) { tiles.push(new GAME.MovementTile(i, true, false, 0, 0)); } @@ -728,7 +729,7 @@ GAME.Game = class { case 2: { // Check all tiles if not blocking. if(block_directions == 0) { - for(let i = 0; i < GAME_DATA.board.tiles.length; ++i) { + for(let i = 0; i < this.board.tiles.length; ++i) { let tile_hex = this.board.tiles[i].hex; let valid = true; @@ -791,6 +792,21 @@ GAME.Game = class { } } } break; + + // King, Swap + case 3: { + // Check all tiles if not blocking. + for(let target of this.board.pieces) { + if(target !== null) { + if(target.piece != piece.piece && target.player == piece.player) { + let tile = this.board.tiles[target.tile]; + if(target.blocking == 0 && tile.threaten[+!piece.player] == 0) { + tiles.push(new GAME.MovementTile(target.tile, true, false, 0, 0)); + } + } + } + } + } break; } } @@ -1074,6 +1090,16 @@ GAME.Const = { .add(5) .add(7) .add(10), + new GAME.PieceMovement() + .add(0) + .add(1) + .add(2) + .add(3) + .add(4) + .add(5) + .add(7) + .add(10) + .add_alt(3), ), ], diff --git a/www/js/game_config_const.js b/www/js/game_config_const.js index 477df98..70f2439 100644 --- a/www/js/game_config_const.js +++ b/www/js/game_config_const.js @@ -139,7 +139,16 @@ const CONFIG_PIECES_STANDARD = [ .add(17) ), new GAME.GamePiece( - "Heart", + "Hearth", + new GAME.PieceMovement() + .add(0) + .add(1) + .add(2) + .add(3) + .add(4) + .add(5) + .add(7) + .add(10), new GAME.PieceMovement() .add(0) .add(1) @@ -192,7 +201,6 @@ const GAME_CONFIGS = { new GameConfig.Layout() .add_piece(0, 0, false, new MATH.Vec2(4, 3)) .add_piece(0, 0, true, new MATH.Vec2(4, 5)) - .add_piece(0, 0, false, new MATH.Vec2(2, 5)) ) .set_rule("turn", false), @@ -213,9 +221,8 @@ const GAME_CONFIGS = { .set_pool(CONFIG_POOL_DEMO) .set_layout( new GameConfig.Layout() - .add_piece(2, 0, false, new MATH.Vec2(4, 3)) + .add_piece(2, 0, false, new MATH.Vec2(3, 3)) .add_piece(2, 0, true, new MATH.Vec2(5, 5)) - .add_piece(2, 0, false, new MATH.Vec2(3, 4)) ) .set_rule("turn", false), @@ -227,8 +234,6 @@ const GAME_CONFIGS = { new GameConfig.Layout() .add_piece(3, 0, false, new MATH.Vec2(4, 3)) .add_piece(3, 0, true, new MATH.Vec2(4, 5)) - .add_piece(4, 0, false, new MATH.Vec2(5, 4)) - .add_piece(2, 0, false, new MATH.Vec2(5, 7)) ) .set_rule("turn", false), @@ -249,12 +254,8 @@ const GAME_CONFIGS = { .set_pool(CONFIG_POOL_DEMO) .set_layout( new GameConfig.Layout() - .add_piece(5, 0, false, new MATH.Vec2(4, 2)) - .add_piece(5, 0, true, new MATH.Vec2(4, 4)) - .add_piece(5, 1, false, new MATH.Vec2(4, 6)) - .add_piece(0, 1, false, new MATH.Vec2(2, 3)) - .add_piece(2, 1, false, new MATH.Vec2(3, 4)) - .add_piece(2, 1, false, new MATH.Vec2(5, 4)) + .add_piece(5, 0, false, new MATH.Vec2(4, 3)) + .add_piece(5, 0, true, new MATH.Vec2(4, 5)) ) .set_rule("turn", false), @@ -265,9 +266,9 @@ const GAME_CONFIGS = { .set_layout( new GameConfig.Layout() .add_piece(6, 0, false, new MATH.Vec2(4, 3)) - .add_piece(6, 0, true, new MATH.Vec2(3, 4)) - .add_piece(2, 1, false, new MATH.Vec2(4, 4)) + .add_piece(6, 0, true, new MATH.Vec2(4, 5)) .add_piece(2, 0, false, new MATH.Vec2(5, 6)) + .add_piece(2, 0, false, new MATH.Vec2(5, 3)) ) .set_rule("turn", false), @@ -277,9 +278,9 @@ const GAME_CONFIGS = { .set_pool(CONFIG_POOL_DEMO) .set_layout( new GameConfig.Layout() - .add_piece(7, 0, false, new MATH.Vec2(4, 4)) - .add_piece(7, 1, false, new MATH.Vec2(3, 5)) - .add_piece(1, 1, false, new MATH.Vec2(5, 7)) + .add_piece(7, 0, false, new MATH.Vec2(4, 3)) + .add_piece(7, 1, true, new MATH.Vec2(3, 4)) + .add_piece(1, 1, false, new MATH.Vec2(5, 6)) ) .set_rule("turn", false), @@ -365,6 +366,17 @@ const GAME_CONFIGS = { .add_piece(6, 1, true, new MATH.Vec2(4, 4)) ), + Guide_Check4: new GameConfig() + .set_pieces(CONFIG_PIECES_STANDARD) + .set_pool(new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])) + .set_layout( + new GameConfig.Layout() + .add_piece(7, 0, true, new MATH.Vec2(4, 0)) + .add_piece(0, 0, false, new MATH.Vec2(7, 4)) + .add_piece(5, 1, true, new MATH.Vec2(3, 1)) + .add_piece(6, 1, true, new MATH.Vec2(5, 3)) + ), + // Checkmate Guide_Checkmate: new GameConfig() .set_pieces(CONFIG_PIECES_STANDARD) diff --git a/www/js/interface.js b/www/js/interface.js index 6a1c895..2546f0f 100644 --- a/www/js/interface.js +++ b/www/js/interface.js @@ -138,31 +138,38 @@ const INTERFACE = { resolve_piece(selection, zone) { // Determine piece movement hints. - let movements = null; let player = 0; - if(selection.source == 0) { - let piece_id = GAME_DATA.board.tiles[selection.tile].piece; - if(piece_id !== null) { - let piece = GAME_DATA.board.pieces[piece_id]; - player = piece.player; - if(piece.moves().alt && INTERFACE_DATA.alt_mode) { - movements = GAME_DATA.movement_tiles_alt(piece); - } else { - movements = GAME_DATA.movement_tiles(piece, selection.tile); - } - } - } else { - player = Math.floor(selection.tile / 7); - player ^= INTERFACE_DATA.player & 1; - if(INTERFACE_DATA.player == 2) { player ^= INTERFACE_DATA.rotate; } + let piece_id = null; - let select_alt = INTERFACE.selection_has_alt(INTERFACE_DATA.select); - if(select_alt !== null) { - movements = GAME_DATA.movement_tiles_alt(select_alt); - } else { + // Get movements from source. + switch(selection.source) { + case 0: { + piece_id = GAME_DATA.board.tiles[selection.tile].piece; + if(piece_id !== null) { + let piece = GAME_DATA.board.pieces[piece_id]; + player = piece.player; + if(piece.moves().alt && INTERFACE_DATA.alt_mode) { + movements = GAME_DATA.movement_tiles_alt(piece); + } else { + movements = GAME_DATA.movement_tiles(piece, selection.tile); + } + } + } break; + + case 1: { + player = Math.floor(selection.tile / 7); + player ^= INTERFACE_DATA.player & 1; + if(INTERFACE_DATA.player == 2) { player ^= INTERFACE_DATA.rotate; } movements = GAME_DATA.placement_tiles(selection.tile % 7, player); - } + } break; + + case 2: { + let select_alt = INTERFACE.selection_has_alt(INTERFACE_DATA.select); + if(select_alt !== null) { + movements = GAME_DATA.movement_tiles_alt(select_alt); + } + } break; } if(movements !== null) { @@ -265,6 +272,8 @@ const INTERFACE = { let tx = (2 * (hx > 0)) + (2 * (hx > 1)); let tile = tile_set + tx + hy; INTERFACE_DATA.hover = new INTERFACE.Selection(1, tile, hex); + } else if(hx == 1 && hy == 3) { + INTERFACE_DATA.hover = new INTERFACE.Selection(2, 0, hex); } } } @@ -281,35 +290,25 @@ const INTERFACE = { click(event) { let play_player = -1; - console.log("CLICK"); switch(event.button) { // Main button case 0: { - console.log("A"); if(INTERFACE_DATA.hover !== null) { - console.log("B"); INTERFACE_DATA.clicked = INTERFACE_DATA.hover; if(INTERFACE.Ui.match_select(INTERFACE_DATA.hover, INTERFACE_DATA.select)) { INTERFACE_DATA.clicked = null; } else { - console.log("C"); // Check if operation can be performed on new tile. // Otherwise, switch selection. let result = 0; if(INTERFACE_DATA.select !== null) { - console.log("Select not null"); - // Play selection. if(INTERFACE_DATA.hover.source == 0 && (INTERFACE_DATA.mode == INTERFACE.Mode.Local || (INTERFACE_DATA.player == ((GAME_DATA.turn + GAME_DATA.config.rules.reverse) & 1) || !GAME_DATA.config.rules.turn))) { - console.log("D1"); - let tile_state = INTERFACE_DATA.Game.board_state[INTERFACE_DATA.hover.tile][1]; result = +(tile_state == INTERFACE.TileStatus.Valid || tile_state == INTERFACE.TileStatus.Threat); - console.log("RES " + result); if(INTERFACE_DATA.select.source == 1) { - console.log("SRC1"); let pool_selected = +(INTERFACE_DATA.select.tile >= 7); pool_selected ^= (INTERFACE_DATA.player & 1); @@ -319,7 +318,6 @@ const INTERFACE = { if(((GAME_DATA.turn + GAME_DATA.config.rules.reverse) & 1) != pool_selected) { if(GAME_DATA.config.rules.turn) { - console.log("NOT POOL"); result = 0; } else { play_player = pool_selected; @@ -329,28 +327,20 @@ const INTERFACE = { } // Alt move selection. - else if(INTERFACE_DATA.select.source == 0 && INTERFACE_DATA.hover.source == 1) { - console.log("D2"); - let alt_piece = INTERFACE.selection_has_alt(INTERFACE_DATA.select); - if(alt_piece !== null) { - console.log("HAS ALT"); - - let pool_player = Math.floor(INTERFACE_DATA.hover.tile / 7); - pool_player ^= INTERFACE_DATA.player & 1; - if(INTERFACE_DATA.player == 2) { pool_player ^= INTERFACE_DATA.rotate; } - - if((INTERFACE_DATA.hover.tile % 7) == alt_piece.piece && alt_piece.player == pool_player) { + else if(INTERFACE_DATA.hover.source == 2) { + if(INTERFACE_DATA.select.source == 0) { + let alt_piece = INTERFACE.selection_has_alt(INTERFACE_DATA.select); + if(alt_piece !== null) { INTERFACE_DATA.alt_mode = !INTERFACE_DATA.alt_mode; - result = 2; } } + result = 2; } } // Handle player action. switch(result) { case 1: { - console.log("ACT1"); let source = INTERFACE_DATA.select.source; if(source == 0 && INTERFACE_DATA.alt_mode) { source = 2; @@ -370,7 +360,6 @@ const INTERFACE = { } break; case 0: { - console.log("ACT2"); // Handle new selection. INTERFACE_DATA.select = null; INTERFACE_DATA.alt_mode = false; @@ -402,6 +391,7 @@ const INTERFACE = { // Clear selection if no tile is hovered. else { INTERFACE_DATA.select = null; + INTERFACE_DATA.alt_mode = false; } } break; @@ -853,6 +843,12 @@ const INTERFACE = { player_identity ^ 1, INTERFACE_DATA.mode == INTERFACE.Mode.Local && INTERFACE_DATA.mirror ); + + // Alt + draw.alt( + basis_x + (14 * radius), + basis_y - (6 * gui_scale), + ); } // Draw informational text @@ -1284,28 +1280,6 @@ const INTERFACE = { border_color = INTERFACE.Color.HintSelectLight; } - let alt_piece = INTERFACE.selection_has_alt(INTERFACE_DATA.hover); - if(alt_piece !== null) { - if(alt_piece.piece == i && alt_piece.player == player) { - background_color = INTERFACE.Color.HintValidDark; - border_color = INTERFACE.Color.HintValidTint; - } - } - - alt_piece = INTERFACE.selection_has_alt(INTERFACE_DATA.select); - if(is_select) { background_color = INTERFACE.Color.HintSelect; } - else if(alt_piece !== null) { - if(alt_piece.piece == i && alt_piece.player == player) { - if(INTERFACE_DATA.alt_mode) { - background_color = INTERFACE.Color.HintSelect; - border_color = INTERFACE.Color.HintSelectLight; - } else { - background_color = INTERFACE.Color.HintValid; - border_color = INTERFACE.Color.HintValidLight; - } - } - } - if(is_hover) { border_color = INTERFACE.Color.HintHover; } // Draw border @@ -1341,6 +1315,67 @@ const INTERFACE = { this.ctx.restore(); } } + + alt(x, y, mirror=false) { + let radius = INTERFACE.Radius * this.scale; + + let is_hover = INTERFACE.Ui.tile_is_hover(2, 0); + let is_select = INTERFACE.Ui.tile_is_select(2, 0); + + let gui_x = x + (1.5 * radius); + let gui_y = y + (2 * this.scale); + + this.ctx.save(); + this.ctx.translate(gui_x, gui_y); + + // Get background color. + let background_color = null; + let border_color = INTERFACE.Color.Promote; + + if(is_select) { + border_color = INTERFACE.Color.HintSelectLight; + } + + let alt_piece = INTERFACE.selection_has_alt(INTERFACE_DATA.hover); + if(alt_piece !== null) { + background_color = INTERFACE.Color.HintValidDark; + border_color = INTERFACE.Color.HintValidTint; + } + + alt_piece = INTERFACE.selection_has_alt(INTERFACE_DATA.select); + if(is_select) { background_color = INTERFACE.Color.HintSelect; } + else if(alt_piece !== null) { + if(INTERFACE_DATA.alt_mode) { + background_color = INTERFACE.Color.HintSelect; + border_color = INTERFACE.Color.HintSelectLight; + } else { + background_color = INTERFACE.Color.HintValid; + border_color = INTERFACE.Color.HintValidLight; + } + } + + if(is_hover) { border_color = INTERFACE.Color.HintHover; } + + // Draw border + this.ctx.fillStyle = border_color; + this.ctx.beginPath(); + this.hex(); + this.ctx.fill(); + + if(background_color === null) { background_color = INTERFACE.Color.TileDark; } + this.ctx.fillStyle = background_color; + this.ctx.beginPath(); + this.hex(0.94); + this.ctx.fill(); + + if(mirror) { + this.ctx.rotate(Math.PI); + } + + GAME_ASSET.Image["Promote"].draw(this.ctx, 1.25 * this.scale, [0, 0], INTERFACE.Color.Promote); + + this.ctx.restore(); + } }, Ui: { @@ -1487,7 +1522,7 @@ const INTERFACE = { }, load(config) { - GAME.init(config); + GAME_DATA.config = config; INTERFACE.reset(); }, @@ -1512,7 +1547,7 @@ const INTERFACE = { INTERFACE_DATA.Game.history.push(INTERFACE_DATA.Game.history_begin[i]); } - INTERFACE_DATA.Replay.turn = INTERFACE_DATA.Game.history.length + 1; + INTERFACE_DATA.Replay.turn = INTERFACE_DATA.Game.history.length; INTERFACE.replay_jump(INTERFACE_DATA.Game.history.length, false); }, @@ -1674,7 +1709,6 @@ const INTERFACE = { process(play) { let valid = true; - console.log("PRC " + play.source); switch(play.source) { case 0: case 2: { @@ -1688,7 +1722,6 @@ const INTERFACE = { valid = true; } break; } - console.log("PRC V=" + valid); if(valid) { // Send message to server for online game. @@ -1777,7 +1810,7 @@ const INTERFACE = { replay_jump(turn, animate=false) { turn = +turn; - + if(turn >= 0 && turn <= INTERFACE_DATA.Game.history.length) { if(turn <= INTERFACE_DATA.Replay.turn) { INTERFACE_DATA.Replay.turn = 0; diff --git a/www/js/scene.js b/www/js/scene.js index 83b444f..d6ef57b 100644 --- a/www/js/scene.js +++ b/www/js/scene.js @@ -677,6 +677,7 @@ const SCENES = { UI.button("1", () => { INTERFACE.load(GAME_CONFIGS.Guide_Check); }), UI.button("2", () => { INTERFACE.load(GAME_CONFIGS.Guide_Check2); }), UI.button("3", () => { INTERFACE.load(GAME_CONFIGS.Guide_Check3); }), + UI.button("4", () => { INTERFACE.load(GAME_CONFIGS.Guide_Check4); }), ], []); } break; case "checkmate": {