PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Games/Zumbis/Mapa.pm

https://github.com/ruoso/Zumbis
Perl | 145 lines | 113 code | 28 blank | 4 comment | 5 complexity | 42323a63edae2f626abb3dd0ea6d8a2d MD5 | raw file
  1. package Games::Zumbis::Mapa;
  2. use Mouse;
  3. use Games::Zumbis;
  4. use Games::Zumbis::L10N;
  5. use XML::Compile::Schema;
  6. use XML::Compile::Util qw(pack_type);
  7. use constant MAP_NS => 'http://perl.org.br/games/zumbis';
  8. my $map_schema = XML::Compile::Schema->new( Games::Zumbis->sharedir->file('mapa.xsd') );
  9. my $map_reader = $map_schema->compile(READER => pack_type(MAP_NS, 'mapa'),
  10. sloppy_integers => 1, sloppy_floats => 1);
  11. use SDL;
  12. use SDL::Color;
  13. use SDL::TTF;
  14. use SDL::Rect;
  15. use SDL::Image;
  16. use SDL::Video;
  17. use Carp ();
  18. SDL::TTF::init;
  19. has arquivo => (is => 'ro', isa => 'Path::Class::File', required => 1);
  20. has dados => (is => 'ro', isa => 'HashRef' );
  21. has colisao => (is => 'ro', isa => 'ArrayRef');
  22. has tileset => (is => 'ro');
  23. my $lh = Games::Zumbis::L10N->get_handle() or die "unable to indentify language";
  24. # translation handles inside renderer was making things too slow
  25. my $texto_mortes = $lh->maketext('Mortes:');
  26. my $texto_segundos = $lh->maketext('segundos');
  27. my $font_p = SDL::TTF::open_font( Games::Zumbis->sharedir->file('dados/AtariSmall.ttf'), 16) or
  28. die $lh->maketext('Erro carregando a fonte');
  29. my $color = SDL::Color->new(0,0,0);
  30. sub BUILDARGS {
  31. my ($self, %args) = @_;
  32. $args{dados} = $map_reader->($args{arquivo});
  33. # povoa a matrix de colisoes com 0
  34. $args{colisao} =
  35. [ map { [ map { 0 } 0..($args{dados}{width}-1) ] } 0..($args{dados}{height}-1) ];
  36. for my $object (@{$args{dados}{object}}) {
  37. my ($x,$y) = split /,/, $object->{position};
  38. $args{colisao}[$x][$y] = 1 if $object->{collide};
  39. }
  40. my $tileset_filename = Games::Zumbis->sharedir->file( $args{dados}{tileset} );
  41. Carp::croak "tileset '$tileset_filename' não encontrado\n"
  42. unless -f $tileset_filename;
  43. $args{tileset} = SDL::Image::load( $tileset_filename );
  44. return \%args;
  45. };
  46. sub playerstart {
  47. my ($self) = @_;
  48. return split(/,/, $self->dados->{playerstart});
  49. };
  50. sub playerstart_px {
  51. my ($self) = @_;
  52. my $tilesize = $self->dados->{tilesize};
  53. return map { $_ * $tilesize } $self->playerstart;
  54. };
  55. sub width {
  56. my ($self) = @_;
  57. return $self->dados->{width};
  58. };
  59. sub height {
  60. my ($self) = @_;
  61. return $self->dados->{height};
  62. };
  63. sub width_px {
  64. my ($self) = @_;
  65. return $self->dados->{width} * $self->dados->{tilesize};
  66. };
  67. sub height_px {
  68. my ($self) = @_;
  69. return $self->dados->{height} * $self->dados->{tilesize};
  70. };
  71. sub tilesize {
  72. my ($self) = @_;
  73. return $self->dados->{tilesize};
  74. }
  75. sub render {
  76. my ($self, $surface, $tempo, $score) = @_;
  77. my $tilesize = $self->dados->{tilesize};
  78. my $tileset = $self->tileset;
  79. # renderizar o background;
  80. my $back_rect = SDL::Rect->new((map {$_ * $tilesize } split /,/, $self->dados->{background}),
  81. $tilesize, $tilesize);
  82. for my $x (0..($self->dados->{width}-1)) {
  83. for my $y (0..($self->dados->{height}-1)) {
  84. my $rect = SDL::Rect->new($x*$tilesize, $y*$tilesize,
  85. $tilesize, $tilesize);
  86. SDL::Video::blit_surface( $tileset, $back_rect,
  87. $surface, $rect );
  88. }
  89. }
  90. # renderizar os objetos;
  91. for my $object (@{$self->dados->{object}}) {
  92. my $src_rect = SDL::Rect->new((map { $_ * $tilesize } split /,/, $object->{tile}),
  93. $tilesize, $tilesize);
  94. my $dst_rect = SDL::Rect->new((map { $_ * $tilesize } split /,/, $object->{position}),
  95. $tilesize, $tilesize);
  96. SDL::Video::blit_surface( $tileset, $src_rect,
  97. $surface, $dst_rect );
  98. }
  99. my $timer =
  100. SDL::TTF::render_text_blended
  101. ($font_p, "$texto_mortes $score. $tempo $texto_segundos", $color)
  102. or die 'TTF render error: ' . SDL::get_error();
  103. my $timer_w = $timer->w;
  104. my $timer_h = $timer->h;
  105. my $timer_srcrect = SDL::Rect->new(0,0,$timer_w,$timer_h);
  106. my $dstrect = SDL::Rect->new(40,$surface->h - $timer_h - 30,$timer_w,$timer_h);
  107. SDL::Video::blit_surface($timer, $timer_srcrect, $surface, $dstrect);
  108. };
  109. sub next_spawnpoint_px {
  110. my ($self) = @_;
  111. my $tilesize = $self->dados->{tilesize};
  112. my $sp_count = scalar @{$self->dados->{zombie}};
  113. my $sp_num = int(rand($sp_count - 1)+0.5);
  114. return map { $_ * $tilesize } split /,/, $self->dados->{zombie}[$sp_num]{posicao};
  115. }
  116. __PACKAGE__->meta->make_immutable();
  117. 1;