| 1 | package Games::GUI::Battleship::Renderer; | ||
| 2 | |||
| 3 | 1 | ▶ | use v5.38; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use experimental 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use feature 'try'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | no warnings 'experimental::try'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use Cairo; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use List::Util qw(min max); | ||
| 9 | |||
| 10 | # Math constant | ||
| 11 | my $PI = 3.14159265358979323846; | ||
| 12 | |||
| 13 | sub new ( $class, %args ) { | ||
| 14 | my $self = { | ||
| 15 | cell_size => 32, | ||
| 16 | player_origin => [ 50, 80 ], | ||
| 17 | opponent_origin => [ 460, 80 ], | ||
| 18 | header_height => 60, | ||
| 19 | label_margin => 26, | ||
| 20 | }; | ||
| 21 | return bless $self, $class; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub cell_size ($self) { | ||
| 25 | return $self->{cell_size}; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub player_origin ($self) { | ||
| 29 | return @{ $self->{player_origin} }; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub opponent_origin ($self) { | ||
| 33 | return @{ $self->{opponent_origin} }; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub update_layout ( $self, $vp_w, $vp_h ) { | ||
| 37 | $self->{vp_w} = $vp_w; | ||
| 38 | $self->{vp_h} = $vp_h; | ||
| 39 | |||
| 40 | my $w_factor = $vp_w / 940; | ||
| 41 | |||
| 42 | # Header height scales smoothly with window height and width | ||
| 43 | my $header_h = int( 72 + ( $vp_h - 600 ) * 0.05 + ( $vp_w - 900 ) * 0.035 ); | ||
| 44 | $header_h = max( 74, min( 170, $header_h ) ); | ||
| 45 | |||
| 46 | # Footer height scales with height and width | ||
| 47 | my $footer_h = int( 40 + ( $vp_h - 600 ) * 0.03 + ( $vp_w - 900 ) * 0.015 ); | ||
| 48 | $footer_h = max( 42, min( 75, $footer_h ) ); | ||
| 49 | |||
| 50 | # Dynamic label margin around each board for larger titles and labels | ||
| 51 | my $avail_h_est = $vp_h - $header_h - $footer_h - 40; | ||
| 52 | my $avail_w_est = ( $vp_w - 60 - 80 ) / 2; | ||
| 53 | my $cs_est = min( int( $avail_h_est / 10 ), int( $avail_w_est / 10 ) ); | ||
| 54 | $cs_est = max( 24, min( 140, $cs_est ) ); | ||
| 55 | |||
| 56 | my $label_m = int( 28 + ( $cs_est * 0.28 ) + ( $vp_w * 0.012 ) ); | ||
| 57 | $label_m = max( 32, min( 80, $label_m ) ); | ||
| 58 | $self->{label_margin} = $label_m; | ||
| 59 | |||
| 60 | my $avail_h = $vp_h - $header_h - $footer_h - $label_m; | ||
| 61 | my $avail_w = ( $vp_w - 60 - ( 2 * $label_m ) ) / 2; | ||
| 62 | |||
| 63 | my $cs_h = int( $avail_h / 10 ); | ||
| 64 | my $cs_w = int( $avail_w / 10 ); | ||
| 65 | |||
| 66 | my $cs = min( $cs_h, $cs_w ); | ||
| 67 | $cs = max( 24, min( 140, $cs ) ); | ||
| 68 | $self->{cell_size} = $cs; | ||
| 69 | |||
| 70 | my $board_px = 10 * $cs; | ||
| 71 | my $gap = | ||
| 72 | max( 24, int( ( $vp_w - ( 2 * $board_px ) - ( 2 * $label_m ) ) / 3 ) ); | ||
| 73 | $gap = min( 120, $gap ); | ||
| 74 | |||
| 75 | my $total_w = ( 2 * $board_px ) + ( 2 * $label_m ) + $gap; | ||
| 76 | my $start_x = max( 15, int( ( $vp_w - $total_w ) / 2 ) ); | ||
| 77 | my $start_y = $header_h + $label_m; | ||
| 78 | |||
| 79 | $self->{player_origin} = [ $start_x + $label_m, $start_y ]; | ||
| 80 | $self->{opponent_origin} = | ||
| 81 | [ $start_x + $board_px + $label_m + $gap + $label_m, $start_y ]; | ||
| 82 | |||
| 83 | return $cs; | ||
| 84 | } | ||
| 85 | |||
| 86 | sub point_to_cell ( $self, $px, $py, $board_type ) { | ||
| 87 | my ( $ox, $oy ) = | ||
| 88 | ( $board_type eq 'player' ) | ||
| 89 | ? $self->player_origin | ||
| 90 | : $self->opponent_origin; | ||
| 91 | |||
| 92 | my $cs = $self->{cell_size}; | ||
| 93 | if ( $px >= $ox | ||
| 94 | && $px < $ox + ( 10 * $cs ) | ||
| 95 | && $py >= $oy | ||
| 96 | && $py < $oy + ( 10 * $cs ) ) | ||
| 97 | { | ||
| 98 | my $col = 1 + int( ( $px - $ox ) / $cs ); | ||
| 99 | my $row = 1 + int( ( $py - $oy ) / $cs ); | ||
| 100 | return ( $col, $row ); | ||
| 101 | } | ||
| 102 | return; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub cell_to_rect ( $self, $col, $row, $board_type ) { | ||
| 106 | my ( $ox, $oy ) = | ||
| 107 | ( $board_type eq 'player' ) | ||
| 108 | ? $self->player_origin | ||
| 109 | : $self->opponent_origin; | ||
| 110 | my $cs = $self->{cell_size}; | ||
| 111 | my $x = $ox + ( ( $col - 1 ) * $cs ); | ||
| 112 | my $y = $oy + ( ( $row - 1 ) * $cs ); | ||
| 113 | return ( $x, $y, $cs, $cs ); | ||
| 114 | } | ||
| 115 | |||
| 116 | # ============================================================================== | ||
| 117 | # MAIN RENDER ENTRY POINT | ||
| 118 | # ============================================================================== | ||
| 119 | |||
| 120 | sub render ( $self, $cr, $vp_w, $vp_h, %state ) { | ||
| 121 | $self->update_layout( $vp_w, $vp_h ); | ||
| 122 | |||
| 123 | # 1. Background | ||
| 124 | $self->draw_background( $cr, $vp_w, $vp_h ); | ||
| 125 | |||
| 126 | # 2. Header & Status | ||
| 127 | $self->draw_status_bar( $cr, $vp_w, $vp_h, %state ); | ||
| 128 | |||
| 129 | # 3. Player Fleet Board | ||
| 130 | $self->draw_board_grid( | ||
| 131 | $cr, 'player', | ||
| 132 | 'YOUR FLEET (DEFENSE)', | ||
| 133 | $state{phase} eq 'placement' ? 1 : 0 | ||
| 134 | ); | ||
| 135 | |||
| 136 | # 4. Opponent Radar Board | ||
| 137 | $self->draw_board_grid( | ||
| 138 | $cr, 'opponent', | ||
| 139 | 'TACTICAL RADAR (ATTACK)', | ||
| 140 | $state{phase} eq 'battle' ? 1 : 0 | ||
| 141 | ); | ||
| 142 | |||
| 143 | # 5. Draw Player's Placed Ships | ||
| 144 | if ( $state{player_board} ) { | ||
| 145 | my ( $pox, $poy ) = $self->player_origin; | ||
| 146 | for my $ship ( $state{player_board}->ships ) { | ||
| 147 | $self->draw_ship( | ||
| 148 | $cr, $ship, $pox, $poy, $self->{cell_size}, | ||
| 149 | sunk => $ship->is_sunk, | ||
| 150 | radar_sweep => $state{radar_sweep_enabled}, | ||
| 151 | sweep_progress => $state{sweep_progress}, | ||
| 152 | ); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | # 6. Draw Placement Ghost Silhouette (if placing ships) | ||
| 157 | if ( $state{phase} eq 'placement' | ||
| 158 | && $state{ghost_ship} | ||
| 159 | && $state{ghost_valid} ne '' ) | ||
| 160 | { | ||
| 161 | my ( $pox, $poy ) = $self->player_origin; | ||
| 162 | $self->draw_ship( | ||
| 163 | $cr, | ||
| 164 | $state{ghost_ship}, | ||
| 165 | $pox, | ||
| 166 | $poy, | ||
| 167 | $self->{cell_size}, | ||
| 168 | ghost => 1, | ||
| 169 | valid => $state{ghost_valid} | ||
| 170 | ); | ||
| 171 | } | ||
| 172 | |||
| 173 | # 7. Draw Opponent's Sunk Ships (revealed on radar) | ||
| 174 | if ( $state{opponent_board} ) { | ||
| 175 | my ( $oox, $ooy ) = $self->opponent_origin; | ||
| 176 | for my $ship ( $state{opponent_board}->ships ) { | ||
| 177 | if ( $ship->is_sunk || $state{phase} eq 'game_over' ) { | ||
| 178 | $self->draw_ship( | ||
| 179 | $cr, $ship, $oox, $ooy, $self->{cell_size}, | ||
| 180 | sunk => 1, | ||
| 181 | radar_sweep => $state{radar_sweep_enabled}, | ||
| 182 | sweep_progress => $state{sweep_progress}, | ||
| 183 | ); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | # 7.5 Draw Radar Sweep Beams on both boards (if enabled) | ||
| 189 | if ( $state{radar_sweep_enabled} ) { | ||
| 190 | my $progress = $state{sweep_progress} // 0.0; | ||
| 191 | my ( $pox, $poy ) = $self->player_origin; | ||
| 192 | my ( $oox, $ooy ) = $self->opponent_origin; | ||
| 193 | $self->draw_radar_beam( $cr, $pox, $poy, $progress ); | ||
| 194 | $self->draw_radar_beam( $cr, $oox, $ooy, $progress ); | ||
| 195 | } | ||
| 196 | |||
| 197 | # 8. Draw Shots on Player Board (AI attacks) | ||
| 198 | if ( $state{player_board} ) { | ||
| 199 | my ( $pox, $poy ) = $self->player_origin; | ||
| 200 | $self->draw_shots( $cr, $state{player_board}, $pox, $poy ); | ||
| 201 | } | ||
| 202 | |||
| 203 | # 9. Draw Shots on Opponent Board (Player attacks) | ||
| 204 | if ( $state{opponent_board} ) { | ||
| 205 | my ( $oox, $ooy ) = $self->opponent_origin; | ||
| 206 | $self->draw_shots( $cr, $state{opponent_board}, $oox, $ooy ); | ||
| 207 | } | ||
| 208 | |||
| 209 | # 10. Draw Hover Reticle (Opponent board during battle) | ||
| 210 | if ( $state{phase} eq 'battle' | ||
| 211 | && $state{hover_opponent_col} | ||
| 212 | && $state{hover_opponent_row} ) | ||
| 213 | { | ||
| 214 | my ( $hx, $hy, $hw, $hh ) = | ||
| 215 | $self->cell_to_rect( $state{hover_opponent_col}, | ||
| 216 | $state{hover_opponent_row}, 'opponent' ); | ||
| 217 | $self->draw_reticle( $cr, $hx, $hy, $hw, $hh ); | ||
| 218 | } | ||
| 219 | |||
| 220 | return; | ||
| 221 | } | ||
| 222 | |||
| 223 | # ============================================================================== | ||
| 224 | # DRAWING PRIMITIVES | ||
| 225 | # ============================================================================== | ||
| 226 | |||
| 227 | sub draw_background ( $self, $cr, $w, $h ) { | ||
| 228 | my $pat = Cairo::LinearGradient->create( 0, 0, 0, $h ); | ||
| 229 | $pat->add_color_stop_rgb( 0, 0.05, 0.08, 0.14 ); | ||
| 230 | $pat->add_color_stop_rgb( 1, 0.02, 0.04, 0.08 ); | ||
| 231 | $cr->set_source($pat); | ||
| 232 | $cr->rectangle( 0, 0, $w, $h ); | ||
| 233 | $cr->fill; | ||
| 234 | return; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub draw_status_bar ( $self, $cr, $w, $h, %state ) { | ||
| 238 | my $cs = $self->{cell_size}; | ||
| 239 | my $w_factor = $w / 940; | ||
| 240 | |||
| 241 | # Sub-headline status banner: scales smoothly with width | ||
| 242 | my $status_msg = $state{status_msg} // 'Ready.'; | ||
| 243 | my $status_font = max( 14, int( 15 * $w_factor ) ); | ||
| 244 | |||
| 245 | # Header title badge: at least as big as the status banner text beneath it | ||
| 246 | my $title_font = max( 20, int( $cs * 0.45 ), int( $status_font * 1.15 ) ); | ||
| 247 | $cr->select_font_face( "sans-serif", 'normal', 'bold' ); | ||
| 248 | $cr->set_font_size($title_font); | ||
| 249 | $cr->set_source_rgb( 0.85, 0.92, 1.0 ); | ||
| 250 | my $title_y = max( 28, int( $title_font * 1.18 ) ); | ||
| 251 | $cr->move_to( 30, $title_y ); | ||
| 252 | $cr->show_text("BATTLESHIP"); | ||
| 253 | my $t_ext = $cr->text_extents("BATTLESHIP"); | ||
| 254 | |||
| 255 | # Difficulty & Mode indicator: scales with width, subordinate to title | ||
| 256 | my $info_font = | ||
| 257 | max( 12, min( int( $title_font * 0.65 ), int( 13 * $w_factor ) ) ); | ||
| 258 | $cr->select_font_face( "sans-serif", 'normal', 'normal' ); | ||
| 259 | $cr->set_font_size($info_font); | ||
| 260 | $cr->set_source_rgb( 0.45, 0.65, 0.85 ); | ||
| 261 | my $diff_offset = | ||
| 262 | 30 + $t_ext->{x_advance} + max( 20, int( $info_font * 1.2 ) ); | ||
| 263 | $cr->move_to( $diff_offset, $title_y ); | ||
| 264 | my $diff_text = sprintf( | ||
| 265 | "Opponent: %s (%s AI)", | ||
| 266 | ( $state{ai_name} // 'ParityAI' ), | ||
| 267 | ( $state{ai_difficulty} // 'Normal' ) | ||
| 268 | ); | ||
| 269 | $cr->show_text($diff_text); | ||
| 270 | |||
| 271 | # Sub-headline status banner drawing | ||
| 272 | $cr->select_font_face( "sans-serif", 'normal', 'normal' ); | ||
| 273 | $cr->set_font_size($status_font); | ||
| 274 | |||
| 275 | my %type_colors = ( | ||
| 276 | hit => [ 1.0, 0.35, 0.25 ], | ||
| 277 | sunk => [ 1.0, 0.80, 0.20 ], | ||
| 278 | victory => [ 0.30, 0.90, 0.40 ], | ||
| 279 | defeat => [ 0.90, 0.20, 0.20 ], | ||
| 280 | ); | ||
| 281 | my $color = $type_colors{ $state{msg_type} // '' } // [ 0.75, 0.85, 0.95 ]; | ||
| 282 | $cr->set_source_rgb(@$color); | ||
| 283 | |||
| 284 | my $status_y = $title_y + max( 22, int( $status_font * 1.35 ) ); | ||
| 285 | $cr->move_to( 30, $status_y ); | ||
| 286 | $cr->show_text($status_msg); | ||
| 287 | |||
| 288 | # Footer fleet status counts and accuracy statistics: scales smoothly with width | ||
| 289 | if ( $state{player_board} && $state{opponent_board} ) { | ||
| 290 | my $p_rem = $state{player_board}->ships_remaining; | ||
| 291 | my $o_rem = $state{opponent_board}->ships_remaining; | ||
| 292 | my $p_acc = $state{player_accuracy} | ||
| 293 | // $state{opponent_board}->shot_accuracy; | ||
| 294 | my $o_acc = $state{opponent_accuracy} | ||
| 295 | // $state{player_board}->shot_accuracy; | ||
| 296 | |||
| 297 | my $foot_font = max( 12, int( 12 * $w_factor ) ); | ||
| 298 | $cr->select_font_face( "sans-serif", 'normal', 'normal' ); | ||
| 299 | $cr->set_font_size($foot_font); | ||
| 300 | $cr->set_source_rgb( 0.55, 0.68, 0.82 ); | ||
| 301 | my $stat_str = | ||
| 302 | sprintf( | ||
| 303 | "Fleet Status: Friendly Ships: %d/5 | Enemy Ships: %d/5 | Accuracy: Player: %.1f%% | Enemy: %.1f%%", | ||
| 304 | $p_rem, $o_rem, $p_acc, $o_acc ); | ||
| 305 | $cr->move_to( 30, $h - max( 8, int( $foot_font * 1 ) ) ); | ||
| 306 | $cr->show_text($stat_str); | ||
| 307 | } | ||
| 308 | return; | ||
| 309 | } | ||
| 310 | |||
| 311 | sub draw_board_grid ( $self, $cr, $board_type, $title, $active = 0 ) { | ||
| 312 | my ( $ox, $oy ) = | ||
| 313 | ( $board_type eq 'player' ) | ||
| 314 | ? $self->player_origin | ||
| 315 | : $self->opponent_origin; | ||
| 316 | my $cs = $self->{cell_size}; | ||
| 317 | my $bw = 10 * $cs; | ||
| 318 | my $bh = 10 * $cs; | ||
| 319 | my $w_factor = $self->{vp_w} ? ( $self->{vp_w} / 940 ) : 1.0; | ||
| 320 | |||
| 321 | # Board Title Badge: scales with both width and cell size | ||
| 322 | my $title_font = max( 13, int( 13 * $w_factor ) ); | ||
| 323 | $title_font = int( $cs * 0.32 ) if int( $cs * 0.32 ) > $title_font; | ||
| 324 | |||
| 325 | # Label font (A..J and 1..10): scales with cell size & width | ||
| 326 | my $label_font = max( 11, int( $cs * 0.34 ) ); | ||
| 327 | my $w_lbl = int( 11 * $w_factor ); | ||
| 328 | $label_font = $w_lbl if $w_lbl > $label_font; | ||
| 329 | |||
| 330 | $cr->select_font_face( "sans-serif", 'normal', 'bold' ); | ||
| 331 | $cr->set_font_size($title_font); | ||
| 332 | if ($active) { | ||
| 333 | $cr->set_source_rgb( 0.3, 0.85, 1.0 ); | ||
| 334 | } | ||
| 335 | else { | ||
| 336 | $cr->set_source_rgb( 0.45, 0.55, 0.68 ); | ||
| 337 | } | ||
| 338 | my $title_y = $oy - $label_font - max( 8, int( $title_font * 0.55 ) ); | ||
| 339 | $cr->move_to( $ox, $title_y ); | ||
| 340 | $cr->show_text($title); | ||
| 341 | |||
| 342 | # Ocean surface | ||
| 343 | $cr->rectangle( $ox, $oy, $bw, $bh ); | ||
| 344 | $cr->set_source_rgb( 0.08, 0.13, 0.21 ); | ||
| 345 | $cr->fill; | ||
| 346 | |||
| 347 | # Grid Lines | ||
| 348 | $cr->set_line_width( max( 1, $cs * 0.025 ) ); | ||
| 349 | $cr->set_source_rgba( 0.18, 0.30, 0.44, 0.65 ); | ||
| 350 | |||
| 351 | for my $i ( 0 .. 10 ) { | ||
| 352 | |||
| 353 | # Vertical | ||
| 354 | $cr->move_to( $ox + ( $i * $cs ), $oy ); | ||
| 355 | $cr->line_to( $ox + ( $i * $cs ), $oy + $bh ); | ||
| 356 | $cr->stroke; | ||
| 357 | |||
| 358 | # Horizontal | ||
| 359 | $cr->move_to( $ox, $oy + ( $i * $cs ) ); | ||
| 360 | $cr->line_to( $ox + $bw, $oy + ( $i * $cs ) ); | ||
| 361 | $cr->stroke; | ||
| 362 | } | ||
| 363 | |||
| 364 | # Outer border | ||
| 365 | $cr->rectangle( $ox, $oy, $bw, $bh ); | ||
| 366 | if ($active) { | ||
| 367 | $cr->set_line_width( max( 2, $cs * 0.045 ) ); | ||
| 368 | $cr->set_source_rgba( 0.2, 0.7, 0.95, 0.8 ); | ||
| 369 | } | ||
| 370 | else { | ||
| 371 | $cr->set_line_width( max( 1.5, $cs * 0.035 ) ); | ||
| 372 | $cr->set_source_rgba( 0.25, 0.38, 0.52, 0.8 ); | ||
| 373 | } | ||
| 374 | $cr->stroke; | ||
| 375 | |||
| 376 | # Row numbers (1..10) and Column letters (A..J) | ||
| 377 | $cr->select_font_face( "sans-serif", 'normal', 'normal' ); | ||
| 378 | $cr->set_font_size($label_font); | ||
| 379 | $cr->set_source_rgb( 0.45, 0.58, 0.72 ); | ||
| 380 | |||
| 381 | my @cols = ( 'A' .. 'J' ); | ||
| 382 | for my $c ( 0 .. 9 ) { | ||
| 383 | my $tx = | ||
| 384 | $ox + ( $c * $cs ) + int( $cs / 2 ) - int( $label_font * 0.32 ); | ||
| 385 | my $ty = $oy - max( 5, int( $label_font * 0.30 ) ); | ||
| 386 | $cr->move_to( $tx, $ty ); | ||
| 387 | $cr->show_text( $cols[$c] ); | ||
| 388 | } | ||
| 389 | |||
| 390 | for my $r ( 1 .. 10 ) { | ||
| 391 | my $tx = $ox - int( $label_font * 1.55 ); | ||
| 392 | $tx -= int( $label_font * 0.45 ) if $r == 10; | ||
| 393 | my $ty = | ||
| 394 | $oy + | ||
| 395 | ( ( $r - 1 ) * $cs ) + | ||
| 396 | int( $cs / 2 ) + | ||
| 397 | int( $label_font * 0.35 ); | ||
| 398 | $cr->move_to( $tx, $ty ); | ||
| 399 | $cr->show_text("$r"); | ||
| 400 | } | ||
| 401 | |||
| 402 | return; | ||
| 403 | } | ||
| 404 | |||
| 405 | sub draw_shots ( $self, $cr, $board, $ox, $oy ) { | ||
| 406 | my $cs = $self->{cell_size}; | ||
| 407 | |||
| 408 | for my $y ( 1 .. 10 ) { | ||
| 409 | for my $x ( 1 .. 10 ) { | ||
| 410 | my $shot = $board->shot_at( $x, $y ); | ||
| 411 | next unless $shot; | ||
| 412 | |||
| 413 | my $cx = $ox + ( ( $x - 1 ) * $cs ) + ( $cs / 2 ); | ||
| 414 | my $cy = $oy + ( ( $y - 1 ) * $cs ) + ( $cs / 2 ); | ||
| 415 | |||
| 416 | if ( $shot eq 'M' ) { | ||
| 417 | $self->draw_miss_marker( $cr, $cx, $cy, $cs ); | ||
| 418 | } | ||
| 419 | elsif ( $shot eq 'H' || $shot eq 'S' ) { | ||
| 420 | $self->draw_hit_marker( $cr, $cx, $cy, $cs, $shot eq 'S' ); | ||
| 421 | } | ||
| 422 | } | ||
| 423 | } | ||
| 424 | return; | ||
| 425 | } | ||
| 426 | |||
| 427 | sub draw_miss_marker ( $self, $cr, $cx, $cy, $cs ) { | ||
| 428 | my $r = $cs * 0.22; | ||
| 429 | |||
| 430 | # Ripple ring 1 | ||
| 431 | $cr->set_line_width(1.5); | ||
| 432 | $cr->set_source_rgba( 0.85, 0.92, 1.0, 0.7 ); | ||
| 433 | $cr->arc( $cx, $cy, $r, 0, 2 * $PI ); | ||
| 434 | $cr->stroke; | ||
| 435 | |||
| 436 | # Center white water bead | ||
| 437 | $cr->set_source_rgba( 0.9, 0.95, 1.0, 0.9 ); | ||
| 438 | $cr->arc( $cx, $cy, 2.5, 0, 2 * $PI ); | ||
| 439 | $cr->fill; | ||
| 440 | return; | ||
| 441 | } | ||
| 442 | |||
| 443 | sub draw_hit_marker ( $self, $cr, $cx, $cy, $cs, $is_sunk = 0 ) { | ||
| 444 | my $r = $cs * 0.32; | ||
| 445 | |||
| 446 | # Fire blast radial gradient | ||
| 447 | my $pat = Cairo::RadialGradient->create( $cx, $cy, 2, $cx, $cy, $r ); | ||
| 448 | if ($is_sunk) { | ||
| 449 | $pat->add_color_stop_rgba( 0, 1.0, 0.9, 0.3, 1.0 ); | ||
| 450 | $pat->add_color_stop_rgba( 0.5, 0.9, 0.2, 0.1, 0.9 ); | ||
| 451 | $pat->add_color_stop_rgba( 1, 0.4, 0.05, 0.05, 0.0 ); | ||
| 452 | } | ||
| 453 | else { | ||
| 454 | $pat->add_color_stop_rgba( 0, 1.0, 1.0, 0.6, 1.0 ); | ||
| 455 | $pat->add_color_stop_rgba( 0.4, 1.0, 0.4, 0.1, 0.9 ); | ||
| 456 | $pat->add_color_stop_rgba( 1, 0.8, 0.15, 0.1, 0.0 ); | ||
| 457 | } | ||
| 458 | |||
| 459 | $cr->set_source($pat); | ||
| 460 | $cr->arc( $cx, $cy, $r, 0, 2 * $PI ); | ||
| 461 | $cr->fill; | ||
| 462 | |||
| 463 | # Sharp central explosion pin | ||
| 464 | $cr->set_source_rgb( 1.0, 0.95, 0.8 ); | ||
| 465 | $cr->arc( $cx, $cy, 3, 0, 2 * $PI ); | ||
| 466 | $cr->fill; | ||
| 467 | |||
| 468 | # Cross spikes | ||
| 469 | $cr->set_line_width(1.5); | ||
| 470 | $cr->set_source_rgba( 1.0, 0.3, 0.2, 0.8 ); | ||
| 471 | $cr->move_to( $cx - ( $r * 0.7 ), $cy ); | ||
| 472 | $cr->line_to( $cx + ( $r * 0.7 ), $cy ); | ||
| 473 | $cr->stroke; | ||
| 474 | $cr->move_to( $cx, $cy - ( $r * 0.7 ) ); | ||
| 475 | $cr->line_to( $cx, $cy + ( $r * 0.7 ) ); | ||
| 476 | $cr->stroke; | ||
| 477 | return; | ||
| 478 | } | ||
| 479 | |||
| 480 | sub draw_reticle ( $self, $cr, $x, $y, $w, $h ) { | ||
| 481 | my $pad = 2; | ||
| 482 | $cr->set_line_width(1.5); | ||
| 483 | $cr->set_source_rgba( 0.2, 0.9, 1.0, 0.8 ); | ||
| 484 | $cr->rectangle( $x + $pad, $y + $pad, $w - ( 2 * $pad ), | ||
| 485 | $h - ( 2 * $pad ) ); | ||
| 486 | $cr->stroke; | ||
| 487 | |||
| 488 | # Crosshair ticks | ||
| 489 | my $cx = $x + ( $w / 2 ); | ||
| 490 | my $cy = $y + ( $h / 2 ); | ||
| 491 | $cr->move_to( $cx - 4, $cy ); | ||
| 492 | $cr->line_to( $cx + 4, $cy ); | ||
| 493 | $cr->stroke; | ||
| 494 | $cr->move_to( $cx, $cy - 4 ); | ||
| 495 | $cr->line_to( $cx, $cy + 4 ); | ||
| 496 | $cr->stroke; | ||
| 497 | return; | ||
| 498 | } | ||
| 499 | |||
| 500 | # ============================================================================== | ||
| 501 | # VECTOR SHIP RENDERING | ||
| 502 | # ============================================================================== | ||
| 503 | |||
| 504 | sub draw_ship ( $self, $cr, $ship, $ox, $oy, $cs, %opts ) { | ||
| 505 | my $sx = $ox + ( ( $ship->x - 1 ) * $cs ); | ||
| 506 | my $sy = $oy + ( ( $ship->y - 1 ) * $cs ); | ||
| 507 | my $len = $ship->length; | ||
| 508 | my $dir = $ship->dir; | ||
| 509 | |||
| 510 | $cr->save; | ||
| 511 | |||
| 512 | # Position and orient to canonical horizontal drawing | ||
| 513 | $cr->translate( $sx, $sy ); | ||
| 514 | if ( $dir eq 'V' ) { | ||
| 515 | $cr->translate( $cs, 0 ); | ||
| 516 | $cr->rotate( $PI / 2 ); | ||
| 517 | } | ||
| 518 | |||
| 519 | my $w = $len * $cs; | ||
| 520 | my $h = $cs; | ||
| 521 | |||
| 522 | if ( $opts{ghost} ) { | ||
| 523 | my $valid = $opts{valid} // 1; | ||
| 524 | $self->draw_ghost_hull( $cr, $ship->type, $w, $h, $valid ); | ||
| 525 | } | ||
| 526 | else { | ||
| 527 | my $method = 'draw_' . lc( $ship->type ); | ||
| 528 | my $draw_sub = $self->can($method) ? $method : 'draw_generic_hull'; | ||
| 529 | |||
| 530 | if ( $opts{radar_sweep} ) { | ||
| 531 | my $progress = $opts{sweep_progress} // 0.0; | ||
| 532 | my $alpha = $self->calc_ship_sweep_alpha( $ship, $cs, $progress ); | ||
| 533 | $cr->push_group; | ||
| 534 | $self->$draw_sub( $cr, $w, $h, %opts ); | ||
| 535 | $cr->pop_group_to_source; | ||
| 536 | $cr->paint_with_alpha($alpha); | ||
| 537 | } | ||
| 538 | else { | ||
| 539 | $self->$draw_sub( $cr, $w, $h, %opts ); | ||
| 540 | } | ||
| 541 | } | ||
| 542 | |||
| 543 | $cr->restore; | ||
| 544 | return; | ||
| 545 | } | ||
| 546 | |||
| 547 | sub draw_ghost_hull ( $self, $cr, $type, $w, $h, $valid ) { | ||
| 548 | my ( $pad_x, $pad_y ) = ( 3, 3 ); | ||
| 549 | my $rw = $w - ( 2 * $pad_x ); | ||
| 550 | my $rh = $h - ( 2 * $pad_y ); | ||
| 551 | |||
| 552 | $cr->rectangle( $pad_x, $pad_y, $rw, $rh ); | ||
| 553 | if ($valid) { | ||
| 554 | $cr->set_source_rgba( 0.1, 0.7, 0.9, 0.35 ); | ||
| 555 | $cr->fill_preserve; | ||
| 556 | $cr->set_line_width(2); | ||
| 557 | $cr->set_source_rgba( 0.2, 0.9, 1.0, 0.9 ); | ||
| 558 | $cr->stroke; | ||
| 559 | } | ||
| 560 | else { | ||
| 561 | $cr->set_source_rgba( 0.9, 0.1, 0.1, 0.35 ); | ||
| 562 | $cr->fill_preserve; | ||
| 563 | $cr->set_line_width(2); | ||
| 564 | $cr->set_source_rgba( 1.0, 0.2, 0.2, 0.9 ); | ||
| 565 | $cr->stroke; | ||
| 566 | |||
| 567 | # Warning cross-line | ||
| 568 | $cr->move_to( $pad_x, $pad_y ); | ||
| 569 | $cr->line_to( $pad_x + $rw, $pad_y + $rh ); | ||
| 570 | $cr->stroke; | ||
| 571 | } | ||
| 572 | return; | ||
| 573 | } | ||
| 574 | |||
| 575 | # --- 1. CARRIER (Size 5) --- | ||
| 576 | sub draw_carrier ( $self, $cr, $w, $h, %opts ) { | ||
| 577 | my ( $x0, $y0 ) = ( 3, 3 ); | ||
| 578 | my $cw = $w - 6; | ||
| 579 | my $ch = $h - 6; | ||
| 580 | my $yc = $h / 2; | ||
| 581 | |||
| 582 | # Flight deck polygonal hull | ||
| 583 | $cr->move_to( $x0 + 8, $y0 ); | ||
| 584 | $cr->line_to( $x0 + $cw - 4, $y0 ); | ||
| 585 | $cr->line_to( $x0 + $cw, $y0 + 5 ); | ||
| 586 | $cr->line_to( $x0 + $cw, $y0 + $ch - 5 ); | ||
| 587 | $cr->line_to( $x0 + $cw - 4, $y0 + $ch ); | ||
| 588 | $cr->line_to( $x0 + 8, $y0 + $ch ); | ||
| 589 | $cr->line_to( $x0, $yc ); | ||
| 590 | $cr->close_path; | ||
| 591 | |||
| 592 | if ( $opts{sunk} ) { | ||
| 593 | $cr->set_source_rgb( 0.16, 0.18, 0.20 ); | ||
| 594 | $cr->fill_preserve; | ||
| 595 | $cr->set_line_width(1.5); | ||
| 596 | $cr->set_source_rgb( 0.7, 0.2, 0.2 ); | ||
| 597 | $cr->stroke; | ||
| 598 | return; | ||
| 599 | } | ||
| 600 | |||
| 601 | $cr->set_source_rgb( 0.22, 0.25, 0.30 ); | ||
| 602 | $cr->fill_preserve; | ||
| 603 | $cr->set_line_width(1.5); | ||
| 604 | $cr->set_source_rgb( 0.45, 0.52, 0.62 ); | ||
| 605 | $cr->stroke; | ||
| 606 | |||
| 607 | # Angled runway line | ||
| 608 | $cr->set_line_width(1); | ||
| 609 | $cr->set_source_rgba( 0.85, 0.90, 0.95, 0.6 ); | ||
| 610 | $cr->set_dash( 0, 4, 3 ); | ||
| 611 | $cr->move_to( $x0 + 12, $yc ); | ||
| 612 | $cr->line_to( $x0 + $cw - 8, $yc ); | ||
| 613 | $cr->stroke; | ||
| 614 | $cr->set_dash(0); | ||
| 615 | |||
| 616 | # Island superstructure (starboard tower) | ||
| 617 | my $ix = $x0 + ( $cw * 0.48 ); | ||
| 618 | my $iy = $y0 + 2; | ||
| 619 | $cr->rectangle( $ix, $iy, $cw * 0.14, $ch * 0.28 ); | ||
| 620 | $cr->set_source_rgb( 0.35, 0.40, 0.48 ); | ||
| 621 | $cr->fill_preserve; | ||
| 622 | $cr->set_line_width(1); | ||
| 623 | $cr->set_source_rgb( 0.6, 0.7, 0.8 ); | ||
| 624 | $cr->stroke; | ||
| 625 | |||
| 626 | # Radar mast on island | ||
| 627 | $cr->move_to( $ix + ( $cw * 0.07 ), $iy ); | ||
| 628 | $cr->line_to( $ix + ( $cw * 0.07 ), $iy - 2 ); | ||
| 629 | $cr->stroke; | ||
| 630 | return; | ||
| 631 | } | ||
| 632 | |||
| 633 | # --- 2. BATTLESHIP (Size 4) --- | ||
| 634 | sub draw_battleship ( $self, $cr, $w, $h, %opts ) { | ||
| 635 | my ( $x0, $y0 ) = ( 3, 3 ); | ||
| 636 | my $cw = $w - 6; | ||
| 637 | my $ch = $h - 6; | ||
| 638 | my $yc = $h / 2; | ||
| 639 | |||
| 640 | # Armored warship clipper hull | ||
| 641 | $cr->move_to( $x0, $yc ); | ||
| 642 | $cr->curve_to( $x0 + 18, $y0, $x0 + 30, $y0, $x0 + $cw - 8, $y0 ); | ||
| 643 | $cr->curve_to( | ||
| 644 | $x0 + $cw, $y0 + 4, $x0 + $cw, | ||
| 645 | $y0 + $ch - 4, | ||
| 646 | $x0 + $cw - 8, | ||
| 647 | $y0 + $ch | ||
| 648 | ); | ||
| 649 | $cr->curve_to( $x0 + 30, $y0 + $ch, $x0 + 18, $y0 + $ch, $x0, $yc ); | ||
| 650 | $cr->close_path; | ||
| 651 | |||
| 652 | if ( $opts{sunk} ) { | ||
| 653 | $cr->set_source_rgb( 0.18, 0.20, 0.22 ); | ||
| 654 | $cr->fill_preserve; | ||
| 655 | $cr->set_line_width(1.5); | ||
| 656 | $cr->set_source_rgb( 0.7, 0.2, 0.2 ); | ||
| 657 | $cr->stroke; | ||
| 658 | return; | ||
| 659 | } | ||
| 660 | |||
| 661 | $cr->set_source_rgb( 0.28, 0.33, 0.40 ); | ||
| 662 | $cr->fill_preserve; | ||
| 663 | $cr->set_line_width(1.5); | ||
| 664 | $cr->set_source_rgb( 0.50, 0.58, 0.68 ); | ||
| 665 | $cr->stroke; | ||
| 666 | |||
| 667 | # Central Command Bridge Tower | ||
| 668 | my $bx = $x0 + ( $cw * 0.38 ); | ||
| 669 | my $bw = $cw * 0.24; | ||
| 670 | $cr->rectangle( $bx, $yc - ( $ch * 0.22 ), $bw, $ch * 0.44 ); | ||
| 671 | $cr->set_source_rgb( 0.38, 0.44, 0.52 ); | ||
| 672 | $cr->fill_preserve; | ||
| 673 | $cr->set_line_width(1); | ||
| 674 | $cr->set_source_rgb( 0.6, 0.7, 0.8 ); | ||
| 675 | $cr->stroke; | ||
| 676 | |||
| 677 | # Forward Gun Turret (base + twin barrels) | ||
| 678 | my $t1_x = $x0 + ( $cw * 0.20 ); | ||
| 679 | $cr->arc( $t1_x, $yc, $ch * 0.22, 0, 2 * $PI ); | ||
| 680 | $cr->set_source_rgb( 0.22, 0.26, 0.32 ); | ||
| 681 | $cr->fill; | ||
| 682 | $cr->set_line_width(1.5); | ||
| 683 | $cr->set_source_rgb( 0.6, 0.7, 0.8 ); | ||
| 684 | $cr->move_to( $t1_x, $yc - 2 ); | ||
| 685 | $cr->line_to( $t1_x - ( $cw * 0.10 ), $yc - 2 ); | ||
| 686 | $cr->stroke; | ||
| 687 | $cr->move_to( $t1_x, $yc + 2 ); | ||
| 688 | $cr->line_to( $t1_x - ( $cw * 0.10 ), $yc + 2 ); | ||
| 689 | $cr->stroke; | ||
| 690 | |||
| 691 | # Aft Gun Turret (base + twin barrels pointing aft) | ||
| 692 | my $t2_x = $x0 + ( $cw * 0.78 ); | ||
| 693 | $cr->arc( $t2_x, $yc, $ch * 0.22, 0, 2 * $PI ); | ||
| 694 | $cr->set_source_rgb( 0.22, 0.26, 0.32 ); | ||
| 695 | $cr->fill; | ||
| 696 | $cr->set_line_width(1.5); | ||
| 697 | $cr->set_source_rgb( 0.6, 0.7, 0.8 ); | ||
| 698 | $cr->move_to( $t2_x, $yc - 2 ); | ||
| 699 | $cr->line_to( $t2_x + ( $cw * 0.10 ), $yc - 2 ); | ||
| 700 | $cr->stroke; | ||
| 701 | $cr->move_to( $t2_x, $yc + 2 ); | ||
| 702 | $cr->line_to( $t2_x + ( $cw * 0.10 ), $yc + 2 ); | ||
| 703 | $cr->stroke; | ||
| 704 | return; | ||
| 705 | } | ||
| 706 | |||
| 707 | # --- 3. CRUISER (Size 3) --- | ||
| 708 | sub draw_cruiser ( $self, $cr, $w, $h, %opts ) { | ||
| 709 | my ( $x0, $y0 ) = ( 3, 3 ); | ||
| 710 | my $cw = $w - 6; | ||
| 711 | my $ch = $h - 6; | ||
| 712 | my $yc = $h / 2; | ||
| 713 | |||
| 714 | # Streamlined hull | ||
| 715 | $cr->move_to( $x0, $yc ); | ||
| 716 | $cr->line_to( $x0 + 16, $y0 + 1 ); | ||
| 717 | $cr->line_to( $x0 + $cw - 4, $y0 + 2 ); | ||
| 718 | $cr->line_to( $x0 + $cw, $yc ); | ||
| 719 | $cr->line_to( $x0 + $cw - 4, $y0 + $ch - 2 ); | ||
| 720 | $cr->line_to( $x0 + 16, $y0 + $ch - 1 ); | ||
| 721 | $cr->close_path; | ||
| 722 | |||
| 723 | if ( $opts{sunk} ) { | ||
| 724 | $cr->set_source_rgb( 0.18, 0.20, 0.22 ); | ||
| 725 | $cr->fill_preserve; | ||
| 726 | $cr->set_line_width(1.5); | ||
| 727 | $cr->set_source_rgb( 0.7, 0.2, 0.2 ); | ||
| 728 | $cr->stroke; | ||
| 729 | return; | ||
| 730 | } | ||
| 731 | |||
| 732 | $cr->set_source_rgb( 0.32, 0.37, 0.44 ); | ||
| 733 | $cr->fill_preserve; | ||
| 734 | $cr->set_line_width(1.5); | ||
| 735 | $cr->set_source_rgb( 0.52, 0.60, 0.70 ); | ||
| 736 | $cr->stroke; | ||
| 737 | |||
| 738 | # Twin smokestacks | ||
| 739 | my $f1_x = $x0 + ( $cw * 0.45 ); | ||
| 740 | my $f2_x = $x0 + ( $cw * 0.62 ); | ||
| 741 | for my $fx ( $f1_x, $f2_x ) { | ||
| 742 | $cr->rectangle( $fx, $yc - ( $ch * 0.22 ), $cw * 0.08, $ch * 0.44 ); | ||
| 743 | $cr->set_source_rgb( 0.24, 0.28, 0.34 ); | ||
| 744 | $cr->fill_preserve; | ||
| 745 | $cr->set_line_width(1); | ||
| 746 | $cr->set_source_rgb( 0.6, 0.7, 0.8 ); | ||
| 747 | $cr->stroke; | ||
| 748 | } | ||
| 749 | |||
| 750 | # Forward deck gun | ||
| 751 | my $tx = $x0 + ( $cw * 0.22 ); | ||
| 752 | $cr->arc( $tx, $yc, $ch * 0.20, 0, 2 * $PI ); | ||
| 753 | $cr->set_source_rgb( 0.22, 0.26, 0.32 ); | ||
| 754 | $cr->fill; | ||
| 755 | $cr->set_line_width(1.5); | ||
| 756 | $cr->set_source_rgb( 0.6, 0.7, 0.8 ); | ||
| 757 | $cr->move_to( $tx, $yc ); | ||
| 758 | $cr->line_to( $tx - ( $cw * 0.10 ), $yc ); | ||
| 759 | $cr->stroke; | ||
| 760 | return; | ||
| 761 | } | ||
| 762 | |||
| 763 | # --- 4. SUBMARINE (Size 3) --- | ||
| 764 | sub draw_submarine ( $self, $cr, $w, $h, %opts ) { | ||
| 765 | my ( $x0, $y0 ) = ( 3, 3 ); | ||
| 766 | my $cw = $w - 6; | ||
| 767 | my $ch = $h - 6; | ||
| 768 | my $yc = $h / 2; | ||
| 769 | my $r = $ch * 0.45; | ||
| 770 | |||
| 771 | # Rounded bulbous sonar bow to tapered stern | ||
| 772 | $cr->arc( $x0 + $r, $yc, $r, $PI / 2, 3 * $PI / 2 ); | ||
| 773 | $cr->line_to( $x0 + $cw - 8, $yc - $r ); | ||
| 774 | $cr->curve_to( $x0 + $cw, $yc, $x0 + $cw, $yc, $x0 + $cw - 8, $yc + $r ); | ||
| 775 | $cr->close_path; | ||
| 776 | |||
| 777 | if ( $opts{sunk} ) { | ||
| 778 | $cr->set_source_rgb( 0.14, 0.18, 0.22 ); | ||
| 779 | $cr->fill_preserve; | ||
| 780 | $cr->set_line_width(1.5); | ||
| 781 | $cr->set_source_rgb( 0.7, 0.2, 0.2 ); | ||
| 782 | $cr->stroke; | ||
| 783 | return; | ||
| 784 | } | ||
| 785 | |||
| 786 | $cr->set_source_rgb( 0.16, 0.24, 0.35 ); | ||
| 787 | $cr->fill_preserve; | ||
| 788 | $cr->set_line_width(1.5); | ||
| 789 | $cr->set_source_rgb( 0.32, 0.48, 0.66 ); | ||
| 790 | $cr->stroke; | ||
| 791 | |||
| 792 | # Conning tower (sail) | ||
| 793 | my $sx = $x0 + ( $cw * 0.48 ); | ||
| 794 | $cr->arc( $sx, $yc, $ch * 0.25, 0, 2 * $PI ); | ||
| 795 | $cr->set_source_rgb( 0.22, 0.32, 0.46 ); | ||
| 796 | $cr->fill_preserve; | ||
| 797 | $cr->set_line_width(1); | ||
| 798 | $cr->set_source_rgb( 0.5, 0.7, 0.9 ); | ||
| 799 | $cr->stroke; | ||
| 800 | |||
| 801 | # Periscope mast | ||
| 802 | $cr->move_to( $sx, $yc ); | ||
| 803 | $cr->line_to( $sx, $y0 - 1 ); | ||
| 804 | $cr->stroke; | ||
| 805 | return; | ||
| 806 | } | ||
| 807 | |||
| 808 | # --- 5. DESTROYER (Size 2) --- | ||
| 809 | sub draw_destroyer ( $self, $cr, $w, $h, %opts ) { | ||
| 810 | my ( $x0, $y0 ) = ( 3, 3 ); | ||
| 811 | my $cw = $w - 6; | ||
| 812 | my $ch = $h - 6; | ||
| 813 | my $yc = $h / 2; | ||
| 814 | |||
| 815 | # Agile patrol boat wedge hull | ||
| 816 | $cr->move_to( $x0, $yc ); | ||
| 817 | $cr->line_to( $x0 + 12, $y0 ); | ||
| 818 | $cr->line_to( $x0 + $cw - 2, $y0 + 1 ); | ||
| 819 | $cr->line_to( $x0 + $cw, $y0 + 3 ); | ||
| 820 | $cr->line_to( $x0 + $cw, $y0 + $ch - 3 ); | ||
| 821 | $cr->line_to( $x0 + $cw - 2, $y0 + $ch - 1 ); | ||
| 822 | $cr->line_to( $x0 + 12, $y0 + $ch ); | ||
| 823 | $cr->close_path; | ||
| 824 | |||
| 825 | if ( $opts{sunk} ) { | ||
| 826 | $cr->set_source_rgb( 0.18, 0.20, 0.22 ); | ||
| 827 | $cr->fill_preserve; | ||
| 828 | $cr->set_line_width(1.5); | ||
| 829 | $cr->set_source_rgb( 0.7, 0.2, 0.2 ); | ||
| 830 | $cr->stroke; | ||
| 831 | return; | ||
| 832 | } | ||
| 833 | |||
| 834 | $cr->set_source_rgb( 0.38, 0.43, 0.50 ); | ||
| 835 | $cr->fill_preserve; | ||
| 836 | $cr->set_line_width(1.5); | ||
| 837 | $cr->set_source_rgb( 0.58, 0.65, 0.75 ); | ||
| 838 | $cr->stroke; | ||
| 839 | |||
| 840 | # Bridge cabin | ||
| 841 | my $bx = $x0 + ( $cw * 0.45 ); | ||
| 842 | $cr->rectangle( $bx, $yc - ( $ch * 0.25 ), $cw * 0.22, $ch * 0.50 ); | ||
| 843 | $cr->set_source_rgb( 0.45, 0.52, 0.60 ); | ||
| 844 | $cr->fill_preserve; | ||
| 845 | $cr->set_line_width(1); | ||
| 846 | $cr->set_source_rgb( 0.7, 0.8, 0.9 ); | ||
| 847 | $cr->stroke; | ||
| 848 | |||
| 849 | # Forward gun mount | ||
| 850 | my $tx = $x0 + ( $cw * 0.25 ); | ||
| 851 | $cr->arc( $tx, $yc, $ch * 0.20, 0, 2 * $PI ); | ||
| 852 | $cr->set_source_rgb( 0.26, 0.30, 0.36 ); | ||
| 853 | $cr->fill; | ||
| 854 | $cr->set_line_width(1.5); | ||
| 855 | $cr->set_source_rgb( 0.7, 0.8, 0.9 ); | ||
| 856 | $cr->move_to( $tx, $yc ); | ||
| 857 | $cr->line_to( $tx - ( $cw * 0.12 ), $yc ); | ||
| 858 | $cr->stroke; | ||
| 859 | return; | ||
| 860 | } | ||
| 861 | |||
| 862 | sub draw_generic_hull ( $self, $cr, $w, $h, %opts ) { | ||
| 863 | my ( $x0, $y0 ) = ( 3, 3 ); | ||
| 864 | my $cw = $w - 6; | ||
| 865 | my $ch = $h - 6; | ||
| 866 | my $yc = $h / 2; | ||
| 867 | |||
| 868 | $cr->move_to( $x0, $yc ); | ||
| 869 | $cr->line_to( $x0 + 10, $y0 ); | ||
| 870 | $cr->line_to( $x0 + $cw, $y0 ); | ||
| 871 | $cr->line_to( $x0 + $cw, $y0 + $ch ); | ||
| 872 | $cr->line_to( $x0 + 10, $y0 + $ch ); | ||
| 873 | $cr->close_path; | ||
| 874 | |||
| 875 | $cr->set_source_rgb( 0.35, 0.40, 0.48 ); | ||
| 876 | $cr->fill_preserve; | ||
| 877 | $cr->set_line_width(1.5); | ||
| 878 | $cr->set_source_rgb( 0.6, 0.7, 0.8 ); | ||
| 879 | $cr->stroke; | ||
| 880 | return; | ||
| 881 | } | ||
| 882 | |||
| 883 | sub calc_ship_sweep_alpha ( $self, $ship, $cs, $progress = 0.0 ) { | ||
| 884 | my $board_w = 10 * $cs; | ||
| 885 | my $len = $ship->length; | ||
| 886 | my $ship_center_x = | ||
| 887 | $ship->dir eq 'H' | ||
| 888 | ? ( ( $ship->x - 1 ) + ( $len / 2 ) ) * $cs | ||
| 889 | : ( ( $ship->x - 1 ) + 0.5 ) * $cs; | ||
| 890 | |||
| 891 | my $beam_x = $progress * $board_w; | ||
| 892 | my $dx = $beam_x - $ship_center_x; | ||
| 893 | $dx += $board_w if $dx < 0; | ||
| 894 | |||
| 895 | my $dist_ratio = $dx / $board_w; | ||
| 896 | |||
| 897 | # Phosphor decay curve: maximum visibility (1.0) just after sweep line passes, | ||
| 898 | # decaying smoothly down toward floor of 0.20 with balanced persistence (decay constant 2.1). | ||
| 899 | my $alpha = 0.05 + 0.95 * exp( -2.1 * $dist_ratio ); | ||
| 900 | $alpha = 1.00 if $alpha > 1.00; | ||
| 901 | $alpha = 0.05 if $alpha < 0.05; | ||
| 902 | |||
| 903 | return $alpha; | ||
| 904 | } | ||
| 905 | |||
| 906 | sub draw_radar_beam ( $self, $cr, $ox, $oy, $progress = 0.0 ) { | ||
| 907 | my $cs = $self->{cell_size}; | ||
| 908 | my $board_w = 10 * $cs; | ||
| 909 | my $board_h = 10 * $cs; | ||
| 910 | |||
| 911 | $cr->save; | ||
| 912 | |||
| 913 | # Clip strictly to the 10x10 board grid bounds | ||
| 914 | $cr->rectangle( $ox, $oy, $board_w, $board_h ); | ||
| 915 | $cr->clip; | ||
| 916 | |||
| 917 | my $bx = $ox + ( $progress * $board_w ); | ||
| 918 | my $tail_w = $board_w * 0.28; | ||
| 919 | |||
| 920 | # 1. Trailing phosphor glow gradient | ||
| 921 | if ( $bx - $tail_w >= $ox ) { | ||
| 922 | my $pat = Cairo::LinearGradient->create( $bx - $tail_w, 0, $bx, 0 ); | ||
| 923 | $pat->add_color_stop_rgba( 0.0, 0.0, 0.85, 0.95, 0.00 ); | ||
| 924 | $pat->add_color_stop_rgba( 0.7, 0.0, 0.85, 0.95, 0.08 ); | ||
| 925 | $pat->add_color_stop_rgba( 1.0, 0.0, 0.85, 0.95, 0.24 ); | ||
| 926 | $cr->set_source($pat); | ||
| 927 | $cr->rectangle( $bx - $tail_w, $oy, $tail_w, $board_h ); | ||
| 928 | $cr->fill; | ||
| 929 | } | ||
| 930 | else { | ||
| 931 | # Beam is near left edge; tail wraps around from right edge | ||
| 932 | my $head_tail_w = $bx - $ox; | ||
| 933 | if ( $head_tail_w > 0 ) { | ||
| 934 | my $pat1 = | ||
| 935 | Cairo::LinearGradient->create( $bx - $tail_w, 0, $bx, 0 ); | ||
| 936 | $pat1->add_color_stop_rgba( 0.0, 0.0, 0.85, 0.95, 0.00 ); | ||
| 937 | $pat1->add_color_stop_rgba( 0.7, 0.0, 0.85, 0.95, 0.08 ); | ||
| 938 | $pat1->add_color_stop_rgba( 1.0, 0.0, 0.85, 0.95, 0.24 ); | ||
| 939 | $cr->set_source($pat1); | ||
| 940 | $cr->rectangle( $ox, $oy, $head_tail_w, $board_h ); | ||
| 941 | $cr->fill; | ||
| 942 | } | ||
| 943 | |||
| 944 | my $wrap_w = $tail_w - $head_tail_w; | ||
| 945 | my $wrap_start = ( $ox + $board_w ) - $wrap_w; | ||
| 946 | my $pat2 = Cairo::LinearGradient->create( $bx - $tail_w + $board_w, | ||
| 947 | 0, $bx + $board_w, 0 ); | ||
| 948 | $pat2->add_color_stop_rgba( 0.0, 0.0, 0.85, 0.95, 0.00 ); | ||
| 949 | $pat2->add_color_stop_rgba( 0.7, 0.0, 0.85, 0.95, 0.08 ); | ||
| 950 | $pat2->add_color_stop_rgba( 1.0, 0.0, 0.85, 0.95, 0.24 ); | ||
| 951 | $cr->set_source($pat2); | ||
| 952 | $cr->rectangle( $wrap_start, $oy, $wrap_w, $board_h ); | ||
| 953 | $cr->fill; | ||
| 954 | } | ||
| 955 | |||
| 956 | # 2. Outer beam glow | ||
| 957 | $cr->set_line_width(3.5); | ||
| 958 | $cr->set_source_rgba( 0.15, 0.90, 1.00, 0.40 ); | ||
| 959 | $cr->move_to( $bx, $oy ); | ||
| 960 | $cr->line_to( $bx, $oy + $board_h ); | ||
| 961 | $cr->stroke; | ||
| 962 | |||
| 963 | # 3. High-intensity sharp core beam line | ||
| 964 | $cr->set_line_width(1.5); | ||
| 965 | $cr->set_source_rgba( 0.75, 1.00, 1.00, 0.95 ); | ||
| 966 | $cr->move_to( $bx, $oy ); | ||
| 967 | $cr->line_to( $bx, $oy + $board_h ); | ||
| 968 | $cr->stroke; | ||
| 969 | |||
| 970 | $cr->restore; | ||
| 971 | return; | ||
| 972 | } | ||
| 973 | |||
| 974 | 1; | ||
| 975 | |||