| 1 | # | ||
| 2 | # This file is part of Games-RailRoad | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2008 by Jerome Quelin. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | package Games::RailRoad; | ||
| 14 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | $Games::RailRoad::VERSION = '1.101330'; | |
| 16 | } | ||
| 17 | # ABSTRACT: a train simulation game | ||
| 18 | |||
| 19 | 1 | ▶ | use POE::Kernel { loop => 'Tk' }; # should come first |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | |||
| 21 | use File::Basename; | ||
| 22 | use File::HomeDir; | ||
| 23 | use File::Path; | ||
| 24 | use File::ShareDir qw{ dist_dir }; | ||
| 25 | use File::Spec::Functions; | ||
| 26 | use POE; | ||
| 27 | use Readonly; | ||
| 28 | use Tk; | ||
| 29 | use Tk::PNG; | ||
| 30 | use Tk::ToolBar; | ||
| 31 | use UNIVERSAL::require; | ||
| 32 | use YAML qw{ DumpFile LoadFile }; | ||
| 33 | |||
| 34 | use Games::RailRoad::Node; | ||
| 35 | use Games::RailRoad::Train; | ||
| 36 | use Games::RailRoad::Vector; | ||
| 37 | use Games::RailRoad::Window::Trains; | ||
| 38 | |||
| 39 | Readonly my $HOME => File::HomeDir->my_home; | ||
| 40 | Readonly my $GRHOME => catfile( $HOME, qw{ .perl Games::RailRoad } ); | ||
| 41 | Readonly my $SAVEDIR => catfile( $GRHOME, 'savedir' ); | ||
| 42 | Readonly my $NBCOLS => 60; | ||
| 43 | Readonly my $NBROWS => 40; | ||
| 44 | Readonly my $TILELEN => 20; # in pixels | ||
| 45 | Readonly my $TICK => 0.050; # in seconds | ||
| 46 | |||
| 47 | #Readonly my @COLORS => ( [255,0,0], [0,0,255], [0,255,0], [255,255,0], [255,0,255], [0,255,255] ); | ||
| 48 | |||
| 49 | our %img = | ||
| 50 | map { $_ => $poe_main_window->Photo( | ||
| 51 | -format => 'png', | ||
| 52 | -file => catfile( | ||
| 53 | dist_dir('Games-RailRoad'), | ||
| 54 | 'icons', | ||
| 55 | "$_.png" | ||
| 56 | ), | ||
| 57 | ) | ||
| 58 | } qw{ main train }; | ||
| 59 | |||
| 60 | |||
| 61 | # -- CONSTRUCTOR | ||
| 62 | |||
| 63 | # | ||
| 64 | # my $id = Games::RailRoad->spawn(%opts); | ||
| 65 | # | ||
| 66 | # create a new game window. refer to the embedded | ||
| 67 | # pod for an explanation of the supported options. | ||
| 68 | # | ||
| 69 | sub spawn { | ||
| 70 | my ($class, %opts) = @_; | ||
| 71 | |||
| 72 | my $session = POE::Session->create( | ||
| 73 | inline_states => { | ||
| 74 | # special poe events | ||
| 75 | _start => \&_do_start, | ||
| 76 | # public events | ||
| 77 | # private events | ||
| 78 | _new => \&_do_new, | ||
| 79 | _open => \&_do_open, | ||
| 80 | _save => \&_do_save, | ||
| 81 | _tick => \&_do_tick, | ||
| 82 | # gui events | ||
| 83 | _b_new => \&_on_b_new, | ||
| 84 | _b_open => \&_on_b_open, | ||
| 85 | _b_quit => \&_on_b_quit, | ||
| 86 | _b_save => \&_on_b_save, | ||
| 87 | _b_trains => \&_on_b_trains, | ||
| 88 | _c_b1_dblclick => \&_on_c_b1_dblclick, | ||
| 89 | _c_b1_motion => \&_on_c_b1_motion, | ||
| 90 | _c_b1_press => \&_on_c_b1_press, | ||
| 91 | _c_b2_press => \&_on_c_b2_press, | ||
| 92 | _c_b3_press => \&_on_c_b3_press, | ||
| 93 | }, | ||
| 94 | args => \%opts, | ||
| 95 | ); | ||
| 96 | return $session->ID; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | # -- PUBLIC EVENTS | ||
| 101 | |||
| 102 | |||
| 103 | # -- PRIVATE EVENTS | ||
| 104 | |||
| 105 | # | ||
| 106 | # _new(); | ||
| 107 | # | ||
| 108 | # reinits the various params to start anew. | ||
| 109 | # | ||
| 110 | sub _do_new { | ||
| 111 | my $h = $_[HEAP]; | ||
| 112 | |||
| 113 | # various heap initialization. | ||
| 114 | $h->{nodes} = {}; | ||
| 115 | $h->{trains} = []; | ||
| 116 | |||
| 117 | # clear the canvas. | ||
| 118 | my $canvas = $h->{w}{canvas}; | ||
| 119 | $canvas->delete('all'); | ||
| 120 | $canvas->createGrid( 0, 0, $TILELEN, $TILELEN, -lines => 0 ); | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | # | ||
| 125 | # _open($file); | ||
| 126 | # | ||
| 127 | # load a game from $file. | ||
| 128 | # | ||
| 129 | sub _do_open { | ||
| 130 | my ($h, $file) = @_[HEAP, ARG0]; | ||
| 131 | my $save = LoadFile($file); | ||
| 132 | |||
| 133 | # load node classes, to be able to bless loaded nodes. | ||
| 134 | use Module::Pluggable search_path => 'Games::RailRoad::Node', sub_name => 'nodes'; | ||
| 135 | $_->require for __PACKAGE__->nodes; | ||
| 136 | |||
| 137 | given ( $save->{version} ) { | ||
| 138 | when ( $_ > __PACKAGE__->VERSION ) { | ||
| 139 | die "uh, loading a file from the future\n" | ||
| 140 | } | ||
| 141 | |||
| 142 | when ( '1.00' ) { | ||
| 143 | warn "converting game saved in version 1.00\n"; | ||
| 144 | foreach my $node ( values %{ $save->{nodes} } ) { | ||
| 145 | my $pos = $node->position; | ||
| 146 | $pos->set_posx( delete $pos->{x} ); | ||
| 147 | $pos->set_posy( delete $pos->{y} ); | ||
| 148 | } | ||
| 149 | foreach my $train ( @{ $save->{trains} } ) { | ||
| 150 | my $from = $train->from; | ||
| 151 | my $to = $train->to; | ||
| 152 | $from->set_posx( delete $from->{x} ); | ||
| 153 | $from->set_posy( delete $from->{y} ); | ||
| 154 | $to->set_posx( delete $to->{x} ); | ||
| 155 | $to->set_posy( delete $to->{y} ); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | # load nodes and draw them. | ||
| 162 | $h->{nodes} = $save->{nodes}; | ||
| 163 | $_->draw($h->{w}{canvas}, $TILELEN) foreach values %{ $h->{nodes} }; | ||
| 164 | |||
| 165 | # load trains and draw them. | ||
| 166 | $h->{trains} = $save->{trains}; | ||
| 167 | $_->draw($h->{w}{canvas}, $TILELEN) foreach values %{ $h->{train} }; | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | # | ||
| 172 | # _save($file); | ||
| 173 | # | ||
| 174 | # save the current game to $file. | ||
| 175 | # | ||
| 176 | sub _do_save { | ||
| 177 | my ($h, $file) = @_[HEAP, ARG0]; | ||
| 178 | return unless defined $file; | ||
| 179 | |||
| 180 | # select what to save. | ||
| 181 | my $save = { | ||
| 182 | version => __PACKAGE__->VERSION, # one never knows | ||
| 183 | nodes => $h->{nodes}, | ||
| 184 | trains => $h->{trains}, | ||
| 185 | }; | ||
| 186 | DumpFile($file, $save); | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | # | ||
| 191 | # _do_start( \%opts ); | ||
| 192 | # | ||
| 193 | # session initialization. %opts is received from spawn(); | ||
| 194 | # | ||
| 195 | sub _do_start { | ||
| 196 | my ($k, $h, $s, $opts) = @_[ KERNEL, HEAP, SESSION, ARG0 ]; | ||
| 197 | |||
| 198 | #-- create gui | ||
| 199 | |||
| 200 | # prettyfying tk app. | ||
| 201 | # see http://www.perltk.org/index.php?option=com_content&task=view&id=43&Itemid=37 | ||
| 202 | $poe_main_window->optionAdd('*BorderWidth' => 1); | ||
| 203 | |||
| 204 | # icon | ||
| 205 | $poe_main_window->iconimage($img{main}); | ||
| 206 | |||
| 207 | # menu | ||
| 208 | # $poe_main_window->optionAdd('*tearOff', 'false'); # no tear-off menus | ||
| 209 | # my $menuitems = [ | ||
| 210 | # [ Cascade => '~File', -menuitems => [ | ||
| 211 | # [ Button => '~Open', | ||
| 212 | # -command => $s->postback('_b_open'), | ||
| 213 | # -accelerator => 'ctrl+o', | ||
| 214 | # -compound => 'left', | ||
| 215 | # -image => $poe_main_window->Photo('fileopen16'), | ||
| 216 | # ], | ||
| 217 | # [ Separator => '' ], | ||
| 218 | # [ Button => '~Quit', | ||
| 219 | # -command => $s->postback('_b_quit'), | ||
| 220 | # -accelerator => 'ctrl+q', | ||
| 221 | # -compound => 'left', | ||
| 222 | # -image => $poe_main_window->Photo('actexit16'), | ||
| 223 | # ], | ||
| 224 | # ], | ||
| 225 | # ], | ||
| 226 | # [ Cascade => '~Run', -menuitems => [ | ||
| 227 | # [ Button => '~Restart', | ||
| 228 | # -command => $s->postback('_b_restart'), | ||
| 229 | # -accelerator => 'R', | ||
| 230 | # -compound => 'left', | ||
| 231 | # -image => $poe_main_window->Photo('playstart16'), | ||
| 232 | # ], | ||
| 233 | # [ Button => '~Pause', | ||
| 234 | # -command => $s->postback('_b_pause'), | ||
| 235 | # -accelerator => 'p', | ||
| 236 | # -compound => 'left', | ||
| 237 | # -image => $poe_main_window->Photo('playpause16'), | ||
| 238 | # ], | ||
| 239 | # [ Button => '~Next', | ||
| 240 | # -command => $s->postback('_b_next'), | ||
| 241 | # -accelerator => 'n', | ||
| 242 | # -compound => 'left', | ||
| 243 | # -image => $poe_main_window->Photo('nav1rightarrow16'), | ||
| 244 | # ], | ||
| 245 | # [ Button => '~Continue', | ||
| 246 | # -command => $s->postback('_b_continue'), | ||
| 247 | # -accelerator => 'c', | ||
| 248 | # -compound => 'left', | ||
| 249 | # -image => $poe_main_window->Photo('nav2rightarrow16'), | ||
| 250 | # ], | ||
| 251 | # [ Separator => '' ], | ||
| 252 | # [ Button => '~Breakpoints', | ||
| 253 | # -command => $s->postback('_b_breakpoints'), | ||
| 254 | # #-accelerator => 'c', | ||
| 255 | # -compound => 'left', | ||
| 256 | # -image => $poe_main_window->Photo('calbell16'), | ||
| 257 | # ], | ||
| 258 | # ], | ||
| 259 | # ], | ||
| 260 | # ]; | ||
| 261 | # my $menubar = $poe_main_window->Menu( -menuitems => $menuitems ); | ||
| 262 | # $poe_main_window->configure( -menu => $menubar ); | ||
| 263 | # $h->{w}{mnu_run} = $menubar->entrycget(1, '-menu'); | ||
| 264 | |||
| 265 | # toolbar | ||
| 266 | my @tb = ( | ||
| 267 | [ 'Button', 'actexit16', 'quit', '<Control-q>', '_b_quit' ], | ||
| 268 | [ 'separator' ], | ||
| 269 | [ 'Button', 'filenew16', 'new', '<Control-n>', '_b_new' ], | ||
| 270 | [ 'Button', 'fileopen16', 'open', '<Control-o>', '_b_open' ], | ||
| 271 | [ 'Button', 'filesave16', 'save', '<Control-s>', '_b_save' ], | ||
| 272 | [ 'separator' ], | ||
| 273 | [ 'Button', $img{train}, 'trains', '<F5>', '_b_trains' ], | ||
| 274 | #[ 'Button', 'calbell16', 'breakpoints', '<F8>', '_b_breakpoints' ], | ||
| 275 | #[ 'separator' ], | ||
| 276 | #[ 'Button', 'playstart16', 'restart', '<R>', '_b_restart' ], | ||
| 277 | #[ 'Button', 'playpause16', 'pause', '<p>', '_b_pause' ], | ||
| 278 | #[ 'Button', 'nav1rightarrow16', 'next', '<n>', '_b_next' ], | ||
| 279 | #[ 'Button', 'nav2rightarrow16', 'continue', '<c>', '_b_continue' ], | ||
| 280 | ); | ||
| 281 | my $tb = $poe_main_window->ToolBar(-movable=>0); | ||
| 282 | foreach my $item ( @tb ) { | ||
| 283 | my $type = shift @$item; | ||
| 284 | $tb->separator( -movable => 0 ), next if $type eq 'separator'; | ||
| 285 | $h->{w}{$item->[3]} = $tb->$type( | ||
| 286 | -image => $item->[0], | ||
| 287 | -tip => $item->[1], | ||
| 288 | -accelerator => $item->[2], | ||
| 289 | -command => $s->postback($item->[3]), | ||
| 290 | ); | ||
| 291 | } | ||
| 292 | |||
| 293 | # playfield | ||
| 294 | #my $fh1 = $->Frame->pack(-fill=>'both', -expand=>1); | ||
| 295 | my $c = $poe_main_window->Scrolled( 'Canvas', | ||
| 296 | -bg => 'white', | ||
| 297 | -scrollbars => 'osoe', | ||
| 298 | -width => $NBCOLS * $TILELEN, | ||
| 299 | -height => $NBROWS * $TILELEN, | ||
| 300 | #-browsecmd => $s->postback('_tm_click'), | ||
| 301 | )->pack(-side=>'left', -fill=>'both', -expand=>1); | ||
| 302 | $h->{w}{canvas} = $c; | ||
| 303 | |||
| 304 | # binding canvas events. | ||
| 305 | my %event = ( | ||
| 306 | '<B1-Motion>' => '_c_b1_motion', | ||
| 307 | '<Double-ButtonPress-1>' => '_c_b1_dblclick', | ||
| 308 | '<ButtonPress-1>' => '_c_b1_press', | ||
| 309 | '<ButtonPress-2>' => '_c_b2_press', | ||
| 310 | '<ButtonPress-3>' => '_c_b3_press', | ||
| 311 | ); | ||
| 312 | $c->CanvasBind($_ , [$s->postback($event{$_}), Ev('x'), Ev('y')] ) | ||
| 313 | foreach keys %event; | ||
| 314 | |||
| 315 | |||
| 316 | #-- finish initialization | ||
| 317 | $k->yield( '_new' ); | ||
| 318 | $k->delay_set( '_tick', $TICK ); | ||
| 319 | } | ||
| 320 | |||
| 321 | |||
| 322 | sub _do_tick { | ||
| 323 | my ($k, $h) = @_[KERNEL, HEAP]; | ||
| 324 | |||
| 325 | $k->delay_set( '_tick', $TICK ); | ||
| 326 | |||
| 327 | foreach my $train ( @{ $h->{trains} } ) { | ||
| 328 | # fetch current nodes for $train | ||
| 329 | my $from = $train->from; | ||
| 330 | my $to = $train->to; | ||
| 331 | my $move = $from - $to; # note it's from minus to | ||
| 332 | my $dir = $move->as_dir; | ||
| 333 | |||
| 334 | # move the train 1/5 of rail further. of course, for diagonals we | ||
| 335 | # need to apply a cos(pi/4) factor (equals to sqrt(2)/2), otherwise | ||
| 336 | # the train would be moving faster in diagonals than in vertical / | ||
| 337 | # horizontal rails. | ||
| 338 | my $frac = $train->frac; | ||
| 339 | $frac += $dir ~~ [ qw{ e n s w } ] ? 1/5 : sqrt(2)/10; | ||
| 340 | |||
| 341 | if ( $frac >= 1 ) { | ||
| 342 | # eh, changing node. | ||
| 343 | $frac -= 1; | ||
| 344 | |||
| 345 | # get next direction (note it's from minus to) | ||
| 346 | my $nextdir = $h->{nodes}{"$to"}->next_dir($dir); | ||
| 347 | next unless defined $nextdir; # dead-end | ||
| 348 | |||
| 349 | # update current nodes for $train | ||
| 350 | my $vec = Games::RailRoad::Vector->new_dir($nextdir); | ||
| 351 | $train->set_from( $to ); | ||
| 352 | $train->set_to( $to + $vec ); | ||
| 353 | } | ||
| 354 | |||
| 355 | $train->set_frac($frac); | ||
| 356 | $train->draw( $h->{w}{canvas}, $TILELEN ); | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | |||
| 361 | # -- GUI EVENTS | ||
| 362 | |||
| 363 | |||
| 364 | # | ||
| 365 | # _b_new(); | ||
| 366 | # | ||
| 367 | # called when the user wants to begin a new game. | ||
| 368 | # | ||
| 369 | sub _on_b_new { | ||
| 370 | $_[KERNEL]->yield('_new'); | ||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | # | ||
| 375 | # _b_open(); | ||
| 376 | # | ||
| 377 | # called when the user wants to open a saved game. | ||
| 378 | # | ||
| 379 | sub _on_b_open { | ||
| 380 | my $k = $_[KERNEL]; | ||
| 381 | |||
| 382 | # create savedir if needed. | ||
| 383 | mkpath( $SAVEDIR ); | ||
| 384 | |||
| 385 | # prompt for save file - yes, i know, it freezes poe. | ||
| 386 | my $file = $poe_main_window->getOpenFile( | ||
| 387 | -defaultextension => '.yaml', | ||
| 388 | -filetypes => [ | ||
| 389 | ['YAML files', '.yaml' ], | ||
| 390 | ['All Files', '*', ], | ||
| 391 | ], | ||
| 392 | -initialdir => $SAVEDIR, | ||
| 393 | #-initialfile => "getopenfile", | ||
| 394 | #-title => "Your customized title", | ||
| 395 | ); | ||
| 396 | return unless defined $file; | ||
| 397 | |||
| 398 | # reinit & open file. | ||
| 399 | $k->yield('_new'); | ||
| 400 | $k->yield('_open', $file); | ||
| 401 | } | ||
| 402 | |||
| 403 | |||
| 404 | # | ||
| 405 | # _b_quit(); | ||
| 406 | # | ||
| 407 | # called when the user wants to quit the application. | ||
| 408 | # | ||
| 409 | sub _on_b_quit { | ||
| 410 | $poe_main_window->destroy; | ||
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | # | ||
| 415 | # _b_save(); | ||
| 416 | # | ||
| 417 | # called when the user wants to save the current game. | ||
| 418 | # | ||
| 419 | sub _on_b_save { | ||
| 420 | # create savedir if needed. | ||
| 421 | mkpath( $SAVEDIR ); | ||
| 422 | |||
| 423 | # prompt for save file - yes, i know, it freezes poe. | ||
| 424 | my $file = $poe_main_window->getSaveFile( | ||
| 425 | -defaultextension => '.yaml', | ||
| 426 | -filetypes => [ | ||
| 427 | ['YAML files', '.yaml' ], | ||
| 428 | ['All Files', '*', ], | ||
| 429 | ], | ||
| 430 | -initialdir => $SAVEDIR, | ||
| 431 | #-initialfile => "getopenfile", | ||
| 432 | #-title => "Your customized title", | ||
| 433 | ); | ||
| 434 | return unless defined $file; | ||
| 435 | $file .= '.yaml' unless $file =~ /\.yaml$/; | ||
| 436 | |||
| 437 | $_[KERNEL]->yield('_save', $file); | ||
| 438 | } | ||
| 439 | |||
| 440 | |||
| 441 | # | ||
| 442 | # _b_trains(); | ||
| 443 | # | ||
| 444 | # called when the user wants to show/hide trains window. | ||
| 445 | # | ||
| 446 | sub _on_b_trains { | ||
| 447 | my ($k, $h) = @_[KERNEL, HEAP]; | ||
| 448 | |||
| 449 | return $k->post($h->{w}{trains}, 'visibility_toggle') | ||
| 450 | if exists $h->{w}{trains}; | ||
| 451 | |||
| 452 | my $id = Games::RailRoad::Window::Trains->spawn(parent=>$poe_main_window); | ||
| 453 | $h->{w}{trains} = $id; | ||
| 454 | } | ||
| 455 | |||
| 456 | |||
| 457 | # | ||
| 458 | # _c_b1_dblclick( [], [$stuff, $x, $y] ); | ||
| 459 | # | ||
| 460 | # called when double-clicking left button on canvas. switch the node if | ||
| 461 | # possible (Games::RailRoad::Node::Switch). | ||
| 462 | # | ||
| 463 | sub _on_c_b1_dblclick { | ||
| 464 | my ($k,$h, $args) = @_[KERNEL, HEAP, ARG1]; | ||
| 465 | my (undef, $x, $y) = @$args; | ||
| 466 | |||
| 467 | # resolve column & row. | ||
| 468 | my $vec = _resolve_coords($x,$y,0.5); | ||
| 469 | my $node = $h->{nodes}{"$vec"}; | ||
| 470 | return unless defined $node; | ||
| 471 | |||
| 472 | # switch the exits. | ||
| 473 | $node->switch; | ||
| 474 | $node->draw($h->{w}{canvas}, $TILELEN); | ||
| 475 | } | ||
| 476 | |||
| 477 | |||
| 478 | # | ||
| 479 | # _c_b1_motion( [], [$stuff, $x, $y] ); | ||
| 480 | # | ||
| 481 | # called when the mouse is moving on canvas while button is down. | ||
| 482 | # creates and link rails together. | ||
| 483 | # | ||
| 484 | sub _on_c_b1_motion { | ||
| 485 | my ($k,$h, $args) = @_[KERNEL, HEAP, ARG1]; | ||
| 486 | my (undef, $x, $y) = @$args; | ||
| 487 | |||
| 488 | # resolve column & row. | ||
| 489 | my $newvec = _resolve_coords($x,$y,2/5); | ||
| 490 | my $oldvec = $h->{position}; | ||
| 491 | |||
| 492 | # basic checks. | ||
| 493 | return unless defined $newvec; # new position is undef | ||
| 494 | return if defined($oldvec) && $oldvec == $newvec; # we did not move | ||
| 495 | |||
| 496 | # we moved: store new position & create new node. | ||
| 497 | $h->{position} = $newvec; | ||
| 498 | $h->{nodes}{"$newvec"} = Games::RailRoad::Node->new({position=>$newvec}) | ||
| 499 | unless exists $h->{nodes}{"$newvec"}; | ||
| 500 | my $newnode = $h->{nodes}{"$newvec"}; | ||
| 501 | |||
| 502 | # try to resolve old position. | ||
| 503 | return unless defined $oldvec; | ||
| 504 | my $oldnode = $h->{nodes}{"$oldvec"}; | ||
| 505 | |||
| 506 | # check if the move is a single segment. | ||
| 507 | my $move = $newvec - $oldvec; | ||
| 508 | my $newdir = $move->as_dir; | ||
| 509 | if ( not defined $newdir ) { | ||
| 510 | warn "cannot move according to $move\n"; | ||
| 511 | return; | ||
| 512 | } | ||
| 513 | $move = -$move; | ||
| 514 | my $olddir = $move->as_dir; | ||
| 515 | |||
| 516 | # check if we can morph the nodes with this move. | ||
| 517 | return unless $oldnode->connectable($newdir) | ||
| 518 | && $newnode->connectable($olddir); | ||
| 519 | $oldnode->connect($newdir); | ||
| 520 | $newnode->connect($olddir); | ||
| 521 | |||
| 522 | # redraw the 2 nodes. | ||
| 523 | $oldnode->draw( $h->{w}{canvas}, $TILELEN ); | ||
| 524 | $newnode->draw( $h->{w}{canvas}, $TILELEN ); | ||
| 525 | } | ||
| 526 | |||
| 527 | |||
| 528 | # | ||
| 529 | # _c_b1_press( [], [$stuff, $x, $y] ); | ||
| 530 | # | ||
| 531 | # called when the button mouse is pressed on canvas. register position | ||
| 532 | # to create rail, to be dragged later on. | ||
| 533 | # | ||
| 534 | sub _on_c_b1_press { | ||
| 535 | my ($k,$h, $args) = @_[KERNEL, HEAP, ARG1]; | ||
| 536 | my (undef, $x, $y) = @$args; | ||
| 537 | |||
| 538 | # resolve column & row. | ||
| 539 | my $vec = _resolve_coords($x,$y,2/5); | ||
| 540 | |||
| 541 | # store current position - even undef. | ||
| 542 | $h->{position} = $vec; | ||
| 543 | |||
| 544 | # create the node if possible. | ||
| 545 | return unless defined $vec; | ||
| 546 | return if defined $h->{nodes}{"$vec"}; | ||
| 547 | my $node = Games::RailRoad::Node->new({position=>$vec}); | ||
| 548 | $h->{nodes}{"$vec"} = $node; | ||
| 549 | } | ||
| 550 | |||
| 551 | |||
| 552 | # | ||
| 553 | # _c_b2_press( [], [$stuff, $x, $y] ); | ||
| 554 | # | ||
| 555 | # called when the right-button mouse is pressed on canvas. this will | ||
| 556 | # place a new train. | ||
| 557 | # | ||
| 558 | sub _on_c_b2_press { | ||
| 559 | my ($h, $args) = @_[HEAP, ARG1]; | ||
| 560 | my (undef, $x, $y) = @$args; | ||
| 561 | |||
| 562 | #return if defined $h->{train}; # only one train | ||
| 563 | |||
| 564 | my $vec = _resolve_coords($x,$y,0.5); | ||
| 565 | |||
| 566 | # check if there's a rail at $vec. | ||
| 567 | if ( not exists $h->{nodes}{"$vec"} ) { | ||
| 568 | warn "no rail at $vec\n"; | ||
| 569 | return; | ||
| 570 | } | ||
| 571 | |||
| 572 | # pick a random dir at first. | ||
| 573 | my @dirs = $h->{nodes}{"$vec"}->connections; | ||
| 574 | if ( scalar @dirs == 0 ) { | ||
| 575 | warn "nowhere to move on\n"; | ||
| 576 | return; | ||
| 577 | } | ||
| 578 | my $dir = $dirs[ rand @dirs ]; | ||
| 579 | my $move = Games::RailRoad::Vector->new_dir($dir); | ||
| 580 | |||
| 581 | # create the train. | ||
| 582 | my $train = Games::RailRoad::Train->new( { | ||
| 583 | from => $vec, | ||
| 584 | to => $vec + $move, | ||
| 585 | frac => 0, | ||
| 586 | } ); | ||
| 587 | $train->draw( $h->{w}{canvas}, $TILELEN ); | ||
| 588 | |||
| 589 | # store the train | ||
| 590 | push @{ $h->{trains} }, $train; | ||
| 591 | } | ||
| 592 | |||
| 593 | |||
| 594 | # | ||
| 595 | # _c_b3_press( [], [$stuff, $x, $y] ); | ||
| 596 | # | ||
| 597 | # called when the right-button mouse is pressed on canvas. this will | ||
| 598 | # mark the beginning corner of the delete area. | ||
| 599 | # | ||
| 600 | sub _on_c_b3_press { | ||
| 601 | my ($h, $args) = @_[HEAP, ARG1]; | ||
| 602 | my (undef, $x, $y) = @$args; | ||
| 603 | my $vec = _resolve_coords($x,$y,0.5); | ||
| 604 | |||
| 605 | my $node = $h->{nodes}{"$vec"}; | ||
| 606 | return unless defined $node; | ||
| 607 | |||
| 608 | # check if we can remove connection | ||
| 609 | my @connections = sort $node->connections; | ||
| 610 | foreach my $dir ( @connections ) { | ||
| 611 | my $move = Games::RailRoad::Vector->new_dir($dir); | ||
| 612 | my $v2 = $vec + $move; | ||
| 613 | my $n = $h->{nodes}{"$v2"}; | ||
| 614 | |||
| 615 | $move = -$move; | ||
| 616 | my $dir2 = $move->as_dir; | ||
| 617 | if ( ! $n->connectable("-$dir2") ) { | ||
| 618 | warn "$v2 cannot be disconnected from ($vec) using -$dir2\n"; | ||
| 619 | return; | ||
| 620 | } | ||
| 621 | } | ||
| 622 | |||
| 623 | # remove the connections | ||
| 624 | foreach my $dir ( @connections ) { | ||
| 625 | my $move = Games::RailRoad::Vector->new_dir($dir); | ||
| 626 | my $v2 = $vec + $move; | ||
| 627 | my $n = $h->{nodes}{"$v2"}; | ||
| 628 | $move = -$move; | ||
| 629 | my $dir2 = $move->as_dir; | ||
| 630 | $n->connect( "-$dir2" ); | ||
| 631 | $n->draw( $h->{w}{canvas}, $TILELEN ); | ||
| 632 | } | ||
| 633 | |||
| 634 | $node->delete( $h->{w}{canvas} ); | ||
| 635 | delete $h->{nodes}{"$vec"}; | ||
| 636 | } | ||
| 637 | |||
| 638 | |||
| 639 | # -- PRIVATE SUBS | ||
| 640 | |||
| 641 | # | ||
| 642 | # my ($pos, $col, $row) = _resolve_coords($x, $y, $precision); | ||
| 643 | # | ||
| 644 | # the canvas deals with pixels: this sub transforms canvas coordinates | ||
| 645 | # ($x,$y) in the $col and $row of the matching node. | ||
| 646 | # | ||
| 647 | # if we're not close enough of a node (within a square of $TILELEN times | ||
| 648 | # $precision), precision is not enough: $pos will be undef. | ||
| 649 | # | ||
| 650 | # $pos is the string "$col-$row". | ||
| 651 | # | ||
| 652 | # | ||
| 653 | sub _resolve_coords { | ||
| 654 | my ($x, $y, $prec) = @_; | ||
| 655 | |||
| 656 | my $col = int( $x/$TILELEN ); | ||
| 657 | my $row = int( $y/$TILELEN ); | ||
| 658 | |||
| 659 | # if we're in the middle of two nodes, it's not precise enough. | ||
| 660 | $x %= $TILELEN; | ||
| 661 | $y %= $TILELEN; | ||
| 662 | given ($x) { | ||
| 663 | when( $_ > $TILELEN * (1-$prec) ) { $col++; } | ||
| 664 | when( $_ <= $TILELEN * $prec ) { } # nothing to do | ||
| 665 | default { return; } # not precise enough | ||
| 666 | } | ||
| 667 | given ($y) { | ||
| 668 | when( $_ > $TILELEN * (1-$prec) ) { $row++; } | ||
| 669 | when( $_ <= $TILELEN * $prec ) { } # nothing to do | ||
| 670 | default { return; } # not precise enough | ||
| 671 | } | ||
| 672 | |||
| 673 | return Games::RailRoad::Vector->new({posx=>$col, posy=>$row}); | ||
| 674 | } | ||
| 675 | |||
| 676 | |||
| 677 | 1; | ||
| 678 | |||
| 679 | |||
| 680 | |||
| 681 | =pod | ||
| 682 | |||
| 683 | =head1 NAME | ||
| 684 | |||
| 685 | Games::RailRoad - a train simulation game | ||
| 686 | |||
| 687 | =head1 VERSION | ||
| 688 | |||
| 689 | version 1.101330 | ||
| 690 | |||
| 691 | =head1 DESCRIPTION | ||
| 692 | |||
| 693 | L<Games::RailRoad> allows you to draw a railroad, create some trains and | ||
| 694 | make them move on it. What you did when you were kid, but on your computer | ||
| 695 | now. | ||
| 696 | |||
| 697 | =head1 CLASS METHODS | ||
| 698 | |||
| 699 | =head2 my $id = Games::RailRoad->spawn( %opts ); | ||
| 700 | |||
| 701 | Create a new game, and return the associated POE session ID. | ||
| 702 | No option supported as of now. | ||
| 703 | |||
| 704 | =head1 PUBLIC EVENTS | ||
| 705 | |||
| 706 | The POE session accepts the following events: | ||
| 707 | |||
| 708 | =over 4 | ||
| 709 | |||
| 710 | =item none yet. | ||
| 711 | |||
| 712 | =back | ||
| 713 | |||
| 714 | =head1 TODO | ||
| 715 | |||
| 716 | Currently the module is very rough and supports very few operations: | ||
| 717 | |||
| 718 | =over 4 | ||
| 719 | |||
| 720 | =item * drawing and connecting rails by left-clicking and dragging mouse | ||
| 721 | on the canvas. | ||
| 722 | |||
| 723 | =item * removing a node by right-clicking on it on the canvas. | ||
| 724 | |||
| 725 | =item * placing a train on a rail by middle-clikcing on a rail on the canvas. | ||
| 726 | |||
| 727 | =item * support for more than one train | ||
| 728 | |||
| 729 | =item * changing switch exits by double-clicking on it. | ||
| 730 | |||
| 731 | =item * saving / loading to a file | ||
| 732 | |||
| 733 | =back | ||
| 734 | |||
| 735 | The amount of work needed is much more vast and includes (but not | ||
| 736 | limited to): | ||
| 737 | |||
| 738 | =over 4 | ||
| 739 | |||
| 740 | =item * adding coaches to trains | ||
| 741 | |||
| 742 | =item * rc-file for the application | ||
| 743 | |||
| 744 | =item * better interface (what about one-button mice?) | ||
| 745 | |||
| 746 | =item * available help | ||
| 747 | |||
| 748 | =item * etc... | ||
| 749 | |||
| 750 | =back | ||
| 751 | |||
| 752 | =head1 SEE ALSO | ||
| 753 | |||
| 754 | You can find more information on this module at: | ||
| 755 | |||
| 756 | =over 4 | ||
| 757 | |||
| 758 | =item * Search CPAN | ||
| 759 | |||
| 760 | L<http://search.cpan.org/dist/Games-RailRoad> | ||
| 761 | |||
| 762 | =item * See open / report bugs | ||
| 763 | |||
| 764 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-RailRoad> | ||
| 765 | |||
| 766 | =item * Git repository | ||
| 767 | |||
| 768 | L<http://github.com/jquelin/games-railroad> | ||
| 769 | |||
| 770 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 771 | |||
| 772 | L<http://annocpan.org/dist/Games-RailRoad> | ||
| 773 | |||
| 774 | =item * CPAN Ratings | ||
| 775 | |||
| 776 | L<http://cpanratings.perl.org/d/Games-RailRoad> | ||
| 777 | |||
| 778 | =back | ||
| 779 | |||
| 780 | =head1 AUTHOR | ||
| 781 | |||
| 782 | Jerome Quelin | ||
| 783 | |||
| 784 | =head1 COPYRIGHT AND LICENSE | ||
| 785 | |||
| 786 | This software is copyright (c) 2008 by Jerome Quelin. | ||
| 787 | |||
| 788 | This is free software; you can redistribute it and/or modify it under | ||
| 789 | the same terms as the Perl 5 programming language system itself. | ||
| 790 | |||
| 791 | =cut | ||
| 792 | |||
| 793 | |||
| 794 | __END__ | ||