Animation/Entity.pm untested

filter: Statementstmt 0.0% 0 / 306 Branchbran 0.0% 0 / 162 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 44 Podpod 0.0% 0 / 38 totaltotal 0.0% 0 / 574 CC 82 SCAR 51.0
CC
82
Cov
0%
new
702.0
move_entity
90.0
_build_mask
72.0
CRAP 164.0
1 package Term::Animation::Entity;
2  
3 use 5.006;
4 use strict;
5 use warnings;
6 use Carp;
7 use Curses;
8 use Term::Animation;
9  
10 =head1 NAME
11  
12 Term::Animation::Entity
13  
14 =head1 SYNOPSIS
15  
16 use Term::Animation::Entity;
17  
18 # Constructor
19 my $entity = Term::Animation::Entity->new(
20 shape => ';-)',
21 position => [ 1, 2, 3 ],
22 callback_args => [ 0, 1, 0, 0 ],
23 );
24  
25 =head1 ABSTRACT
26  
27 A sprite object for use with Term::Animation
28  
29 =head1 DESCRIPTION
30  
31 Term::Animation::Entity is used by L<Term::Animation|Term::Animation> to
32 represent a single sprite on the screen.
33  
34 =head1 PARAMETERS
35  
36 name < SCALAR >
37 A string uniquely identifying this object
38  
39 shape < REF >
40 The ASCII art for this object. It can be provided as:
41 1) A single multi-line text string (no animation)
42 2) An array of multi-line text strings, where each
43 element is a single animation frame
44 3) An array of 2D arrays. Each element in the outer
45 array is a single animation frame.
46 If you provide an array, each element is a single frame of animation.
47 If you provide either 1) or 2), a single newline will be stripped off
48 of the beginning of each string. 3) is what the module uses internally.
49  
50 auto_trans < BOOLEAN >
51 Whether to automatically make whitespace at the beginning of each line
52 transparent. Default: 0
53  
54 position < ARRAY_REF >
55 A list specifying initial x,y and z coordinates
56 Default: [ 0, 0, 0 ]
57  
58 callback < SUBROUTINE_REF >
59 Callback routine for this entity. Default: I<move_entity()>
60  
61 callback_args < REF >
62 Arguments to the callback routine.
63  
64 curr_frame < INTEGER >
65 Animation frame to begin with. Default: 0
66  
67 wrap < BOOLEAN >
68 Whether this entity should wrap around the edge of the screen. Default: 0
69  
70 transparent < SCALAR >
71 Character used to indicate transparency. Default: ?
72  
73 die_offscreen < BOOLEAN >
74 Whether this entity should be killed if
75 it goes off the screen. Default: 0
76  
77 die_entity < ENTITY >
78 Specifies an entity (ref or name). When the named
79 entity dies, this entity should die as well. Default: undef
80  
81 die_time < INTEGER >
82 The time at which this entity should be killed. This
83 should be a UNIX epoch time, as returned
84 by I<time>. Default: undef
85  
86 die_frame < INTEGER >
87 Specifies the number of frames that should be displayed
88 before this entity is killed. Default: undef
89  
90 death_cb < SUBROUTINE_REF >
91 Callback routine used when this entity dies
92  
93 dcb_args < REF >
94 Arguments to the entity death callback routine
95  
96 color
97 Color mask. This follows the same format as 'shape'.
98 See the 'COLOR' section below for more details.
99  
100 default_color < SCALAR >
101 A default color to use for the entity. See the 'COLOR' section
102 for more details.
103  
104 data < REF >
105 Store some data about this entity. It is not used by the module.
106 You can use it to store state information about this entity.
107  
108 =head1 METHODS
109  
110 =over 4
111  
112 =item I<new>
113  
114 my $entity = Term::Animation::Entity->new(
115 shape => ';-)',
116 position => [ 1, 2, 3 ],
117 callback_args => [ 0, 1, 0, 0 ],
118 );
119  
120 Create a Term::Animation::Entity instance. See the PARAMETERS section for
121 details.
122  
123 =cut
124 sub new {
125 my $proto = shift;
126 my $class = ref($proto) || $proto;
127 my $self = {};
128 my %p = @_;
129  
130 # default sprite is a single asterisk
131 unless(defined($p{'shape'})) { $p{'shape'} = '*'; }
132  
133 # if no name is supplied, generate a random one
134 if(defined($p{'name'})) {
135 $self->{NAME} = $p{'name'};
136 } else {
137 my $rand_name = rand();
138 while(defined($self->{OBJECTS}{$rand_name})) {
139 $rand_name = rand();
140 }
141 $self->{NAME} = $rand_name;
142 }
143  
144 # appearance
145 $self->{TRANSPARENT} = defined($p{'transparent'}) ? $p{'transparent'} : '?';
146 $self->{AUTO_TRANS} = defined($p{'auto_trans'}) ? $p{'auto_trans'} : 0;
147 if($self->{AUTO_TRANS}) { $p{'shape'} = _auto_trans($p{'shape'}, $self->{TRANSPARENT}); }
148 ($self->{SHAPE}, $self->{HEIGHT}, $self->{WIDTH}) = _build_shape($self, $p{'shape'});
149 ($self->{X}, $self->{Y}, $self->{Z}) = defined($p{'position'}) ? @{$p{'position'}} : ( 0, 0, 0 );
150 $self->{DEF_COLOR} = defined($p{'default_color'}) ? Term::Animation::color_id($p{'default_color'}) : 'w';
151 _build_mask($self, $p{'color'});
152  
153 # collision detection
154 $self->{DEPTH} = defined($p{'depth'}) ? $p{'depth'} : 1;
155 $self->{PHYSICAL} = defined($p{'physical'}) ? $p{'physical'} : 0;
156 $self->{COLL_HANDLER} = defined($p{'coll_handler'}) ? $p{'coll_handler'} : undef;
157  
158 # behavior
159 $self->{CALLBACK_ARGS} = defined($p{'callback_args'})? $p{'callback_args'}: undef;
160 $self->{WRAP} = defined($p{'wrap'}) ? $p{'wrap'} : 0;
161 if (defined($p{'callback'})) { $self->{CALLBACK} = $p{'callback'}; }
162 elsif(defined($p{'callback_args'})) { $self->{CALLBACK} = \&move_entity; }
163 else { $self->{CALLBACK} = undef; }
164 $self->{FOLLOW_ENTITY} = defined($p{'follow_entity'})? $self->_get_entity_name($p{'follow_entity'}) : undef;
165 $self->{FOLLOW_OFFSET} = defined($p{'follow_offset'})? $p{'follow_offset'} : undef;
166  
167 # state
168 $self->{CURR_FRAME} = defined($p{'curr_frame'}) ? $p{'curr_frame'} : 0;
169  
170 # entity death
171 $self->{DIE_OFFSCREEN} = defined($p{'die_offscreen'}) ? $p{'die_offscreen'} : 0;
172 $self->{DIE_TIME} = defined($p{'die_time'}) ? $p{'die_time'} : undef;
173 $self->{DIE_FRAME} = defined($p{'die_frame'}) ? $p{'die_frame'} : undef;
174 $self->{DEATH_CB} = defined($p{'death_cb'}) ? $p{'death_cb'} : undef;
175 $self->{DCB_ARGS} = defined($p{'dcb_args'}) ? $p{'dcb_args'} : undef;
176 $self->{DIE_ENTITY} = defined($p{'die_entity'}) ? $self->_get_entity_name($p{'die_entity'}) : undef;
177  
178 # misc
179 $self->{TYPE} = defined($p{'type'}) ? $p{'type'} : "self";
180 $self->{DATA} = defined($p{'data'}) ? $p{'data'} : undef;
181  
182 bless($self, $class);
183 return $self;
184 }
185  
186 =item I<physical>
187  
188 $entity->physical( 1 );
189 $state = $entity->physical();
190  
191 Enables or disabled collision detection for this entity.
192  
193 =cut
194 sub physical {
195 my $self = shift;
196 if (@_) {
197 my $new_physical = shift;
198 if($new_physical != $self->{PHYSICAL}) {
199 $self->{PHYSICAL} = $new_physical;
200 if(defined($self->{ANIMATION})) {
201 $self->{ANIMATION}->_update_physical($self);
202 }
203 }
204 }
205 return $self->{PHYSICAL};
206 }
207  
208 =item I<auto_trans>
209  
210 $entity->auto_trans( 1 );
211 $state = $entity->auto_trans();
212  
213 Enables or disables automatic transparency for this entity's sprite.
214 This will only affect subsequent calls to I<shape>, the current sprite
215 will be unchanged.
216  
217 =cut
218 sub auto_trans {
219 my $self = shift;
220 if(@_) { $self->{AUTO_TRANS} = shift; }
221 return $self->{AUTO_TRANS};
222 }
223  
224 =item I<transparent>
225  
226 $entity->transparent( '*' );
227 $trans_char = $entity->transparent();
228  
229 Gets or sets the transparent character for this entity's sprite.
230 This will only affect subsequent calls to I<shape>, the current
231 sprite will be unchanged.
232  
233 =cut
234 sub transparent {
235 my $self = shift;
236 if(@_) { $self->{TRANSPARENT} = shift; }
237 return $self->{TRANSPARENT};
238 }
239  
240 =item I<wrap>
241  
242 $entity->wrap( 1 );
243 $wrap = $entity->wrap;
244  
245 Gets or sets the boolean that indicates whether this entity
246 should wrap around when it gets to an edge of the screen.
247  
248 =cut
249 sub wrap {
250 my $self = shift;
251 if(@_) { $self->{WRAP} = shift; }
252 return $self->{WRAP};
253 }
254  
255 =item I<data>
256  
257 $entity->data( $stuff );
258 $data = $entity->data();
259  
260 Get or set the 'data' associated with the entity. It should
261 be a single scalar or ref. This can be whatever you want,
262 it is not used by the module and is provided for convenience.
263  
264 =cut
265 sub data {
266 my $self = shift;
267 if(@_) { $self->{DATA} = shift; }
268 return $self->{DATA};
269 }
270  
271 =item I<name>
272  
273 $name = $entity->name();
274  
275 Returns the name of the entity.
276  
277 =cut
278 sub name {
279 my $self = shift;
280 return $self->{NAME};
281 }
282  
283 =item I<type>
284  
285 $entity->type( 'this_type' );
286 $type = $entity->type();
287  
288 Get or set the 'type' of the entity. The type can be any string,
289 and is not used by the animation itself.
290  
291 =cut
292 sub type {
293 my $self = shift;
294 if (@_) { $self->{TYPE} = shift }
295 return $self->{TYPE};
296 }
297  
298 =item I<frame>
299  
300 $entity->frame( 3 );
301 $current_frame = $entity->frame();
302  
303 Gets or sets the current animation frame of the entity.
304  
305 =cut
306 sub frame {
307 my $self = shift;
308 if (@_) {
309 my $new_frame = shift;
310 unless($new_frame =~ /^\d+$/ &&
311 $new_frame >= 0 &&
312 $new_frame <= $#{$self->{SHAPE}}) {
313 carp "Invalid frame number: $new_frame\n";
314 return $self->{CURR_FRAME};
315 }
316 $self->{CURR_FRAME} = $new_frame;
317 }
318 return $self->{CURR_FRAME};
319 }
320  
321 =item I<width>
322  
323 my $width = $entity->width();
324  
325 Returns the width (columns) of the entity.
326  
327 =cut
328 sub width {
329 my $self = shift;
330 return $self->{WIDTH};
331 }
332  
333 =item I<height>
334  
335 my $height = $entity->height();
336  
337 Returns the height (rows) of the entity.
338  
339 =cut
340 sub height {
341 my $self = shift;
342 return $self->{HEIGHT};
343 }
344  
345 =item I<depth>
346  
347 my $depth = $entity->depth();
348  
349 Returns the depth of the entity.
350  
351 =cut
352 sub depth {
353 my $self = shift;
354 return $self->{DEPTH};
355 }
356  
357 =item I<size>
358  
359 my ($width, $height, $depth) = $entity->size();
360  
361 Returns the X / Y / Z dimensions of the entity.
362  
363 =cut
364  
365 sub size {
366 my $self = shift;
367 return ($self->{WIDTH}, $self->{HEIGHT}, $self->{DEPTH});
368 }
369  
370 =item I<position>
371  
372 my ($x, $y, $z) = $entity->position();
373 $entity->position($x, $y, $z);
374  
375 Gets or sets the X / Y / Z coordinates of the entity. You can also
376 access each coordinate individually.
377  
378 my $x = $entity->x;
379 $entity->x(5);
380  
381 Note that you should normally position an entity using its callback routine,
382 instead of calling one of these methods.
383  
384 =cut
385 sub position {
386 my $self = shift;
387 if(@_) { ($self->{X}, $self->{Y}, $self->{Z}) = @_; }
388 return ($self->{X}, $self->{Y}, $self->{Z});
389 }
390  
391 sub x {
392 my $self = shift;
393 if(@_) { ($self->{X}) = @_ }
394 return $self->{X};
395 }
396  
397 sub y {
398 my $self = shift;
399 if(@_) { ($self->{Y}) = @_ }
400 return $self->{Y};
401 }
402  
403 sub z {
404 my $self = shift;
405 if(@_) { ($self->{Z}) = @_ }
406 return $self->{Z};
407 }
408  
409 =item I<callback_args>
410  
411 $entity->callback_args( $args );
412 $args = $entity->callback_args();
413  
414 Get or set the arguments to the entity's callback routine. This
415 should be either a single scalar or a single ref.
416  
417 =cut
418 sub callback_args {
419 my $self = shift;
420 if(@_) { $self->{CALLBACK_ARGS} = shift; }
421 return $self->{CALLBACK_ARGS};
422 }
423  
424 =item I<callback>
425  
426 $entity->callback( \&callback_routine );
427 $callback_routine = $entity->callback();
428  
429 Get or set the callback routine for the entity
430  
431 =cut
432 sub callback {
433 my $self = shift;
434 if(@_) { $self->{CALLBACK} = shift; }
435 return $self->{CALLBACK};
436 }
437  
438 =item I<death_cb>
439  
440 $entity->death_cb( \&death_callback_routine );
441 $death_callback_routine = $entity->death_cb();
442  
443 Get or set the callback routine that is called
444 when the entity dies. Set to undef if you do not
445 want anything to be called.
446  
447 =cut
448 sub death_cb {
449 my $self = shift;
450 if(@_) { $self->{DEATH_CB} = shift; }
451 return $self->{DEATH_CB};
452 }
453  
454 =item I<die_offscreen>
455  
456 $entity->die_offscreen( 1 );
457 $die_offscreen = $entity->die_offscreen;
458  
459 Get or set the flag that indicates whether this
460 entity should die when it is entirely off the screen.
461  
462 =cut
463 sub die_offscreen {
464 my $self = shift;
465 if(@_) { $self->{DIE_OFFSCREEN} = shift; }
466 return $self->{DIE_OFFSCREEN};
467 }
468  
469 =item I<die_frame>
470  
471 $entity->die_frame( 1 );
472 $die_frame = $entity->die_frame;
473  
474 Get or set the frame number in which this entity
475 should die, counting from the time when die_frame
476 is called. Set to undef to disable.
477  
478 =cut
479 sub die_frame {
480 my $self = shift;
481 if(@_) { $self->{DIE_FRAME} = shift; }
482 return $self->{DIE_FRAME};
483 }
484  
485 =item I<die_time>
486  
487 $entity->die_time( time() + 20 );
488 $die_time = $entity->die_time;
489  
490 Get or set the time at which this entity should die.
491 The time is a UNIX epoch time. Set to undef to disable.
492  
493 =cut
494 sub die_time {
495 my $self = shift;
496 if(@_) { $self->{DIE_TIME} = shift; }
497 return $self->{DIE_TIME};
498 }
499  
500 =item I<die_entity>
501  
502 $entity->die_entity( $other_entity );
503 $other_entity = $entity->die_entity;
504  
505 Get or set an entity whose death will cause the
506 death of this entity. Either an entity name or
507 Term::Animation::Entity reference are accepted, but
508 an entity name is always returned. Set to undef to disable.
509  
510 =cut
511 sub die_entity {
512 my $self = shift;
513 if(@_) {
514 my $ent = shift;
515 if(ref($ent)) {
516 $ent = $ent->name;
517 }
518 $self->{DIE_ENTITY} = $ent;
519 }
520 return $self->{DIE_ENTITY};
521 }
522  
523 sub follow_entity {
524 my $self = shift;
525 if(@_ && defined($self->animation)) {
526
527 my ($ent) = @_;
528  
529 if(defined($ent)) {
530 $self->{FOLLOW_ENTITY} = $self->_get_entity_name($ent);
531 } else {
532 $self->{FOLLOW_ENTITY} = undef;
533 }
534 }
535 return $self->{FOLLOW_ENTITY};
536  
537 }
538  
539 sub follow_offset {
540 my $self = shift;
541  
542 if(@_) {
543 $self->{FOLLOW_OFFSET} = shift;
544 }
545 return $self->{FOLLOW_OFFSET};
546 }
547  
548 =item I<shape>
549  
550 $entity->shape($new_shape);
551
552 Set the sprite image for the entity. See the C<shape> argument
553 to I<new> for details.
554  
555 =cut
556 sub shape {
557 my $self = shift;
558 if(@_) {
559 my $shape = shift;
560 if($self->{AUTO_TRANS}) {
561 $shape = _auto_trans($shape, $self->{TRANSPARENT});
562 }
563 ($self->{SHAPE},$self->{HEIGHT},$self->{WIDTH}) = $self->_build_shape($shape);
564 }
565 }
566  
567 =item I<collisions>
568  
569 $collisions = $entity->collisions();
570  
571 Returns a reference to a list of entities that this entity
572 collided with during this animation cycle.
573  
574 =cut
575 sub collisions {
576 my $self = shift;
577 return $self->{COLLISIONS};
578 }
579  
580 =item I<animation>
581  
582 $entity->animation( $anim );
583 $anim = $entity->animation();
584  
585 Get or set the Term::Animation object that this entity is
586 part of.
587  
588 =cut
589 sub animation {
590 my $self = shift;
591 if(@_) { $self->{ANIMATION} = shift; }
592 return $self->{ANIMATION};
593 }
594  
595 =item I<default_color>
596  
597 $entity->default_color( 'blue' );
598 $def_color = $entity->default_color();
599  
600 Get or set the default color for the entity. The color can
601 be either a single character or the full name of the color.
602  
603 =cut
604 sub default_color {
605 my $self = shift;
606 if(@_) {
607 my $color = shift;
608 if(is_valid_color($color)) {
609 $self->{DEF_COLOR} = color_id($color);
610 $self->_build_mask();
611 } else {
612 carp("Invalid color supplied: $color");
613 }
614 }
615 return $self->{DEF_COLOR};
616 }
617  
618 =item I<color_mask>
619  
620 $entity->color_mask( $mask );
621  
622 Set the color mask for the entity. See the L<Term::Animation/COLOR|COLOR> section of
623 L<Term::Animation> for details.
624  
625 =cut
626 sub color_mask {
627 my $self = shift;
628 if(@_) { $self->_build_mask(shift); }
629 }
630  
631  
632  
633 =item I<move_entity>
634  
635 The default callback. You can also call this from your own
636 callback to do the work of moving and animating the entity
637 after you have done whatever other processing you want to do.
638  
639 sub my_callback {
640 my ($entity, $animation) = @_;
641
642 # do something here
643
644 return $entity->move_object($animation);
645  
646 }
647  
648 =cut
649 sub move_entity {
650 my ($ent, $anim) = @_;
651 my $cb_args;
652 my $f;
653 # figure out if we just have a set of deltas, or if we have
654 # a full animation path to follow
655 if(ref($ent->{CALLBACK_ARGS}[1]) eq 'ARRAY') {
656 $cb_args = $ent->{CALLBACK_ARGS}[1][$ent->{CALLBACK_ARGS}[0]];
657 $ent->{CALLBACK_ARGS}[0]++;
658 if($ent->{CALLBACK_ARGS}[0] > $#{$ent->{CALLBACK_ARGS}[1]}) {
659 $ent->{CALLBACK_ARGS}[0] = 0;
660 }
661 $f = $cb_args->[3];
662 } else {
663 $cb_args = $ent->{CALLBACK_ARGS};
664 if($cb_args->[3]) {
665 $f = $ent->{CURR_FRAME} + $cb_args->[3];
666 $f = ($f - int($f)) + ($f % ($#{$ent->{SHAPE}} + 1));
667 }
668 }
669  
670 my $x = $ent->{X} + $cb_args->[0];
671 my $y = $ent->{Y} + $cb_args->[1];
672 my $z = $ent->{Z} + $cb_args->[2];
673  
674 if($ent->{WRAP}) {
675 if($x >= $anim->{WIDTH}) { $x = ($x - int($x)) + ($x % $anim->{WIDTH}); }
676 elsif($x < 0) { $x = ($x - int($x)) + ($x % $anim->{WIDTH}); }
677 if($y >= $anim->{HEIGHT}) { $y = ($y - int($y)) + ($y % $anim->{HEIGHT}); }
678 elsif($y < 0) { $y = ($y - int($y)) + ($y % $anim->{HEIGHT}); }
679 }
680 return($x, $y, $z, $f);
681 }
682  
683 =item I<kill>
684  
685 $entity->kill();
686  
687 Remove this entity from the animation. This is equivilent
688 to:
689  
690 $animation->del_entity($entity);
691  
692 This does not destroy the object, so you can still
693 readd it later (or put it in a different animation) as long
694 as you have a reference to it.
695  
696 =cut
697 sub kill {
698 my $self = shift;
699 if(defined($self->{ANIMATION})) {
700 $self->{ANIMATION}->del_entity($self);
701 }
702 }
703  
704 # create a color mask for an entity
705 sub _build_mask {
706 my ($self, $shape) = @_;
707  
708 my @amask;
709 my $mask = ();
710  
711 # store the color mask in case we are asked to
712 # change the default color later
713 if(defined($shape)) {
714 $self->{SUPPLIED_MASK} = $shape;
715 ($mask) = _build_shape($self, $shape);
716 } elsif(defined($self->{SUPPLIED_MASK})) {
717 $shape = $self->{SUPPLIED_MASK};
718 ($mask) = _build_shape($self, $shape);
719 }
720  
721 # if we were given fewer mask frames
722 # than we have animation frames, then
723 # repeat what we got to make up the difference.
724 # this allows the user to pass a single color
725 # mask that is the same for every animation frame
726 if($#{$mask} < $#{$self->{SHAPE}}) {
727 my $diff = $#{$self->{SHAPE}} - $#{$mask};
728 for (1..$diff) {
729 push(@{$mask}, $mask->[$_ - 1]);
730 }
731 }
732  
733 $self->{COLOR} = ();
734 for my $f (0..$#{$self->{SHAPE}}) {
735 for my $i (0..$self->{HEIGHT}-1) {
736 for my $j (0..$self->{WIDTH}-1) {
737 if(!defined($mask->[$f][$i][$j]) or $mask->[$f][$i][$j] eq ' ') {
738 $mask->[$f][$i][$j] = $self->{DEF_COLOR};
739 } elsif(defined($mask->[$f][$i][$j])) {
740 # make sure it's a valid color
741 unless(Term::Animation::is_valid_color($mask->[$f][$i][$j]) ) {
742 carp("Invalid color mask: $mask->[$f][$i][$j]");
743 $mask->[$f][$i][$j] = undef;
744 }
745 }
746  
747 # capital letters indicate bold colors
748 if($mask->[$f][$i][$j] =~ /[A-Z]/) {
749 $self->{COLOR}->[$f][$i][$j] = lc($mask->[$f][$i][$j]);
750 $amask[$f][$i][$j] = Curses::A_BOLD;
751 } else {
752 $self->{COLOR}->[$f][$i][$j] = lc($mask->[$f][$i][$j]);
753 }
754 }
755 }
756 }
757 $self->{ATTR} = \@amask;
758 }
759  
760 # automatically make whitespace appearing on a line before the first non-
761 # whitespace character transparent
762 sub _auto_trans {
763 my ($shape, $char) = @_;
764 unless(defined($char)) { $char = '?'; }
765  
766 if(ref($shape) eq 'ARRAY') {
767 my @shape_array = ();
768 foreach my $i (0..$#{$shape}) {
769 if(ref($shape->[$i] eq 'ARRAY')) {
770 # unimplemented
771 }
772 else { push(@shape_array, _trans_fill_string($shape->[$i], $char)); }
773 }
774 return \@shape_array;
775 } else {
776 return _trans_fill_string($shape, $char);
777 }
778  
779 }
780  
781 # called by _auto_trans to handle a single string
782 sub _trans_fill_string {
783 my ($shape, $char) = @_;
784 my $new = '';
785 foreach my $line (split("\n", $shape)) {
786 my $len = length(($line =~ /^(\s*)/)[0]);
787 my $fill = ${char}x$len;
788 $line =~ s/^\s{$len}/$fill/;
789 $new .= $line . "\n";
790 }
791 return $new;
792 }
793  
794 # take one of 1) a string 2) an array of strings 3) an array of 2D arrays
795 # use these to generate a shape in the format we want (which is #3 above)
796 sub _build_shape {
797 my ($self, $shape) = @_;
798  
799 my @shape_array = ();
800 my $height = 0;
801 my $width = 0;
802  
803 if(ref($shape) eq 'ARRAY') {
804 for my $i (0..$#{$shape}) {
805 my $this_height = 0;
806 if(ref($shape->[$i]) eq 'ARRAY') {
807 $this_height = $#{$shape->[$i]};
808 $shape_array[$i] = $shape->[$i];
809 }
810 else {
811 # strip an empty line from the top, for convenience
812 $shape->[$i] =~ s/^\n//;
813 for my $line (split("\n", $shape->[$i])) {
814 $this_height++;
815 if(length($line) > $width) { $width = length($line); }
816 push @{$shape_array[$i]}, [split('', $line)];
817 }
818 }
819 if($this_height > $height) { $height = $this_height; }
820 }
821 } else {
822 # strip an empty line from the top, for convenience
823 $shape =~ s/^\n//;
824 for my $line (split("\n", $shape)) {
825 $height++;
826 if(length($line) > $width) { $width = length($line); }
827 push @{$shape_array[0]}, [split('', $line)];
828 }
829 }
830 return \@shape_array, $height, $width;
831 }
832  
833 # look up the name of an entity if given an entity,
834 # just return the string if we got a string
835 sub _get_entity_name {
836 my ($self, $entity) = @_;
837  
838 if(ref($entity)) {
839 return $entity->name;
840 } else {
841 return $entity;
842 }
843 }
844  
845 1;
846  
847 =back
848  
849 =head1 AUTHOR
850  
851 Kirk Baucom E<lt>kbaucom@schizoid.comE<gt>
852  
853 =head1 SEE ALSO
854  
855 L<Term::Animation|Term::Animation>
856  
857 =cut