| 1 | package Games::Snake::Level; | ||
| 2 | { | ||
| 3 | $Games::Snake::Level::VERSION = '0.000001'; | ||
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: Level object | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use Moo; | ||
| 11 | use MooX::Types::MooseLike::Base qw( Int ArrayRef ); | ||
| 12 | use Sub::Quote qw(quote_sub); | ||
| 13 | |||
| 14 | has w => ( | ||
| 15 | is => 'ro', | ||
| 16 | isa => Int, | ||
| 17 | required => 1, | ||
| 18 | ); | ||
| 19 | |||
| 20 | has h => ( | ||
| 21 | is => 'ro', | ||
| 22 | isa => Int, | ||
| 23 | required => 1, | ||
| 24 | ); | ||
| 25 | |||
| 26 | has walls => ( | ||
| 27 | is => 'ro', | ||
| 28 | isa => ArrayRef, | ||
| 29 | lazy => 1, | ||
| 30 | builder => '_build_walls', | ||
| 31 | ); | ||
| 32 | |||
| 33 | has size => ( | ||
| 34 | is => 'ro', | ||
| 35 | isa => Int, | ||
| 36 | required => 1, | ||
| 37 | ); | ||
| 38 | |||
| 39 | has color => ( | ||
| 40 | is => 'ro', | ||
| 41 | isa => Int, | ||
| 42 | default => quote_sub q{ 0x0000FFFF }, | ||
| 43 | ); | ||
| 44 | |||
| 45 | sub _build_walls { | ||
| 46 | my ($self) = @_; | ||
| 47 | |||
| 48 | my @walls; | ||
| 49 | |||
| 50 | my $w = $self->w; | ||
| 51 | my $h = $self->h; | ||
| 52 | |||
| 53 | foreach my $x ( 0 .. $w - 1 ) { | ||
| 54 | push @walls, [ $x, 0 ], [ $x, $h - 1 ]; | ||
| 55 | } | ||
| 56 | |||
| 57 | foreach my $y ( 1 .. $self->w - 2 ) { | ||
| 58 | push @walls, [ 0, $y ], [ $w - 1, $y ]; | ||
| 59 | } | ||
| 60 | |||
| 61 | return \@walls; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub is_wall { | ||
| 65 | my ( $self, $coord ) = @_; | ||
| 66 | return | ||
| 67 | scalar grep { $coord->[0] == $_->[0] && $coord->[1] == $_->[1] } | ||
| 68 | @{ $self->walls }; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub draw { | ||
| 72 | my ( $self, $surface ) = @_; | ||
| 73 | |||
| 74 | my $size = $self->size; | ||
| 75 | my $color = $self->color; | ||
| 76 | |||
| 77 | foreach my $wall ( @{ $self->walls } ) { | ||
| 78 | $surface->draw_rect( [ ( map { $_ * $size } @$wall ), $size, $size ], | ||
| 79 | $color ); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; | ||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | =pod | ||
| 88 | |||
| 89 | =head1 NAME | ||
| 90 | |||
| 91 | Games::Snake::Level - Level object | ||
| 92 | |||
| 93 | =head1 VERSION | ||
| 94 | |||
| 95 | version 0.000001 | ||
| 96 | |||
| 97 | =for Pod::Coverage color draw h is_wall size w | ||
| 98 | |||
| 99 | =head1 SEE ALSO | ||
| 100 | |||
| 101 | =over 4 | ||
| 102 | |||
| 103 | =item * L<Games::Snake> | ||
| 104 | |||
| 105 | =back | ||
| 106 | |||
| 107 | =head1 AUTHOR | ||
| 108 | |||
| 109 | Jeffrey T. Palmer <jtpalmer@cpan.org> | ||
| 110 | |||
| 111 | =head1 COPYRIGHT AND LICENSE | ||
| 112 | |||
| 113 | This software is Copyright (c) 2012 by Jeffrey T. Palmer. | ||
| 114 | |||
| 115 | This is free software, licensed under: | ||
| 116 | |||
| 117 | The MIT (X11) License | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | |||
| 122 | __END__ |