Skip to content

Commit

Permalink
added grid aligned role
Browse files Browse the repository at this point in the history
  • Loading branch information
eilara committed Dec 7, 2011
1 parent 49512d8 commit 5d7b7bd
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 3 deletions.
2 changes: 1 addition & 1 deletion eg/06-sprite.pl
Expand Up @@ -29,7 +29,7 @@ sub on_mouse_button_up {
my $self = shift;
push @{$self->children}, Games::CamelDefense::eg_StickySprite->new(
rect => $self->rect,
image => 'arrow',
image => $self->image,
layer => 'middle',
centered => 1,
);
Expand Down
49 changes: 49 additions & 0 deletions eg/08-grid-aligned-sprite.pl
@@ -0,0 +1,49 @@
#!/usr/bin/perl

package Games::CamelDefense::eg_GridAlignedSprite;
use Games::CamelDefense::Demo qw(Grid::Markers);

consume qw(Render::Sprite Grid::Aligned);

# ------------------------------------------------------------------------------

package Games::CamelDefense::eg_GridAlignedSprite_Cursor;
use Games::CamelDefense::Demo;

has children => (is => 'ro', required => 1, default => sub { [] });
has markers => (is => 'ro', required => 1, handles => [qw(cell_center_xy)]);

consume qw(Behavior::Cursor Render::Sprite);

sub on_mouse_button_up {
my $self = shift;
push @{$self->children}, Games::CamelDefense::eg_GridAlignedSprite->new(
rect => $self->rect,
image => $self->image,
markers => $self->markers,
layer => 'middle',
);
}

# ------------------------------------------------------------------------------

package main;
use Games::CamelDefense::Demo qw(Grid::Markers);
use Games::CamelDefense::App title => 'Grid Aligned Sprite',
size => [640, 480],
layers => [qw(middle top)],
hide_cursor => 1;

my $markers = Markers->new
(size => App->size, xy => [0, 0], spacing => 32);

my $cursor = Games::CamelDefense::eg_GridAlignedSprite_Cursor->new(
rect => [100, 100, 22, 26],
image => 'arrow',
layer => 'top',
markers => $markers,
);

App->run;


34 changes: 34 additions & 0 deletions lib/Games/CamelDefense/Behavior/Cursor.pm
@@ -0,0 +1,34 @@
package Games::CamelDefense::Behavior::Cursor;

use Games::CamelDefense::Role;

consume qw(
Geometry::Positionable
Event::Handler::SDL
);

sub on_mouse_motion { shift->xy([@_]) }

sub on_app_mouse_focus { shift->is_visible(pop) }

1;


=head1 NAME
Games::CamelDefense::Behvaior::Cursor - position follows mouse
=head1 DESCRIPTION
A positionable that sets its position on mouse motion, and hides when app loses
focus.
=head1 DOES
L<Games::CamelDefense::Geometry::Positionable>, L<Games::CamelDefense::Event::Handler::SDL>
=cut


58 changes: 58 additions & 0 deletions lib/Games/CamelDefense/Grid/Aligned.pm
@@ -0,0 +1,58 @@
package Games::CamelDefense::Grid::Aligned;

use Games::CamelDefense::Role qw(Grid::Markers);

consume 'Geometry::Rectangular';

has markers => (
is => 'ro',
required => 1,
isa => Markers,
handles => [qw(cell_center_xy)],
);

around BUILDARGS => sub {
my ($orig, $class, %args) = @_;
$args{centered} = 1;
return $class->$orig(%args);
};

after BUILD => sub {
my $self = shift;
$self->align_to_cell_center($self->xy);
};

sub align_to_cell_center {
my ($self, $xy) = @_;
$self->xy( $self->cell_center_xy($xy) );
}

1;


=head1 NAME
Games::CamelDefense::Grid::Aligned - a positionable centered on cell center
=head1 DESCRIPTION
Treat as a normal positionable, but provide with grid markers in constructor.
You can set xy using C<align_to_cell_center($xy)>, where C<$xy> is a 2D
array ref. The positionable will always be centered on the cell center of
the cell that includes the C<$xy> position.
=head1 REQUIRES
L<Games::CamelDefense::Grid::Markers> in C<markers> key of constructor
=head1 DOES
L<Games::CamelDefense::Geometry::Rectangular>
=cut


2 changes: 1 addition & 1 deletion lib/Games/CamelDefense/Grid/Markers.pm
Expand Up @@ -55,7 +55,7 @@ sub cell_center_xy {
my ($self, $xy) = @_;
my $pos = V( @{ $self->compute_cell_pos($xy) } );
my $s = $self->spacing;
my $offset = $self->xy_vec;
my $offset = $self->xy;
return [@{ $offset + $pos * $s + V($s/2, $s/2) }];
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Games/CamelDefense/Render/Paintable.pm
Expand Up @@ -38,7 +38,7 @@ sub sdl_paint {

# permonks stvn trick to get BUILD time action from roles
sub BUILD {}
before 'BUILD' => sub {
before BUILD => sub {
my $self = shift;
$Layer_Manager->add_paintable_to_layer($self->layer, $self)
if $self->auto_paint;
Expand Down

0 comments on commit 5d7b7bd

Please sign in to comment.