|
NAMETerm::Animation - ASCII sprite animation frameworkSYNOPSISuse Term::Animation; # Constructors $anim = Term::Animation->new(); $anim = Term::Animation->new($curses_window); ABSTRACTA framework to produce sprite animations using ASCII art.DESCRIPTIONThis module provides a framework to produce sprite animations using ASCII art. Each ASCII 'sprite' is given one or more frames, and placed into the animation as an 'animation object'. An animation object can have a callback routine that controls the position and frame of the object.If the constructor is passed no arguments, it assumes that it is running full screen, and behaves accordingly. Alternatively, it can accept a curses window (created with the Curses newwin call) as an argument, and will draw into that window. EXAMPLESThis example moves a small object across the screen from left to right.use Term::Animation; use Curses; $anim = Term::Animation->new(); # set the delay for getch halfdelay( 2 ); # create a simple shape we can move around $shape = "<=O=>"; # turn our shape into an animation object $anim->new_entity( shape => $shape, # object shape position => [3, 7, 10], # row / column / depth callback_args => [1, 0, 0, 0], # the default callback # routine takes a list # of x,y,z,frame deltas wrap => 1 # turn screen wrap on ); # animation loop while(1) { # run and display a single animation frame $anim->animate(); # use getch to control the frame rate, and get input at the # same time. (not a good idea if you are expecting much input) my $input = getch(); if($input eq 'q') { last; } } # cleanly end the animation, to avoid hosing up the user's terminal $anim->end(); This illustrates how to draw your animation into an existing Curses window. use Term::Animation; use Curses; # Term::Animation will not call initscr for you if # you pass it a window initscr(); $win = newwin(5,10,8,7); $anim = Term::Animation->new($win); Everything else would be identical to the previous example. METHODS
CALLBACK ROUTINESCallback routines for all entities are called each time animate is called. A default callback routine is supplied, move_entity, which is sufficient for most basic movement. If you want to create an entity that exhibits more complex behavior, you will have to write a custom callback routine for it.Callback routines take two arguments, a reference to the Term::Animation::Entity object that it should act on, and a reference to the Term::Animation instance that called it. Any arguments required to tell the callback what to do with the object, or any state that needs to be maintained, should be put in the callback_args element of the object. callback_args is only referenced by the callback routine, and thus can contain any datastructure that you find useful. Here is an example custom callback that will make an entity move randomly around the screen: sub random_movement { my ($entity, $anim) = @_; # get the current position of the entity my ($x, $y, $z) = $entity->position(); # we'll use callback_args to store the last axis we moved in my $last_move = $entity->callback_args(); # if we moved in x last time, move in y this time if($last_move eq 'x') { $entity->callback_args('y'); # move by -1, 0 or 1 $y += int(rand(3)) - 1; } else { $entity->callback_args('x'); $x += int(rand(3)) - 1; } # return the absolute x,y,z coordinates to move to return ($x, $y, $z); } The return value of your callback routine should be of the form: return ($x, $y, $z, $frame) $x, $y and $z represent the X, Y and Z coordinates to which the object should move. $frame is the frame number that the object should display, if it has multiple frames of animation. Any values that are unspecified or undef will remain unchanged. You can also call the default callback from within your callback, if you want it to handle movement for you. For example, if your callback is simply used to decide when an entity should die: sub wait_for_file { my ($entity, $anim) = @_; # kill this entity if a certain file shows up if(-e "/path/to/file") { $entity->kill(); return(); } # use the default callback to handle the actual movement return $entity->move_entity($anim); } If you use this, be aware that move_entity relies on callback_args, so you cannot use it to store your own arbitrary data. COLORANSI color is available for terminals that support it. Only a single background color can be used for the window (it would look terrible in most cases otherwise anyway). Colors for entities are specified by using a 'mask' that indicates the color for each character. For example, say we had a single frame of a bird:$bird = q# ---. .-. .--- --\'v'/-- \ / " " #; To indicate the colors you want to use for the bird, create a matching mask, with the first letter of each color in the appropriate position (except black, which is 'k'). Pass this mask as the color parameter. $mask = q# BBBB BBB BBBB BBBWYWBBB B B Y Y #; When specifying a color, using uppercase indicates the color should be bold. So 'BLUE' or 'B' means bold blue, and 'blue' or 'b' means non-bold blue. 'Blue' means you get an error message. You can also provide a default color with the default_color parameter. This color will be used for any character that does not have an entry in the mask. If you want the entire entity to be a single color, you can just provide a default color with no mask. The available colors are: red, green, blue, cyan, magenta, yellow, black and white. Here's an example call to build_object for the bird above. $anim->new_entity ( name => "Bird", shape => $bird, position => [ 5, 8, 7 ], callback_args => [ 1, 2, 0, 0 ], color => $mask, default_color => "BLUE" ); AUTHORKirk Baucom, <kbaucom@schizoid.com>SEE ALSOCursesPOD ERRORSHey! The above document had some coding errors, which are explained below:
Visit the GSP FreeBSD Man Page Interface. |