blib/lib/Game/Floppy/Raylib.pm

filter: Statementstmt 58.6% 102 / 174 Branchbran 28.3% 17 / 60 Conditioncond 16.6% 4 / 24 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 88.8% 24 / 27 Podpod 90.9% 10 / 11 totaltotal 51.1% 159 / 311 CC 43 SCAR 48.8
CC
43
Cov
48%
start
413.6
__ANON__
30.0
__ANON__
20.0
CRAP 131.2
↑ Summary blib/lib/Game/Floppy/Terminal.pm »
1 package Game::Floppy::Raylib;
2  
3 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Game::Floppy;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Game::Floppy::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Game::Terminal::Kit::Ticker;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 our $VERSION = '0.10';
19  
20 2 my $WHY;
21  
22 2 ▶sub available {
Subroutine available: called CC=3 SCAR=11.5
Pod documented
23 0 ▶ return 1 if eval { require Raylib::FFI; require Raylib::Color; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require Raylib::FFI; require Raylib::Color; 1 } } 0 2 2
24 2 my $why = $@;
25 2 $why =~ s/ at \S+ line \d+\.?\s*//g;
26 2 $why =~ s/\n.*//s;
27 2 ▶ $why = 'the window needs Raylib::FFI, which is not installed: cpanm Raylib::FFI, or play in the terminal without --raylib'
Branch: 1 branch
BranchTrueFalseTotal
if $why =~ /\ACan't locate Raylib/u 0 2 2
28 if $why =~ /\ACan't locate Raylib/;
29 2 $why =~ s/\s+\z//;
30 2 $WHY = $why;
31 2 return 0;
32 }
33  
34 3 ▶sub why_not { return $WHY }
Subroutine why_not: called CC=1 SCAR=0.0
Pod documented
35  
36 2 my %KEY = (
37 32 => 'flap', 265 => 'flap', 257 => 'flap', 87 => 'flap', 75 => 'flap',
38 80 => 'pause',
39 81 => 'quit', 256 => 'quit',
40 );
41  
42 2 my $INSET = 20;
43  
44 2 my %SPRITE = (
45 background => [ 288, 512 ],
46 bird => [ 136, 24 ],
47 pipe => [ 52, 320 ],
48 );
49  
50 2 my ($BIRD_FRAMES, $BIRD_W, $BIRD_H) = (4, 34, 24);
51  
52 11 ▶sub key_for {
Subroutine key_for: called CC=2 SCAR=6.9
Pod documented
53 11 my ($class, $code) = @_;
54 11 ▶ return defined $code ? $KEY{$code} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $code ? : 10 1 11
55 }
56  
57 1 ▶sub inset { return $INSET }
Subroutine inset: called CC=1 SCAR=0.0
Pod documented
58  
59 10 ▶sub frame_for {
Subroutine frame_for: called CC=1 SCAR=0.0
Pod documented
60 10 my ($class, $ticks) = @_;
61 10 return $ticks % $BIRD_FRAMES;
62 }
63  
64 4 ▶sub source_for {
Subroutine source_for: called CC=1 SCAR=0.0
Pod documented
65 4 my ($class, $frame) = @_;
66 4 return [ $BIRD_W * ($frame % $BIRD_FRAMES), 0, $BIRD_W, $BIRD_H ];
67 }
68  
69 1 ▶sub pipe_at {
Subroutine pipe_at: called CC=1 SCAR=0.0
Pod documented
70 1 my ($class, $tube) = @_;
71 1 my ($w, $h) = @{ $SPRITE{pipe} };
72 return {
73 top => [ $tube->{x} + $w, $tube->{gap_top} + $INSET, 180 ],
74 1 bottom => [ $tube->{x}, $tube->{gap_bottom} - $INSET, 0 ],
75 };
76 }
77  
78 4 ▶sub sheet {
Subroutine sheet: called CC=4 SCAR=14.1
Pod documented
79 4 my ($class, $name) = @_;
80 4 my %file = (background => 'background-day.png', bird => 'bluebird.png', pipe => 'pipe-green.png');
81 Carp::croak("Game::Floppy::Raylib: '" . (defined $name ? $name : 'undef') . "' is not a sprite")
82 4 ▶ unless defined $name && $file{$name};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $name and $file{$name} 1 3 4
defined $name ? : 1 0 1
Condition66%2/3
defined $name and $file{$name}
!ll&&!rl&&r
013
MC/DC50%1/2
defined $name and $file{$name}
defined $name $file{$name}
Truth table66%2/3
A and B
A= defined $nameB= $file{$name}
ABresult
0X0
100
111
83 3 return File::Spec->catfile(File::Basename::dirname(__FILE__), 'resources', $file{$name});
84 }
85  
86 3 ▶sub sprite_size {
Subroutine sprite_size: called CC=2 SCAR=7.1
Pod documented
87 3 my ($class, $name) = @_;
88 3 ▶ return $SPRITE{$name} ? [ @{ $SPRITE{$name} } ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$SPRITE{$name} ? : 3 0 3
89 }
90  
91 has seed => (
92 is => 'ro',
93 isa => Str,
94 default => ''
95 2 );
96  
97 has level => (
98 is => 'ro',
99 isa => Int,
100 default => 3
101 2 );
102  
103 has watch => (
104 is => 'ro',
105 isa => Int,
106 default => 0
107 2 );
108  
109 has fps => (
110 is => 'ro',
111 isa => Int,
112 default => 20
113 2 );
114  
115 has title => (
116 is => 'ro',
117 isa => Str,
118 default => 'Floppy'
119 2 );
120  
121 has max_ticks => (
122 is => 'ro',
123 isa => Int,
124 default => 0
125 2 );
126  
127 has game => (
128 is => 'rw',
129 isa => Object
130 2 );
131  
132 has clock => (
133 is => 'ro',
134 isa => Any,
135 5 ▶ default => sub { \&Time::HiRes::time }
Subroutine __ANON__: called CC=1 SCAR=0.0
136 2 );
137  
138 has ticker => (
139 is => 'rw',
140 isa => Object
141 2 );
142  
143 has frames => (
144 is => 'rw',
145 isa => Int,
146 default => 0
147 2 );
148  
149 has refused => (
150 is => 'rw',
151 isa => Int,
152 default => 0
153 2 );
154  
155 has best => (
156 is => 'rw',
157 isa => Int,
158 default => 0
159 2 );
160  
161 5 ▶sub BUILD {
Subroutine BUILD: called CC=9 SCAR=22.5
Pod undocumented
162 5 my ($self) = @_;
163  
164 5 Game::Floppy::Bot->describe($self->level);
165  
166 4 for my $name (qw/ fps max_ticks /) {
167 7 my $value = $self->$name;
168 7 ▶ Carp::croak("Game::Floppy::Raylib: $name must be a whole number, not '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ /\A[0-9]+\z/u 1 6 7
Condition66%2/3
defined $value and $value =~ /\A[0-9]+\z/u
!ll&&!rl&&r
016
MC/DC50%1/2
defined $value and $value =~ /\A[0-9]+\z/u
defined $value $value =~ /\A[0-9]+\z/u
Truth table66%2/3
A and B
A= defined $valueB= $value =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
169 unless defined $value && $value =~ /\A[0-9]+\z/;
170 }
171 3 ▶ Carp::croak('Game::Floppy::Raylib: the frame rate is at least 1') if $self->fps < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fps < 1 1 2 3
172 2 ▶ Carp::croak('Game::Floppy::Raylib: the clock is a code reference')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->clock eq "CODE" 0 2 2
173 unless ref $self->clock eq 'CODE';
174  
175 2 ▶ unless ($self->game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->game) 2 0 2
176 2 my $seed = $self->seed;
177 2 ▶ $seed = Digest::SHA::sha256_hex(join ':', $self->clock->(), $$)
Branch: 1 branch
BranchTrueFalseTotal
unless length $seed 0 2 2
178 unless CORE::length $seed;
179 2 $self->game(Game::Floppy->new(seed => $seed));
180 }
181  
182 2 ▶ $self->ticker(Game::Terminal::Kit::Ticker->new(clock => $self->clock)) unless $self->ticker;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ticker 2 0 2
183  
184 2 return;
185 }
186  
187 0 ▶my $rectangle = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 0 my ($x, $y, $w, $h) = @_;
189 0 return Raylib::FFI::Rectangle->new(x => $x, y => $y, width => $w, height => $h);
190 2 };
191  
192 2 my $origin;
193  
194 0 ▶my $draw = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
195 0 my ($self, $textures, $paused) = @_;
196 0 my $game = $self->game;
197 0 my ($width, $height) = ($game->width, $game->height);
198  
199 0 Raylib::FFI::ClearBackground(Raylib::Color::BLACK());
200  
201 0 my ($bw, $bh) = @{ $SPRITE{background} };
202 Raylib::FFI::DrawTexturePro($textures->{background},
203 0 $rectangle->(0, 0, $bw, $bh), $rectangle->(0, 0, $width, $height),
204 $origin, 0, Raylib::Color::WHITE());
205  
206 0 my ($pw, $ph) = @{ $SPRITE{pipe} };
207 0 for my $tube (@{ $game->tubes }) {
208 0 my $at = __PACKAGE__->pipe_at($tube);
209 0 for my $end (qw/ top bottom /) {
210 0 my ($x, $y, $rotation) = @{ $at->{$end} };
211 Raylib::FFI::DrawTexturePro($textures->{pipe},
212 0 $rectangle->(0, 0, $pw, $ph), $rectangle->($x, $y, $pw, $ph),
213 $origin, $rotation, Raylib::Color::WHITE());
214 }
215 }
216  
217 0 my $bird = $game->bird;
218 0 my $source = __PACKAGE__->source_for(__PACKAGE__->frame_for($game->ticks));
219 Raylib::FFI::DrawTexturePro($textures->{bird},
220 $rectangle->(@$source), $rectangle->($bird->{x}, $bird->{y}, $bird->{radius}, $bird->{radius}),
221 0 $origin, $bird->{tilt}, Raylib::Color::WHITE());
222  
223 0 Raylib::FFI::DrawText(sprintf('Current Score: %d High Score: %d', $game->score, $self->best),
224 10, 10, 22, Raylib::Color::WHITE());
225  
226 0 ▶ if ($game->status eq 'finished') {
Branch: 2 branches
BranchTrueFalseTotal
if ($game->status eq 'finished') { } 0 0 0
elsif ($paused) { } 0 0 0
227 0 Raylib::FFI::DrawText('Game Over', 10, 40, 22, Raylib::Color::WHITE());
228 0 Raylib::FFI::DrawText($game->result->stringify, 10, 70, 22, Raylib::Color::WHITE());
229 0 Raylib::FFI::DrawText('Press space to try again', 10, 100, 22, Raylib::Color::WHITE());
230 }
231 elsif ($paused) {
232 0 Raylib::FFI::DrawText('Paused', 10, 40, 22, Raylib::Color::WHITE());
233 }
234 0 return;
235 2 };
236  
237 0 ▶my $flap = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
238 0 my ($self) = @_;
239 0 my $game = $self->game;
240 0 ▶ if ($game->status eq 'finished') {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->status eq "finished") 0 0 0
241 0 $game->reset;
242 0 $self->ticker->start(1 / 20);
243 0 return;
244 }
245 0 ▶ return if $self->watch;
Branch: 1 branch
BranchTrueFalseTotal
if $self->watch 0 0 0
246 0 ▶ $self->refused($self->refused + 1) unless $game->flap;
Branch: 1 branch
BranchTrueFalseTotal
unless $game->flap 0 0 0
247 0 return;
248 2 };
249  
250 1 ▶sub start {
Subroutine start: called CC=21 SCAR=60.3
Pod documented
251 1 my ($self) = @_;
252 1 ▶ return undef unless $self->available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->available 1 0 1
253  
254 0 my $game = $self->game;
255 0 my $period = 1 / 20;
256 0 ▶ $origin ||= Raylib::FFI::Vector2D->new(x => 0, y => 0);
Condition0%0/3
$origin ||= "Raylib::FFI::Vector2D"->new("x", 0, "y", 0)
l!l&&r!l&&!r
000
MC/DC0%0/2
$origin ||= "Raylib::FFI::Vector2D"->new("x", 0, "y", 0)
$origin "Raylib::FFI::Vector2D"->new("x", 0, "y", 0)
Truth table0%0/3
A ||= B
A= $originB= "Raylib::FFI::Vector2D"->new("x", 0, "y", 0)
ABresult
1X1
011
000
257  
258 0 Raylib::FFI::InitWindow($game->width, $game->height, $self->title);
259 0 Raylib::FFI::SetTargetFPS($self->fps);
260 0 Raylib::FFI::SetExitKey(0);
261 0 my %textures = map { $_ => Raylib::FFI::LoadTexture($self->sheet($_)) } qw/ background bird pipe /;
262  
263 0 my $ticker = $self->ticker->start($period);
264 0 my ($paused, $quit) = (0, 0);
265  
266 0 ▶ until ($quit || Raylib::FFI::WindowShouldClose()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quit or Raylib::FFI::WindowShouldClose()) 0 0 0
Condition0%0/3
$quit or Raylib::FFI::WindowShouldClose()
l!l&&r!l&&!r
000
MC/DC0%0/2
$quit or Raylib::FFI::WindowShouldClose()
$quit Raylib::FFI::WindowShouldClose()
Truth table0%0/3
A or B
A= $quitB= Raylib::FFI::WindowShouldClose()
ABresult
1X1
011
000
267 0 ▶ while (my $code = Raylib::FFI::GetKeyPressed()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = Raylib::FFI::GetKeyPressed()) 0 0 0
268 0 ▶ my $key = $KEY{$code} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $key = $KEY{$code} or next 0 0 0
269 0 ▶ if ($key eq 'quit') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'quit') { } 0 0 0
elsif ($key eq 'pause') { } 0 0 0
270 0 $quit = 1;
271 }
272 elsif ($key eq 'pause') {
273 0 $paused = !$paused;
274 0 ▶ $ticker->start($period) unless $paused;
Branch: 1 branch
BranchTrueFalseTotal
unless $paused 0 0 0
275 }
276 else {
277 0 $self->$flap;
278 }
279 }
280 0 ▶ $self->$flap if Raylib::FFI::IsMouseButtonReleased(0);
Branch: 1 branch
BranchTrueFalseTotal
if Raylib::FFI::IsMouseButtonReleased(0) 0 0 0
281  
282 0 ▶ if (!$paused && $game->status ne 'finished' && $ticker->due) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $paused and $game->status ne "finished" and $ticker->due) 0 0 0
Condition0%0/6
not $paused and $game->status ne "finished" and $ticker->due
!ll&&!rl&&r
000
not $paused and $game->status ne "finished"
!ll&&!rl&&r
000
MC/DC0%0/3
not $paused and $game->status ne "finished" and $ticker->due
not $paused $game->status ne "finished" $ticker->due
Truth table0%0/4
not(A) and B and C
A= not $pausedB= $game->status ne "finished"C= $ticker->due
ABCresult
0XX0
10X0
1100
1111
283 0 ▶ $game->flap if $self->watch && Game::Floppy::Bot->choose($game, level => $self->level);
Branch: 1 branch
BranchTrueFalseTotal
if $self->watch and "Game::Floppy::Bot"->choose($game, "level", $self->level) 0 0 0
Condition0%0/3
$self->watch and "Game::Floppy::Bot"->choose($game, "level", $self->level)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->watch and "Game::Floppy::Bot"->choose($game, "level", $self->level)
$self->watch "Game::Floppy::Bot"->choose($game, "level", $self->level)
Truth table0%0/3
A and B
A= $self->watchB= "Game::Floppy::Bot"->choose($game, "level", $self->level)
ABresult
0X0
100
111
284 0 $game->tick;
285 0 $ticker->tick($period);
286 0 ▶ $self->best($game->score) if $game->score > $self->best;
Branch: 1 branch
BranchTrueFalseTotal
if $game->score > $self->best 0 0 0
287 }
288  
289 0 ▶ last if $self->max_ticks && $ticker->ticks >= $self->max_ticks;
Branch: 1 branch
BranchTrueFalseTotal
if $self->max_ticks and $ticker->ticks >= $self->max_ticks 0 0 0
Condition0%0/3
$self->max_ticks and $ticker->ticks >= $self->max_ticks
!ll&&!rl&&r
000
MC/DC0%0/2
$self->max_ticks and $ticker->ticks >= $self->max_ticks
$self->max_ticks $ticker->ticks >= $self->max_ticks
Truth table0%0/3
A and B
A= $self->max_ticksB= $ticker->ticks >= $self->max_ticks
ABresult
0X0
100
111
290  
291 0 Raylib::FFI::BeginDrawing();
292 0 $self->$draw(\%textures, $paused);
293 0 Raylib::FFI::EndDrawing();
294 0 $self->frames($self->frames + 1);
295 }
296  
297 0 Raylib::FFI::UnloadTexture($_) for values %textures;
298 0 Raylib::FFI::CloseWindow();
299  
300 0 ▶ $game->quit unless $game->status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $game->status eq "finished" 0 0 0
301 0 return $game->result;
302 }
303  
304 2 1;
305  
306 __END__
↑ Summary blib/lib/Game/Floppy/Terminal.pm »