| 1 | |||
| 2 | use File::Basename; | ||
| 3 | use POSIX qw/acos atan2/; | ||
| 4 | use subs qw/do_awake do_move do_sleep do_stop do_togi frame init_plug position_to_heading set_state stopped/; | ||
| 5 | use vars qw/$akubi_time $awake_time $canvas $close_enough $debug %dxdy $init $jare_time $kaki_time $maxx $maxy $nx | ||
| 6 | $ny $pi $pix $pixbits @pixlist %pixmaps $r2d $state %states $state_count $stop_time $togi_time $velocity $x $y | ||
| 7 | $where/; | ||
| 8 | use strict; | ||
| 9 | |||
| 10 | sub Animation { | ||
| 11 | |||
| 12 | # neko | ||
| 13 | # | ||
| 14 | # A Tk::LockDisplay plugin that emulates Masayuki Koba's xneko game. This "mainloop" dispatches control to | ||
| 15 | # one of 5 state processors, each of which displays pixmaps based on the state's cycle count. | ||
| 16 | # | ||
| 17 | # Stephen.O.Lidie@Lehigh.EDU, 98/10/20. | ||
| 18 | |||
| 19 | $canvas = $_[0]; | ||
| 20 | |||
| 21 | if ($init) { # if plugin already initialized | ||
| 22 | |||
| 23 | $where = sprintf("state=%s, nx/ny=%d/%d", $state, $nx, $ny) if $debug; | ||
| 24 | $state_count++; # current state's cycle count | ||
| 25 | STATES: | ||
| 26 | foreach my $regex (keys %states) { | ||
| 27 | next STATES unless (my $match) = $state =~ /^$regex$/; | ||
| 28 | &{$states{$regex}}($match); | ||
| 29 | return 1; # success | ||
| 30 | } | ||
| 31 | print STDERR "Illegal neko state=$state!\n"; | ||
| 32 | return 0; # fail | ||
| 33 | |||
| 34 | } else { | ||
| 35 | |||
| 36 | init_plug; # plugin initialization | ||
| 37 | return 1000/10; # animate with a frequency of 10 cycles/second | ||
| 38 | |||
| 39 | } | ||
| 40 | |||
| 41 | } # end neko Animation | ||
| 42 | |||
| 43 | sub frame { | ||
| 44 | |||
| 45 | # Display a frame unless it's already visible. | ||
| 46 | |||
| 47 | my($frame) = @_; | ||
| 48 | |||
| 49 | return if $pix eq "$frame.ppm"; | ||
| 50 | $canvas->coords($pixmaps{$pix}, -1000, -1000); | ||
| 51 | $pix = "$frame.ppm"; | ||
| 52 | $canvas->coords($pixmaps{$pix}, $nx, $ny); | ||
| 53 | |||
| 54 | } # end frame | ||
| 55 | |||
| 56 | sub init_plug { | ||
| 57 | |||
| 58 | $debug = 0; | ||
| 59 | my $base = Tk->findINC('LockDisplay/images'); | ||
| 60 | my $cursor = $^O eq 'MSWin32' ? 'mouse' : ["\@$base/mouse.xbm", "$base/mouse.mask", qw/black white/]; | ||
| 61 | $canvas->configure(-background => 'white', -cursor => $cursor); | ||
| 62 | $canvas->idletasks; | ||
| 63 | $canvas->createWindow(300, 300, -window => $canvas->Label(-textvariable => \$where)) if $debug; | ||
| 64 | |||
| 65 | $pi = acos(-1); # pi | ||
| 66 | $r2d = 180.0 / $pi; # radians to degrees | ||
| 67 | ($maxx, $maxy) = ($canvas->screenwidth, $canvas->screenheight); # display size | ||
| 68 | ($nx, $ny) = ($maxx/2, $maxy/2 + 80); # current neko position | ||
| 69 | $velocity = 10; # in pixels/cycle | ||
| 70 | $canvas->createWindow($nx, $ny - 35, -window => $canvas->Scale(qw/-background white -from 0 -to 20 | ||
| 71 | -showvalue 1 -resolution 1 -orient horizontal -relief flat -font fixed -highlightthickness 0 | ||
| 72 | -variable/ => \$velocity), | ||
| 73 | ); | ||
| 74 | $canvas->idletasks; | ||
| 75 | $close_enough = 5; # neko has caught mouse if within this pixel distance | ||
| 76 | %dxdy = ( | ||
| 77 | LEFT => [-1, 0], | ||
| 78 | RIGHT => [+1, 0], | ||
| 79 | UP => [ 0, -1], | ||
| 80 | DOWN => [ 0, +1], | ||
| 81 | DWLEFT => [-1, +1], | ||
| 82 | DWRIGHT => [+1, +1], | ||
| 83 | UPLEFT => [-1, -1], | ||
| 84 | UPRIGHT => [+1, -1], | ||
| 85 | ); # x/y pixel delta multipliers | ||
| 86 | $pix = ''; # currently displayed Pixmap | ||
| 87 | $pixbits = 16; # 0.5 Pixmap size in bits | ||
| 88 | $state = ''; # current game state | ||
| 89 | %states = ( | ||
| 90 | 'NEKO_(AWAKE)' => \&do_awake, | ||
| 91 | 'NEKO_(UP|UPRIGHT|RIGHT|DWRIGHT|DOWN|DWLEFT|LEFT|UPLEFT)' => \&do_move, | ||
| 92 | 'NEKO_(STOP)' => \&do_stop, | ||
| 93 | 'NEKO_(UTOGI|RTOGI|DTOGI|LTOGI)' => \&do_togi, | ||
| 94 | 'NEKO_(SLEEP)' => \&do_sleep, | ||
| 95 | ); # neko state table | ||
| 96 | $state_count = 0; # current state's cycle count | ||
| 97 | set_state 'NEKO_AWAKE'; | ||
| 98 | $akubi_time = 3 * 2; # yawn cycles | ||
| 99 | $awake_time = 3 * 2; # awake cycles | ||
| 100 | $jare_time = 10 * 2; # stomp cycles | ||
| 101 | $kaki_time = 4 * 2; # scratch neko cycles | ||
| 102 | $stop_time = 4 * 2; # stop cycles | ||
| 103 | $togi_time = 10 * 2; # scratch wall cycles | ||
| 104 | |||
| 105 | # Load and momentarily display Pixmaps (probably poor Japanese translations my own). | ||
| 106 | # | ||
| 107 | # Icon - neko icon | ||
| 108 | # awake - freshly awake | ||
| 109 | # down1 - south #1 | ||
| 110 | # down2 - south #2 | ||
| 111 | # dtogi1 - south wall scratch #1 | ||
| 112 | # dtogi2 - south wall scratch #2 | ||
| 113 | # dwleft1 - southwest #1 | ||
| 114 | # dwleft2 - southwest #2 | ||
| 115 | # dwright1 - southeast #1 | ||
| 116 | # dwright2 - southeast #2 | ||
| 117 | # jare2 - stopped #2 (stomp ground) | ||
| 118 | # kaki1 - scratch #1 | ||
| 119 | # kaki2 - scratch #2 | ||
| 120 | # left1 - west #1 | ||
| 121 | # left2 - west #2 | ||
| 122 | # ltogi1 - west wall scratch #1 | ||
| 123 | # ltogi2 - west wall scratch #2 | ||
| 124 | # mati2 - stopped #1 | ||
| 125 | # mati3 - yawn | ||
| 126 | # rtogi1 - east wall scratch #1 | ||
| 127 | # rtogi2 - east wall scratch #2 | ||
| 128 | # sleep1 - sleep #1 | ||
| 129 | # sleep2 - sleep #2 | ||
| 130 | # north1 - north #1 | ||
| 131 | # north2 - north #2 | ||
| 132 | # upleft1 - northwest #1 | ||
| 133 | # upleft2 - northwest #2 | ||
| 134 | # upright1 - northeast #1 | ||
| 135 | # upright2 - northeast #2 | ||
| 136 | # utogi1 - north wall scratch #1 | ||
| 137 | # utogi2 - north wall scratch #2 | ||
| 138 | |||
| 139 | my $x = 40; | ||
| 140 | my $y = 30; | ||
| 141 | my $i = $canvas->createText(120, 20, -fill => 'black', -text => "Loading pixmaps ..."); | ||
| 142 | my $n = 1; | ||
| 143 | foreach my $pfn ( <$base/*.ppm> ) { | ||
| 144 | my $bpfn = basename $pfn; | ||
| 145 | $pixmaps{$bpfn} = $canvas->createImage($x, $y, -image => $canvas->Photo(-file => $pfn)); | ||
| 146 | $canvas->idletasks; | ||
| 147 | $x += 35; | ||
| 148 | if ($n++ >= 8 or $bpfn eq 'Icon.ppm') { | ||
| 149 | $y += 50; | ||
| 150 | $x = 40; | ||
| 151 | $n = 1; | ||
| 152 | } | ||
| 153 | } # forend all Pixmaps | ||
| 154 | |||
| 155 | # Hide Pixmaps off-canvas until we need them. | ||
| 156 | |||
| 157 | $canvas->delete($i); | ||
| 158 | $canvas->after(1000); | ||
| 159 | foreach my $pxid (keys %pixmaps) { | ||
| 160 | $canvas->coords($pixmaps{$pxid}, -1000, -1000); | ||
| 161 | $canvas->after(50); | ||
| 162 | $canvas->idletasks; | ||
| 163 | } | ||
| 164 | |||
| 165 | $init = 1; | ||
| 166 | |||
| 167 | } # end init_plug | ||
| 168 | |||
| 169 | sub position_to_heading { | ||
| 170 | |||
| 171 | # Swiped and modified from my TclRobots entry #2, position_to_heading() determines the direction (as one of | ||
| 172 | # eight cardinal compass points) from the neko to the mouse. 0 degress at three o'clock, moving clockwise. | ||
| 173 | |||
| 174 | ($x, $y) = $canvas->pointerxy; | ||
| 175 | $y -= ($pixbits / 2 + 3 ); | ||
| 176 | |||
| 177 | # Don't let the neko run off the display. | ||
| 178 | |||
| 179 | if ($x < 0 + $pixbits) { | ||
| 180 | $x = $pixbits; | ||
| 181 | } elsif ($x > $maxx - $pixbits) { | ||
| 182 | $x = $maxx - $pixbits; | ||
| 183 | } elsif ($y < 0 + $pixbits) { | ||
| 184 | $y = $pixbits; | ||
| 185 | } elsif ($y > $maxy - $pixbits) { | ||
| 186 | $y = $maxy - $pixbits; | ||
| 187 | } | ||
| 188 | return if stopped; | ||
| 189 | |||
| 190 | # Return heading from the neko to the mouse. | ||
| 191 | |||
| 192 | my $h = int( $r2d * CORE::atan2( ($y - $ny), ($x - $nx) ) ) % 360; | ||
| 193 | my($degrees, $dir); | ||
| 194 | |||
| 195 | foreach ( | ||
| 196 | [[ 22.5, 67.5], 'DWRIGHT'], | ||
| 197 | [[ 67.5, 112.5], 'DOWN'], | ||
| 198 | [[112.5, 157.5], 'DWLEFT'], | ||
| 199 | [[157.5, 202.5], 'LEFT'], | ||
| 200 | [[202.5, 247.5], 'UPLEFT'], | ||
| 201 | [[247.5, 292.5], 'UP'], | ||
| 202 | [[292.5, 337.5], 'UPRIGHT'], | ||
| 203 | [[337.5, 22.5], 'RIGHT'], | ||
| 204 | ) { | ||
| 205 | ($degrees, $dir) = ($_->[0], $_->[1]); | ||
| 206 | last if $h >= $degrees->[0] and $h < $degrees->[1]; | ||
| 207 | } # forend | ||
| 208 | |||
| 209 | set_state "NEKO_$dir"; | ||
| 210 | |||
| 211 | } # end positition_to_heading | ||
| 212 | |||
| 213 | sub set_state { | ||
| 214 | |||
| 215 | # Initialize for a new state if it's different from the current state. | ||
| 216 | |||
| 217 | my($new_state) = @_; | ||
| 218 | |||
| 219 | return if $new_state eq $state; | ||
| 220 | $state = $new_state; | ||
| 221 | $state_count = 0; | ||
| 222 | |||
| 223 | } # end set_state | ||
| 224 | |||
| 225 | sub stopped { | ||
| 226 | |||
| 227 | # See if the neko and mouse are close enough to pretend we are stopped. $close_enough is tied | ||
| 228 | # to the neko's velocity to prevent "directional hysteresis". | ||
| 229 | |||
| 230 | $close_enough = $velocity; | ||
| 231 | ( abs($x - $nx) <= $close_enough and abs($y - $ny) <= $close_enough ) ? 1 : 0; | ||
| 232 | |||
| 233 | } # end stopped | ||
| 234 | |||
| 235 | # Neko state processors. | ||
| 236 | |||
| 237 | sub do_awake { | ||
| 238 | |||
| 239 | frame 'awake'; | ||
| 240 | return if $state_count < $awake_time; | ||
| 241 | position_to_heading; | ||
| 242 | |||
| 243 | } # end do_awake | ||
| 244 | |||
| 245 | sub do_move { | ||
| 246 | |||
| 247 | my($dir) = @_; | ||
| 248 | if (stopped) { | ||
| 249 | ($nx, $ny) = ($x, $y); | ||
| 250 | set_state 'NEKO_STOP'; | ||
| 251 | } else { | ||
| 252 | my($dx, $dy) = @{$dxdy{$dir}}; | ||
| 253 | ($nx, $ny) = ($nx + ($dx * $velocity), $ny + ($dy * $velocity)); | ||
| 254 | frame lc($dir) . (($state_count % 2) + 1); | ||
| 255 | position_to_heading; | ||
| 256 | } | ||
| 257 | |||
| 258 | } # end do_move | ||
| 259 | |||
| 260 | sub do_sleep { | ||
| 261 | |||
| 262 | position_to_heading; | ||
| 263 | if (stopped) { | ||
| 264 | if ($state_count < $jare_time) { | ||
| 265 | frame (($state_count % 2) ? 'jare2' : 'mati2'); | ||
| 266 | } elsif ($state_count < $jare_time + $kaki_time) { | ||
| 267 | frame 'kaki' . (($state_count % 2) + 1); | ||
| 268 | } elsif ($state_count < $jare_time + $kaki_time + $akubi_time) { | ||
| 269 | frame 'mati3'; | ||
| 270 | } else { | ||
| 271 | frame 'sleep' . ((($state_count % 8) <= 3) ? '1' : '2'); | ||
| 272 | } | ||
| 273 | } else { | ||
| 274 | set_state 'NEKO_AWAKE'; | ||
| 275 | } | ||
| 276 | |||
| 277 | } # end do_sleep | ||
| 278 | |||
| 279 | sub do_stop { | ||
| 280 | |||
| 281 | if (stopped) { | ||
| 282 | if ($state_count < $stop_time) { | ||
| 283 | frame 'mati2'; | ||
| 284 | } elsif ($nx <= 0 + $pixbits) { | ||
| 285 | set_state 'NEKO_LTOGI'; | ||
| 286 | } elsif ($nx >= $maxx - $pixbits) { | ||
| 287 | set_state 'NEKO_RTOGI'; | ||
| 288 | } elsif ($ny <= 0 + $pixbits) { | ||
| 289 | set_state 'NEKO_UTOGI'; | ||
| 290 | } elsif ($ny >= $maxy - $pixbits) { | ||
| 291 | set_state 'NEKO_DTOGI'; | ||
| 292 | } else { | ||
| 293 | set_state 'NEKO_SLEEP'; | ||
| 294 | } | ||
| 295 | } else { | ||
| 296 | set_state 'NEKO_AWAKE'; | ||
| 297 | } | ||
| 298 | |||
| 299 | } # end do_stop | ||
| 300 | |||
| 301 | sub do_togi { | ||
| 302 | |||
| 303 | my($dir) = @_; | ||
| 304 | |||
| 305 | position_to_heading; | ||
| 306 | if (stopped) { | ||
| 307 | if ($state_count < $togi_time) { | ||
| 308 | frame lc($dir) . (($state_count % 2) + 1); | ||
| 309 | } elsif ($state_count < $togi_time + $kaki_time) { | ||
| 310 | frame 'kaki' . (($state_count % 2) + 1); | ||
| 311 | } elsif ($state_count < $togi_time + $kaki_time + $akubi_time) { | ||
| 312 | frame 'mati3'; | ||
| 313 | } else { | ||
| 314 | frame 'sleep' . ((($state_count % 8) <= 3) ? '1' : '2'); | ||
| 315 | } | ||
| 316 | } else { | ||
| 317 | set_state 'NEKO_AWAKE'; | ||
| 318 | } | ||
| 319 | |||
| 320 | } # end do_togi | ||
| 321 | |||
| 322 | 1; |