Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters