From: Breno G. de Oliveira Date: Sun, 4 Oct 2009 07:29:21 +0000 (-0300) Subject: implemented colliderect X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=50d0e9aca27e76457937927dd6ead320674bbe22;p=sdlgit%2FSDL_perl.git implemented colliderect --- diff --git a/lib/SDL/Game/Rect.pm b/lib/SDL/Game/Rect.pm index 7cffffa..a552f75 100644 --- a/lib/SDL/Game/Rect.pm +++ b/lib/SDL/Game/Rect.pm @@ -590,7 +590,6 @@ sub contains { return $contained; } - sub collidepoint { my ($self, $x, $y) = (@_); @@ -607,6 +606,33 @@ sub collidepoint { return $inside; } +sub _do_rects_intersect { + my ($rect_A, $rect_B) = (@_); + + return ( + ($rect_A->x >= $rect_B->x && $rect_A->x < $rect_B->x + $rect_B->w) + || ($rect_B->x >= $rect_A->x && $rect_B->x < $rect_A->x + $rect_A->w) + ) + && + ( + ($rect_A->y >= $rect_B->y && $rect_A->y < $rect_B->y + $rect_B->h) + || ($rect_B->y >= $rect_A->y && $rect_B->y < $rect_A->y + $rect_A->h) + ) + ; +} + + +sub colliderect { + my ($self, $rect) = (@_); + + unless ($rect->isa('SDL::Rect')) { + croak "must receive an SDL::Rect-based object"; + } + + return _do_rects_intersect($self, $rect); +} + + 42; __END__